]> piware.de Git - bin.git/blob - backup
build-cockpit-toolbox: tasks container moved to ghcr.io
[bin.git] / backup
1 #!/bin/sh
2 set -eu
3 cd $HOME
4 LOG=.cache/backup/log
5 PATH=$PATH:/sbin:/usr/sbin
6 RESTIC="restic --password-file $HOME/.config/backup-passphrase --repo sftp:piware.de:backup/restic"
7
8 fail() {
9     notify-send -i /usr/share/icons/Adwaita/48x48/status/network-error-symbolic.symbolic.png -u critical -t 180000 "${1:-BACKUP FAILED!}"
10     exit 1
11 }
12
13 # do backup every day
14 if [ -e "$LOG" ] && [ $(( `date +%s` - `stat -c %Y $LOG` )) -lt 86300 ]; then
15     exit 0
16 fi
17
18 # figure out $DISPLAY when running from cron
19 export DISPLAY="${DISPLAY:-:0}"
20
21 if ! ip route show default | grep -Eq 'dev (enp|wl)'; then
22     notify-send "Backup skipped, not on WLAN"
23     exit 0
24 fi
25
26 # figure out ssh agent when running from cron
27 if [ -z "${SSH_AUTH_SOCK:-}" ]; then
28     ssh_socket=$(ls /run/user/`id -u`/keyring*/ssh 2>/dev/null)
29     if [ -S "$ssh_socket" ]; then
30         export SSH_AUTH_SOCK="$ssh_socket"
31     fi
32 fi
33
34 notify-send "Backup started"
35 mkdir -p $(dirname $LOG)
36
37 $RESTIC backup --exclude-file=$HOME/.config/backup-ignore $HOME >> $LOG 2>&1 || fail
38 # TODO: forget --prune policy: https://restic.readthedocs.io/en/stable/060_forget.html
39 notify-send "Backup finished successfully"
40
41 scp .config/backup-passphrase piware.de:.cache/
42 ssh piware.de chmod u+w .cache/backup-passphrase
43 trap "ssh piware.de shred -u .cache/backup-passphrase" EXIT INT QUIT PIPE
44
45 ssh piware.de restic --password-file .cache/backup-passphrase --repo backup/restic forget --prune --keep-within-hourly 24h --keep-within-daily 7d --keep-within-weekly 30d --keep-within-monthly 12m
46 notify-send "Backup pruned successfully"
47
48 ssh piware.de restic --password-file .cache/backup-passphrase --repo backup/restic check || fail "BACKUP CHECK FAILED!"
49 notify-send "Backup checked successfully"