Using Rsync for Remote Backup
Being an IT professional, you not only need to secure your data from those hackers, but also to have a good backup copy when it is needed. I have been using the rsync for remote backup since 1998. Here is a little bash script I used for remote backup. It is a nice handy tool that can be combined with the cron scheduler to do the backup.
#— Beginning of the Script ————————
#!/bin/bash
PATH=”/bin:/usr/bin:/sbin”
ADMIN_USER=”xxxx”
NOW=`date +%Y%m%d%t%T`
CURRENT_TIME=`date +%F-%H%M%S`
SOURCE_DIR=$1
TARGET_DIR=$2
TMP_LOG_FILE=”/var/log/.sync_dir-${CURRENT_TIME}.log”
LOG_FILE=”/var/log/sync_dir.log”
if [ -z “$1” ]
then
echo ‘Error: Argument Required’
echo ‘Syntax: ‘ ${0} ‘ source_dir target_dir’
echo ‘ Note: must include the last / ‘
exit
fi
echo “” > $TMP_LOG_FILE
echo “=====================================” >> $TMP_LOG_FILE
echo “Bash Script: ” ${0} >> $TMP_LOG_FILE
echo “Host…….: ” ${HOSTNAME} >> $TMP_LOG_FILE
echo “Source Dir.: ” ${SOURCE_DIR} >> $TMP_LOG_FILE
echo “Target Dir.: ” ${TARGET_DIR} >> $TMP_LOG_FILE
echo “” >> $TMP_LOG_FILE
until rsync -avzr –delete $SOURCE_DIR $TARGET_DIR >> $TMP_LOG_FILE
do
echo “Another Try: `date +%Y%m%d%t%T`” >> $TMP_LOG_FILE
sleep 1m
done
echo “” >> $TMP_LOG_FILE
echo “Start……: ${NOW}” >> $TMP_LOG_FILE
echo “End……..: `date +%Y%m%d%t%T`” >> $TMP_LOG_FILE
echo “=====================================” >> $TMP_LOG_FILE
mail -s “$0 – ${HOSTNAME} ($1 -> $2)” $ADMIN_USER < $TMP_LOG_FILE
#— accumulate the log file
cat $LOG_FILE >> $TMP_LOG_FILE
cp -f $TMP_LOG_FILE $LOG_FILE && rm -f $TMP_LOG_FILE
#— End of Script ———————————————————-
Things you need to configure inside your server. That is the rsyncd.conf. You need to add entries to allow remote host to come in and get the data. More details can be found from the man page of rsyncd.conf. There are some advantages of using this script. Upon successful, the ADMIN_USER will receive the backup status by email. Of course, you have to key in the user name for receiving the email. Also in case of connection failure or unstable connection, the script will loop endlessly to do the backup for you.
So happy backing up your data.
References
http://rsync.samba.org/