11/05/2021

Backup-script using cronjob and rsync when external disk is connected

original blog (german) 

original script

The original script  worked partial for me! I missed notifications though. No idea why! The original script works in Debian.

 

First place the backup script in /root.

On Arch Linux make sure libnotify is installed and cronie.service is enabled:

$ sudo pacman -S libnotify

$ sudo systemctl enable cronie.service

$ sudo systemctl start cronie.service  


Get the external disk UUID:

$ ls -l /dev/disk/by-uuid

respectively

$ sudo blkid /dev/sdX

change sdX with your disk.  

 

Get your userid:

$ id -u <username>

 

Do a new cronjob:

$ sudo crontab -e

Fill in:

*/1 * * * * /root/backup.sh >> /dev/null 2>&1

Like this the script  is called every minute by cron.


This is my changed script:

Replace UUID, <yourusername> and <youruserid>!

Be careful! The script will sync source-directory with destination-directory. Non existing files in source-directory will be deleted from destination-directory!

The script below will only do a testrun - change rsync options from -avn to -av


________________________________________________________________________________

#!/bin/bash
#holzboa
#Use at your own risk!


########## Konfiguration ############ 
UUID="d2428c4a-1a0b-4f1a-97eb-582b2b1adb94" #replace with you external disk UUID
CHECK_DISC="/dev/disk/by-uuid/$UUID"
DATE=`/bin/date +%Y%m%d`
CHECK_DATE_DIR="/root" #date-file directory and temporary mount backup-disk
BACKUP_DIR_MEDIA="backup" #here the backup-disk will be mounted temporary
BACKUP_DIR="$CHECK_DATE_DIR/$BACKUP_DIR_MEDIA" #backup directory, in this case root of backup disk
DATE_FILE="$CHECK_DATE_DIR/$DATE.date" #date-file, shows if backup was already done
CHECK_RSYNC=`ps -aef | grep -v grep | grep rsync | wc -l`
CHECK_MOUNT=`df -h | awk '{print $6}' | grep $BACKUP_DIR_MEDIA | wc -l`
SOURCE_DIR="/directory/to/save /other/directory/to/save /next/directory/to/save" # if you put a / in the end of path only the files inside the directory get saved

#####################################

/bin/umount $CHECK_DISC

if [ ! -d $CHECK_DATE_DIR/$BACKUP_DIR_MEDIA ]; then
    /bin/mkdir $CHECK_DATE_DIR/$BACKUP_DIR_MEDIA
fi

if [ $CHECK_RSYNC = 0 ]; then #if no other rsync-process is running, start it up
    if [ -e $CHECK_DISC ]; then
        if [ $CHECK_MOUNT = 0 ]; then
            if [ ! -f $DATE_FILE ]; then
                /bin/mount UUID=$UUID $CHECK_DATE_DIR/$BACKUP_DIR_MEDIA
                sleep 5
                rm -f $CHECK_DATE_DIR/*.date
                sudo -u <yourusername> DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/<youruserid>/bus notify-send "backup-disk detectet" "start\ backup\ \(You\ will\ recieve\ a\ message\ after\ finishing\!\)"
                /usr/bin/rsync -avn --safe-links --delete --ignore-errors $SOURCE_DIR $BACKUP_DIR #only does a testrun. To do your backup change -avn to -av
                /bin/sync
                /usr/bin/touch $DATE_FILE
                /bin/umount /dev/disk/by-uuid/$UUID
            else
                sudo -u <yourusername> DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/<youruserid>/bus notify-send "Backup already done today" "You\ may\ plug\ out\ the\ disk\ now\!"
            fi
        fi
    fi
fi