aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbackup/backup.sh75
-rw-r--r--backup/systemd/max-backup@.service8
-rw-r--r--backup/udev/rules.d/99-max-autobackup.rules2
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
2logger "================================================================================"
3
4# BACKUP_NAME=$ID_SERIAL-part$ID_PART_ENTRY_NUMBER
5BACKUP_NAME=$(echo $1 | sed -e 's/\\x2d/-/g')
6MOUNT_TARGET=/media/$BACKUP_NAME
7
8function 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
14function 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
22function 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
30function 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
36start $1
37
38if ! findmnt $MOUNT_TARGET; then
39 logger "$MOUNT_TARGET is not mounted. Cannot back it up."
40 exit 0
41fi
42
43logger ">> Inspecting $BACKUP_NAME"
44ZFS_PATH=$(zfs get mountpoint -H -o value media-backup/$BACKUP_NAME) || fail "$MOUNT_TARGET" "(no zfs dataset found)"
45ZFS_MOUNTPATH=$(findmnt -no source $ZFS_PATH) || fail "$ZFS_PATH (not mounted)" "(no association)"
46LAST_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
50if [ "$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
58else
59 fail $ZFS_PATH $ZFS_MOUNTPATH
60 exit 1
61fi
62
63
64
65stop $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]
2Description=Generic backup
3
4[Service]
5Type=simple
6ExecStart=/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 @@
1ACTION=="add", ENV{ID_PART_ENTRY_NUMBER}=="[0-9]", ENV{SYSTEMD_WANTS}="max-backup@$env{ID_SERIAL}\\x2dpart$env{ID_PART_ENTRY_NUMBER}.service"
2LABEL="External disk drive backup"
..