#!/bin/bash
#
# Minecraft Incremental Backup Script
# Author: Randy (cncr04s) (cncr04s@gmail.com)
# Modified By: Joseph Gullo (surfrock66) (surfrock66@surfrock66.com)
#
# This script makes an incremental backup of the current set of maps, then
# pushes the old backups to an older folder. The result is incremental
# backups with 30 minute intervals, stored in folders with names
# indicating their age. The key to this script is that backups
# only store changed files from the most recent backup, so there's less
# reads from the current map, reducing the chance of errors in frequently
# changed chunk files.
#
# This is the name of the screen holding the server process
NAME="Bukkit"
# This is the description of the server, used in sending in-game notices
DESC="teh3l3m3nts Minecraft Server"
echo -n "Backing up $DESC"
echo -n " "
screen -dr "$NAME" -p 0 -X stuff "$(printf "\r")"
# Commit the server to do a write-to-disc for all chunks
screen -dr "$NAME" -p 0 -X stuff "$(printf "save-all\r")"
# Send message to the server indicating beginning of backup, mostly as
# a notice to players that there may be lag
screen -dr "$NAME" -p 0 -X stuff "$(printf "say Backing up world(s)\r")"
sleep 3
# Move into the Backup directory; all following commands use relative
# direcory references
cd /Backup
# Delete the oldest backup
rm -rf backup.60
# Move each backup up the folder chain, incrementing by age
mv backup.59 backup.60
mv backup.58 backup.59
mv backup.57 backup.58
mv backup.56 backup.57
mv backup.55 backup.56
mv backup.54 backup.55
mv backup.53 backup.54
mv backup.52 backup.53
mv backup.51 backup.52
mv backup.50 backup.51
mv backup.49 backup.50
mv backup.48 backup.49
mv backup.47 backup.48
mv backup.46 backup.47
mv backup.45 backup.46
mv backup.44 backup.45
mv backup.43 backup.44
mv backup.42 backup.43
mv backup.41 backup.42
mv backup.40 backup.41
mv backup.39 backup.40
mv backup.38 backup.39
mv backup.37 backup.38
mv backup.36 backup.37
mv backup.35 backup.36
mv backup.34 backup.35
mv backup.33 backup.34
mv backup.32 backup.33
mv backup.31 backup.32
mv backup.30 backup.31
mv backup.29 backup.30
mv backup.28 backup.29
mv backup.27 backup.28
mv backup.26 backup.27
mv backup.25 backup.26
mv backup.24 backup.25
mv backup.23 backup.24
mv backup.22 backup.23
mv backup.21 backup.22
mv backup.20 backup.21
mv backup.19 backup.20
mv backup.18 backup.19
mv backup.17 backup.18
mv backup.16 backup.17
mv backup.15 backup.16
mv backup.14 backup.15
mv backup.13 backup.14
mv backup.12 backup.13
mv backup.11 backup.12
mv backup.10 backup.11
mv backup.09 backup.10
mv backup.08 backup.09
mv backup.07 backup.08
mv backup.06 backup.07
mv backup.05 backup.06
mv backup.04 backup.05
mv backup.03 backup.04
mv backup.02 backup.03
mv backup.01 backup.02
mv backup.00 backup.01
# Create a new copy of the last backup, using --archive and -links
# To explain those flags, look at these examples:
# cp -al file1 file2
# To really delete the file, you have to delete file1 and file2.
# If applied to a directory, "cp -al" creates hard links (equivalent to "ln")
# recursively for all files inside the directory.
# This means that if the "original directory" is deleted the
# "copied link" does not lose its target.
# Practically, this means that unchanged files only exist once.
cp -al backup.01/. backup.00
# This pushes changes from the server directory into the copy of
# the most recent backup. It does 2 things; ignores the dynmap
# tiles, and deletes any files it's overwriting. Also uses the
# -a archive flag, with use similar to that above.
rsync -a --exclude 'tiles' --delete /var/bukkit/ backup.00/
# Store the date and time in a variable; write that variable to a file
_now=$(date +"%Y_%m_%d_%k_%M")
echo $_now > backup.00/$_now.txt
sleep 3
# Return the console back to the bukkit directory
cd /var/bukkit
# Send messages saying backups are complete
echo " done."
screen -dr "$NAME" -p 0 -X stuff "$(printf "say Backup complete.\r")"