Back up your Minecraft world
A world takes hours or days to build, and a single bad command, a corrupted file or a full disk can destroy it. This article creates a script that copies the server folder on a schedule, keeps a week of backups, and shows how to restore one when you need it.
Before you begin
- The Minecraft server running as a systemd service named
minecraft(see the systemd article). - Enough free disk space for several copies of the world. Check with
df -h /. A small world often takes a few hundred megabytes per backup. - A sudo user on the server (this guide uses
alex).
Step 1: Check the space and the folders
Before you write a script, confirm what it will copy. The world folders sit inside /opt/minecraft, and the logs folder grows over time but is not needed in a backup:
df -h /
sudo du -sh /opt/minecraft/*
The du command lists the size of each item in the server folder. Expected output lists the world folders, such as world, along with logs, server.jar and the configuration files. The backup will skip logs.
Step 2: Write the backup script
The script stops the server so the world files are not changing during the copy, archives the server folder, starts the server again, and deletes backups older than seven days. Create the file:
sudo nano /usr/local/bin/backup-minecraft.sh
#!/bin/bash
set -euo pipefail
BACKUP_DIR=/var/backups/minecraft
STAMP=$(date +%F-%H%M)
mkdir -p "$BACKUP_DIR"
systemctl stop minecraft
trap 'systemctl start minecraft' EXIT
tar -czf "$BACKUP_DIR/minecraft-$STAMP.tar.gz" --exclude=logs -C /opt/minecraft .
find "$BACKUP_DIR" -name 'minecraft-*.tar.gz' -mtime +7 -delete
echo "Backup finished: minecraft-$STAMP.tar.gz"
Here is what the script does. set -euo pipefail makes it stop on the first error instead of continuing with a broken backup. The trap line starts the server again on exit, even if the archive step fails, so a failed backup never leaves the server down. tar -czf creates a compressed archive, and --exclude=logs skips the logs folder. The final find line deletes archives older than seven days.
Make the script executable and run it once by hand to test it:
sudo chmod +x /usr/local/bin/backup-minecraft.sh
sudo /usr/local/bin/backup-minecraft.sh
ls -lh /var/backups/minecraft
Expected output ends with Backup finished, and the listing shows one .tar.gz file. Confirm the server is back with sudo systemctl status minecraft --no-pager.
Step 3: Schedule the backup
cron runs commands on a schedule. Ubuntu reads files in /etc/cron.d, and each line lists a time, the user to run as, and the command. This line runs the backup every day at 4:30 in the morning, server time, and writes its output to a log file:
sudo nano /etc/cron.d/minecraft-backup
30 4 * * * root /usr/local/bin/backup-minecraft.sh >> /var/log/minecraft-backup.log 2>&1
The >> part appends output to the log, and 2>&1 sends error messages to the same file. Check the time the server uses with timedatectl, and adjust the hour if needed. Make sure the file ends with a blank line, which cron requires.
Step 4: Copy backups off the server
A backup on the same disk does not protect you if the whole server fails. Keep a copy somewhere else too. Snapshots from Afterglow, LightServe's backup feature, can complement the local archives. You can also copy the folder to a second machine with rsync over SSH, which you can run from that machine:
rsync -av alex@203.0.113.10:/var/backups/minecraft/ ~/minecraft-backups/
Replace the address and username with your own. Run the command on the machine that will hold the copies, not on the server.
Step 5: Test a restore
A backup you have never restored is only a hope. First, look inside an archive without changing anything, and check that it contains the world:
sudo tar -tzf /var/backups/minecraft/minecraft-2026-10-31-0430.tar.gz | head -n 20
To restore, stop the server, extract the archive over the server folder, make sure the files belong to the minecraft user, and start the server again:
sudo systemctl stop minecraft
sudo tar -xzf /var/backups/minecraft/minecraft-2026-10-31-0430.tar.gz -C /opt/minecraft
sudo chown -R minecraft:minecraft /opt/minecraft
sudo systemctl start minecraft
Use the real archive name from ls /var/backups/minecraft. Extracting over the top replaces current files with the archived ones, so make a fresh backup first if you want to keep the current state.
Troubleshooting
- The backup fails with "No space left on device": remove old archives or move them to another machine. Check with
df -h /, and reduce the retention period in the script if needed. - The cron job does not run: check the time with
timedatectl, then look at/var/log/minecraft-backup.logfor output. Make sure the file in/etc/cron.dis not executable. - The server stays stopped after a failed backup: run
sudo systemctl start minecraft. The trap normally handles this, so check the log for the error that stopped the script. - "Permission denied" during restore: the files were extracted as root. Run
sudo chown -R minecraft:minecraft /opt/minecraftbefore starting the server.
Next steps
Your world now has scheduled, tested backups. Review the backup folder occasionally, check that archives are growing as expected, and restore a copy to a test folder from time to time. Combine this with the resource article's advice to keep disk space free, and your server stays both safe and healthy.
Something not working? Chat or email our support team and we will help you get unstuck.
Contact support