ansible/roles/tmodloader/templates/backup-nightly.sh

50 lines
1.6 KiB
Bash

#! /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