From 47b526920d2b08ce6cfa3de813e2762304d4f9a8 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Wed, 19 Feb 2020 20:17:13 -0500 Subject: [PATCH] initial commit --- backup.sh | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100755 backup.sh diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..6650c1d --- /dev/null +++ b/backup.sh @@ -0,0 +1,165 @@ +#!/usr/bin/env bash + +print_ascii () { +cat <<'EOF' + _ _ _ +| |__ __ _ ___| | ___ _ _ __ ___| |__ +| '_ \ / _` |/ __| |/ / | | | '_ \ / __| '_ \ +| |_) | (_| | (__| <| |_| | |_) |\__ \ | | | +|_.__/ \__,_|\___|_|\_\\__,_| .__(_)___/_| |_| + |_| +EOF +# Copyright (c) 2020 Andrew Davidson +} + +print_help () { + echo -e """ + Usage: + backup.sh 'command' 'destination' + + Supported Commands: + backup - initiate a backup of the home folder to the destination + list - list backups on the destination + prune - prune old backups on the destination + + Supported destinations: + royal - local borg/SFTP backup to Royal + wasabi - remote restic backup to Wasabi + """ +} + + +set -o errexit +set -o pipefail + +ACTION=$1 +DESTINATION=$2 + + +### +# ~/.env should contain these variables: +# +# ## Borg Royal Environment +# export BORG_PASSPHRASE= +# +# ## Restic Wasabi Environment +# export AWS_ACCESS_KEY_ID= +# export AWS_SECRET_ACCESS_KEY= +# export RESTIC_REPOSITORY=s3:s3.wasabisys.com/backup +# export RESTIC_PASSWORD= +### +source ~/.env + +case $DESTINATION in + "royal") + case $ACTION in + "backup") + echo "$(date) starting backup to $DESTINATION" + + if on_ac_power; then + borg create --progress -s -v \ + --exclude $HOME/tmp \ + --exclude $HOME/Downloads \ + --exclude $HOME/Desktop \ + --exclude $HOME/.cache \ + --exclude $HOME/.local/gnome-boxes \ + backup:/bkup/$(hostname)::$(date '+%s') \ + $HOME + else + echo "Not plugged in, canceling backup." + fi + + echo "$(date) finished backup to $DESTINATION" + ;; + "list") + borg list backup:/bkup/$(hostname) + ;; + "prune") + echo """ + Pruning $DESTINATION backups... + Keeping: + - 24 hourly backups + - 90 daily backups + - 12 monthly backups + - 5 yearly backups + """ + borg prune \ + --stats --list \ + --keep-hourly 24 \ + --keep-daily 90 \ + --keep-monthly 12 \ + --keep-yearly 5 \ + backup:/bkup/$(hostname) + ;; + "help") + print_ascii + print_help + ;; + *) + echo "Action: $ACTION not recognized." + print_help + ;; + esac + ;; + "wasabi") + case $ACTION in + "backup") + echo "$(date) starting backup to $DESTINATION" + + if on_ac_power; then + restic backup \ + --verbose \ + --exclude $HOME/tmp \ + --exclude $HOME/Desktop \ + --exclude $HOME/Downloads \ + --exclude $HOME/.cache \ + --exclude $HOME/.cargo \ + --exclude $HOME/.local/share/gnome-boxes \ + $HOME + else + echo "Not plugged in, canceling backup." + fi + + echo "$(date) finished backup to $DESTINATION" + ;; + "list") + restic snapshots + ;; + "prune") + echo """ + Pruning $DESTINATION backups... + Keeping: + - 4 hourly backups + - 90 daily backups + - 12 monthly backups + - 5 yearly backups + """ + + restic forget --prune \ + --keep-hourly 4 \ + --keep-daily 90 \ + --keep-monthly 12 \ + --keep-yearly 5 + + ;; + "help") + print_ascii + print_help + ;; + *) + echo "Action: $ACTION not recognized." + print_help + ;; + esac + ;; + *) + if [[ -z $DESTINATION && $ACTION == "help" ]]; then + print_ascii + print_help + else + echo "Destination: $DESTINATION not recognized." + print_help + fi + ;; +esac +