aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-04-08 11:17:41 +0200
committerGravatar GitHub <noreply@github.com> 2020-04-08 11:17:41 +0200
commitaddb1c3770270a1d40b76eb557b0397f4de09cbd (patch)
tree484d126329507023496f21da358b984de4038cc8
parent8bec29597d873ce025d63c1e4cc8e72f91d878e6 (diff)
parentcee1fc32e72679261ba65a8ceafab5b94617517f (diff)
downloadminecraft-server-tools-addb1c3770270a1d40b76eb557b0397f4de09cbd.tar.gz
Merge pull request #1 from KamiCreed/master
Pull KamiCreed commits
-rw-r--r--Readme.md11
-rw-r--r--daily-mc-backup.timer9
-rw-r--r--mc-backup.service15
-rw-r--r--mc-backup.timer8
-rw-r--r--mc-fbackup.service15
-rw-r--r--minecraft.service2
-rwxr-xr-xserver.sh157
7 files changed, 176 insertions, 41 deletions
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/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-backup.service b/mc-backup.service
new file mode 100644
index 0000000..0ca0718
--- /dev/null
+++ b/mc-backup.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=Automatic backup of Minecraft Server
+
+[Service]
+Type=simple
+User=minecraft
+Group=minecraft
+ExecStart=/var/minecraft/server.sh backup
+Restart=on-failure
+RestartSec=30s
+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/mc-fbackup.service b/mc-fbackup.service
new file mode 100644
index 0000000..a5a52ae
--- /dev/null
+++ b/mc-fbackup.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=Force backup of Minecraft Server
+
+[Service]
+Type=simple
+User=minecraft
+Group=minecraft
+ExecStart=/var/minecraft/server.sh fbackup
+Restart=on-failure
+RestartSec=10s
+StartLimitInterval=10min
+StartLimitBurst=8
+
+[Install]
+WantedBy=multi-user.target
diff --git a/minecraft.service b/minecraft.service
index f261625..428c9af 100644
--- a/minecraft.service
+++ b/minecraft.service
@@ -8,6 +8,8 @@ Type=forking
User=minecraft
Group=minecraft
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
diff --git a/server.sh b/server.sh
index 9b8156b..1654d3c 100755
--- a/server.sh
+++ b/server.sh
@@ -2,32 +2,31 @@
#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'
+#Constants
+CUR_YEAR=`date +"%Y"`
+
function backup_hook_example {
- echo $ARCHNAME
+ bup -d $CUR_BACK_DIR ls -l $BACKUP_NAME/latest/var/minecraft
}
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 +34,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 +43,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 +81,21 @@ function server_stop() {
function server_attach() {
assert_not_running
- screen -r -p 0 $SCREEN_WINDOW
+ tmux attach -t $SCREEN_WINDOW
exit
}
-function server_status() {
- ps -p $(cat $PIDFILE) > /dev/null
+function server_running() {
+ if [ -f $PIDFILE ] && [ "$(cat $PIDFILE)" != "" ]; then
+ ps -p $(cat $PIDFILE) > /dev/null
+ return
+ fi
+
+ false
+}
- if [ $? -eq 0 ]
+function server_status() {
+ if server_running
then
echo "Server is running"
else
@@ -102,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
@@ -113,9 +134,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 +153,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 +162,28 @@ function server_backup_unsafe() {
fi
}
+function create_bup_backup() {
+ BACKUP_DIR="mc-backups"
+ 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"
+}
+
+# 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"
@@ -154,18 +198,42 @@ 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() {
- screen -list $SCREEN_WINDOW > /dev/null
- if [ $? -eq 0 ]
- then #Server is running
- server_backup_safe
- else #Not running
+ 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 "$force"
+ else
server_backup_unsafe
fi
exit
}
+function ls_bup() {
+ bup -d "mc-backups/${CUR_YEAR}" ls "mc-sad-squad/$1"
+}
+
cd $(dirname $0)
case $1 in
@@ -181,9 +249,16 @@ case $1 in
"backup")
server_backup
;;
+ # TODO: Add restore command
"status")
server_status
;;
+ "fbackup")
+ server_backup "true"
+ ;;
+ "ls")
+ ls_bup $2
+ ;;
*)
echo "Usage: $0 start|stop|attach|status|backup"
;;