]> piware.de Git - bin.git/blobdiff - backup
system-maintenance: Update flatpaks
[bin.git] / backup
diff --git a/backup b/backup
index 0f16d58dc778ecce1d8d3bbe228916d3ab7f4e0f..7a424862cc100015608c6cee15d85a5e0d62c5ae 100755 (executable)
--- a/backup
+++ b/backup
@@ -1,44 +1,32 @@
-#!/bin/sh -e
-
-# call rsnapshot daily/weekly/monthly regularly
-# this should be called from cron very often (several times a day) to not miss
-# a cycle when the machine is powered down for extended times.
-#
-# Author: Martin Pitt <martin@piware.de>
-# License: Public Domain
-
-backupdir=`grep ^snapshot_root ~/.rsnapshotrc | awk '{print $2}'`
-
-now=`date +%s`
-
-get_age() {
-    if [ -e $backupdir/$1 ]; then
-        age=$(($now - `stat -c %Y $backupdir/$1`))
-    else
-        unset age
+#!/bin/sh
+set -eu
+cd $HOME
+LOG=.cache/duplicity/log
+PATH=$PATH:/sbin:/usr/sbin
+
+# do backup every day
+if [ -e "$LOG" ] && [ $(( `date +%s` - `stat -c %Y $LOG` )) -lt 86300 ]; then
+    exit 0
+fi
+
+# figure out $DISPLAY when running from cron
+export DISPLAY="${DISPLAY:-:0}"
+
+if ! ip route show default | grep -Eq 'dev (enp|wl)'; then
+    notify-send "Backup skipped, not on WLAN"
+    exit 0
+fi
+
+# figure out ssh agent when running from cron
+if [ -z "${SSH_AUTH_SOCK:-}" ]; then
+    ssh_socket=$(ls /run/user/`id -u`/keyring*/ssh 2>/dev/null)
+    if [ -S "$ssh_socket" ]; then
+        export SSH_AUTH_SOCK="$ssh_socket"
     fi
-}
-
-# abort if last backup was less than a day ago
-get_age daily.0
-[ -n "$age" ] && [ $age -lt 86300 ] && exit 0
-
-print '------------1---------------'
-# monthly backup if last monthly is older than a month (if it doesn't exist,
-# check oldest weekly)
-get_age monthly.0
-[ -n "$age" ] || get_age weekly.3
-[ -n "$age" ] && [ $age -ge 2591900 ] && mode=monthly
-
-# weekly backup if last weekly is older than a week (if it doesn't exist,
-# check oldest daily)
-get_age weekly.0
-[ -n "$age" ] || get_age daily.6
-[ -n "$age" ] && [ $age -ge 604700 ] && mode=weekly
-
-# default to daily
-[ -n "$mode" ] || mode=daily
+fi
 
-# call rsnapshot
-echo mode: $mode
-rsnapshot -c ~/.rsnapshotrc $mode
+notify-send "Backup started"
+mkdir -p $(dirname $LOG)
+env PASSPHRASE="$(cat ~/.backup-passphrase)" duplicity --allow-source-mismatch --full-if-older-than 1M --exclude-filelist .duplicity-ignore . rsync://piware.de/backup/laptop >> $LOG || { notify-send "BACKUP FAILED!"; exit 1; }
+notify-send "Backup finished successfully"
+duplicity remove-all-but-n-full 6 --force rsync://piware.de/backup/laptop