minecraft-backup/README.md

207 lines
7.7 KiB
Markdown
Raw Normal View History

2016-09-06 23:33:42 -07:00
# Minecraft Backup
2020-03-21 20:14:33 -07:00
Backup script for Linux servers running a Minecraft server in a GNU Screen.
2016-09-06 23:33:42 -07:00
2020-03-21 20:54:21 -07:00
## Quick Start
```bash
# Download the scripts
git clone https://github.com/nicolaschan/minecraft-backup.git
./minecraft-backup/backup.sh -c -s $SCREEN_NAME -i $WORLD_DIR -o $BACKUP_DIR
```
2020-03-21 20:14:33 -07:00
## Why?
### Why not just put `tar` in crontab?
If the Minecraft server is currently running, you need to disable world autosaving, or you will likely get an error like this:
```
tar: /some/path/here/world/region/r.1.11.mca: file changed as we read it
```
This script will take care of disabling and then re-enabling autosaving for you, and also alert players in the chat of successful backups or errors.
This way, you don't have to shut down the server to take backups.
2020-03-21 21:01:15 -07:00
You'll also probably need some way to delete old backups, and this script can handle keeping either a number of most recent backups, or thinning them out based on hour/day/week. It can also use another backend such as [restic](https://github.com/restic/restic).
2019-04-08 19:28:26 -07:00
2020-03-21 20:14:33 -07:00
### Alternatives
This script is developed with vanilla servers in mind. If you are running a server with plugins or mods, then you can probably find a backup plugin/mod to do a similar job.
2016-09-06 23:33:42 -07:00
## Features
- Create backups of your world folder
- Manage deletion of old backups
- "thin" - keep last 24 hourly, last 30 daily, and use remaining space for monthly backups
- "sequential" - delete oldest backup
- Choose your own compression algorithm (tested with: `gzip`, `xz`, `zstd`)
2020-03-21 20:14:33 -07:00
- Print backup status and info to the Minecraft chat
2020-03-21 20:54:21 -07:00
- Customizable backup backends and Minecraft server interface (currently supports locally managed tar archives or [restic](https://github.com/restic/restic))
2016-09-06 23:33:42 -07:00
## Requirements
2020-03-21 20:14:33 -07:00
- Linux computer (tested on Arch Linux)
2016-09-06 23:33:42 -07:00
- GNU Screen (running your Minecraft server)
2020-03-21 20:14:33 -07:00
- Minecraft server
2016-09-06 23:33:42 -07:00
## Installation
2020-03-21 20:14:33 -07:00
```bash
# Download the scripts
git clone https://github.com/nicolaschan/minecraft-backup.git
```
*NOTE*: You will need to keep `backup.sh` in the same directory as the `src/` directory, since it looks for dependencies in `src/`.
2019-04-08 19:28:26 -07:00
2020-03-21 20:14:33 -07:00
## Usage Options
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)
2020-03-21 20:14:33 -07:00
-g Do not backup (exit) if screen is not running (default: always backup)
2019-04-08 19:28:26 -07:00
-h Shows this help text
-i Input directory (path to world folder)
-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
-s Minecraft server screen name
-v Verbose mode
```
2020-03-21 20:14:33 -07:00
## Example Usage
### One-off Example
```bash
./backup.sh -c -s minecraft -i minecraft-server/world -o backups/
```
In this example, we print the status to the Minecraft chat (`-c`) and save a backup of `minecraft-server/world` into `backups/` using the default thinning delete policy for old backups. While this works for performing a single backup, it is _highly_ recommended that you automate your backups.
### Automated with cron
- Edit the crontab:
2019-04-08 19:28:26 -07:00
```bash
2020-03-21 20:14:33 -07:00
crontab -e
```
- Example for hourly backups:
```
00 * * * * /path/to/minecraft-backup/backup.sh -c -s minecraft -i /path/to/minecraft-server/world -o /path/to/backups
2019-04-08 19:28:26 -07:00
```
2020-03-21 20:54:21 -07:00
### Automated using systemd timers
#### Simple example (single server)
`~/.config/systemd/user/minecraft-backup.timer`
```systemd
[Unit]
Description=Run Minecraft backup hourly
[Timer]
OnCalendar=hourly
Persistent=false
Unit=minecraft-backup.service
[Install]
WantedBy=timers.target
```
`~/.config/systemd/user/minecraft-backup.service`
```systemd
[Unit]
2020-03-21 21:00:42 -07:00
Description=Perform Minecraft backup
2020-03-21 20:54:21 -07:00
[Service]
Type=oneshot
ExecStart=/path/to/minecraft-backup/backup.sh -c -s minecraft -i /path/to/world -o /path/to/backups
[Install]
WantedBy=multi-user.target
```
2020-03-21 21:00:42 -07:00
Then you can run the following to enable the timer:
```bash
# enable the timer right now only
systemd --user start minecraft-backup.timer
# start the timer on reboot
systemd --user enable minecraft-backup.timer
# see status of timers
systemd --user list-timers
```
2020-03-21 20:54:21 -07:00
#### Advanced example (with restic and multiple servers)
2020-03-21 21:00:42 -07:00
If you have multiple servers, you can use `@` to create timers on-demand for each server. This assumes the server directories are named the same as the screen name.
2020-03-21 20:54:21 -07:00
`~/.config/systemd/user/minecraft-backup.timer`
```systemd
[Unit]
Description=Run Minecraft backup hourly
[Timer]
OnCalendar=hourly
Persistent=false
Unit=minecraft-backup@.service
[Install]
WantedBy=timers.target
```
`~/.config/systemd/user/minecraft-backup@.service`
```systemd
[Unit]
2020-03-21 21:00:42 -07:00
Description=Perform Minecraft backup
2020-03-21 20:54:21 -07:00
[Service]
Type=oneshot
Environment="RESTIC_PASSWORD_FILE=/path/to/restic-password.txt"
ExecStart=/path/to/minecraft-backup/backup-restic.sh -c -s %i -i /path/to/server/%i/world -o /path/to/restic-repo
[Install]
WantedBy=multi-user.target
```
2020-03-21 21:00:42 -07:00
To enable:
```bash
systemd --user enable minecraft-backup@your_server_name_here
```
2019-04-08 19:28:26 -07:00
## Retrieving Backups
2020-03-21 20:14:33 -07:00
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`:
2019-04-08 19:28:26 -07:00
Example:
```bash
mkdir restored-world
2020-03-21 20:14:33 -07:00
# if using gzip:
gzip -cd 2017-07-31_00-00-00.tar.gz | tar xf - -C restored-world
# if using zstd:
zstd -cd 2017-07-31_00-00-00.tar.zst | tar xf - -C restored-world
2019-04-08 19:28:26 -07:00
```
Then you can move your restored world (`restored-world` in this case) to your Minecraft server folder and rename it (usually called `world`) so the Minecraft server uses it.
2020-03-21 20:14:33 -07:00
## Using [restic](https://github.com/restic/restic)
The `backup-restic.sh` script provides a similar interface for restic.
To specify your repository's password, you'll need to export the `$RESTIC_PASSWORD_FILE` or `$RESTIC_PASSWORD_COMMAND` environment variable.
```bash
restic init -r /path/to/restic-backups
touch restic-password.txt # make a new file for your restic password
chmod 600 restic-password.txt # make sure only you can read your password
echo "my_restic-password" > restic_password.txt
export RESTIC_PASSWORD_FILE=$(pwd)/restic_password.txt
/path/to/minecraft-backup/backup-restic.sh -c -s minecraft -i /path/to/minecraft-server/world -o /path/to/restic-backups
```
When you automate this, you can set the environment variable in the command like so:
```bash
RESTIC_PASSWORD_FILE=/path/to/restic-password.txt /path/to/minecraft-backup/backup-restic.sh -c -s minecraft -i /path/to/minecraft-server/world -o /path/to/restic-backups
```
2016-09-06 23:33:42 -07:00
## Help
2020-03-21 20:14:33 -07:00
- Make sure the compression algorithm you specify is installed on your system. (zstd is not always installed by default)
2016-09-06 23:33:42 -07:00
- 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
- If "thin" delete method is behaving weirdly, try emptying your backup directory or switch to "sequential"
2020-03-21 20:14:33 -07: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 that 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.