From ad9f9ac72d338d8cf38fcfaa0200cd550d3aff22 Mon Sep 17 00:00:00 2001 From: earnest ma Date: Wed, 29 Dec 2021 21:37:33 -0500 Subject: [PATCH] backups! initial version --- .restic/.gitignore | 3 ++ .restic/README.md | 8 ++++ .restic/ignore.txt | 46 ++++++++++++++++++++++ .restic/restic-do | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 .restic/.gitignore create mode 100644 .restic/README.md create mode 100644 .restic/ignore.txt create mode 100755 .restic/restic-do diff --git a/.restic/.gitignore b/.restic/.gitignore new file mode 100644 index 0000000..66f02da --- /dev/null +++ b/.restic/.gitignore @@ -0,0 +1,3 @@ +.env +.repo-name +.repo-pass diff --git a/.restic/README.md b/.restic/README.md new file mode 100644 index 0000000..ce10802 --- /dev/null +++ b/.restic/README.md @@ -0,0 +1,8 @@ +You've run across the directory where I set up everything relating to how I do personal backups. I should/ will/ do test, but if you see something wrong/ questionable, please let me know, thanks :) + +Let's make sure we have these installed: + +- Bash +- Restic +- msmtp set up +- credentials set up w/ pash diff --git a/.restic/ignore.txt b/.restic/ignore.txt new file mode 100644 index 0000000..22bf2a8 --- /dev/null +++ b/.restic/ignore.txt @@ -0,0 +1,46 @@ + +$HOME/.Trash +$HOME/.cache +$HOME/.cargo +$HOME/.config/google-chrome +$HOME/.esmtp_queue +$HOME/.java +$HOME/.local/bin +$HOME/.local/lib +$HOME/.mozilla/firefox/*/storage/default/*/cache +$HOME/.npm +$HOME/.rustup +$HOME/.steam +$HOME/.steampath +$HOME/.steampid +$HOME/.subversion +$HOME/.thunderbird/*/ImapMail +$HOME/.thunderbird/*/Mail +$HOME/.var/app/*/cache # Flatpak + +$HOME/Downloads/nobkup +$HOME/Music +$HOME/tmp +$HOME/Videos # temporary until i get it cleared up + +$HOME/ghq # consider removing +$HOME/ghq/**/node_modules + +$HOME/.restore-info/tmp + +# Nix +$HOME/.nix-profile +$HOME/.nix-defexpr + +# Nextcloud +.owncloudsync.log +.sync_* +.sync-exclude.lst + +*.part +.DS_Store +.TemporaryItems +.Trash-1000 +.Trashes +._* +__pycache__ diff --git a/.restic/restic-do b/.restic/restic-do new file mode 100755 index 0000000..c8f8591 --- /dev/null +++ b/.restic/restic-do @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +# set -euo pipefail + +mkdir -p ~/.restore-info/tmp +chmod 700 ~/.restore-info + +# ./restic -r s3:https:/// 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 <