Created
February 17, 2020 20:19
-
-
Save amdavidson/a2dd0f75c4e1db145f4ff72ada77fc5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
### Borg Royal Environment | |
export BORG_PASSPHRASE= | |
### Restic Wasabi Environment | |
export AWS_ACCESS_KEY_ID= | |
export AWS_SECRET_ACCESS_KEY= | |
export RESTIC_REPOSITORY= | |
export RESTIC_PASSWORD= | |
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/.local/gnome-boxes \ | |
--exclude $HOME/Downloads \ | |
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 | |
""" | |
borg prune \ | |
--stats --list \ | |
--keep-hourly 24 \ | |
--keep-daily 90 \ | |
--keep-monthly 12 \ | |
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/.cache \ | |
--exclude $HOME/.cargo \ | |
--exclude $HOME/.local/share/gnome-boxes \ | |
--exclude $HOME/Downloads \ | |
$HOME | |
else | |
echo "Not plugged in, canceling backup." | |
fi | |
echo "$(date) finished backup to $DESTINATION" | |
;; | |
"list") | |
restic snapshots | |
;; | |
"prune") | |
echo """ | |
Pruning $DESTINATION backups... | |
Keeping: | |
- 24 hourly backups | |
- 90 daily backups | |
- 12 monthly backups | |
""" | |
$HOME/bin/restic forget -n \ | |
--keep-hourly 24 \ | |
--keep-daily 90 \ | |
--keep-monthly 12 | |
$HOME/bin/restic prune | |
;; | |
"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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment