dotfiles/.restic/restic-do

98 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# set -euo pipefail
mkdir -p ~/.restore-info/tmp
chmod 700 ~/.restore-info
# ./restic -r s3:https://<WASABI-SERVICE-URL>/<WASABI-BUCKET-NAME> init
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)
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
;;
prune)
echo "not implemented yet" ;;
exec)
shift
restic -r "$repo" "$@" ;;
esac