Automated backup on external hard drive

Hello, 

I found a script on the internet, which I slightly modified for the My Cloud and to fit my needs (see  http://frustratedtech.com/post/52316736743/bash-script-mount-drive-automatically-to-backup)..) I want to share it with you and especially discuss it with you to know if what I have done is reliable (as I am not an expert I’d like to have someone more expert to judge this saving method). 

Caution: Don’t use this script if you don’t exactly know what you are doing (risking to brick your device or lose your data). I am not an expert to be sure that it will work on YOUR device configuration without further modifications.

The idea is to have an external hard drive permanently connected to the My Cloud. Each week (or on whatever desired frequency), the drive is automatically mounted, a backup of the data on the MyCloud performed and when finished the drive is dismounted. And so on. 

The script is the following : 

backup.sh

#!/bin/bash

#backup harddrive variables
UUID='E04D-1EF3'
BACKUPDIR='/shares/USB'
TOBACKUP='/shares/Public/exchange'
PARTITION=`/bin/readlink -f /dev/disk/by-uuid/$UUID`
TIMER=3

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

echo 'UUID: ' $UUID
echo 'Drive Partition: ' $PARTITION;

#check drive and partiton match
if /sbin/blkid | grep -s "$PARTITION" | grep -q "$UUID"; then
    echo 'Drive and partition match...'
else
        exit
fi

#if [-z "$DIRECTORY"]; then
# echo 'No directory present...'
#fi

#check if drive mounted
if grep -qs $PARTITION /proc/mounts; then
        #its already mounted
        MOUNTED='1'
        echo 'Drive already mounted...'
        BACKUPDIR=`grep $PARTITION /proc/mounts | awk '{ print $2;exit; }'`
        echo 'Directory: ' $BACKUPDIR
else
        #not mounted
        MOUNTED='0'
        echo 'Mounting drive...'
        sudo mkdir -p "$BACKUPDIR"
        sudo mount /dev/disk/by-uuid/$UUID "$BACKUPDIR"
         echo 'Mounted: ' $BACKUPDIR
fi

#perform home backup
sudo rsync -rtv --modify-window=1 --delete --stats --progress --log-file=/share/logs/backup.log $TOBACKUP $BACKUPDIR
#clean up
#if [$MOUNTED = 0]; then
        #unmount and remove dir
        echo 'Unmounting drive...'
        sudo umount -l "$PARTITION"
        sudo umount -l "$PARTITION"
        sudo rmdir "$BACKUPDIR"
        find /shares/ -maxdepth 1 -type l -delete
#else
# echo 'Leaving drive mounted...'
#fi

echo 'Backup Complete!'

 There are three variables to indicate :

  • UUID : the id of your drive, so that it can be automatically mounted

  • BACKUPDIR : where to mount the drive WHEN it is dismounted (if you plug the external hard drive the first time, the disk will be already mounted and BACKUPDIR will be found by the script)

  • TOBACKUP : what folder needs to be saved on the external hard drive

The UUID can be found as following :

1/ Connect your USB drive to the the My Cloud and type

dmesg

 You will get something like :

[487.550389] sd 3:0:0:0: Attached scsi generic sg1 type 0
[487.561710] sd 3:0:0:0: [sdb] 31405824 512-byte logical blocks: (16.0 GB/14.9 GiB)
[487.570577] sd 3:0:0:0: [sdb] Write Protect is off
[487.575472] sd 3:0:0:0: [sdb] Mode Sense: 43 00 00 00
[487.581085] sd 3:0:0:0: [sdb] No Caching mode page present
[487.586699] sd 3:0:0:0: [sdb] Assuming drive cache: write through
[487.598774] sd 3:0:0:0: [sdb] No Caching mode page present
[487.604572] sd 3:0:0:0: [sdb] Assuming drive cache: write through
[487.614063] sdb: sdb1
[487.620443] sd 3:0:0:0: [sdb] No Caching mode page present
[487.626026] sd 3:0:0:0: [sdb] Assuming drive cache: write through
[487.632218] sd 3:0:0:0: [sdb] Attached SCSI removable disk
[527.171966] usb 1-1: reset high-speed USB device number 3 using xhci-hcd

 Now you know that your drive is named sdb1. Now type:

blkid

 And you will get:

/dev/sda1: UUID="ef947a82-072c-be3b-997c-eb89519129b4" TYPE="linux_raid_member" 
/dev/sda2: UUID="ef947a82-072c-be3b-997c-eb89519129b4" TYPE="linux_raid_member" 
/dev/sda3: UUID="90956816-75ac-43df-bad8-388eec6978fb" TYPE="swap" 
/dev/sda4: UUID="26dc8643-9e24-44d5-86e9-48dda1c4625c" TYPE="ext4" 
/dev/md1: UUID="dea09609-1dfc-4e3f-bc27-08e8988ceaae" TYPE="ext3" 
/dev/sdb1: LABEL="USB-DISK" UUID="E04D-1EF3" TYPE="vfat"

 Now you know that the UUID of the drive is : 

E04D-1EF3

Simply run the script :

bash backup.sh

 And that should work :slight_smile:

You can than periodically run the backup with crontab. Add a crontab rule by typing:

crontab -e

 And add something like that (to run in every day on 5pm)

00 17 * * * cd /root/ && /bin/bash backup.sh >> /shares/logs/crontab.log

wdmcphoto stuff must also be stopped (since every time a USB disk is mounted) these processes run and use a lot of ressources (and are not useful for me).

 My questions are:

1/ May that somehow damage the My Cloud (I restarted the device, it found its blue light --> all seems to be OK) ? 

2/ What is more consumming for both the external usb hard drive and the my cloud? Letting the USB drive always mounted or mounting / dismounting it one time per week? 

Thank you.

1 Like

And one more question: I will now buy a bigger external USB hard drive to have more space for my backups. Can My cloud handle an external hard drive which has not it’s own power supply? (I want to buy a auto alimented USB hard drive)

As long as its a bus-powered USB drive yes.

Yes but if you have a larger drive, it’s better to get those external powered. Remember WDMyCloud is only 12v 2a powered. The USB3 bus could just give up in midst of heavy transfers.