commit ad70c7420c49a571c697f1015e3ee9fdf42aa84e Author: minecraft Date: Tue Nov 30 14:51:26 2021 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ef85fe9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +backup.log diff --git a/README.md b/README.md new file mode 100644 index 0000000..55d9085 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Various scripts to ease administration of play.geekify.de + +Create a file named `CONFIG.py` containing your rcon credentials, example: + +``` +HOST="127.0.0.1" +PORT=25575 +PW="supersecretpassword" +``` diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..98d718d --- /dev/null +++ b/backup.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -e +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +filename="maps-$(date +%s).tar.gz" +( +echo "Starting backup at $(date) to $filename" +$DIR/backup_prepare.py +cd /opt/minecraft/server +tar -czf "../backups/$filename" world* || true 2>&1 +size="$(du -h ../backups/$filename | awk '{ print $1 }')" +echo "Backup completed at $(date) with a size of $size" +$DIR/enable_autosave.py +echo "Backup done at $(date)!" +if [[ $1 == "--restart-after-backup" ]]; then + echo "Restart after backup was requested, initializing restart in 10 minutes" + $DIR/restart_server.py & +fi +) >> /opt/minecraft/tools/backup.log +echo $filename diff --git a/backup_prepare.py b/backup_prepare.py new file mode 100755 index 0000000..130f632 --- /dev/null +++ b/backup_prepare.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +import time +import sys +from mcrcon import MCRcon +from CONFIG import HOST,PW,PORT + +flushed=False +with MCRcon(HOST, PW, port=PORT) as mcr: + resp = mcr.command("say BACKUP STARTED") + resp = mcr.command("save-off") + resp = mcr.command("save-all flush") + if "Saved the game" in resp: + flushed=True + print("Flushed map successfully. Don't forget to run enable_autosave.py after the map backup") + +if flushed: + sys.exit(0) +else: + sys.exit(1) diff --git a/cli.py b/cli.py new file mode 100755 index 0000000..5b92d75 --- /dev/null +++ b/cli.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 +from mcrcon import mcrcon_cli +mcrcon_cli() diff --git a/enable_autosave.py b/enable_autosave.py new file mode 100755 index 0000000..04d69d4 --- /dev/null +++ b/enable_autosave.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +import time +import sys +from mcrcon import MCRcon +from CONFIG import HOST,PW,PORT + +autosave_on=False +with MCRcon(HOST, PW, port=PORT) as mcr: + resp = mcr.command("save-on") + if "Automatic saving is now enabled" in resp: + autosave_on=True + resp = mcr.command("say BACKUP FINISHED") + +if autosave_on: + print("Autosave successfuly enabled.") + sys.exit(0) +else: + print("Enabling autosave failed. Was it already turned on?") + sys.exit(1) diff --git a/players.py b/players.py new file mode 100755 index 0000000..789813f --- /dev/null +++ b/players.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import time +import sys +import re +from mcrcon import MCRcon +import pdb +from CONFIG import HOST,PW,PORT + +flushed=False +with MCRcon(HOST, PW, port=PORT) as mcr: + resp = mcr.command("list uuids") +resp = resp.split(":") +player_list = resp[1].strip().split(",") +player_list = list(map(lambda x: x.strip(), player_list)) + +count_regex = re.match("There are ([0-9]+) of a max of ([0-9]+) players online", resp[0]) +num_players = count_regex.group(1) +max_players = count_regex.group(2) +print("Current # players:", num_players) +print("Max # players:", max_players) +print(player_list) +pdb.set_trace() + +if flushed: + sys.exit(0) +else: + sys.exit(1) diff --git a/restart_server.py b/restart_server.py new file mode 100755 index 0000000..05e19d5 --- /dev/null +++ b/restart_server.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import time +import sys +import datetime +from mcrcon import MCRcon +from CONFIG import HOST,PW,PORT + +autosave_on=False +with MCRcon(HOST, PW, port=PORT) as mcr: + current_time = datetime.datetime.now().strftime("%H:%M") + resp = mcr.command("say {}: Server will restart in §a10§f minutes".format(current_time)) + time.sleep(30) + current_time = datetime.datetime.now().strftime("%H:%M") + resp = mcr.command("say {}: Server will restart in §e5§f minutes".format(current_time)) + time.sleep(24) + current_time = datetime.datetime.now().strftime("%H:%M") + resp = mcr.command("say {}: Server will restart in §61§f minute".format(current_time)) + time.sleep(5) + for i in range(10): + resp = mcr.command("say Server will restart in §4{}§f seconds".format(10-i)) + time.sleep(1) + + resp = mcr.command("restart") + +sys.exit(0)