Sunday, January 2, 2011

Backup local workspace to external hdd

Backup local development workspace to external hard drive or other storage devices.
Ubuntu backup files script

1. Plug in external disk

2. Create new mount point directory for the disk (/dev/sda1 in this example; to see which one is it, run df -h)
$ mkdir /mnt/My-External-Disk

3. Create backup dir
$ mkdir /mnt/My-External-Disk/backup-dir/

4. Create backup script with content:



filesno=`ls $ddir | wc -l`
if [[ $filesno = 0 ]]; then
echo "mounting $ddir"
mount /dev/sda1 $ddir

filesno=`ls $ddir | wc -l`
if [[ -d $bkpdir && $filesno > 0 ]]; then
echo "synchronizing dirs"
rsync -a --delete /home/user/dir1 $bkpdir
rsync -a --delete /home/user/dir2 $bkpdir

# Make sure you check the line mount /dev/sda1 $ddir and replace with adequate device.

5. Make script executable
chmod +x

6. Run it for test
$ sudo ./

7. Create new cron entry for it ($ sudo mcedit /etc/crontab) and set it to run every 10 minutes:
*/10 * * * * root /path-to-script/

The script only runs if the backup directory exists. This is a protection in case another disk is plugged in and is assigned as "/dev/sda1". This way the script will always back up files only to the right disk.

No comments: