diff options
author | Max Christian Pohle | 2020-05-06 00:01:37 +0200 |
---|---|---|
committer | Max Christian Pohle | 2020-05-06 00:01:37 +0200 |
commit | d9804a00d1d2164f5c25d743d18bd4f6a8776f31 (patch) | |
tree | 4bb7bab7260251d4739c775a4a4f6632bc44d21f | |
parent | 2b59a5c8b5e77e115b6b4ca2463cdfe70b0b51ba (diff) | |
download | zfs-bash-tools-d9804a00d1d2164f5c25d743d18bd4f6a8776f31.tar.bz2 zfs-bash-tools-d9804a00d1d2164f5c25d743d18bd4f6a8776f31.zip |
Added automatic backup script
This is WIP and should not be used by anybody until I have fixed hard
coded paths and stuff like that.
The idea of this script is: Once you plug a disk drive in, it will
automatically be backed up, but only if a zfs dataset with the name of
that drive already exists.
-rwxr-xr-x | backup/backup.sh | 75 | ||||
-rw-r--r-- | backup/systemd/max-backup@.service | 8 | ||||
-rw-r--r-- | backup/udev/rules.d/99-max-autobackup.rules | 2 |
3 files changed, 85 insertions, 0 deletions
diff --git a/backup/backup.sh b/backup/backup.sh new file mode 100755 index 0000000..91acdfc --- /dev/null +++ b/backup/backup.sh | |||
@@ -0,0 +1,75 @@ | |||
1 | #!/usr/bin/bash | ||
2 | logger "================================================================================" | ||
3 | |||
4 | # BACKUP_NAME=$ID_SERIAL-part$ID_PART_ENTRY_NUMBER | ||
5 | BACKUP_NAME=$(echo $1 | sed -e 's/\\x2d/-/g') | ||
6 | MOUNT_TARGET=/media/$BACKUP_NAME | ||
7 | |||
8 | function fail() { | ||
9 | logger -s -t "$0" "Backup '$1' -> '$2' failed." | ||
10 | sudo -u max beep -f 40 -l 500 | ||
11 | # sudo -u max beep -f 523 -l 150 -n -f 54 -l 600 | ||
12 | } | ||
13 | |||
14 | function start() { | ||
15 | DEVNAME=$(echo $1 | sed 's/-/\\x2d/') | ||
16 | systemctl restart media-$DEVNAME.automount | ||
17 | |||
18 | logger -t "$0" "Backup '$1' -> '$2' started." | ||
19 | sudo -u max beep -f 147 -l 120 -n -f 294 -l 200 -n -f 831 | ||
20 | } | ||
21 | |||
22 | function stop() { | ||
23 | DEVNAME=$(echo $1 | sed 's/-/\\x2d/') | ||
24 | systemctl stop media-$DEVNAME.automount | ||
25 | |||
26 | logger -t "$0" "Backup '$1' -> '$2' finished." | ||
27 | sudo -u max beep -f 831 -l 120 -n -f 294 -l 200 -n -f 147 | ||
28 | } | ||
29 | |||
30 | function zfs_snapshot() { | ||
31 | zfs diff $LAST_SNAPSHOT > $ZFS_PATH/backup-differences.txt | ||
32 | zfs snap media-backup/$BACKUP_NAME@$(date +%Y-%m-%d--%H-%M) | ||
33 | echo "Here comes a list of changes since your last backup: $LAST_SNAPSHOT" | mailx -A typesafe -a $ZFS_PATH/backup-differences.txt -s 'Backup overview' max@entwicklerseite.de | ||
34 | } | ||
35 | |||
36 | start $1 | ||
37 | |||
38 | if ! findmnt $MOUNT_TARGET; then | ||
39 | logger "$MOUNT_TARGET is not mounted. Cannot back it up." | ||
40 | exit 0 | ||
41 | fi | ||
42 | |||
43 | logger ">> Inspecting $BACKUP_NAME" | ||
44 | ZFS_PATH=$(zfs get mountpoint -H -o value media-backup/$BACKUP_NAME) || fail "$MOUNT_TARGET" "(no zfs dataset found)" | ||
45 | ZFS_MOUNTPATH=$(findmnt -no source $ZFS_PATH) || fail "$ZFS_PATH (not mounted)" "(no association)" | ||
46 | LAST_SNAPSHOT=$(zfs list -t snap -o name -s creation media-backup/$BACKUP_NAME | tail -n1) | ||
47 | |||
48 | |||
49 | # Make sure, that we are really writing where a mountpoint is | ||
50 | if [ "$ZFS_PATH" == "/$ZFS_MOUNTPATH" ]; then | ||
51 | sudo -u max beep -f 831 | ||
52 | logger "We can start $MOUNT_TARGET -> $ZFS_PATH" | ||
53 | rsync -ahoi -x --delete --info=all \ | ||
54 | "/media/$BACKUP_NAME/" \ | ||
55 | "$ZFS_PATH" \ | ||
56 | 2>&1 | tee $ZFS_PATH/backup-report.txt && zfs_snapshot | ||
57 | |||
58 | else | ||
59 | fail $ZFS_PATH $ZFS_MOUNTPATH | ||
60 | exit 1 | ||
61 | fi | ||
62 | |||
63 | |||
64 | |||
65 | stop $1 | ||
66 | |||
67 | # sleep a second so that /dev/disk/by-id/*-part become available | ||
68 | # sleep 1 | ||
69 | |||
70 | # DISK=$(readlink -f /dev/disk/by-id/$ID_BUS-$ID_SERIAL) | ||
71 | # if ! [ -e "$DISK" ]; then | ||
72 | # fail | ||
73 | # logger "$DISK: Disk not there :(" | ||
74 | # exit 1 | ||
75 | # fi | ||
diff --git a/backup/systemd/max-backup@.service b/backup/systemd/max-backup@.service new file mode 100644 index 0000000..44c6bf8 --- /dev/null +++ b/backup/systemd/max-backup@.service | |||
@@ -0,0 +1,8 @@ | |||
1 | [Unit] | ||
2 | Description=Generic backup | ||
3 | |||
4 | [Service] | ||
5 | Type=simple | ||
6 | ExecStart=/home/max/backup/backup.sh %I | ||
7 | |||
8 | |||
diff --git a/backup/udev/rules.d/99-max-autobackup.rules b/backup/udev/rules.d/99-max-autobackup.rules new file mode 100644 index 0000000..e17bba0 --- /dev/null +++ b/backup/udev/rules.d/99-max-autobackup.rules | |||
@@ -0,0 +1,2 @@ | |||
1 | ACTION=="add", ENV{ID_PART_ENTRY_NUMBER}=="[0-9]", ENV{SYSTEMD_WANTS}="max-backup@$env{ID_SERIAL}\\x2dpart$env{ID_PART_ENTRY_NUMBER}.service" | ||
2 | LABEL="External disk drive backup" | ||