Skip to content

Instantly share code, notes, and snippets.

@SafPlusPlus
Created February 17, 2015 17:05
Show Gist options
  • Save SafPlusPlus/70c96266c7808f529c55 to your computer and use it in GitHub Desktop.
Save SafPlusPlus/70c96266c7808f529c55 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Author: [email protected]
# attempt at decent backup script
# Some inspiration drawn from http://wiki.canarymod.net/Install_Canary
INSTANCE="tk_prod"
SESSION_NAME="tk_prod"
USER="tk_prod"
ME=`whoami`
as_user() {
if [ "$ME" == "$USERNAME" ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
server_command() {
sudo -u ${USER} screen -p 0 -S ${SESSION_NAME} -X eval "stuff '$1'\015"
}
server_announce() {
server_command "say $1"
}
# detect screen of correct minecraft instance
# message about imminent backup
#server_announce "§7Server snapshot backup starting, going read-only..."
echo "Notified players..."
# autosave off, so no random stuff gets saved too soon after the explicit save ans stuff gets inconsistent
server_command "save-off"
echo "Turned autosave off..."
# explicit save
server_command "save-all"
echo "Commanded explicit save..."
sleep 3 # wait a bit
# flush disk buffers with sync
/bin/sync
echo "Flushing disk buffers..."
sleep 1
# make snapshot
/sbin/btrfs subvolume snapshot /opt/minecraft/instances/${INSTANCE}/ /opt/minecraft/instances/${INSTANCE}.snapshot
# autosave on + message
server_command "save-on"
#server_announce "§7Snapshot completed, going read-write again and backing up the snapshot..."
echo "Turned autosave on again and notified players..."
# incremental rsync
source_dir=/opt/minecraft/instances/${INSTANCE}.snapshot/
target_dir=/opt/backup/minecraft/instances/${INSTANCE}/`date +%Y-%m-%d`/`date +%H.%M`/
# rsync -a --delete --link-dest=../backup.1 source_directory/ backup.0/
mkdir -p ${target_dir}
# rsync --verbose -a --delete ${source_dir} ${target_dir} # for initial backup
ionice -c3 rsync -a --delete --link-dest=/opt/backup/minecraft/instances/${INSTANCE}/latest_backup ${source_dir} ${target_dir} # for incremental backups
unlink /opt/backup/minecraft/instances/${INSTANCE}/latest_backup
ln -s ${target_dir} /opt/backup/minecraft/instances/${INSTANCE}/latest_backup
# remove snapshot
/sbin/btrfs subvolume delete /opt/minecraft/instances/${INSTANCE}.snapshot
#server_announce "§7All done turning the snapshot into an incremental backup, have a nice day! :)"
server_announce "§7(backup partition is `df -h /opt/backup/ | grep backup | awk '{print $5}'` full)"
echo "===== Backup finished at `date +%H:%M:%S` ====="
# optional thinning
# message status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment