Skip to content

Instantly share code, notes, and snippets.

@lae
Created June 13, 2020 21:15
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lae/a33339a0ee670745831ddd13ecdcb157 to your computer and use it in GitHub Desktop.
minecraft server differential backup
#!/usr/bin/env bash
# Runs a backup of the Hololive Fan Minecraft server and stores it offsite.
# Bits and pieces from https://github.com/nicolaschan/minecraft-backup (MIT License)
ROOT=/home/lae/.hololive-mc
MCROOT=$ROOT/data
WORLD=world
SAVEROOT=$ROOT/backups
OFFSITE_SAVEROOT="lae@10.17.3.10:/mnt/umaru/backups/hololive-mc"
SSH_ARGS="-i $ROOT/backup.key"
# the server should accept an rsync command with the above key, authorized_keys:
# command="rsync --server -vlogDtprCze.iLsfxC --delete . /mnt/umaru/backups/hololive-mc/" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ7zsWDB0f9tJmDnMJnKUCNp79rUuIGMWfi6OAWaalf+ hololive-mc backup key
RCONCMD="docker-compose exec -T minecraft rcon-cli"
execute-command () {
local COMMAND=$1
cd $ROOT
$RCONCMD "$COMMAND"
}
message-players () {
local MESSAGE=$1
local HOVER_MESSAGE=$2
message-players-color "$MESSAGE" "$HOVER_MESSAGE" "gray"
}
message-players-error () {
local MESSAGE=$1
local HOVER_MESSAGE=$2
message-players-color "$MESSAGE" "$HOVER_MESSAGE" "red"
}
message-players-success () {
local MESSAGE=$1
local HOVER_MESSAGE=$2
message-players-color "$MESSAGE" "$HOVER_MESSAGE" "green"
}
message-players-color () {
local MESSAGE=$1
local HOVER_MESSAGE=$2
local COLOR=$3
if [[ -z "$HOVER_MESSAGE" ]]; then
echo "$MESSAGE"
execute-command "tellraw @a [\"\",{\"text\":\"[backup.sh] \",\"color\":\"gray\",\"italic\":true},{\"text\":\"$MESSAGE\",\"color\":\"$COLOR\",\"italic\":true}]"
else
echo "$MESSAGE ($HOVER_MESSAGE)"
execute-command "tellraw @a [\"\",{\"text\":\"[backup.sh] \",\"color\":\"gray\",\"italic\":true},{\"text\":\"$MESSAGE\",\"color\":\"$COLOR\",\"italic\":true,\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"$HOVER_MESSAGE\"}]}}}]"
fi
}
rdiff-backup --compare $MCROOT/$WORLD $SAVEROOT/$WORLD >/dev/null
if [[ $? -eq 0 ]]; then
echo "No differences found. Not running backup."
exit
fi
START_TIMESTAMP=$(date +%s)
echo "Creating a backup."
# Disable world autosaving
execute-command "save-off"
# Backup world
START_TIME=$(date +"%s")
rdiff-backup $MCROOT/$WORLD $SAVEROOT/$WORLD
RDIFF_STATUS=$?
rdiff-backup --remove-older-than 2W --force $SAVEROOT/$WORLD
CLEAN_STATUS=$?
sync
rsync -Cazz --info=STATS --delete -e "ssh $SSH_ARGS" $SAVEROOT/ $OFFSITE_SAVEROOT/
RSYNC_STATUS=$?
END_TIME=$(date +"%s")
# Enable world autosaving
execute-command "save-on"
# Save the world
execute-command "save-all"
# Notify players of completion
STATISTICS=$(rdiff-backup-statistics --begin-time $START_TIME $SAVEROOT/$WORLD)
CHANGED_SIZE=$(echo "$STATISTICS" | grep -oP "(?<=^TotalDestinationSizeChange ).*" | grep -oP "(?<=\().*(?=\))")
NEW_FILES=$(echo "$STATISTICS" | grep -oP "(?<=^NewFiles )\d+")
CHANGED_FILES=$(echo "$STATISTICS" | grep -oP "(?<=^ChangedFiles )\d+")
TIME_DELTA=$((END_TIME - START_TIME))
if [[ $RDIFF_STATUS -eq 0 && $CLEAN_STATUS -eq 0 && $RSYNC_STATUS -eq 0 ]]; then
echo "success: $TIME_DELTAs, $CHANGED_SIZE changed (files: $CHANGED_FILES changed, $NEW_FILES new)"
else
message-players-error "Backup failed to complete!" "Please notify @lae in #gaming."
echo -e "rdiff: $RDIFF_STATUS\nclean: $CLEAN_STATUS\nrsync: $RSYNC_STATUS"
fi
[Unit]
Description=Executes a backup of the Hololive Fan Minecraft server
Wants=holomc-backup.timer
[Service]
ExecStart=/home/lae/.hololive-mc/backup.sh
Slice=holomc-backup.slice
[Unit]
Description=Run Holo-MC backup every 8 hours
Requires=holomc-backup.service
[Timer]
Unit=holomc-backup.service
OnUnitActiveSec=1h
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment