From d9804a00d1d2164f5c25d743d18bd4f6a8776f31 Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Wed, 6 May 2020 00:01:37 +0200 Subject: 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. --- backup/backup.sh | 75 +++++++++++++++++++++++++++++ backup/systemd/max-backup@.service | 8 +++ backup/udev/rules.d/99-max-autobackup.rules | 2 + 3 files changed, 85 insertions(+) create mode 100755 backup/backup.sh create mode 100644 backup/systemd/max-backup@.service create mode 100644 backup/udev/rules.d/99-max-autobackup.rules (limited to 'backup') 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 @@ +#!/usr/bin/bash +logger "================================================================================" + +# BACKUP_NAME=$ID_SERIAL-part$ID_PART_ENTRY_NUMBER +BACKUP_NAME=$(echo $1 | sed -e 's/\\x2d/-/g') +MOUNT_TARGET=/media/$BACKUP_NAME + +function fail() { + logger -s -t "$0" "Backup '$1' -> '$2' failed." + sudo -u max beep -f 40 -l 500 + # sudo -u max beep -f 523 -l 150 -n -f 54 -l 600 +} + +function start() { + DEVNAME=$(echo $1 | sed 's/-/\\x2d/') + systemctl restart media-$DEVNAME.automount + + logger -t "$0" "Backup '$1' -> '$2' started." + sudo -u max beep -f 147 -l 120 -n -f 294 -l 200 -n -f 831 +} + +function stop() { + DEVNAME=$(echo $1 | sed 's/-/\\x2d/') + systemctl stop media-$DEVNAME.automount + + logger -t "$0" "Backup '$1' -> '$2' finished." + sudo -u max beep -f 831 -l 120 -n -f 294 -l 200 -n -f 147 +} + +function zfs_snapshot() { + zfs diff $LAST_SNAPSHOT > $ZFS_PATH/backup-differences.txt + zfs snap media-backup/$BACKUP_NAME@$(date +%Y-%m-%d--%H-%M) + 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 +} + +start $1 + +if ! findmnt $MOUNT_TARGET; then + logger "$MOUNT_TARGET is not mounted. Cannot back it up." + exit 0 +fi + +logger ">> Inspecting $BACKUP_NAME" +ZFS_PATH=$(zfs get mountpoint -H -o value media-backup/$BACKUP_NAME) || fail "$MOUNT_TARGET" "(no zfs dataset found)" +ZFS_MOUNTPATH=$(findmnt -no source $ZFS_PATH) || fail "$ZFS_PATH (not mounted)" "(no association)" +LAST_SNAPSHOT=$(zfs list -t snap -o name -s creation media-backup/$BACKUP_NAME | tail -n1) + + +# Make sure, that we are really writing where a mountpoint is +if [ "$ZFS_PATH" == "/$ZFS_MOUNTPATH" ]; then + sudo -u max beep -f 831 + logger "We can start $MOUNT_TARGET -> $ZFS_PATH" + rsync -ahoi -x --delete --info=all \ + "/media/$BACKUP_NAME/" \ + "$ZFS_PATH" \ + 2>&1 | tee $ZFS_PATH/backup-report.txt && zfs_snapshot + +else + fail $ZFS_PATH $ZFS_MOUNTPATH + exit 1 +fi + + + +stop $1 + +# sleep a second so that /dev/disk/by-id/*-part become available +# sleep 1 + +# DISK=$(readlink -f /dev/disk/by-id/$ID_BUS-$ID_SERIAL) +# if ! [ -e "$DISK" ]; then +# fail +# logger "$DISK: Disk not there :(" +# exit 1 +# 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 @@ +[Unit] +Description=Generic backup + +[Service] +Type=simple +ExecStart=/home/max/backup/backup.sh %I + + 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 @@ +ACTION=="add", ENV{ID_PART_ENTRY_NUMBER}=="[0-9]", ENV{SYSTEMD_WANTS}="max-backup@$env{ID_SERIAL}\\x2dpart$env{ID_PART_ENTRY_NUMBER}.service" +LABEL="External disk drive backup" -- cgit v1.2.3