23 February 2011

File Backup Script

I have a server runing Linux from Scratch (LFS) that I need to be able to back up and have a reserve system ready to take it's place with minium downtime. I had two identical boxes one was the server the other was not in use. I decided to put a single pull-out HDD tray in each one. I obtained two identical Harddrives and used Acronis True Image or Norton Ghost to re-images the drives to be identical and carry my LFS server. I installed one disk INSIDE the server and the other went in to the pullout tray.

I then used cron to run my backup script every night at 2am. The script mounts the pull-out drive analyzes the files and directories of my two hard drives and copies new or updated files from specific directories on the primary to the pull-out drive. and Finally unmounts the pull-out drive.

Now if my Primary HDD or server die all I have to do is grab the pull-out drive slam it the the backup server and boot it up.

I assume that a USB drive would work this way for backups, but you'll need to think about how to get the recovery system to boot from USB.

The Script after the jump

#!/bin/bash

# Put in /usr/local/scripts or wherever you like.
#
# Crontab entry
# 30 3 * * * /usr/local/scripts/MasterRaidSyncScript 2>/dev/null
# This will run every night at 3:30
# Put this in roots crontab otherwise you cannot mount drives
# hint (run as root): crontab -e


SRCMOUNT="/mnt/source"
DSTMOUNT="/mnt/dest"

function quit {
cd /
/usr/bin/smbumount /mnt/source
/usr/bin/smbumount /mnt/dest
/bin/umount /mnt/source
/bin/umount /mnt/dest
exit 1
}

function sambamount {
# /usr/bin/smbmount [Server Shsare] [Mount Point] -o username=[Username],password=[Password]
/usr/bin/smbmount $1 $2 -o username=$3,password=$4 &> /dev/null
if [ "$?" -ne "0" ]; then
echo Sambamount failed to mount $1
quit
fi
}


function rosambamount {
/usr/bin/smbmount $1 $2 -o ro,username=$3,password=$4 &> /dev/null
if [ "$?" -ne "0" ]; then
echo Sambamount failed to mount $1
quit
fi
}



function sambaumount {
/usr/bin/smbumount $1
if [ "$?" -ne "0" ]; then
echo Sambaumount failed to mount $1
quit
fi
}


function localmount {
# /bin/mount [Server Share]
/bin/mount $1 $2
if [ "$?" -ne "0" ]; then
echo Mount failed to mount $1
quit
fi
}

function localunmount {
# /bin/mount [Server Share]
/bin/umount $1
if [ "$?" -ne "0" ]; then
echo Mount failed to unmount $1
quit
fi
}

function SyncSambaDirectories {
SRCSHARE=$1
DSTSHARE=$2
USER=$3
PASS=$4
echo Syncing share $SRCSHARE to $DSTSHARE
rosambamount $SRCSHARE $SRCMOUNT $USER $PASS
sambamount $DSTSHARE $DSTMOUNT $USER $PASS
cd $DSTMOUNT
if [ "$?" -ne "0" ]; then
echo Error changing directory to $DSTSHARE
quit
fi
/usr/bin/rsync -va --delete $SRCMOUNT/ .
if [ "$?" -ne "0" ]; then
echo Rsync returned an error copying $SRCSHARE to $DSTSHARE
quit
fi
cd /
sambaumount $SRCMOUNT
sambaumount $DSTMOUNT
}



function SyncLocalDirectories {
SRCSHARE=$1
DSTSHARE=$2
echo Syncing folder $SRCSHARE to $DSTSHARE
cd $DSTSHARE
if [ "$?" -ne "0" ]; then
echo Error changing directory to $DSTSHARE
quit
fi
/usr/bin/rsync -va --delete $SRCSHARE/ .
if [ "$?" -ne "0" ]; then
echo Rsync returned an error copying $SRCSHARE to $DSTSHARE
quit
fi
cd /
}


localmount /dev/hdb1 /mnt/dest
SyncLocalDirectories /home /mnt/dest/home
SyncLocalDirectories /etc /mnt/dest/etc
SyncLocalDirectories /var/www /mnt/dest/var/www
SyncLocalDirectories /var/virtualhosts /mnt/dest/var/virtualhosts
#SyncLocalDirectories /var/lib/ldap /mnt/dest/var/lib/ldap
SyncLocalDirectories /var/lib/mysql /mnt/dest/var/lib/mysql
SyncLocalDirectories /root /mnt/dest/root
SyncLocalDirectories /usr/local /mnt/dest/usr/local
localunmount /mnt/dest

No comments:

Post a Comment