Skip to content

Instantly share code, notes, and snippets.

@EnTeQuAk

EnTeQuAk/backup.sh

Forked from thetooth/backup.sh
Created Nov 23, 2017
Embed
What would you like to do?
#!/usr/bin/env bash
# This script will
# 1. (Optionally) First snapshot your BTRFS /home subvolume as /resticbackup
# 2. Generate a list of packages and backup as `pkglist'
# 3. Backup the pacman cache directory
# 4. Backup the users home directory (from the BTRFS snapshot or directly)
# Try to load a credentials file
source ./backup-creds &> /dev/null || true
# Pretty printers
msg() { printf "\033[1;34m$* \033[0m\n" && (echo $* >> /tmp/backup.log); }
err() { printf "\033[1;31m$* \033[0m\n" && (echo ERROR: $* >> /tmp/backup.log); exit 1; }
# Handle ^C etc
function finish {
# Uninstall trap to stop double trigger
trap - INT TERM
# Clean up files and processes
rm -f /tmp/pkglist &> /dev/null &
killall restic &> /dev/null &
# Wait for cleanup tasks to finish
wait; err Interrupt received
}
trap finish INT TERM
# Exit on errors and pipe errors
set -e -o pipefail
# Clear old log
echo -n > /tmp/backup.log
# START!
msg Starting backup $(date)
# Sanic
echo -e "\033[0;33m"
cat << 'EOF'
- = .--._
- - ~_= =~_- = - `. `-.
==~_ = =_ ~ - = .-' `.
--=~_ - ~ == - = .' _..:._
---=~ _~ = =- = `. .--.' `.
--=_-=- ~= _ - = - _.' `. .--.:
-=_~ -- = = ~- .' : : :
-=-_ ~= = - _-`--. : .--: D
-=~ _= = -~_= `; .'.: ,`---'@
--=_= = ~- -= .' .' `._ `-.__.'
--== ~_ - = =- .' .' _.`---'
--=~_= = - = ~ .'--'' . `-..__.--.
jgs--==~ _= - ~-= =-~_- `-..___( ===;
--==~_==- =__ ~-= - - .' `---'
EOF
echo -e "\033[0m"
# Status
restic snapshots -q &>> /tmp/backup.log & wait || { err "( ≖‿≖)"; }
# Snapshot /home subvolume
# msg Taking BTRFS snapshot...
# SNAP="/resticbackup"
# [ -d $SNAP ] && btrfs subvol del "$SNAP"
# if ! btrfs subvol snap /home "$SNAP" | tee -a /tmp/backup.log
# then
# err Error taking filesystem snapshot
# fi
# Generate list of currently installed packages
msg Generating pkglist...
if ! pacman -Qqe | grep -v "$(pacman -Qmq)" | tee -a /tmp/backup.log 1> /tmp/pkglist
then
err Error, couldn\'t generate a package list
fi
# Backup package list and cache
msg Backup package list and cache...
if ! restic backup --tag pacman --tag pkglist /tmp/pkglist -q | tee -a /tmp/backup.log
then
err Error backing up pkglist
fi
if ! restic backup --tag pacman --tag cache /var/cache/pacman/pkg/ -q | tee -a /tmp/backup.log
then
err Error backing up package cache
fi
# Backup home directory
msg Backup $HOME...
if ! restic backup --tag home --exclude-caches \
$HOME \ #--prefix $HOME --strip-components 1 $SNAP/$HOME \
-q | tee -a /tmp/backup.log
then
err Could not backup home\!
fi
msg Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment