Monday, November 28, 2011

Use RSYNC to backup data on a second hard disk

On a production server, if you have two hard disk and do not want to spend extra $50 each month, then you could use rsync to backup the data from your main disk to the second disk. It’s very easy to setup. All you to do is use to the following scripts. Save the following script as /disk3/rsync_backup.sh
#!/bin/bash
unset PATH
# USER VARIABLES
BACKUPDIR=/disk3                            # Folder on the backup server where the backups shall be located
#KEY=/root/.ssh/id_rsa                        # SSH key
#MYSQL_BACKUPSCRIPT=/root/my_backup.sh        # Path to the remote mysql backup script
#PRODUCTION_USER=root@production.server.com    # The user and the address of the production server
EXCLUDES=/disk1/backup_exclude                # File containing the excluded directories
DAYS=60                                        # The number of days after which old backups will be deleted
# PATH VARIABLES
SH=/bin/sh                                    # Location of the bash bin in the production server!!!!
CP=/bin/cp;                                    # Location of the cp bin
FIND=/usr/bin/find;                            # Location of the find bin
ECHO=/bin/echo;                                # Location of the echo bin
MK=/bin/mkdir;                                # Location of the mk bin
SSH=/usr/bin/ssh;                            # Location of the ssh bin
DATE=/bin/date;                                # Location of the date bin
RM=/bin/rm;                                    # Location of the rm bin
GREP=/bin/grep;                                # Location of the grep bin
MYSQL=/usr/bin/mysql;                        # Location of the mysql bin
MYSQLDUMP=/usr/bin/mysqldump;                # Location of the mysql_dump bin
RSYNC=/usr/bin/rsync;                        # Location of the rsync bin
TOUCH=/bin/touch;                            # Location of the touch bin
##                                                      ##
##      –       DO NOT EDIT BELOW THIS HERE     –     ##
##                                                      ##
# CREATING NECESSARY FOLDERS
$MK $BACKUPDIR
CURRENT=$BACKUPDIR/current
OLD=$BACKUPDIR/old
$MK $CURRENT
$MK $OLD
# CREATING CURRENT DATE / TIME
NOW=`$DATE ‘+%Y-%m’-%d_%H:%M`
NOW=$OLD/$NOW
$MK $NOW
# CREATE REMOTE MYSQL BACKUP BY RUNNING THE REMOTE BACKUP SCRIPT
# $SSH -i $KEY $PRODUCTION_USER “$SH $MYSQL_BACKUPSCRIPT”
# RUN RSYNC INTO CURRENT
#$RSYNC                                                            \
#    -apvz –delete –delete-excluded                        \
#     –exclude-from=”$EXCLUDES”                                \
#      -e “$SSH -i $KEY”                                        \
#       $PRODUCTION_USER:/                                        \
#        $CURRENT ;
// No Compression
$RSYNC                                                            \
-apv –delete –delete-excluded                        \
–exclude-from=”$EXCLUDES”                                \
/                                \
$CURRENT ;
# UPDATE THE MTIME TO REFELCT THE SNAPSHOT TIME
$TOUCH $BACKUPDIR/current
# MAKE HARDLINK COPY
$CP -al $CURRENT/* $NOW
# REMOVE OLD BACKUPS
for FILE in “$( $FIND $OLD -maxdepth 1 -type d -mtime +$DAYS )”
do
#    $RM -Rf $FILE
$ECHO $FILE
done
exit 0
Save the following to /disk3/backup_exclude. Essentially, these are the files/folders you do not wish to backup using rsync.
/disk3/
/bin/
/boot/
/dev/
/lib/
/lost+found/
/mnt/
/opt/
/proc/
/sbin/
/sys/
/tmp/
/usr/
/var/log/
/var/spool/
/var/lib/php4/
/var/lib/mysql/

No comments:

Post a Comment