moving to be a wrapper around restic specifically

This commit is contained in:
Andrew Davidson 2021-07-15 21:09:57 -07:00
parent 170ff58d8c
commit 00a45d80a5
Signed by: amd
GPG key ID: 17AF8F2A49CF25C6

269
backup.sh
View file

@ -8,8 +8,10 @@ cat <<'EOF'
| |_) | (_| | (__| <| |_| | |_) |\__ \ | | | | |_) | (_| | (__| <| |_| | |_) |\__ \ | | |
|_.__/ \__,_|\___|_|\_\\__,_| .__(_)___/_| |_| |_.__/ \__,_|\___|_|\_\\__,_| .__(_)___/_| |_|
|_| |_|
A wrapper around restic to help control configuration.
EOF EOF
# Copyright (c) 2020 Andrew Davidson # Copyright (c) 2021 Andrew Davidson
} }
print_help () { print_help () {
@ -18,16 +20,41 @@ print_help () {
backup.sh 'command' 'destination' backup.sh 'command' 'destination'
Supported Commands: Supported Commands:
init - intiialize a new backup repository
backup - initiate a backup of the home folder to the destination backup - initiate a backup of the home folder to the destination
list - list backups on the destination check - check repository for consistency
snapshots - list backups on the destination
prune - prune old backups on the destination prune - prune old backups on the destination
stats - print statistics about the backup repository
help - print this help
Required Configuration:
\$XDG_CONFIG_HOME/backup.sh/\$DESTINATION/\$DESTINATION.repo
- Define the repository path in this file
\$XDG_CONFIG_HOME/backup.sh/\$DESTINATION/\$DESTINATION.pwd
- Define the repository password in this file
\$XDG_CONFIG_HOME/backup.sh/\$DESTINATION/\$DESTINATION.paths
- list of paths to be backed up
\$XDG_CONFIG_HOME/backup.sh/\$DESTINATION/\$DESTINATION.exclude
- list of paths to be excluded
\$XDG_CONFIG_HOME/backup.sh/\$DESTINATION/\$DESTINATION.keys
- OPTIONAL: bash exports of required keys for s3/b2 backups
\$XDG_DATA_HOME/backup.sh/\$DESTINATION/\$DESTINATION.log
- empty file for logging
Supported destinations:
royal - local borg/SFTP backup to Royal
wasabi - remote restic backup to Wasabi
""" """
} }
print_and_log () {
/usr/bin/ts "%Y-%m-%dT%H:%M:%S" | /usr/bin/tee -a $BACKUP_LOG
}
set -o errexit set -o errexit
set -o pipefail set -o pipefail
@ -35,140 +62,124 @@ set -o pipefail
ACTION=$1 ACTION=$1
DESTINATION=$2 DESTINATION=$2
if [[ -z $DESTINATION && $ACTION == "help" ]]; then
print_logo
print_help
elif [[ -z $DESTINATION ]]; then
echo "Destination must not be blank"
print_help
else
### if [[ -f "$XDG_CONFIG_HOME/backup.sh/$DESTINATION/$DESTINATION.repo" ]]; then
# ~/.env should contain these variables: BACKUP_REPOSITORY=$(cat $XDG_CONFIG_HOME/backup.sh/$DESTINATION/$DESTINATION.repo)
# else
# ## Borg Royal Environment echo "Must configure repository at $XDG_CONFIG_HOME/backup.sh/$DESTINATION"
# export BORG_PASSPHRASE= print_help
# exit 2
# ## Restic Wasabi Environment fi
# export AWS_ACCESS_KEY_ID= if [[ -f "$XDG_CONFIG_HOME/backup.sh/$DESTINATION/$DESTINATION.keys" ]]; then
# export AWS_SECRET_ACCESS_KEY= source "$XDG_CONFIG_HOME/backup.sh/$DESTINATION/$DESTINATION.keys"
# export RESTIC_REPOSITORY=s3:s3.wasabisys.com/backup fi
# export RESTIC_PASSWORD= BACKUP_PASSWORD="$XDG_CONFIG_HOME/backup.sh/$DESTINATION/$DESTINATION.pwd"
### BACKUP_EXCLUDE_FILE="$XDG_CONFIG_HOME/backup.sh/$DESTINATION/$DESTINATION.exclude"
source ~/.env BACKUP_PATHS="$XDG_CONFIG_HOME/backup.sh/$DESTINATION/$DESTINATION.paths"
BACKUP_LOG="$XDG_DATA_HOME/backup.sh/$DESTINATION/$DESTINATION.log"
### if [[ -f "/sys/class/power_supply/AC/online" ]]; then
# For the borg backup to work properly, the backup host should be AC_POWER=$(cat /sys/class/power_supply/AC/online)
# configured in `$HOME/.ssh/config`: else
# Host backup AC_POWER=1
# Hostname backup.hostname fi
# Port 22
# User backupuser
###
case $DESTINATION in case $ACTION in
"royal") "backup")
case $ACTION in if [[ $AC_POWER == 1 ]]; then
"backup") echo "backing up to $DESTINATION." | print_and_log
if on_ac_power; then /usr/bin/restic \
borg create \ -r "$BACKUP_REPOSITORY" \
--exclude $HOME/backups \ -p "$BACKUP_PASSWORD" \
--exclude $HOME/tmp \ --cleanup-cache \
--exclude $HOME/Downloads \ --exclude-caches \
--exclude $HOME/Desktop \ --exclude-file="$BACKUP_EXCLUDE_FILE" \
--exclude $HOME/.cache \ --files-from="$BACKUP_PATHS" \
--exclude $HOME/.local/gnome-boxes \ --verbose \
backup:/bkup/$(hostname)::$(date '+%s') \ backup | print_and_log
$HOME else
else echo "not plugged in, canceling backup."
echo "Not plugged in, canceling backup." fi
fi ;;
;; "check")
"check") echo "Checking backup repository at $DESTINATION" | print_and_log
borg check backup:/bkup/$(hostname) /usr/bin/restic \
;; -r "$BACKUP_REPOSITORY" \
"list") -p "$BACKUP_PASSWORD" \
borg list backup:/bkup/$(hostname) --verbose \
;; check | print_and_log
"prune") ;;
"init")
echo "Initializing backup repository at $DESTINATION" | print_and_log
/usr/bin/restic \
-r "$BACKUP_REPOSITORY" \
-p "$BACKUP_PASSWORD" \
--verbose \
init | print_and_log
;;
"unlock")
echo "Unlocking backup repository at $DESTINATION" | print_and_log
/usr/bin/restic \
-r "$BACKUP_REPOSITORY" \
-p "$BACKUP_PASSWORD" \
--verbose \
unlock | print_and_log
;;
"snapshots")
echo "Listing snapshots on $DESTINATION"
/usr/bin/restic \
-r "$BACKUP_REPOSITORY" \
-p "$BACKUP_PASSWORD" \
--verbose \
snapshots
;;
"stats")
echo "Printing statistics for $DESTINATION"
/usr/bin/restic \
-r "$BACKUP_REPOSITORY" \
-p "$BACKUP_PASSWORD" \
--verbose \
stats
;;
"prune")
if [[ $AC_POWER == 1 ]]; then
echo """ echo """
Pruning $DESTINATION backups... Pruning backups at $DESTINATION ...
Keeping: Keeping:
- 24 hourly backups - 24 hourly backups
- 90 daily backups - 90 daily backups
- 12 monthly backups - 12 monthly backups
- 5 yearly backups - 5 yearly backups
""" """ | print_and_log
borg prune \ /usr/bin/restic \
--stats --list \ -r "$BACKUP_REPOSITORY" \
--keep-hourly 24 \ -p "$BACKUP_PASSWORD" \
--keep-daily 90 \ --verbose \
--keep-monthly 12 \ --prune \
--keep-yearly 5 \ --keep-hourly=24 \
backup:/bkup/$(hostname) --keep-daily=90 \
;; --keep-monthly=12 \
"help") --keep-yearly=5 \
print_logo forget | print_and_log
print_help else
;; echo "Not plugged in, canceling prune."
*) fi
echo "Action: $ACTION not recognized." ;;
print_help "help")
;;
esac
;;
"wasabi")
case $ACTION in
"backup")
if on_ac_power; then
restic backup \
--quiet \
--exclude $HOME/backups \
--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
;;
"check")
restic check
;;
"list")
restic snapshots
;;
"prune")
echo """
Pruning $DESTINATION backups...
Keeping:
- 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_logo
print_help
;;
*)
echo "Action: $ACTION not recognized."
print_help
;;
esac
;;
*)
if [[ -z $DESTINATION && $ACTION == "help" ]]; then
print_logo print_logo
print_help print_help
else ;;
echo "Destination: $DESTINATION not recognized." *)
echo "Action: $ACTION not recognized."
print_help print_help
fi ;;
;; esac
esac fi