aboutsummaryrefslogtreecommitdiffstats
path: root/arch-system/borgbackup
blob: 14b4795c97066cb9728ac835d6633d287dfb5516 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash

set -o errexit -o errtrace

BORG_HOST="borg@borg1.th73.ovh"
export BORG_REPO="$BORG_HOST:."
export BORG_PASSPHRASE="$(< ~/.borg_secret)"
export BORG_RSH="ssh -o IdentitiesOnly=yes -o ConnectionAttempts=20 -i ~/.ssh/borg_key"
export LC_COLLATE=C

SNAPSHOT_TARGET="/.snapshots/home-$(date "+%Y-%m-%dT%H:%M:%S")"
NETWORK_TIMEOUT=30

cleanup() {
    cd /
    sudo -n /usr/bin/backup-sudo cleanup
}

handle_failure() {
    if ! [ -t 1 ]; then
        notify-send -u critical "Backup failed!"
    fi

    echo "Backup failed!" >&2
    cleanup
    exit 2
}

trap cleanup EXIT
trap handle_failure INT TERM ERR

cleanup

backup() {
    # borg's caching of 'known' files is really dumb - it takes the full
    # canonical path to check if it has already seen a file. This means that the
    # path of files must never change if you want fast backups.
    # As symlinks won't work, we use mount(8) to ensure the paths stay the same.
    sudo -n backup-sudo mount "$1"

    if [ -t 1 ]; then
        borg_progress="--progress"
    fi

    cd /backup
    borg create --stats $borg_progress \
                --exclude-from ~/.borg_exclude \
                --show-rc \
                ::"$(basename "$1")" .

    cleanup
    sudo -n backup-sudo delete_snapshot "$1"
}

if [ ! -d "$SNAPSHOT_TARGET" ]; then
    sudo -n backup-sudo snapshot "$SNAPSHOT_TARGET"
fi

if ! ssh -To BatchMode=yes -o ConnectTimeout=$NETWORK_TIMEOUT \
         -o IdentitiesOnly=yes -o ConnectionAttempts=20 -i ~/.ssh/borg_key \
         "$BORG_HOST" "borg --version" > /dev/null; then
    echo "SSH server not reachable, skipping upload" >&2
    exit 0
fi

# break locks in case the previous run was interrupted
borg break-lock

for d in /.snapshots/*/ ; do
    echo "Now working on: $d"
    backup "$d"
done

echo "Pruning repository."

borg prune --show-rc --list \
           --keep-hourly 24 \
           --keep-daily   7 \
           --keep-weekly  4 \
           --keep-monthly 6

echo "Backup finished."