Hi Guys,
I'm relatively new to the Linux scene, got a dedicated server to host some sites and have now setup RHEL 3 at my home/office for testing websites and for doing backups of my other computers.
I have just spent the past few day writing the following backup shell script (see end of post) to backup all my pc's running winblows. The script backups all the computers on a samba network to my linuxserver and then another script rsync's a copy to an external Hard Drive that will be stored offsite and rotated weekly.
I have a number of questions so I figured this would be a great place to ask..... so here goes.....
1. How would I create a separate partition on my hard drive called "backup" where I can store all my backups, without deleting any existing data? There is about 180MB free at the mo.
2. I would like to allocate about 120MB for this partition and will probably need to defrag it every 6 months or so, how do you do this in linux?
3. Rather than using cygwin, as I did want to have to install on all my networked computers, I am mounting my networked computers from my linuxserver via samba and then rsyncing the files from there. Is this a bad idea?
4. When using rsync I have tested with and without using compression (z). The compression seems to make the whole process take a lot longer, is there any benefit of using this on a home/small office network?
Thank you so much for reading this post and if you have any advice or suggestions it would be much appreciated,
Thanks
PS: If see a way to improve the code below then please let me know.
the config file
workstation_backup.config file
Code:
#!/bin/bash
# config file for workstation backup
# this file will be imported by tar_rsync_backup.sh
####### MAIN CONFIG START ##################
#create variable for computername
COMPUTERNAME="workstation"
#username to access remote computer
USERNAME="Administrator"
#password to access remote computer
PASSWORD="*********"
#most of the time you will want to use the $COMPUTERNAME but if you want it in a different directory name it here
BACKUPNAME=$COMPUTERNAME
#set up the directories
ARCHIVEDIR="/backup/$BACKUPNAME/archives"
LOGSDIR="/backup/$BACKUPNAME/logs"
#destination directory where the backup will be saved
DESTINATIONDIR="/backup/$BACKUPNAME/current_backup"
#mount directory
MOUNTDIR=/mnt/computers
#mountpoint directory reference to computer on samba network
MOUNTPOINT=$MOUNTDIR/$COMPUTERNAME
#base source directory on remote computer
BASESOURCEDIR="c_drive"
#create an array with all the directories to be backed up
#directory to be backed up on the computer use " "quotes for the directory name as this
#allows for spaces in the directory names
SOURCEDIRS[0]="$MOUNTPOINT/Documents and Settings/master/My Documents"
SOURCEDIRS[1]="$MOUNTPOINT/Documents and Settings/master/Favorites"
SOURCEDIRS[2]="$MOUNTPOINT/Documents and Settings/master/Local Settings/Application Data/Identities/{2C7A8CD0-78FA-427F-BF86-AE333A20DC52}/Microsoft/Outlook Express"
SOURCEDIRS[3]="$MOUNTPOINT/Documents and Settings/master/Local Settings/Application Data/Microsoft/Outlook"
SOURCEDIRS[4]="$MOUNTPOINT/code"
SOURCEDIRS[5]="$MOUNTPOINT/Nokia"
SOURCEDIRS[6]="$MOUNTPOINT/tutorials & notes"
#SOURCEDIRS[7]="$MOUNTPOINT/Program Files/Macromedia/Flash MX/Configuration/Include
#SOURCEDIRS[8]="$MOUNTPOINT/Program Files/Macromedia/Flash MX 2004/en/First Run/Include"
#maximum archive number to keep before deleting old ones
MAX_ARCHIVES=3
####### MAIN CONFIG END #######################################
main script that tars old version and rsync new info
tar_rsync_backup.sh
Code:
#!/bin/bash
# backup script
# This script works as follows;
# 1. Import the main config variables from eg. workstation_backup.config file.
# 1. Archives the current_backup folder as a tar.gz and stores it in /archives/ directory eg. /archives/2005-01-12.tar.gz
# 2. Creates a mount point for the computer using samba and places it in /mnt/computers/
# 3. Run rsync on source directories array and transfer the lastest changes to current_backup directory
#import all the main config variables for doing the backup
#the backup or computer name is passed as a the 1st command line argument eg. tar_rsync_back.sh workstation
#this would then import eg. "workstation_backup.config"
source "/backup/sh/$1_backup.config"
echo "Backing up: $BACKUPNAME"
#command line variable to enable testing and debugging without transferring all the files each time
#rsync options from bash varible
if [ "$2" = "rsync_test" ]; then
echo "rsync test run"
RSYNC_OPTIONS="n"
else
echo "rsync real"
RSYNC_OPTIONS=""
fi
#tar options from bash variable
if [ "$3" = "tar_false" ]; then
echo "tar is disabled"
TAR="false"
else
echo "tar is enabled"
TAR="true"
fi
echo "desination directory: $DESTINATIONDIR"
#check if the $BACKUPNAME is a directory in /backup/ if not create one and a logs, and archives folder
if [ ! -d "/backup/$BACKUPNAME" ]; then
mkdir "/backup/$BACKUPNAME"
mkdir "$LOGSDIR"
mkdir "$ARCHIVEDIR"
fi
ARCHIVESOURCEDIR=$DESTINATIONDIR
#log file name will use the current date eg. 2005-01-05.log
LOGFILENAME=`date +"%F"`.log
echo "$LOGFILENAME"
echo "checking for last backup ....."
echo "archive source directory: $ARCHIVESOURCEDIR"
echo "archive destination directory: $ARCHIVEDIR"
#check if the destination directory exists otherwise make one
if [ ! -d $DESTINATIONDIR ]; then
mkdir $DESTINATIONDIR
echo "no backup found"
echo "destination directory doesn't exist so created: $DESTINATIONDIR"
else
#the destination directory exists so now we must archive it
echo "last backup found"
echo "archiving last backup ......"
#get the list of all archives from the archives directory and reserve sort via time
#eg make the oldest appear first in the array[0]
ARCHIVES_ARRAY=(`ls -rt $ARCHIVEDIR`)
#do the same for the log files
LOGFILE_ARRAY=(`ls -rt $LOGSDIR`)
#total number of archives in the archive directory
TOTAL_ARCHIVES=${#ARCHIVES_ARRAY[@]}
echo "TOTAL_ARCHIVES: $TOTAL_ARCHIVES"
#check if the total achives are equal to the max archive number
#if so delete the oldest archive so we can have room for a new one
if [ $TOTAL_ARCHIVES -eq $MAX_ARCHIVES ]; then
echo "deleting oldest achive: ${ARCHIVES_ARRAY[0]}"
rm -f $ARCHIVEDIR/${ARCHIVES_ARRAY[0]}
#also delete its log file
rm -f $LOGSDIR/${LOGFILES_ARRAY[0]}
fi
#get the date of the last backup from the last_backup.log file eg. 2005-01-04
LASTBACKUPDATE=`cat $LOGSDIR/last_backup.log`
echo "Logs Directory: $LOGSDIR"
#archive name using the last backup date eg. 2005-01-04.tar.gz
ARCHIVENAME=$LASTBACKUPDATE.tar.gz
#check if tar has been disabled from the command line
if [ $TAR = "true" ]; then
#now create a tar archive with the date and time and compress with gzip
tar -zcvf $ARCHIVEDIR/$ARCHIVENAME $ARCHIVESOURCEDIR
echo "created: $ARCHIVEDIR/$ARCHIVENAME"
#IMPORTANT
#pehaps in future remove gzip compression from the tar achive command (z) and do it here instead
#this way by using gzip on its own we can make it work with rsync friendly
#add this in later. Though it has been said that rsyncing tar.gz files is not reliable.
fi
fi
#set up the samba mount
echo "mountpoint: $MOUNTPOINT"
echo "checking mount point directory..."
#check if the computer's mount point directory exists in /mnt/computers
if [ ! -d $MOUNTPOINT ]; then
#make the mount point directory in /mnt/computers
mkdir $MOUNTPOINT
echo "creating mountpoint"
else
echo "mount point directory exists"
fi
#mount the computer from samba
echo "creating mountpoint"
mount -t smbfs -o username="$USERNAME",password="$PASSWORD" //$COMPUTERNAME/$BASESOURCEDIR $MOUNTPOINT
#loop through all the source directorys and rsych them
#put "" quotes around the directory names variables so they won't spilt
for i in "${SOURCEDIRS[@]}"
do
echo "backing up directory: $i"
#get the name of the current source dir without all the other stuff eg.Outlook Express
SOURCEDIRNAME="${i##*/}"
echo "destination name: $DESTINATIONDIR/$SOURCEDIRNAME"
#rsync the current source directory to the destination directory
rsync -"$RSYNC_OPTIONS"va --delete "$i" "$DESTINATIONDIR"
done
#un mount the mount point
umount $MOUNTPOINT
#write the today's date to last_backup.log so we know the last date a backup was made
#we will use this date for archiving when this script is run again
date +"%F" > $LOGSDIR/last_backup.log
exit