diff options
author | KamiCreed <9517086+KamiCreed@users.noreply.github.com> | 2020-03-29 04:45:46 -0700 |
---|---|---|
committer | KamiCreed <9517086+KamiCreed@users.noreply.github.com> | 2020-03-29 04:45:46 -0700 |
commit | fc198700f0454d5f145de19bb4da31ad46feffe8 (patch) | |
tree | 465a53285650af05b5112d542f2ec7c2a8ff7317 | |
parent | 631e796812bdd9830fd2fb2f5c05d8323e67b50c (diff) | |
download | minecraft-server-tools-fc198700f0454d5f145de19bb4da31ad46feffe8.tar.gz |
Change backups to only when a player is online
Also, adds a fallback daily forced backup and error handling to prevent
concurrent backup.
-rw-r--r-- | daily-mc-backup.timer | 9 | ||||
-rw-r--r-- | mc-fbackup.service | 15 | ||||
-rwxr-xr-x | server.sh | 74 |
3 files changed, 89 insertions, 9 deletions
diff --git a/daily-mc-backup.timer b/daily-mc-backup.timer new file mode 100644 index 0000000..eef3832 --- /dev/null +++ b/daily-mc-backup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Force Backup Minecraft Server Daily + +[Timer] +OnCalendar=daily +Unit=mc-fbackup.service + +[Install] +WantedBy=multi-user.target diff --git a/mc-fbackup.service b/mc-fbackup.service new file mode 100644 index 0000000..a922b69 --- /dev/null +++ b/mc-fbackup.service @@ -0,0 +1,15 @@ +[Unit] +Description=Force backup of Minecraft Server + +[Service] +Type=simple +User=minecraftuser +Group=minecraftuser +ExecStart=/var/minecraft/server.sh fbackup +Restart=on-failure +RestartSec=10s +StartLimitInterval=10min +StartLimitBurst=8 + +[Install] +WantedBy=multi-user.target @@ -10,11 +10,15 @@ WORLD_NAME="world" BACKUP_NAME="mc-sad-squad" LOGFILE="logs/latest.log" PIDFILE="server-screen.pid" + #HOOKS BACKUP_HOOK='backup_hook_example' +#Constants +CUR_YEAR=`date +"%Y"` + function backup_hook_example { - bup -d $CUR_BACK_DIR ls $BACKUP_NAME + bup -d $CUR_BACK_DIR ls -l $BACKUP_NAME/latest/var/minecraft } function send_cmd () { @@ -82,10 +86,12 @@ function server_attach() { } function server_running() { - [ -f $PIDFILE ] && [ "$(cat $PIDFILE)" != "" ] - return # Returns the status of above conditional. Failure (1) if false, Success (0) if true. - - ps -p $(cat $PIDFILE) > /dev/null + if [ -f $PIDFILE ] && [ "$(cat $PIDFILE)" != "" ]; then + ps -p $(cat $PIDFILE) > /dev/null + return + fi + + false } function server_status() { @@ -98,10 +104,29 @@ function server_status() { exit } +function players_online() { + send_cmd "list" + sleep 1 + while [ $(tail -n 3 "$LOGFILE" | grep -c "There are") -lt 1 ] + do + sleep 1 + done + + [ `tail -n 3 "$LOGFILE" | grep -c "There are 0"` -lt 1 ] +} + + function server_backup_safe() { - echo "Detected running server. Disabling autosave" + force=$1 + echo "Detected running server. Checking if players online..." + if [ "$force" != "true" ] && ! players_online; then + echo "Players are not online. Not backing up." + return + fi + + echo "Disabling autosave" send_cmd "save-off" - send_cmd "save-all flush" + send_cmd "save-all" echo "Waiting for save... If froze, run /save-on to re-enable autosave!!" sleep 1 @@ -139,7 +164,6 @@ function server_backup_unsafe() { function create_bup_backup() { BACKUP_DIR="mc-backups" - CUR_YEAR=`date +"%Y"` CUR_BACK_DIR="mc-backups/$CUR_YEAR" if [ ! -d "$CUR_BACK_DIR" ]; then @@ -174,9 +198,31 @@ function create_backup_archive() { fi } +function backup_running() { + systemctl is-active --quiet mc-backup.service +} + +function fbackup_running() { + systemctl is-active --quiet mc-fbackup.service +} + function server_backup() { + force=$1 + + if [ "$force" = "true" ]; then + if backup_running; then + echo "A backup is running. Aborting..." + return + fi + else + if fbackup_running; then + echo "A force backup is running. Aborting..." + return + fi + fi + if server_running; then - server_backup_safe + server_backup_safe "$force" else server_backup_unsafe fi @@ -184,6 +230,10 @@ function server_backup() { exit } +function ls_bup() { + bup -d "mc-backups/${CUR_YEAR}" ls "mc-sad-squad/$1" +} + cd $(dirname $0) case $1 in @@ -203,6 +253,12 @@ case $1 in "status") server_status ;; + "fbackup") + server_backup "true" + ;; + "ls") + ls_bup $2 + ;; *) echo "Usage: $0 start|stop|attach|status|backup" ;; |