Skip to content

Instantly share code, notes, and snippets.

@johnmlang
Last active December 17, 2015 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmlang/53d257b262b652883b1b to your computer and use it in GitHub Desktop.
Save johnmlang/53d257b262b652883b1b to your computer and use it in GitHub Desktop.
Automatic backup and map rendering script for the Survival Inc Minecraft server.
#!/bin/bash
## This script does following tasks daily for a Minecraft world:
## - Downloads the world (and related server files) from the server.
## - Checks the world for corruption errors.
## - Generates a backup archive.
## - Rotates existing backups. The the six most recent daily, three most recent
## weekly, and all monthly backups are kept.
## https://gist.github.com/johnmlang/53d257b262b652883b1b
## Add as cron job to run daily at 2AM:
## $ crontab -e
## 0 2 * * * /home/minecraft/MapBackup/map-backup.sh
## Define server IP address and port,
SERVERIP=
SERVERPORT=
## Owning user of the Minecraft files, login via SSH keys
SFTPUSER=
## Remote path to the Minecraft instance that will be backed-up
SFTPPATH=
## Local base path for map backup files
ROOT=/home/minecraft/MapBackup
## Directory to download files to be backuped in ${ROOT}
BACKUPDIR=Survival-Inc
## Path to World directory
WORLDDIR=${ROOT}/${BACKUPDIR}/Skyline
## Path to log file
LOGFILE=${ROOT}/logs/$( date +%Y%m%d ).log
## Define path to Dinnerbone's server query script
## https://github.com/Dinnerbone/mcstatus
QUERY=${ROOT}/minecraft_query-cli.py
## Define path to the archive rotation script
## https://github.com/maxharp3r/archive-rotator
ROTATE=${ROOT}/archive_rotator.py
echo $(date)": Beginning Backup..." >> ${LOGFILE}
## Query the server and write output to log for diagnostics
python ${QUERY} -p ${SERVERPORT} ${SERVERIP} >> ${LOGFILE}
## Download all server files (excluding backups)
echo "============= SFTP Download =============" >> ${LOGFILE}
mkdir -p ${ROOT}/${BACKUPDIR}
cd ${ROOT}
nice /usr/bin/rsync -a \
--rsync-path="nice rsync" \
--log-file=${ROOT}/rsync.log \
-e "/usr/bin/ssh" \
${SFTPUSER}@${SERVERIP}:${SFTPPATH}/ \
${ROOT}/${BACKUPDIR}/ \
--exclude=backups \
--delete
chmod +rwX ${ROOT}/${BACKUPDIR}
cat rsync.log >> ${LOGFILE}
rm rsync.log
## Check world for corruption using Minecraft Region Fixer
## https://github.com/Fenixin/Minecraft-Region-Fixer
echo "======== World Corruption Check ========" >> ${LOGFILE}
cd ${ROOT}
rm -r Minecraft-Region-Fixer-master/
## Download latest version and unzip
wget https://github.com/Fenixin/Minecraft-Region-Fixer/archive/master.zip
unzip master.zip
rm master.zip
## Scan the world
cd Minecraft-Region-Fixer-master/
python region-fixer.py ${WORLDDIR} -l region-fixer.log
## Copy log to world directory and append the scan-log to the main log
cp region-fixer.log ${WORLDDIR}
cat region-fixer.log >> ${LOGFILE}
cd ${ROOT}
## Archive downloaded files, compress using lzma
echo "============ World Archive =============" >> ${LOGFILE}
tar -Jcvf Backups/Survival-Inc.tar.xz -C ${ROOT}/ ${BACKUPDIR}/ >> ${LOGFILE}
## Rotate Archives. Keep 6 daily, 3 weekly, and 100 (or more) monthly backups.
python ${ROTATE} --ext .tar.xz --tiered -v -n 6 -n 3 -n 100 \
Backups/Survival-Inc.tar.xz >> ${LOGFILE}
echo $(date)": Backup Complete!" >> ${LOGFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment