From 8cc418411bcc426ed5e8c6b8b557e6885e1aa36b Mon Sep 17 00:00:00 2001 From: Salt Date: Tue, 29 Sep 2020 05:38:39 -0500 Subject: [PATCH] Add nightly full backups to tmodloader --- roles/tmodloader/tasks/main.yml | 5 ++ roles/tmodloader/templates/backup-nightly.sh | 49 ++++++++++++++++++++ roles/tmodloader/templates/backup.sh | 9 ++-- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 roles/tmodloader/templates/backup-nightly.sh diff --git a/roles/tmodloader/tasks/main.yml b/roles/tmodloader/tasks/main.yml index 5d4cac0..f2de43c 100644 --- a/roles/tmodloader/tasks/main.yml +++ b/roles/tmodloader/tasks/main.yml @@ -108,6 +108,11 @@ owner: terraria group: ter-admin mode: "0700" + - name: Template out nightly backup script + template: + src: "backup-nightly.sh" + dest: "/opt/backups/modules/tmodloader-{{ terraria_name }}.sh" + mode: "0600" - name: Set up backup cronjob cron: minute: "*/30" diff --git a/roles/tmodloader/templates/backup-nightly.sh b/roles/tmodloader/templates/backup-nightly.sh new file mode 100644 index 0000000..a474a86 --- /dev/null +++ b/roles/tmodloader/templates/backup-nightly.sh @@ -0,0 +1,49 @@ +#! /bin/bash +# +# tmodloader-nightly.sh +# Backup script for tModLoader. Meant to be sourced by our main backup script +# Copyright (C) 2020 Vintage Salt +# +# Distributed under terms of the MIT license. +# + +set -e + +export TERRARIADIR="{{ terraria_root }}/{{ terraria_name }}" +export OUTDIR="$BACKUPSDIR/tmodloader/{{ terraria_name }}" +retention=5 # 5-day retention period + +# Sanity checks +if [ -z "$BACKUPSDIR" ]; then + log "BACKUPSDIR was undefined. Run the main backup script instead of this one." + return 1 +fi +if ! [ -d "$OUTDIR" ]; then + if ! mkdir -p "$OUTDIR"; then + log "Unable to find or create output directory: $OUTDIR" + return 2 + fi +fi +# Enforce permissions on our output directory +chown terraria.ter-admin "$OUTDIR" +chmod 770 "$OUTDIR" + +# Purge oldest backup if we need to +currentbackupcount="$(ls -1 "$OUTDIR" | wc -l)" +# Multiplying by three here because our backups are three-parters +if (( currentbackupcount >= retention * 3 )); then + find "$OUTDIR"/ -iname "*.tar.gz" -mtime +30 -delete +fi +# WE MAKE BACKUP NOW SERGEI +if cd "$TERRARIADIR"; then +{% if aws.backup_bucket is defined %} + log "Initiating full tmodloader backup" + tar czh --exclude="$TERRARIADIR/backups" "$TERRARIADIR" | aws s3 cp - "s3://{{ aws.backup_bucket }}/tmodloader/{{ terraria_name }}/full-$date-server.tar.gz" --storage-class STANDARD + log "Backing up mod configuration" + tar czh "{{ terraria_root }}/.local/share/Terraria/ModLoader" | aws s3 cp - "s3://{{ aws.backup_bucket }}/tmodloader/{{ terraria_name }}/full-$date-mods.tar.gz" --storage-class STANDARD +{% endif %} +else + log "Could not change directory: $TERRARIADIR" + return 3 +fi + diff --git a/roles/tmodloader/templates/backup.sh b/roles/tmodloader/templates/backup.sh index 6608b7f..ee905df 100644 --- a/roles/tmodloader/templates/backup.sh +++ b/roles/tmodloader/templates/backup.sh @@ -32,12 +32,9 @@ chmod 770 "$OUTDIR" # Purge oldest backup if we need to currentbackupcount="$(ls -1 "$OUTDIR" | wc -l)" -if (( currentbackupcount >= retention )); then - lastbackup="$(find "$OUTDIR" -name \*.wld 2>/dev/null | sort | head -n 1)" - if [ -f "$lastbackup" ]; then - log "Removing old backup: $lastbackup" - rm "$lastbackup" - fi +# Two-part backups get two-part removal +if (( currentbackupcount >= retention * 2 )); then + find "$OUTDIR/" -iname "\*wld" -mtime +30 -delete fi # WE MAKE BACKUP NOW SERGEI if cd "$OUTDIR"; then