docker-mc-backup

docker-mc-backup

Docker容器实现Minecraft服务器数据自动备份

docker-mc-backup是一个辅助容器,专门用于自动备份itzg/minecraft-server容器的世界数据。它利用RCON协调备份过程,实现数据刷新、写入暂停和恢复。支持tar、rsync、restic和rclone等多种备份方法。通过环境变量,用户可以灵活设置备份间隔、保留策略和排除规则。此外,该项目还提供备份还原和按需备份功能,适用于Docker和Kubernetes环境。

DockerMinecraft备份容器化自动化Github开源项目

Docker Pulls Build Discord

Provides a side-car container to back up itzg/minecraft-server world data. Backups are coordinated automatically by using RCON to flush data, pause writes, and resume after backup is completed.

This does NOT support Bedrock edition. Use a community provided solution for that.

Environment variables

Common variables

  • SRC_DIR=/data

  • BACKUP_NAME=world

  • BACKUP_METHOD=tar : see below

  • INITIAL_DELAY=2m

  • BACKUP_INTERVAL=24h

  • BACKUP_ON_STARTUP=true : Set to false to skip first backup on startup.

  • PAUSE_IF_NO_PLAYERS=false

  • PLAYERS_ONLINE_CHECK_INTERVAL=5m

  • PRUNE_BACKUPS_DAYS=7

  • PRUNE_BACKUPS_COUNT= -disabled unless set (only works with tar/rsync)

  • PRUNE_RESTIC_RETENTION=--keep-within 7d

  • RCON_HOST=localhost

  • RCON_PORT=25575

  • RCON_PASSWORD=minecraft

  • RCON_PASSWORD_FILE: Can be set to read the RCON password from a file. Overrides RCON_PASSWORD if both are set.

  • RCON_RETRIES=5 : Set to a negative value to retry indefinitely

  • RCON_RETRY_INTERVAL=10s

  • SERVER_HOST=RCON_HOST : Can be set if the game and RCON are accessible on different addresses.

  • SERVER_PORT=25565

  • INCLUDES=. : comma separated list of include patterns relative to directory specified by SRC_DIR where . specifies all of that directory should be included in the backup.

    For Restic the default is the value of SRC_DIR to remain backward compatible with previous images.

  • EXCLUDES=*.jar,cache,logs,*.tmp : commas separated list of file patterns to exclude from the backup. To disable exclusions, set to an empty string.

  • EXCLUDES_FILE: Can be set to read the list of excludes (one per line) from a file. Can be used with EXCLUDES to add more excludes.

  • RESTIC_ADDITIONAL_TAGS=mc_backups : additional tags to apply to the backup. Set to an empty string to disable additional tags.

  • RESTIC_VERBOSE=false : set to "true" to enable verbose output during restic backup operation

  • TZ : Can be set to the timezone to use for logging

  • PRE_SAVE_ALL_SCRIPT, PRE_BACKUP_SCRIPT, PRE_SAVE_ON_SCRIPT, POST_BACKUP_SCRIPT, *_SCRIPT_FILE: See Backup scripts

If PRUNE_BACKUPS_DAYS is set to a positive number, it'll delete old .tgz backup files from DEST_DIR. By default deletes backups older than a week.

If BACKUP_INTERVAL is set to 0 or smaller, script will run once and exit.

Both INITIAL_DELAY and BACKUP_INTERVAL accept times in sleep format: NUMBER[SUFFIX] NUMBER[SUFFIX] .... SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days.

Examples:

  • BACKUP_INTERVAL="1.5d" -> backup every one and a half days (36 hours)
  • BACKUP_INTERVAL="2h 30m" -> backup every two and a half hours
  • INITIAL_DELAY="120" -> wait 2 minutes before starting

The PAUSE_IF_NO_PLAYERS option lets you pause backups if no players are online.

If PAUSE_IF_NO_PLAYERS="true" and there are no players online after a backup is made, then instead of immediately scheduling the next backup, the script will start checking the server's player count every PLAYERS_ONLINE_CHECK_INTERVAL (defaults to 5 minutes). Once a player joins the server, the next backup will be scheduled in BACKUP_INTERVAL.

EXCLUDES is a comma-separated list of glob(3) patterns to exclude from backups. By default excludes all jar files (plugins, server files), logs folder and cache (used by i.e. PaperMC server).

Backup methods

Set BACKUP_METHOD to one of the following, where the default is tar.

tar

  • DEST_DIR=/backups
  • LINK_LATEST=false
  • TAR_COMPRESS_METHOD=gzip
  • ZSTD_PARAMETERS=-3 --long=25 --single-thread

LINK_LATEST is a true/false flag that creates a symbolic link to the latest backup.

TAR_COMPRESS_METHOD is the compression method used by tar. Valid value: gzip bzip2 zstd

ZSTD_PARAMETERS sets the parameters for zstd compression. The --long parameter affects RAM requirements for both compression and decompression (the default of 25 means 2^25 bytes = 32 MB).

rsync

  • DEST_DIR=/backups
  • LINK_LATEST=false

LINK_LATEST is a true/false flag that creates a symbolic link to the latest backup.

restic

See restic documentation on what variables are needed to be defined. At least one of RESTIC_PASSWORD* variables need to be defined, along with RESTIC_REPOSITORY.

Use the RESTIC_ADDITIONAL_TAGS variable to define a space separated list of additional restic tags. The backup will always be tagged with the value of BACKUP_NAME. e.g.: RESTIC_ADDITIONAL_TAGS=mc_backups foo bar will tag your backup with foo, bar, mc_backups and the value of BACKUP_NAME.

By default, the hostname, typically the container/pod's name, will be used as the Restic backup's hostname. That can be overridden by setting RESTIC_HOSTNAME

You can fine tune the retention cycle of the restic backups using the PRUNE_RESTIC_RETENTION variable. Take a look at the restic documentation for details.

EXAMPLE
Setting PRUNE_RESTIC_RETENTION to --keep-daily 7 --keep-weekly 5 --keep-monthly 12 --keep-yearly 75 will keep the most recent 7 daily snapshots, then 4 (remember, 7 dailies already include a week!) last-day-of-the-weeks and 11 or 12 last-day-of-the-months (11 or 12 depends if the 5 weeklies cross a month). And finally 75 last-day-of-the-year snapshots. All other snapshots are removed.

:warning:When using restic as your backup method, make sure that you fix your container hostname to a constant value! Otherwise, each time a container restarts it'll use a different, random hostname which will cause it not to rotate your backups created by previous instances!
:warning:When using restic, at least one of HOSTNAME or BACKUP_NAME must be unique, when sharing a repository. Otherwise other instances using the same repository might prune your backups prematurely.
:warning:SFTP restic backend is not directly supported. Please use RCLONE backend with SFTP support.

rclone

Rclone acts as the tar backup method but automatically moves the compressed files to a remote drive via rclone.

There are a few special environment variables for the rclone method.

  • RCLONE_REMOTE is the name of the remote you've configured in your rclone.conf, see remote setup.
  • RCLONE_COMPRESS_METHOD=gzip
  • DEST_DIR=/backups is the container path where the archive is temporarily created
  • RCLONE_DEST_DIR is the directory on the remote

Other parameters such as PRUNE_BACKUPS_DAYS, ZSTD_PARAMETERS, and BACKUP_NAME are all used as well.

Note that you will need to place your rclone config file in /config/rclone/rclone.conf. This can be done by adding it through docker-compose,

- ./rclone.config:/config/rclone/rclone.conf:ro

or by running the config wizard in a container and mounting the volume.

docker run -it --rm -v rclone-config:/config/rclone rclone/rclone config

then you must bind the volume for the mc-backup process

volumes: - rclone-config:/config/rclone

and the service

volumes: rclone-config: external: true

Volumes

  • /data : Should be attached read-only to the same volume as the /data of the itzg/minecraft-server container
  • /backups : The volume where incremental tgz files will be created, if using tar backup method.

Restoring tar backups

This image includes a script called restore-backup which will:

  1. Check if the $SRC_DIR (default is /data) is empty
  2. and if any files are available in $DEST_DIR (default is /backups),
  3. then un-tars the newest one into $SRC_DIR

The compose file example shows creating an "init container" to run the restore

Restoring rsync backups

This image includes a script called restore-rsync-backup which will:

  1. Check if the $SRC_DIR (default is /data) is empty
  2. and if any folders are available in $DEST_DIR (default is /backups),
  3. then rsyncs back the newest one into $SRC_DIR

The compose file example shows creating an "init container" to run the restore

On-demand backups

If you would like to kick off a backup prior to the next backup interval, you can exec the command backup now within the running backup container. For example, using the Docker Compose example where the service name is backups, the exec command becomes:

docker-compose exec backups backup now

This mechanism can also be used to avoid a long running container completely by running a temporary container, such as:

docker run --rm ...data and backup -v args... itzg/mc-backup backup now

Backup scripts

The PRE_SAVE_ALL_SCRIPT, PRE_BACKUP_SCRIPT, PRE_SAVE_ON_SCRIPT, and POST_BACKUP_SCRIPT, variables may be set to a bash script to run before and after the backup process. Potential use-cases include sending notifications, or replicating a restic repository to a remote store.

The backup waits for the server to respond to a rcon "save-on" command before running the scripts. After, the PRE_SAVE_ALL_SCRIPT is run, followed by rcon "save-off" and "save-all" commands. The, the PRE_BACKUP_SCRIPT is run, followed by the backup process. Then, the PRE_SAVE_ON_SCRIPT is run, followed by a rcon "save-on" command. Finally, the POST_BACKUP_SCRIPT is run.

Alternatively PRE_SAVE_ALL_SCRIPT_FILE PRE_BACKUP_SCRIPT_FILE, PRE_SAVE_ON_SCRIPT_FILE, and POST_BACKUP_SCRIPT_FILE may be set to the path of a script that has been mounted into the container. The file must be executable.

Note that *_FILE variables will be overridden by their non-FILE versions if both are set.

Some notes:

  • When specifying the script directly in Docker compose files any $ that are being used to refer to environment variables must be doubled up (i.e. $$) else Compose will try to substitute them

Example

With an executable file called post-backup.sh next to the compose file with the following contents

echo "Backup from $RCON_HOST to $DEST_DIR finished"

and the following compose definition

version: '3.7' services: mc: image: itzg/minecraft-server ports: - "25565:25565" environment: EULA: "TRUE" TYPE: PAPER volumes: - mc:/data backups: image: itzg/mc-backup environment: BACKUP_INTERVAL: "2h" RCON_HOST: mc PRE_BACKUP_SCRIPT: | echo "Before backup!" echo "Also before backup from $$RCON_HOST to $$DEST_DIR" POST_BACKUP_SCRIPT_FILE: /post-backup.sh volumes: # mount the same volume used by server, but read-only - mc:/data:ro # use a host attached directory so that it in turn can be backed up # to external/cloud storage - ./mc-backups:/backups - ./post-backup.sh:/post-backup.sh:ro volumes: mc: {}

Example

Kubernetes

An example StatefulSet deployment is provided in this repository.

The important part is the containers definition of the deployment:

containers: - name: mc image: itzg/minecraft-server env: - name: EULA value: "TRUE" volumeMounts: - mountPath: /data name: data - name: backup image: mc-backup imagePullPolicy: Never securityContext: runAsUser: 1000 env: - name: BACKUP_INTERVAL value: "2h 30m" volumeMounts: - mountPath: /data name: data readOnly: true - mountPath: /backups name: backups

Docker Compose

version: "3.8" services: mc: image: itzg/minecraft-server:latest ports: - "25565:25565" environment: EULA: "TRUE" TYPE: PAPER depends_on: restore-backup: condition: service_completed_successfully volumes: - ./mc-data:/data # "init" container for mc to restore the data volume when empty restore-backup: # Same image as mc, but any base image with bash and tar will work image: itzg/mc-backup restart: "no" entrypoint: restore-tar-backup volumes: # Must be same mount as mc service, needs to be writable - ./mc-data:/data # Must be same mount as backups service, but can be read-only - ./mc-backups:/backups:ro backups: image: itzg/mc-backup depends_on: mc: condition: service_healthy environment: BACKUP_INTERVAL: "2h" RCON_HOST: mc # since this service waits for mc to be healthy, no initial delay is needed INITIAL_DELAY: 0 volumes: - ./mc-data:/data:ro - ./mc-backups:/backups

Restic with rclone

Setup the rclone configuration for the desired remote location

docker run -it --rm -v rclone-config:/config/rclone rclone/rclone config

Setup the itzg/mc-backup container with the following specifics

  • Set BACKUP_METHOD to restic
  • Set RESTIC_PASSWORD to a restic backup repository password to use
  • Use rclone: as the prefix on the RESTIC_REPOSITORY
  • Append the rclone config name, colon (:), and specific sub-path for the config type

In the following example CFG_NAME and BUCKET_NAME need to be changed to specifics for the rclone configuration you created:

version: "3" services: mc: image: itzg/minecraft-server environment: EULA: "TRUE" ports: - 25565:25565 volumes: - mc:/data backup: image: itzg/mc-backup environment: RCON_HOST: mc BACKUP_METHOD: restic RESTIC_PASSWORD: password RESTIC_REPOSITORY: rclone:CFG_NAME:BUCKET_NAME volumes: # mount volume pre-configured using a host mounted file - ./rclone.conf:/config/rclone/rclone.conf # or configure one into a named volume using # docker run -it --rm -v rclone-config:/config/rclone rclone/rclone config # and change the above to # - rclone-config:/config/rclone - mc:/data:ro - backups:/backups volumes: # Uncomment this if using the config step above # rclone-config: # external: true mc: {} backups:

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

openai-agents-python

openai-agents-python

OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。

openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。

下拉加载更多