]> piware.de Git - bin.git/blob - backup
backup: figure out ssh agent when running from cron
[bin.git] / backup
1 #!/bin/sh
2 set -eu
3 cd $HOME
4 LOG=.cache/duplicity/log
5
6 # do backup every day
7 if [ -e "$LOG" ] && [ $(( `date +%s` - `stat -c %Y $LOG` )) -lt 86300 ]; then
8     exit 0
9 fi
10
11 # figure out $DISPLAY when running from cron
12 if [ -z "${DISPLAY:-}" ]; then
13     socket="/run/user/`id -u`/X11-display"
14     if [ -L "$socket" ]; then
15         socket=$(readlink "$socket")
16         export DISPLAY=:${socket##*/X}
17     fi
18 fi
19
20 if ! ip route | grep -q '^default.*wlan'; then
21     notify-send "Backup skipped, not on WLAN"
22     exit 0
23 fi
24
25 # figure out ssh agent when running from cron
26 if [ -z "${SSH_AUTH_SOCK:-}" ]; then
27     ssh_socket=$(ls /run/user/`id -u`/keyring-*/ssh 2>/dev/null)
28     if [ -S "$ssh_socket" ]; then
29         export SSH_AUTH_SOCK="$ssh_socket"
30     fi
31 fi
32
33 notify-send "Backup started"
34 mkdir -p $(dirname $LOG)
35 env PASSPHRASE="$(cat ~/.backup-passphrase)" duplicity --full-if-older-than 1M --exclude-globbing-filelist .duplicity-ignore . rsync://piware.de/backup/donald >> $LOG || { notify-send "BACKUP FAILED!"; exit 1; }
36 notify-send "Backup finished successfully"