From 779c17744764029f8f3f3cda533c2d7f4fe165b0 Mon Sep 17 00:00:00 2001 From: Salt Date: Mon, 12 Oct 2020 10:18:47 -0500 Subject: [PATCH] Add hourly backups to paper serbers --- roles/minecraft-paper/tasks/main.yml | 6 ++- roles/minecraft-paper/templates/hourly.sh | 52 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 roles/minecraft-paper/templates/hourly.sh diff --git a/roles/minecraft-paper/tasks/main.yml b/roles/minecraft-paper/tasks/main.yml index 860f46b..bf630ea 100644 --- a/roles/minecraft-paper/tasks/main.yml +++ b/roles/minecraft-paper/tasks/main.yml @@ -40,10 +40,14 @@ template: src=server.properties dest="{{ paper_home }}/{{ paper_name }}/server.properties" notify: "restart paper {{ paper_name }}" - name: Accept EULA - template: src=eula.txt dest="{{ paper_home }}/{{ paper_name }}/eula.txt" + template: src=eula.txt dest="{{ paper_home }}/{{ paper_name }}/eula.txt" mode="0644" - name: Download Paper get_url: url="{{ paper_downloadurl }}" dest="{{ paper_home }}/{{ paper_name }}/server.jar" notify: "restart paper {{ paper_name }}" + - name: Template out hourly backup job + template: src=hourly.sh dest="{{ paper_home }}/{{ paper_name }}/hourly.sh" mode="0775" + - name: Set up hourly backup job + cron: minute="0" name="{{ paper_name }}-nightly" job="{{ paper_home }}/{{ paper_name }}/hourly.sh" become: yes become_user: minecraft-paper - name: Enable service diff --git a/roles/minecraft-paper/templates/hourly.sh b/roles/minecraft-paper/templates/hourly.sh new file mode 100644 index 0000000..8aa6879 --- /dev/null +++ b/roles/minecraft-paper/templates/hourly.sh @@ -0,0 +1,52 @@ +#! /bin/sh +# +# nightly.sh +# Nightly world backups for Minecraft +# Copyright (C) 2020 Vintage Salt +# +# Distributed under terms of the MIT license. +# + +set -e + +export MCDIR="{{ paper_home }}/{{ paper_name }}" +export OUTDIR="$MCDIR/backups" +retention=24 # 1hr intervals + +# Helper functions +log() { + [ -z "$1" ] && return 1 + printf "$(date -Iseconds): $1\n" +} +stuff() { + [ -z "$1" ] && return 1 + screen -p 0 -S "{{ paper_name }}" -X eval "stuff \"say $1\\015\"" +} + +# Sanity checks +if ! [ -d "$OUTDIR" ]; then + if ! mkdir "$OUTDIR"; then + log "Unable to find or create output directory: $OUTDIR" + stuff "World backup failed: see log for details" + retuirn 2 + fi +fi + +# Enforce permissions on our output dir +chown -R minecraft-paper. "$OUTDIR" + +# Remove old backups +find "$OUTDIR" -type f -mtime +1 -iname "backup-*.tar.gz" -delete + +# WE MAKE BACKUP NOW SERGEI +if cd "$OUTDIR"; then + log "Initiating world backup" + stuff "Initiating world backup" + tar czhf "$OUTDIR/backup-$(date -Iseconds).tar.gz" "$MCDIR"/world* + log "World backup complete" + stuff "World backup complete" +else + log "Could not change directory: $OUTDIR" + stuff "World backup failed: see log for details" + return 3 +fi