Add nightly full backups to tmodloader

This commit is contained in:
Salt 2020-09-29 05:38:39 -05:00
parent 0cf490b562
commit 8cc418411b
3 changed files with 57 additions and 6 deletions

View File

@ -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"

View File

@ -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 <rehashedsalt@cock.li>
#
# 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

View File

@ -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