Automated Backup on external HDD when device is connected via USB?


Tracker
Go to solution Solved by Hoopster,

Recommended Posts

Hi everyone,

 

I am searching for a backup solution, which starts autmatically the backup process on a attached external device and detache the device when backup is completed. Additionally a short notification e.g. via mail would be helpfull.

Is their a kind of solution?

 

I had this concept before with my old synology, to have manual backup. It was a additional fall back solution in the case of ransomware attack or fire/water damage.

 

BR,

Tracker

 

 

 

Link to comment
36 minutes ago, Tracker said:

I am searching for a backup solution, which starts autmatically the backup process on a attached external device and detache the device when backup is completed. Additionally a short notification e.g. via mail would be helpfull

AS Kilrah noted, check out the backup script example in the UD plugin thread.

 

I modified the script to backup four of my shares (instead of just Photos in the example).

 

As soon as I plug in the external USB backup drive:

  1.  The drive mounts automatically via UD
  2.  The backup script runs automatically, with a GUI and email notification it is running, until complete.  The script backs up new/modified files only which makes it faster after the initial backup of everything
  3.  There is a beep when it completes and you are notified in the unRAID GUI and via email
  4.  The USB drive is unmounted in UD
Edited by Hoopster
  • Thanks 1
Link to comment
  • Solution
41 minutes ago, Tracker said:

do you have an example code for me

Here is the script with my modifications for backing up four shares:

#!/bin/bash
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
## Available variables: 
# AVAIL      : available space
# USED       : used space
# SIZE       : partition size
# SERIAL     : disk serial number
# ACTION     : if mounting, ADD; if unmounting, UNMOUNT; if unmounted, REMOVE; if error, ERROR_MOUNT, ERROR_UNMOUNT
# MOUNTPOINT : where the partition is mounted
# FSTYPE     : partition filesystem
# LABEL      : partition label
# DEVICE     : partition device, e.g /dev/sda1
# OWNER      : "udev" if executed by UDEV, otherwise "user"
# PROG_NAME  : program name of this script
# LOGFILE    : log file for this script

case $ACTION in
  'ADD' )
    #
    # Beep that the device is plugged in.
    #
    beep  -l 200 -f 600 -n -l 200 -f 800
    sleep 2

    if [ -d $MOUNTPOINT ]
    then
      if [ $OWNER = "udev" ]
      then
        beep  -l 100 -f 2000 -n -l 150 -f 3000
        beep  -l 100 -f 2000 -n -l 150 -f 3000

        logger Started -t$PROG_NAME
        echo "Started: `date`" > $LOGFILE

    logger Pictures share -t$PROG_NAME
    rsync -a -v /mnt/user/Pictures $MOUNTPOINT/ 2>&1 >> $LOGFILE
		
	logger Videos share -t$PROG_NAME
    rsync -a -v /mnt/user/Videos $MOUNTPOINT/ 2>&1 >> $LOGFILE
		
	logger Movies share -t$PROG_NAME
    rsync -a -v /mnt/user/Movies $MOUNTPOINT/ 2>&1 >> $LOGFILE

	logger Family Videos share -t$PROG_NAME
    rsync -a -v /mnt/user/FamVideos $MOUNTPOINT/ 2>&1 >> $LOGFILE

        logger Syncing -t$PROG_NAME
        sync

        beep  -l 100 -f 2000 -n -l 150 -f 3000
        beep  -l 100 -f 2000 -n -l 150 -f 3000
        beep  -r 5 -l 100 -f 2000

        logger Unmounting Backup -t$PROG_NAME
        /usr/local/sbin/rc.unassigned umount $DEVICE

       echo "Completed: `date`" >> $LOGFILE
        logger Backup drive can be removed -t$PROG_NAME

        /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Server Backup" -d "Backup completed" -i "normal"
    fi
    else
        logger Backup Drive Not Mounted -t$PROG_NAME
  fi
  ;;

  'REMOVE' )
    #
    # Beep that the device is unmounted.
    #
    beep  -l 200 -f 800 -n -l 200 -f 600
  ;;

  'ERROR_MOUNT' )
	/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Server Backup" -d "Could not mount Backup" -i "normal"
  ;;

  'ERROR_UNMOUNT' )
	/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Server Backup" -d "Could not unmount Backup" -i "normal"
  ;;
esac

I just added these lines for each share I wanted to backup to the Unassigned Devices USB drive:

        logger {name of share} share -t$PROG_NAME
        rsync -a -v /mnt/user/{name of share} $MOUNTPOINT/ 2>&1 >> $LOGFILE

 

You just make the script the device script that is run when the USB drive is connected and mounted by Unassigned Devices.

image.thumb.png.c08cdec885c9d93e641b8dff8b46e147.png

 

47 minutes ago, Tracker said:

where I can see how the mail notification works

Email notifications are not controlled by the script.  This is just part of the Settings-->Notification Settings configuration in the GUI.  Here are my email settings to get unRAID to email notifications to me for the events selected in the Notification Setting section:

 

1004435461_SMTPSettings.thumb.jpg.396ad7fb601e44a232780f572f8a1b9e.jpg

  • Like 2
  • Thanks 1
Link to comment

Following script iterates over the array entries, which contains the shares that needs to be backuped:

#!/bin/bash
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
## Available variables:
# ACTION - if mounting, ADD; if unmounting, UNMOUNT; if unmounted, REMOVE; if error, ERROR_MOUNT, ERROR_UNMOUNT
# DEVICE - partition device, e.g. /dev/sda1
# UD_DEVICE - unassigned devX designation
# SERIAL - disk serial number
# LABEL - partition label
# LUKS - if the device is encrypted, this is the partition device, e.g. /dev/sda1
# FSTYPE - partition filesystem
# MOUNTPOINT - where the partition is mounted
# OWNER - "udev" if executed by UDEV, otherwise "user"
# PROG_NAME - program name of this script
# LOGFILE - log file for this script

directorys=( "/mnt/user/folder1"
"/mnt/user/folder2"
"/mnt/user/folder3"
"/boot/config/plugins/unassigned.devices")

notify_event="<Name of NAS>"
notify_subject="backup ext. disk"
notify_message="Follwoing shares were synced:"$'\n'


case $ACTION in
  'ADD' )
    #
    # Beep that the device is plugged in.
    #
    beep  -l 200 -f 600 -n -l 200 -f 800
    sleep 2

    if mountpoint -q $MOUNTPOINT; then
      if [ $OWNER = "udev" ]
      then
        beep  -l 100 -f 2000 -n -l 150 -f 3000
        beep  -l 100 -f 2000 -n -l 150 -f 3000

        logger Started -t $PROG_NAME
        echo "Started: `date`" > $LOGFILE        

                for dir in "${directorys[@]}";
                do
                        echo "#### Start: [`date`] ####" >> $LOGFILE
                        echo "# sync of share: ${dir} #" >> $LOGFILE
                        rsync -a -v "$dir" $MOUNTPOINT/ 2>&1 >> $LOGFILE
                        echo "## End: [`date`] ##" >> $LOGFILE
                        notify_message+="$dir"$'\n'
                done

        logger Syncing -t$PROG_NAME
        sync -f $MOUNTPOINT

        beep  -l 100 -f 2000 -n -l 150 -f 3000
        beep  -l 100 -f 2000 -n -l 150 -f 3000
        beep  -r 5 -l 100 -f 2000

        logger Unmounting Ext. HDDBackup Backup -t $PROG_NAME
        /usr/local/sbin/rc.unassigned umount $DEVICE

       echo "Completed: `date`" >> $LOGFILE
       logger Backup drive can be removed -t$PROG_NAME

        /usr/local/emhttp/webGui/scripts/notify -e "$notify_event" -s "$notify_subject" -d "Backup completed" -i "alert" -m "$notify_message"
    fi
    else
        logger Backup drive not mounted -t$PROG_NAME
  fi
  ;;

  'REMOVE' )
    #
    # Beep that the device is unmounted.
    #
    beep  -l 200 -f 800 -n -l 200 -f 600
  ;;

  'ERROR_MOUNT' )
        /usr/local/emhttp/webGui/scripts/notify -e "$notify_event" -s "$notify_subject" -d "Could not mount disk!" -i "alert"
  ;;

  'ERROR_UNMOUNT' )
        /usr/local/emhttp/webGui/scripts/notify -e "$notify_event" -s "$notify_subject" -d "Could not unmount disk!" -i "alert"
  ;;

 

The mail notifcation works via "alert"-state for the notification method -> 'ERROR_MOUNT'/'ERROR_UNMOUNT' -> In the system settings of the NAS, under the notification conriguration, I configuered that system notification with alert state will send as mail to.

  • Like 2
Link to comment
  • 4 months later...

Hey All,

I know this is an old thread, however I want to back up the entire share content of my unraid server to a USB disk.

 

Can I use copy and paste to use this script by @Tracker and just modify the directory's folder names right, do I need to name my unassigned drive to HDDBackup Backup to match this line in the above script

 

logger Unmounting Ext. HDDBackup Backup -t $PROG_NAME 

 

Let me know

PEACE

Kosti

Edited by Kosti
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.