From d698f0b5883394d210b06a2ce8b964780559a9d7 Mon Sep 17 00:00:00 2001 From: Salt Date: Thu, 25 Jun 2020 08:42:29 -0500 Subject: [PATCH] Set up a recovery script for MC servers this shit happens to goddamn often- --- roles/minecraft/tasks/main.yml | 25 ++++++++++++++--- roles/minecraft/templates/recover.sh | 42 ++++++++++++++++++++++++++++ roles/terraria/tasks/main.yml | 7 +++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 roles/minecraft/templates/recover.sh diff --git a/roles/minecraft/tasks/main.yml b/roles/minecraft/tasks/main.yml index 574de53..933df8a 100644 --- a/roles/minecraft/tasks/main.yml +++ b/roles/minecraft/tasks/main.yml @@ -47,6 +47,14 @@ dest: "/var/minecraft/{{ mcname }}/modpack.zip" url: "{{ mczip }}" register: get_mcpack + - name: Template out scripts + template: + src: "{{ item.src }}" + dest: "/var/minecraft/{{ mcname }}/{{ item.dest }}" + mode: "{{ item.mode }}" + loop: + - { src: "install.sh", dest: "install.sh", mode: "0755" } + - { src: "recover.sh", dest: "recover.sh", mode: "0755" } - name: Install modpack block: - name: Disable service @@ -71,10 +79,6 @@ remote_src: yes dest: "/var/minecraft/{{ mcname }}" notify: "restart minecraft {{ mcname }}" - - name: Template out install script - template: - src: "install.sh" - dest: "/var/minecraft/{{ mcname }}/install.sh" - name: Run install script command: chdir: "/var/minecraft/{{ mcname }}" @@ -123,6 +127,19 @@ state: link when: not stat_mcroot_newjar.stat.exists when: not stat_mcroot_forgejar.stat.exists + - name: Check for world + stat: + path: "/var/minecraft/{{ mcname }}/world" + register: stat_mcroot_world + - name: Recover world, if available + block: + - name: Run recover script + command: + chdir: "/var/minecraft/{{ mcname }}" + argv: + - bash + - "./recover.sh" + notify: "restart minecraft {{ mcname }}" - name: Configure server block: - name: Template out server.properties diff --git a/roles/minecraft/templates/recover.sh b/roles/minecraft/templates/recover.sh new file mode 100644 index 0000000..cc3c0ef --- /dev/null +++ b/roles/minecraft/templates/recover.sh @@ -0,0 +1,42 @@ +#! /bin/sh +# +# recover.sh +# Recover a Minecraft world save from our S3 bucket +# Copyright (C) 2020 Vintage Salt +# +# Distributed under terms of the MIT license. +# + +set -e + +# Get to our magic dir +export MINECRAFT_DIR="/var/minecraft/{{ mcname }}" +cd "$MINECRAFT_DIR" || exit 50 + +# If the world directory exists, we need to ensure that we don't clobber it +if [ -d "world" ]; then + echo "Backing up current world" + tar czf "recover-world-$(date -Iseconds).tar.gz" world --remove-files +fi +# If it STILL exists, then we have a problem +if [ -d "world" ]; then + echo "World was found to exist after tarring; bailing" + exit 51 +fi + +# Get our latest good backup +backup="$(aws s3 ls "s3://{{ aws_backup_bucket }}/{{ mcname }}/" | tail -n 1 | awk '{print $4}')" +echo "Restoring backup: $backup" +aws s3 cp "s3://{{ aws_backup_bucket }}/{{ mcname }}/$backup" world.tgz +# Decompress it +tar xzf world.tgz +# Find the world +worlddir="$(find . -type d -name "world" | head -n 1)" +# Move it in place +mv "$worlddir" . +# Verify our work +if [ -f "world/level.dat" ]; then + echo "Recovered from backup: $backup" +else + echo "Failed to recover from backup: $backup" +fi diff --git a/roles/terraria/tasks/main.yml b/roles/terraria/tasks/main.yml index a75374e..dc1f967 100644 --- a/roles/terraria/tasks/main.yml +++ b/roles/terraria/tasks/main.yml @@ -7,6 +7,13 @@ apt: name: - mono-complete + - name: Disable bundled services + systemd: + name: "{{ item }}" + enabled: no + state: stopped + loop: + - "mono-xsp4" - name: Assure terraria admin group group: name: ter-admin