===== Créer le fichier /etc/udev/rules.d/10-custom-usb-disk-detect.rules =====
vi /etc/udev/rules.d/10-custom-usb-disk-detect.rules
Contenu :
ACTION=="add", KERNEL=="sd?1", SUBSYSTEM=="block", RUN+="/usr/bin/systemctl --no-block start usb_backup@%k.service"
===== Créer le fichier /opt/usb_backup.sh =====
vi /opt/usb_backup.sh
Contenu :
#!/bin/bash
# apt install beep udisks2
LOG_FILE=$0.log
# CHANGEME ::::
PARTITION_LABEL="backuplgc"
MOUNT_POINT="/mnt/usb_backup_backuplgc"
RSYNC_PATHS[0]="/mnt/pve/hdd_8to/dump/"
RSYNC_PATHS[1]="/etc/"
RSYNC_PATHS[2]="/opt/"
RSYNC_PATHS[3]="/root/"
horoDate()
{
echo $(date "+%Y%m%d-%H%M%S")
}
function uptime_int()
{
if [ -e /proc/uptime ] ; then
echo `cat /proc/uptime | awk '{printf "%0.f", $1}'`
else
set +e
sysctl kern.boottime &> /dev/null
if [ $? -eq 0 ] ; then
local kern_boottime=`sysctl kern.boottime 2> /dev/null | sed "s/.* sec\ =\ //" | sed "s/,.*//"`
local time_now=`date +%s`
local uptime=$(($time_now - $kern_boottime))
echo $uptime
else
echo "-1"
fi
set -e
fi
}
errorBeep()
{
beep -f 220 -l 800 -d 200 -r 4
}
tooSoonBeep()
{
beep -f 1600 -l 100 -d 20 -r 4
}
victoryBeep()
{
beep -f 130 -l 100 \
-n -f 262 -l 100 \
-n -f 330 -l 100 \
-n -f 392 -l 100 \
-n -f 523 -l 100 \
-n -f 660 -l 100 \
-n -f 784 -l 300 \
-n -f 660 -l 300 \
-n -f 146 -l 100 \
-n -f 262 -l 100 \
-n -f 311 -l 100 \
-n -f 415 -l 100 \
-n -f 523 -l 100 \
-n -f 622 -l 100 \
-n -f 831 -l 300 \
-n -f 622 -l 300 \
-n -f 155 -l 100 \
-n -f 294 -l 100 \
-n -f 349 -l 100 \
-n -f 466 -l 100 \
-n -f 588 -l 100 \
-n -f 699 -l 100 \
-n -f 933 -l 300 \
-n -f 933 -l 100 \
-n -f 933 -l 100 \
-n -f 933 -l 100 \
-n -f 1047 -l 1000
}
echo "===============================================================================" >> $LOG_FILE
echo "$(horoDate) New usb device detected: $1." >> $LOG_FILE
if [ $(uptime_int) -lt 120 ]; then
echo "$(horoDate) Uptime too low, not running script." >> $LOG_FILE
tooSoonBeep
exit 1
fi
if [ -e /dev/disk/by-label/$PARTITION_LABEL ]; then
echo "$(horoDate) Partition label \"$PARTITION_LABEL\" found." >> $LOG_FILE
mount /dev/disk/by-label/$PARTITION_LABEL $MOUNT_POINT >> $LOG_FILE 2>&1
if [ $? -eq 0 ]; then
echo "$(horoDate) Partition /dev/disk/by-label/$PARTITION_LABEL mounted on $MOUNT_POINT." >> $LOG_FILE
for RSYNC_PATH in ${RSYNC_PATHS[*]}; do
echo "$(horoDate) Rsync "$RSYNC_PATH" to $MOUNT_POINT/$(basename $RSYNC_PATH)">> $LOG_FILE
rsync -aAX --delete --delete-before "$RSYNC_PATH" $MOUNT_POINT/$(basename $RSYNC_PATH) >> $LOG_FILE 2>&1
done
echo "$(horoDate) Backup ended, unmounting partition, shutting off disk and signal end of script." >> $LOG_FILE
sync
sync
umount $MOUNT_POINT >> $LOG_FILE 2>&1
sync
sync
echo "$(horoDate) unmounting done" >> $LOG_FILE
echo -e "Script de backup termine correctement :\n\n\n$(tail -n 10 $(echo $LOG_FILE))" | mail -s "[$(hostname -f)] USB backup script : OK" root
victoryBeep
udisksctl power-off --block-device $(readlink -f "/dev/disk/by-label/backuplgc" | sed -e 's/[- 0-9]*$//') >> $LOG_FILE 2>&1
exit 0
else
echo "$(horoDate) Cannot mount /dev/disk/by-label/$PARTITION_LABEL on $MOUNT_POINT, end of script." >> $LOG_FILE
echo -e "Script de backup termine AVEC PROBLEMES :\n\n\n$(tail -n 10 $(echo $LOG_FILE))" | mail -s "[$(hostname -f)] /!\\ USB backup script : KO /!\\" root
errorBeep
exit 2
fi
else
echo "$(horoDate) Partition label \"$PARTITION_LABEL\" no found, end of script." >> $LOG_FILE
echo -e "Script de backup termine AVEC PROBLEMES :\n\n\n$(tail -n 10 $(echo $LOG_FILE))" | mail -s "[$(hostname -f)] /!\\ USB backup script : KO /!\\" root
errorBeep
exit 3
chmod 750 /opt/usb_backup.sh
===== Ajouter le service systemd =====
systemctl edit --full --force usb_backup\@.service
Mettre :
[Unit]
Description=Backup to USB Flash Disk
BindsTo=dev-%i.device
[Service]
Type=simple
ExecStart=/opt/usb_backup.sh %I
Puis lancer :
systemctl daemon-reload
udevadm control --reload-rules && udevadm trigger
systemctl restart systemd-udevd
Pour créer un disque de backup :
* faire une table de partition GPT
* créer 1 partition de type Linux, avec le label correspondant à $PARTITION_LABEL
* formater la partition
* tester