aboutsummaryrefslogtreecommitdiff
path: root/systemd-zfs-partition-backup.sh
blob: e40c2411f3c8e876802e521e59f1b20f8ddc11c3 (plain)
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
83
84
85
86
87
88
89
90
#!/usr/bin/bash
# Written in 2020 by Max Christian Pohle <webmaster@coderonline.de>

# check for a configuration file and source it in if its there
test -f /etc/default/systemd-zfs-partition-backup &&\
   source /etc/default/systemd-zfs-partition-backup

DISK_NAME=$1
DISK_NAME_ESCAPED=$(systemd-escape "$DISK_NAME")

logger "Disk: $DISK_NAME"
logger "    : $DISK_NAME_ESCAPED (escaped)"

function beep() {
    test -f /usr/bin/beep &&\
        env -u SUDO_GID -u SUDO_COMMAND -u SUDO_USER -u SUDO_UID /usr/bin/beep $@
}

function fail() {
    # logger -s -t "$0" "Backup '$1' -> '$2' failed."
    logger -s "$@"
    beep -f 40 -l 500
    # sudo -u max beep -f 523 -l 150 -n -f 54 -l 600
}

function start() {
    systemctl start media-$DISK_NAME_ESCAPED.automount ||\
        (fail "could not start disk with service media-$DISK_NAME_ESCAPED.automount"; exit 1)
}

function stop() {
    systemctl stop media-$DISK_NAME_ESCAPED.automount ||\
        (fail "could not stop disk with service media-$DISK_NAME_ESCAPED.automount"; exit 1)
}


# iterate over all datasets
for i in $(zfs list -H -o name);
do
    # compare the 'tail' of the PATH against the disk name...
    if [ "$(basename $i)" == "$DISK_NAME" ]; then
        start $DISK_NAME

        DISK_MOUNTPOINT="/media/$DISK_NAME"
        if ! findmnt $DISK_MOUNTPOINT; then
            logger -s "Expected to find disk '$DISK_NAME' mounted under '$DISK_MOUNTPOINT'. But it is not there. Aborting backup."
            beep -f 40 -l 500
            exit 1
        fi

        DATASET=$i
        DATASET_LAST_SNAPSHOT=$(zfs list -t snap -o name -s creation "$DATASET"  | tail -n1) ||\
            (fail "we could not find a previous backup. Backup may still be possible (first time backup?)")

        ZFS_MOUNTPOINT=$(zfs get mountpoint -H -o value "$DATASET") ||\
            (fail "could not determine mountpoint for dataset '$DATASET'"; exit 1)

        # sanity check: is it really mounted?
        findmnt -no source "$ZFS_MOUNTPOINT" ||\
            (fail "Dataset $DATASET is not mounted at $ZFS_MOUNTPOINT" ; exit 1)

        logger -s "Backup from '$DISK_MOUNTPOINT' -> '$ZFS_MOUNTPOINT' possible. Starting in 10 seconds..."

        beep -f 147 -l 120 -n -f 294 -l 200 -n -f 831

        set -x
        rsync -ahoi -x --delete --info=all \
            "$DISK_MOUNTPOINT" "$ZFS_MOUNTPOINT" \
            2>&1 | tee $ZFS_MOUNTPOINT/backup-report.txt
        set +x

        zfs snap $DATASET@$(date +%Y-%m-%d--%H-%M-%S) ||\
            (fail "Backup successful, but a snapshot of the dataset '$DATASET' was impossible :(")


        if [ -n "$SYSTEMD_ZFS_PARTITION_BACKUP_DIFF_FILENAME" ]; then
            zfs diff $DATASET_LAST_SNAPSHOT > $ZFS_MOUNTPOINT_VERIFIED/"$SYSTEMD_ZFS_PARTITION_BACKUP_DIFF_FILENAME"

            if [ -n "$SYSTEMD_ZFS_PARTITION_BACKUP_EMAIL" ]; then
                logger -s "Here comes a list of changes since your last backup: $LAST_SNAPSHOT" |\
                    mailx -A typesafe -a $ZFS_MOUNTPOINT_VERIFIED/"$SYSTEMD_ZFS_PARTITION_BACKUP_DIFF_FILENAME" -s 'Backup overview' "$SYSTEMD_ZFS_PARTITION_BACKUP_EMAIL"
            fi
        fi

        stop $DISK_NAME

        beep -f 831 -l 120 -n -f 294 -l 200 -n -f 147
    fi
done

..