2020-05-03 03:06:16 -05:00
|
|
|
#! /bin/bash
|
|
|
|
#
|
|
|
|
# backup.sh
|
|
|
|
# General-purpose backup script that accepts subtasks
|
|
|
|
# Copyright (C) 2020 Vintage Salt <rehashedsalt@cock.li>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
#
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2020-05-03 05:48:01 -05:00
|
|
|
export BACKUPSDIR="/backups"
|
2020-05-03 06:37:58 -05:00
|
|
|
export OUTDIR="$BACKUPSDIR/out"
|
2020-05-03 05:48:01 -05:00
|
|
|
export MODULESDIR="/opt/backups/modules"
|
2020-05-03 03:06:16 -05:00
|
|
|
export DATE="$(date -Iseconds)"
|
|
|
|
|
|
|
|
# Helper functions
|
|
|
|
log() {
|
|
|
|
[ -z "$1" ] && return 1
|
|
|
|
printf "$(date -Iseconds): $1\n"
|
|
|
|
}
|
|
|
|
|
2020-05-03 06:08:36 -05:00
|
|
|
# Sanity checks
|
2020-05-03 03:06:16 -05:00
|
|
|
if ! [ -d "$MODULESDIR" ]; then
|
|
|
|
log "Unable to find modules directory: $MODULESDIR"
|
2020-05-03 06:37:58 -05:00
|
|
|
exit 1
|
2020-05-03 03:06:16 -05:00
|
|
|
fi
|
2020-05-03 06:08:36 -05:00
|
|
|
# Source an RC, if we have it
|
|
|
|
if [ -r "$MODULESDIR/backuprc" ]; then
|
|
|
|
source "$MODULESDIR/backuprc"
|
|
|
|
fi
|
2020-05-03 06:09:10 -05:00
|
|
|
# More sanity checks
|
|
|
|
if ! [ -d "$BACKUPSDIR" ]; then
|
|
|
|
log "Unable to find backups directory: $BACKUPSDIR"
|
2020-05-03 06:37:58 -05:00
|
|
|
exit 2
|
2020-05-03 06:09:10 -05:00
|
|
|
fi
|
2020-05-03 06:08:36 -05:00
|
|
|
# Do the do
|
2020-05-03 03:06:16 -05:00
|
|
|
log "Beginning backups"
|
|
|
|
for file in "$MODULESDIR"/*; do
|
|
|
|
# Just keep going if we don't have any tasks to do
|
|
|
|
[ -f "$file" ] || continue
|
|
|
|
# Execute the module and alert if it fails
|
|
|
|
log "Executing module: $file"
|
|
|
|
(
|
2020-05-03 06:37:58 -05:00
|
|
|
# Define a log function for our module to use
|
|
|
|
log() {
|
|
|
|
[ -z "$1" ] && return 1
|
|
|
|
printf "$(date -Iseconds): $1\n"
|
|
|
|
}
|
2020-05-03 03:06:16 -05:00
|
|
|
source "$file"
|
|
|
|
) || {
|
|
|
|
log "Error executing module: $file"
|
|
|
|
}
|
|
|
|
done
|
2020-06-08 08:45:17 -05:00
|
|
|
# If we have a fancy schmancy bucket, use it
|
|
|
|
s3bucket="{{ aws_backup_bucket }}"
|
2020-06-09 05:16:22 -05:00
|
|
|
if command -v aws > /dev/null 2>&1 && aws s3 ls "s3://$s3bucket" > /dev/null 2>&1; then
|
2020-06-08 08:45:17 -05:00
|
|
|
log "Moving files to S3 bucket $s3bucket"
|
2020-06-09 05:14:47 -05:00
|
|
|
aws s3 mv "$BACKUPSDIR" "s3://$s3bucket" \
|
|
|
|
--recursive \
|
|
|
|
--only-show-errors \
|
|
|
|
--exclude "*.log" \
|
|
|
|
--storage-class STANDARD_IA
|
2020-06-08 08:45:17 -05:00
|
|
|
fi
|
2020-05-03 03:06:16 -05:00
|
|
|
|