Simple Backup Script with Rsync

Rsync can be used for a numerous tasks. One of them is to create backups of your valuable data. I have used Rsync as my simple backup of my home directory to an external hardisk drive for some time.

The script below simply create a copy of what i want to backup to an external HDD. However, i did not use incremental backup method provided by Rsync. I use the "delete" and "update" switch to enable Rsync to overwrites older data and delete obsolete data in my backup drive. This will eliminate significant amount of time taken if your whole data that need to be backup is huge.

Most of the time i run this backup manually because i will only backup if i plug in my external HDD to my PC. However, this script can also be use to scheduled it into cron and backup data to a separate internal HDD or volumes/partition.

-----Start-----

#!/bin/bash

#INITIALIZATION

#Log file name
NOW=$(date +"%d%b%y")
LOGFILE="$NOW.log"
LOGDIR= specify your log files detination

#Destination definition
DESTINATION= specify your backup destination

#Sources List (using shell array)
#Make sure the array index numbers are in order.
SOURCE[0]= specify your 1st backup source
SOURCE[1]= specify your 2nd backup source
add on if you have more backup source

#Test and LOGS directory creation
if [ -d $LOGDIR ]
then
    echo "Note: LOGS exists"
else
    echo "Note: LOGS directory does not exists"
    echo "Note: Creating LOGS directory"
    mkdir $LOGDIR
fi

#BACKUP!
SOURCENUM=${#SOURCE[*]}
echo "Note: ##########################" | tee -a $LOGDIR/$LOGFILE
echo "Note: Today is $NOW" | tee -a $LOGDIR/$LOGFILE
echo "Note: There are $SOURCENUM backup sources" | tee -a $LOGDIR/$LOGFILE
echo "Note: ##########################" | tee -a $LOGDIR/$LOGFILE

for ((i=0;i<$SOURCENUM;i++)); do
    echo "Note: ****************************************************" | tee -a $LOGDIR/$LOGFILE
    echo "Note: Backing up ${SOURCE[${i}]} to $DESTINATION" | tee -a $LOGDIR/$LOGFILE
    echo "Note: ****************************************************" | tee -a $LOGDIR/$LOGFILE
    #rsync -r -v --progress --delete -u -l ${SOURCE[${i}]} $DESTINATION | tee -a $LOGDIR/$LOGFILE
    rsync -r -v --delete -u -l ${SOURCE[${i}]} $DESTINATION | tee -a $LOGDIR/$LOGFILE
done

/usr/bin/gedit $LOGDIR/$LOGFILE &
echo "Note: Press enter to exit."
read


-----End-----

Comments

Popular posts from this blog