minecraft-backup/README.md

126 lines
6.5 KiB
Markdown
Raw Permalink Normal View History

2016-09-06 23:33:42 -07:00
# Minecraft Backup
2021-03-06 14:59:30 -08:00
[![CI](https://github.com/nicolaschan/minecraft-backup/actions/workflows/ci.yml/badge.svg)](https://github.com/nicolaschan/minecraft-backup/actions/workflows/ci.yml)
2021-03-03 18:39:57 -08:00
[![codecov](https://codecov.io/gh/nicolaschan/minecraft-backup/branch/master/graph/badge.svg?token=LCbVC4TbYJ)](https://codecov.io/gh/nicolaschan/minecraft-backup)
2021-03-03 19:50:47 -08:00
Backup script for Minecraft servers on Linux.
2021-05-03 14:41:44 -07:00
Supports servers running in [screen](https://en.wikipedia.org/wiki/GNU_Screen), [tmux](https://en.wikipedia.org/wiki/Tmux), or with [RCON](https://wiki.vg/RCON) enabled. Supports `tar` file or [`restic`](https://restic.net/) backup backends.
2016-09-06 23:33:42 -07:00
## Features
- Create backups of your world folder
- Manage deletion of old backups
2021-03-03 19:52:10 -08:00
- "thin" - keep last 24 hourly, last 30 daily, and use remaining space for weekly backups
2016-09-06 23:33:42 -07:00
- "sequential" - delete oldest backup
2021-03-03 19:52:10 -08:00
- Works on vanilla (no plugins required)
- Print backup status to the Minecraft chat
2016-09-06 23:33:42 -07:00
2021-03-03 19:50:47 -08:00
## Install
2021-03-03 18:39:57 -08:00
```bash
2021-06-03 23:36:57 -07:00
wget https://github.com/nicolaschan/minecraft-backup/releases/latest/download/backup.sh
2021-03-03 18:39:57 -08:00
chmod +x backup.sh
```
2019-04-08 19:28:26 -07:00
2021-03-04 13:18:31 -08:00
Make sure your system has `tar` and your chosen compression algorithm (`gzip` by default) installed.
If using RCON, you will also need to have the [`xxd`](https://linux.die.net/man/1/xxd) command.
2021-03-03 19:50:47 -08:00
## Usage
```bash
2021-03-04 13:03:40 -08:00
# If connecting with RCON:
2021-03-04 13:05:51 -08:00
./backup.sh -c -i /home/user/server/world -o /mnt/storage/backups -s localhost:25575:secret -w rcon
2021-03-04 13:03:40 -08:00
# If running on screen called "minecraft":
2021-03-04 13:05:51 -08:00
./backup.sh -c -i /home/user/server/world -o /mnt/storage/backups -s minecraft
2021-03-04 13:03:40 -08:00
# If running on tmux session 0:
2021-03-04 13:05:51 -08:00
./backup.sh -c -i /home/user/server/world -o /mnt/storage/backups -s 0 -w tmux
2021-05-03 14:41:44 -07:00
# Using restic (and RCON)
export RESTIC_PASSWORD="restic-pass-secret" # your password here
./backup.sh -c -i /home/user/server/world -r /mnt/storage/backups-restic -s localhost:25575:secret -w rcon
# Using Docker and RCON
# You will have to set up networking so that this Docker image can access the RCON server
# In this example, the RCON server hostname is `server-host`
docker run \
-v /home/user/server/world:/mnt/server \
-v /mnt/storage/backups:/mnt/backups \
ghcr.io/nicolaschan/minecraft-backup -c -i /mnt/server -o /mnt/backups -s server-host:25575:secret -w rcon
2023-02-05 11:35:44 +01:00
# Using itzg/docker-minecraft-server container and rcon cli
./backup.sh -c -i /home/user/server/world -o /mnt/storage/backups -s container-name -w docker-rcon
2021-03-03 19:50:47 -08:00
```
2021-03-04 13:05:51 -08:00
This will show chat messages (`-c`) and save a backup of `/home/user/server/world` into `/mnt/storage/backups` using the default thinning deletion policy for old backups.
2021-03-03 19:50:47 -08:00
2019-04-08 19:28:26 -07:00
Command line options:
```text
-a Compression algorithm (default: gzip)
-c Enable chat messages
-d Delete method: thin (default), sequential, none
-e Compression file extension, exclude leading "." (default: gz)
-f Output file name (default is the timestamp)
-h Shows this help text
-H Set hostname for restic backup (restic only)
2021-06-02 20:51:24 -07:00
-i Input directory (path to world folder, use -i once for each world)
2019-04-08 19:28:26 -07:00
-l Compression level (default: 3)
-m Maximum backups to keep, use -1 for unlimited (default: 128)
-o Output directory
-p Prefix that shows in Minecraft chat (default: Backup)
-q Suppress warnings
2021-05-03 14:41:44 -07:00
-r Restic repo name (if using restic)
2023-02-05 11:35:44 +01:00
-s Screen name, tmux session name, hostname:port:password for RCON or [container name](https://github.com/itzg/docker-minecraft-server) for docker-rcon
2021-05-03 21:20:03 -07:00
-t Enable lock file (lock file not used by default)
-u Lock file timeout seconds (empty = unlimited)
2019-04-08 19:28:26 -07:00
-v Verbose mode
2021-05-03 21:20:03 -07:00
-w Window manager: screen (default), tmux, RCON
2019-04-08 19:28:26 -07:00
```
2021-03-03 19:50:47 -08:00
### Automate backups with cron
- Edit the crontab with `crontab -e`
- Example for hourly backups:
```
2021-03-04 13:05:51 -08:00
00 * * * * /path/to/backup.sh -c -i /home/user/server/world -o /mnt/storage/backups -s minecraft
2019-04-08 19:28:26 -07:00
```
## Retrieving Backups
Always test your backups! Backups are in the `tar` format and compressed depending on the option you choose. To restore, first decompress if necessary and then extract using tar. You may be able to do this in one command if `tar` supports your compression option, as is the case with `gzip`:
Example:
```bash
mkdir restored-world
cd restored-world
tar -xzvf /path/to/backups/2019-04-09_02-15-01.tar.gz
```
The restored worlds should be inside the `restored-world` directory, possibly nested under the parent directories. Then you can move your restored world to your Minecraft server folder under the proper name and path so the Minecraft server uses it.
2019-04-08 19:28:26 -07:00
2021-05-03 14:41:44 -07:00
### With `restic`
Use [`restic restore`](https://restic.readthedocs.io/en/latest/050_restore.html) to restore from backup.
2021-03-03 19:50:47 -08:00
## Why not use `tar` directly?
2021-03-03 22:12:28 -08:00
If you use `tar` while the server is running, you will likely get an error like this because Minecraft autosaves the world periodically:
2021-03-03 19:50:47 -08:00
```
tar: /some/path/here/world/region/r.1.11.mca: file changed as we read it
```
2021-03-03 22:12:28 -08:00
To fix this problem, the backup script disables autosaving with the `save-off` Minecraft command before running `tar` and then re-enables autosaving after `tar` is done.
2021-03-03 19:50:47 -08:00
2016-09-06 23:33:42 -07:00
## Help
- Make sure the compression algorithm you specify is installed on your system. (zstd is not installed by default)
- Make sure your compression algorithm is in the crontab's PATH
- Make sure cron has permissions for all the files involved and access to the Minecraft server's GNU Screen
- It's surprising how much space backups can take--make sure you have enough empty space
- Do not put trailing `/` in the `SERVER_DIRECTORY` or `BACKUP_DIRECTORY`
- If "thin" delete method is behaving weirdly, try emptying your backup directory or switch to "sequential"
2021-03-03 19:50:47 -08:00
2021-03-04 13:24:08 -08:00
## Known Issues
There is a Minecraft bug [MC-217729](https://bugs.mojang.com/projects/MC/issues/MC-217729) in recent Minecraft server versions that cause them to automatically save the world even after receiving the `save-off` command. Until this is fixed, there is a chance that the backup will fail because the world files are modified by Minecraft in the process of creating the backup. This script will try to detect and report this problem if it does occur.
2021-03-03 19:50:47 -08:00
## Disclaimer
Backups are essential to the integrity of your Minecraft world. You should automate regular backups and **check that your backups work**. It is up to you to make sure that your backups work and that you have a reliable backup policy.
Some backup tips:
- Drives get corrupted or fail! Backup to a _different_ drive than the one your server is running on, so if your main drive fails then you have backups.
- _Automate_ backups so you never lose too much progress.
- Check that your backups work from time to time.
Please refer to the LICENSE (MIT License) for the full legal disclaimer.