Skip to content

Instantly share code, notes, and snippets.

@marcsello
Created October 20, 2020 19:42
Show Gist options
  • Save marcsello/0de1adc3dac9f124d8b3aa971d114e54 to your computer and use it in GitHub Desktop.
Save marcsello/0de1adc3dac9f124d8b3aa971d114e54 to your computer and use it in GitHub Desktop.
Script that safely creates the backups for the EZO.TV Minecraft server
#!/bin/bash
# Requirements:
# - mcrcon: https://github.com/Tiiffi/mcrcon
# - sentry-cli: https://github.com/getsentry/sentry-cli
# configure
SENTRY_DSN=
export SENTRY_DSN
MCRCON_HOST=127.0.0.1
export MCRCON_HOST
MCRCON_PASS=
export MCRCON_PASS
LOG_FILE="/tmp/last_ezobackup.log"
# setup sentry
eval "$(/opt/ezobackup/sentry-cli bash-hook)"
# only minecraft user should run this script
if [[ "$EUID" -ne $(id -u minecraft) ]]; then
echo "De minecraft user pls"
exit 1
fi
# Setup logging stuff
rm -f $LOG_FILE # remove leftover from last run
function log_print {
echo "$1" | tee -a "$LOG_FILE"
}
function die {
log_print "$1"
/opt/ezobackup/sentry-cli send-event -m "$1" --logfile "$LOG_FILE"
exit 0 # sentry should already took care of reporting the error
}
# start backup!!!
# prepare
FILENAME="backup_"$(date +"%Y_%m_%d_%H_%M_%S")".tar.gz" # Do not forget to adjust the delete rule bellow
log_print "Creating $FILENAME"
if ! /usr/local/bin/mcrcon "say Biztonsági mentés..."; then
die "Could not communicate with the minecraft server!"
fi
log_print "Flushing map, and disabling saving..."
/usr/local/bin/mcrcon -w 1 "save-off" "save-all flush"
sleep 2 # Make sure it's really saved
# From now on, Minecraft server is not supposed to touch the world direcotry
INCOMPLETE_ARCHIVE_PATH=$(mktemp -p /opt/ezobackup/tmp/ incompleteXXXXXX.tar.gz)
log_print "Creating archive ..."
log_print "Saving incomplete output to $INCOMPLETE_ARCHIVE_PATH"
tar --owner=0 --group=0 --numeric-owner -cf - -C /opt/minecraft/ world/ world_nether/ world_the_end/ | nice -10 pigz -9 > "$INCOMPLETE_ARCHIVE_PATH"
log_print "Re-enabling autosave..."
/usr/local/bin/mcrcon "save-on"
log_print "Moving complete backup to destination..."
mv -fuv "$INCOMPLETE_ARCHIVE_PATH" "/mnt/backup/backups/$FILENAME"
/usr/local/bin/mcrcon "say Biztonsági mentés kész! Fájlnév: $FILENAME"
log_print "Deleting old backups..."
# delete files older than seven days
find /mnt/backup/backups/ -name 'backup_*.tar.gz' -type f -mtime +7 -delete
log_print "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment