Designdesk

Automatic backups on usb plug-in and sata hot-plug

Here is a method to fully automate your offline backups. Plug your backup drive, it's mounted automatically, runs rsync, then unmounts and initiate disk spin down for safe removal. Tones are played at each step, then alarm rings when the procedure is finished, so you know when you can remove the disk on a headless machine. A detailed log is kept so you can make sure everything was done properly.

You may need to apt-get update && apt-get install autofs beep

Label backup partitions

e2label /dev/sdX1 backup1
e2label /dev/sdZ1 backup2

Udev

Create /etc/udev/rules.d/90-cold-backups.rules

ACTION=="add", SUBSYSTEM=="block", ATTR{size}=="7160532148", TAG+="systemd", ENV{SYSTEMD_WANTS}+="cold_backup1.service"
ACTION=="add", SUBSYSTEM=="block", ATTR{size}=="2720217168", TAG+="systemd", ENV{SYSTEMD_WANTS}+="cold_backup2.service"

Then 3, 4, 5... as much as you have backup drives. Same with autofs and the rest.

Returns ATTR{size} for specified device : udevadm info -a -n /dev/sdX | grep size

To apply : udevadm control --reload-rules

I still need to find a better way than using size, to be able to use disks that are the same size. Using size is the most simple way i found to make it work with any device.

Autofs

Add to /etc/auto.master

/mnt /etc/auto.mnt

Create or add to /etc/auto.mnt

backup1 -fstype=auto    :/dev/disk/by-label/backup1
backup2 -fstype=auto    :/dev/disk/by-label/backup2

To apply : service autofs restart

Systemd

Create /etc/systemd/system/cold_backup1.service

[Service]
ExecStart=/root/bkp/cold_backup.sh backup1
User=root

With spindown option to shut down disk after completition :

[Service]
ExecStart=/root/bkp/cold_backup.sh backup1 spindown
User=root

To apply : systemctl daemon-reload

To run manually : systemctl start cold_backup1

Backup script

I run the code as root because I backup files that only root can access, if you don't need root, don't use it.

mkdir /root/bkp
mkdir /root/bkp/log
mkdir /root/bkp/exc

Create /root/bkp/cold_backup1.sh

#!/bin/sh

cd $HOME/bkp/

label=$1
name="cold_$label"
origin="/storage/"
target="/mnt/$label"

device=`readlink /dev/disk/by-label/$label`
device=${device##*/}
device=${device%?}

ignored=`cat exc/$name`

echo "\n\n\n///// STARTING BACKUP ///// `date`\n\nlabel:$label name:$name origin:$origin target:$target device:/dev/$device\n\nignored:\n\n$ignored\n\n***** STARTING RSYNC *****\n" > log/$name.log
beep -f 300 -n -f 350 -n -f 400 -n -f 300 -n -f 350 -n -f 400

rsync -aAHX --delete-before --exclude-from exc/$name --info=REMOVE,NAME1,PROGRESS2 $origin $target > log/$name.log

echo "\n\n***** RSYNC FINISHED *****\n`date`\n" > log/$name.log

du -shc /mnt/$label/*/ > log/$name.log
df -h /dev/disk/by-label/$label > log/$name.log

sync
udisks --unmount /dev/disk/by-label/$label

beep -f 450 -n -f 500 -n -f 550 -n -f 450 -n -f 500 -n -f 550

if [ "$2" = "spindown" ]; then
echo 1 > /sys/block/$device/device/delete
sleep 120
beep -r 3 -f 2200 -n -r 2 -f 2000
sleep 10
beep -r 3 -f 2200 -n -r 2 -f 2000
fi

Ignore list

Create /root/bkp/exc/cold_backup1 and fill with folders and files you dont want to rsync on this drive
Example :

Backup/Misc/
Backup/Documents/Videos/
Somefolder/Backed-up-on-other-drive/
Media/Not-critical/
Media/Stuff/
Misc/File