From ac7b43637873f0b4a3d370939343f98e30f95715 Mon Sep 17 00:00:00 2001 From: KamiCreed <9517086+KamiCreed@users.noreply.github.com> Date: Wed, 25 Mar 2020 03:09:52 -0700 Subject: Use tmux and bup for screen and backups, respectively --- minecraft.service | 8 +++-- server.sh | 93 +++++++++++++++++++++++++++++++++---------------------- 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/minecraft.service b/minecraft.service index f261625..2565bc8 100644 --- a/minecraft.service +++ b/minecraft.service @@ -5,12 +5,16 @@ Description=Minecraft Server [Service] Type=forking -User=minecraft -Group=minecraft +User=minecraftuser +Group=minecraftuser PIDFile=/var/minecraft/server-screen.pid +Restart=on-failure +RestartSec=30s ExecStart=/var/minecraft/server.sh start TimeoutStopSec= 180 ExecStop=/var/minecraft/server.sh stop +StartLimitInterval=10min +StartLimitBurst=2 [Install] WantedBy=multi-user.target diff --git a/server.sh b/server.sh index 9b8156b..5dd0e0f 100755 --- a/server.sh +++ b/server.sh @@ -2,32 +2,27 @@ #CONFIG JRE_JAVA="java" -JVM_ARGS="-Xms512M -Xmx512M" -JAR="server.jar" +JVM_ARGS="-Xms4096M -Xmx6144M" +JAR="minecraft_server.jar" JAR_ARGS="-nogui" -SCREEN_WINDOW="minecraftserverscreen" +SCREEN_WINDOW="minecraft" WORLD_NAME="world" -LOGFILE="server-screen.log" +BACKUP_NAME="mc-sad-squad" +LOGFILE="logs/latest.log" PIDFILE="server-screen.pid" #HOOKS BACKUP_HOOK='backup_hook_example' function backup_hook_example { - echo $ARCHNAME + bup -d $CUR_BACK_DIR ls $BACKUP_NAME } function send_cmd () { - screen -S $SCREEN_WINDOW -p 0 -X stuff "$1^M" + tmux send -t $SCREEN_WINDOW "$1" enter } function assert_running() { - if [ ! -e $PIDFILE ]; then - return - fi - - ps -p $(cat $PIDFILE) > /dev/null - if [ $? -eq 0 ] - then + if server_running; then echo "It seems a server is already running. If this is not the case,\ manually attach to the running screen and close it." exit 1 @@ -35,14 +30,7 @@ function assert_running() { } function assert_not_running() { - if [ ! -e $PIDFILE ]; then - echo "Server not running" - exit 1 - fi - - ps -p $(cat $PIDFILE) > /dev/null - if [ ! $? -eq 0 ] - then + if ! server_running; then echo "Server not running" exit 1 fi @@ -51,21 +39,24 @@ function assert_not_running() { function server_start() { assert_running - if [ ! -e "eula.txt" ] + if [ ! -f "eula.txt" ] then echo "eula.txt not found. Creating and accepting EULA." - echo "eula=true" > eula.txt + echo "eula=true" > "eula.txt" fi - rm -f $LOGFILE - screen -L -Logfile "$LOGFILE" -S $SCREEN_WINDOW -p 0 -D -m \ - $JRE_JAVA $JVM_ARGS -jar $JAR $JAR_ARGS > /dev/null & - echo $! > $PIDFILE - echo Started with PID $! + tmux new-session -s $SCREEN_WINDOW -d \ + $JRE_JAVA $JVM_ARGS -jar $JAR $JAR_ARGS + pid=`tmux list-panes -t $SCREEN_WINDOW -F "#{pane_pid}"` + echo $pid > $PIDFILE + echo Started with PID $pid exit } function server_stop() { + # Allow success even if server is not running + #trap "exit 0" EXIT + assert_not_running send_cmd "stop" @@ -86,14 +77,19 @@ function server_stop() { function server_attach() { assert_not_running - screen -r -p 0 $SCREEN_WINDOW + tmux attach -t $SCREEN_WINDOW exit } -function server_status() { +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 [ $? -eq 0 ] +function server_status() { + if server_running then echo "Server is running" else @@ -113,9 +109,10 @@ function server_backup_safe() { do sleep 1 done + sleep 2 echo "Done! starting backup..." - create_backup_archive + create_bup_backup local RET=$? echo "Re-enabling auto-save" @@ -131,7 +128,7 @@ function server_backup_safe() { function server_backup_unsafe() { echo "No running server detected. Running Backup" - create_backup_archive + create_bup_backup if [ $? -eq 0 ] then @@ -140,6 +137,29 @@ function server_backup_unsafe() { fi } +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 + mkdir -p "$CUR_BACK_DIR" + fi + + + bup -d "$CUR_BACK_DIR" index "$WORLD_NAME" + status=$? + if [ $status -eq 1 ]; then + bup -d "$CUR_BACK_DIR" init + bup -d "$CUR_BACK_DIR" index "$WORLD_NAME" + fi + + bup -d "$CUR_BACK_DIR" save -n "$BACKUP_NAME" "$WORLD_NAME" + + echo "Backup using bup to $CUR_BACK_DIR is complete" +} + +# Deprecated function create_backup_archive() { ARCHNAME="backup/$WORLD_NAME-backup_`date +%d-%m-%y-%T`.tar.gz" tar -czf "$ARCHNAME" "./$WORLD_NAME" @@ -155,11 +175,9 @@ function create_backup_archive() { } function server_backup() { - screen -list $SCREEN_WINDOW > /dev/null - if [ $? -eq 0 ] - then #Server is running + if server_running; then server_backup_safe - else #Not running + else server_backup_unsafe fi @@ -181,6 +199,7 @@ case $1 in "backup") server_backup ;; + # TODO: Add restore command "status") server_status ;; -- cgit v1.2.3 From 631e796812bdd9830fd2fb2f5c05d8323e67b50c Mon Sep 17 00:00:00 2001 From: KamiCreed <9517086+KamiCreed@users.noreply.github.com> Date: Wed, 25 Mar 2020 04:25:45 -0700 Subject: Add automated backup systemd scripts --- Readme.md | 11 +++++++++++ mc-backup.service | 15 +++++++++++++++ mc-backup.timer | 8 ++++++++ minecraft.service | 2 -- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 mc-backup.service create mode 100644 mc-backup.timer diff --git a/Readme.md b/Readme.md index b794932..757e394 100644 --- a/Readme.md +++ b/Readme.md @@ -45,6 +45,17 @@ Place `minecraft.service` in `/etc/systemd/system/` and run `systemctl start minecraft` to start once or `systemctl enable minecraft` to enable autostarting. +To backup automatically, place or symlink `mc-backup.service` and +`mc-backup.timer` in `/etc/systemd/system/`. Run the following: + +``` +sudo systemctl enable mc-backup.timer +sudo sytemctl start mc-backup.timer +``` + +This wil start the enable the timer upon startup and start the timer +to run the backup after every interval specified in mc-backup.timer. + ## Disclaimer The scripts are provided as-is at no warranty. diff --git a/mc-backup.service b/mc-backup.service new file mode 100644 index 0000000..d94656c --- /dev/null +++ b/mc-backup.service @@ -0,0 +1,15 @@ +[Unit] +Description=Automatic backup of Minecraft Server + +[Service] +Type=simple +User=minecraftuser +Group=minecraftuser +ExecStart=/var/minecraft/server.sh backup +Restart=on-failure +RestartSec=10s +StartLimitInterval=10min +StartLimitBurst=3 + +[Install] +WantedBy=multi-user.target diff --git a/mc-backup.timer b/mc-backup.timer new file mode 100644 index 0000000..986ff42 --- /dev/null +++ b/mc-backup.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Backup Minecraft Server every 30 mins + +[Timer] +OnCalendar=*:0/30 + +[Install] +WantedBy=multi-user.target diff --git a/minecraft.service b/minecraft.service index 2565bc8..5f2a6c4 100644 --- a/minecraft.service +++ b/minecraft.service @@ -13,8 +13,6 @@ RestartSec=30s ExecStart=/var/minecraft/server.sh start TimeoutStopSec= 180 ExecStop=/var/minecraft/server.sh stop -StartLimitInterval=10min -StartLimitBurst=2 [Install] WantedBy=multi-user.target -- cgit v1.2.3 From fc198700f0454d5f145de19bb4da31ad46feffe8 Mon Sep 17 00:00:00 2001 From: KamiCreed <9517086+KamiCreed@users.noreply.github.com> Date: Sun, 29 Mar 2020 04:45:46 -0700 Subject: Change backups to only when a player is online Also, adds a fallback daily forced backup and error handling to prevent concurrent backup. --- daily-mc-backup.timer | 9 +++++++ mc-fbackup.service | 15 +++++++++++ server.sh | 74 ++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 daily-mc-backup.timer create mode 100644 mc-fbackup.service 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 diff --git a/server.sh b/server.sh index 5dd0e0f..6dd2db8 100755 --- a/server.sh +++ b/server.sh @@ -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" ;; -- cgit v1.2.3 From 2fc13856de14f580c8e688f8fd91faf321658f8b Mon Sep 17 00:00:00 2001 From: KamiCreed <9517086+KamiCreed@users.noreply.github.com> Date: Sun, 29 Mar 2020 05:03:57 -0700 Subject: Change username back --- minecraft.service | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minecraft.service b/minecraft.service index 5f2a6c4..428c9af 100644 --- a/minecraft.service +++ b/minecraft.service @@ -5,8 +5,8 @@ Description=Minecraft Server [Service] Type=forking -User=minecraftuser -Group=minecraftuser +User=minecraft +Group=minecraft PIDFile=/var/minecraft/server-screen.pid Restart=on-failure RestartSec=30s -- cgit v1.2.3 From d0d55a23581bd792abc63557a83713720b1307f9 Mon Sep 17 00:00:00 2001 From: KamiCreed <9517086+KamiCreed@users.noreply.github.com> Date: Sun, 29 Mar 2020 05:08:13 -0700 Subject: Add TODO to incorporate previous backup method --- server.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.sh b/server.sh index 6dd2db8..1654d3c 100755 --- a/server.sh +++ b/server.sh @@ -183,7 +183,7 @@ function create_bup_backup() { echo "Backup using bup to $CUR_BACK_DIR is complete" } -# Deprecated +# TODO: Make default .tar with optional bup function create_backup_archive() { ARCHNAME="backup/$WORLD_NAME-backup_`date +%d-%m-%y-%T`.tar.gz" tar -czf "$ARCHNAME" "./$WORLD_NAME" -- cgit v1.2.3 From cee1fc32e72679261ba65a8ceafab5b94617517f Mon Sep 17 00:00:00 2001 From: KamiCreed <9517086+KamiCreed@users.noreply.github.com> Date: Mon, 30 Mar 2020 00:04:05 -0700 Subject: Fix user for backup services --- mc-backup.service | 6 +++--- mc-fbackup.service | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mc-backup.service b/mc-backup.service index d94656c..0ca0718 100644 --- a/mc-backup.service +++ b/mc-backup.service @@ -3,11 +3,11 @@ Description=Automatic backup of Minecraft Server [Service] Type=simple -User=minecraftuser -Group=minecraftuser +User=minecraft +Group=minecraft ExecStart=/var/minecraft/server.sh backup Restart=on-failure -RestartSec=10s +RestartSec=30s StartLimitInterval=10min StartLimitBurst=3 diff --git a/mc-fbackup.service b/mc-fbackup.service index a922b69..a5a52ae 100644 --- a/mc-fbackup.service +++ b/mc-fbackup.service @@ -3,8 +3,8 @@ Description=Force backup of Minecraft Server [Service] Type=simple -User=minecraftuser -Group=minecraftuser +User=minecraft +Group=minecraft ExecStart=/var/minecraft/server.sh fbackup Restart=on-failure RestartSec=10s -- cgit v1.2.3