dotfiles/.restic/restic-do

126 lines
4.0 KiB
Plaintext
Raw Normal View History

2021-12-29 21:37:33 -05:00
#!/usr/bin/env bash
# set -euo pipefail
mkdir -p ~/.restore-info/tmp
chmod 700 ~/.restore-info
2021-12-30 14:14:51 -05:00
# restic-do exec init
# restic-do exec mount /path/to-restorepoint (mkdir first)
2021-12-29 21:37:33 -05:00
export AWS_ACCESS_KEY_ID=$(pash show personal-backups/aws-key-id)
export AWS_SECRET_ACCESS_KEY=$(pash show personal-backups/aws-access-key)
repo=$(cat ~/.dotfiles/.restic/.repo-name)
script_started=$(date +%Y-%M-%d_%k%M%S)
# $RESTIC_PASSWORD_FILE or $RESTIC_PASSWORD_COMMAND
if [[ -f "$HOME/.dotfiles/.restic/.repo-pass" ]]; then
export RESTIC_PASSWORD_FILE="$HOME/.dotfiles/.restic/.repo-pass"
else
export RESTIC_PASSWORD_COMMAND="pash show personal-backups/repo-pass"
fi
# set email vars here
[ -f ~/.dotfiles/.restic/.env ] && source ~/.dotfiles/.restic/.env
# Check internet connectivity
echo "checking internet connectivity, pinging google.com"
ping -c 1 google.com
internet_check="$?"
if [[ $internet_check != 0 ]]; then
echo "You may not be connected to the internet"
notify-send -u critical "The backup was unable to run:" "you are not connected to the internet"
zenity --error --text "The backup was unable to run: you are not connected to the internet"
exit 1
fi
# Check if metered
# https://github.com/erikw/restic-systemd-automatic-backup
# http://opensource.org/licenses/BSD-3-Clause
systemctl is-active dbus.service >/dev/null 2>&1 || exit 0
systemctl is-active NetworkManager.service >/dev/null 2>&1 || exit 0
metered_status=$(dbus-send --system --print-reply=literal \
--system --dest=org.freedesktop.NetworkManager \
/org/freedesktop/NetworkManager \
org.freedesktop.DBus.Properties.Get \
string:org.freedesktop.NetworkManager string:Metered \
| grep -o ".$")
if [[ $metered_status =~ (1|3) ]]; then
echo Current connection is metered
notify-send -u critical "The backup was unable to run:" "your internet connection is metered"
zenity --error --text "The backup was unable to run: your internet connection is metered"
exit 1
fi
case "$1" in
backup)
cd ~ || exit 1
echo "exporting list of packages"
rm ~/.restore-info/packages-* # clear existing
# https://unix.stackexchange.com/questions/82880/how-to-replicate-installed-package-selection-from-one-fedora-instance-to-another
# install w/ < $file xargs dnf -y install
dnf repoquery --qf '%{name}' --userinstalled \
| grep -v -- '-debuginfo$' \
| grep -v '^\(kernel-modules\|kernel\|kernel-core\|kernel-devel\)$' \
> ~/.restore-info/packages-${script_started}.txt
echo "exporting dnf repos list"
rm ~/.restore-info/dnfrepos-* # clear existing
dnf repolist enabled > ~/.restore-info/dnfrepos-${script_started}.txt
echo "starting backup"
restic backup -r "$repo" \
--one-file-system \
--exclude-caches --exclude-file ~/.dotfiles/.restic/ignore.txt \
--exclude-if-present .exclude_from_backup \
~ -v > ~/.restore-info/tmp/backup-log-${script_started}.txt
restic_exit_code="$?"
backup_stopped=$(date +%k%M%S)
2021-12-30 14:14:51 -05:00
echo "sending email report"
2021-12-29 21:37:33 -05:00
cat <<EOMAIL | msmtp --read-envelope-from $BACKUP_EMAIL_TO
To: $BACKUP_EMAIL_TO
From: $BACKUP_EMAIL_FROM
Subject: Backup report $script_started exiting $restic_exit_code
Stopped $backup_stopped
$(cat ~/.restore-info/tmp/backup-log-${script_started}.txt)
EOMAIL
;;
maintenance)
restic forget -r "$repo" -v --prune \
--keep-last 2 --keep-daily 7 --keep-weekly 5 --keep-monthly 6 --keep-yearly 3 \
> ~/.restore-info/tmp/maint-forget-log-${script_started}.txt
forget_exit_code="$?"
restic -r "$repo" check --read-data-subset=10% \
> ~/.restore-info/tmp/maint-check-log-${script_started}.txt
check_exit_code="$?"
maint_done=$(date +%k%M%S)
2021-12-30 14:14:51 -05:00
echo "sending email report"
cat <<EOMAIL | msmtp --read-envelope-from $BACKUP_EMAIL_TO
To: $BACKUP_EMAIL_TO
From: $BACKUP_EMAIL_FROM
Subject: Backup maintenance report: $script_started
- Finished $maint_done
- Forgetting exited $forget_exit_code
- Checking exiting $check_exit_code
$(cat ~/.restore-info/tmp/maint-forget-log-${script_started}.txt)
$(cat ~/.restore-info/tmp/maint-check-log-${script_started}.txt)
EOMAIL
;;
2021-12-29 21:37:33 -05:00
exec)
shift
restic -r "$repo" "$@" ;;
esac