An example rotational backup with remote backup options script.

#!/bin/sh
# Rotational Backup with remote backup
# Version 1.1
# Fire Eater <LinuxRockz@gmail.com>
# Feburary 4, 2006
# Released under the GPL License- http://www.fsf.org/licensing/licenses/gpl.txt
##############################################################################
#
SERVER_HOSTNAME="`hostname`"

BACKUP_UID="yourbackupuser"
BACKUP_GID="yourbackupusersgroup"
#
ROOT_BACKUP_DIR="/your/backup/directory"
CONF_DIR="$ROOT_BACKUP_DIR/etc"
#Example: HTTPD, BIND, MYSQL
BACKUP_TYPE="HTTPD"
#
####################
#  Remote Backup   #
####################
#
## Use Rsync
# You will need to properly set up a key bassed ssh login.
USE_RSYNC="n"
RSYNC_HOST="yourserver"
SSH_RSYNC_USER="yoursshuser"
SSH_RSYNC_DEST_DIR="/your/remote/folder/$SERVER_HOSTNAME"

## SCP Information and Destination
# Your will need to properly set up a key bassed ssh login.
USE_SCP="n"
SCP_HOST="yourserver"
SCP_USER="sshuser"
SCP_REMOTEDIR="/your/remote/folder/$SERVER_HOSTNAME"

## FTP
USE_FTP="n"
#  Syntax for FTP_SERVER:  ftp://user:pass@ftp.server.com/folder  (WITHOUT Trailing /)
FTP_SERVER="ftp://"

## E-Mail Information and Destination
EMAIL_INFO="n"
BACKUP_EMAIL="youremailaddress"

#################################
#  Config Directory/File Check  #
#################################

if [ ! -d $CONF_DIR ] ; then
        mkdir -p $CONF_DIR
fi

if [ ! -f $CONF_DIR/homedirs ] ; then
        echo "No Home Directories List Found"
        echo "Please create $CONF_DIR/homedirs"
        exit
fi

#####################
#  Rotational Setup #
#####################
#
BACKUP_DIR=$ROOT_BACKUP_DIR/$BACKUP_TYPE
BACKUP_DEST="$BACKUP_DIR/backup.0"
BACKUP="$BACKUP_DIR/backup.0"
BACKUP1="$BACKUP_DIR/backup.1"
BACKUP2="$BACKUP_DIR/backup.2"
BACKUP3="$BACKUP_DIR/backup.3"
BACKUP4="$BACKUP_DIR/backup.4"
BACKUP5="$BACKUP_DIR/backup.5"
BACKUP6="$BACKUP_DIR/backup.6"

if [ ! -d $BACKUP_DEST ] ; then
        mkdir -p $BACKUP_DEST
fi

if [ ! -d $BACKUP ] ; then
        mkdir $BACKUP_DIR/backup.0
fi

if [ ! -d $BACKUP1 ] ; then
        mkdir $BACKUP_DIR/backup.1
fi
if [ ! -d $BACKUP2 ] ; then
        mkdir $BACKUP_DIR/backup.2
fi
if [ ! -d $BACKUP3 ] ; then
        mkdir $BACKUP_DIR/backup.3
fi
if [ ! -d $BACKUP4 ] ; then
        mkdir $BACKUP_DIR/backup.4
fi
if [ ! -d $BACKUP5 ] ; then
        mkdir $BACKUP_DIR/backup.5
fi
if [ ! -d $BACKUP6 ] ; then
        mkdir $BACKUP_DIR/backup.6
fi

/bin/rm -rf $BACKUP_DIR/backup.6
mv $BACKUP_DIR/backup.5 $BACKUP_DIR/backup.6
mv $BACKUP_DIR/backup.4 $BACKUP_DIR/backup.5
mv $BACKUP_DIR/backup.3 $BACKUP_DIR/backup.4
mv $BACKUP_DIR/backup.2 $BACKUP_DIR/backup.3
mv $BACKUP_DIR/backup.1 $BACKUP_DIR/backup.2
mv $BACKUP_DIR/backup.0 $BACKUP_DIR/backup.1

if [ ! -d $BACKUP ] ; then
        mkdir $BACKUP_DIR/backup.0
fi
#
##################################
#     START BACKUP               #
##################################
START_TIME=`date +%a-%D-@-%X-%Z`
##################################
#
HTTP_CONF_DIR="/etc/httpd"
USERS_HOME_DIRECTORY="/home"
USERS_LIST="$CONF_DIR/homedirs"
DATENAME=`date +%Y%m%d`
START_TIME=`date +%a-%D-@-%X-%Z`
#
echo "Backing up HTTPD Conf Directories"
tar cfj $BACKUP_DEST/httpd-$DATENAME.tar.bz2 $HTTP_CONF_DIR  >> /dev/null
#
echo "Backing up Home Directories"
#
for USER_DIR in `cat $USERS_LIST`;
do
tar cfj $BACKUP_DEST/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2 $USERS_HOME_DIRECTORY/$USER_DIR  >> /dev/null
done
#
#Set Permissions
chown $BACKUP_UID:$BACKUP_GID $BACKUP_DIR
#
##################################
END_TIME=`date +%a-%D-@-%X-%Z`
##################################
#       END BACKUP               #
##################################
#
##########################
# Remote Backup Options  #
##########################
clear
#
if [ $USE_RSYNC = y ] ; then
echo "Preforming Remote Backup via Rsync"
echo ""
su -l $BACKUP_UID -c "rsync -az --stats --partial -e ssh $BACKUP_DIR $SSH_RSYNC_USER@$RSYNC_HOST:$SSH_RSYNC_DEST_DIR"  2>&1
echo "$BACKUP_TYPE backup has been Rsynced to $RSYNC_HOST"
echo ""
else
echo "Rsync not turned on"
fi
#
if [ $USE_SCP = y ] ; then
echo "Preforming Remote Backup via SCP"
echo ""
scp $BACKUP_DEST/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2 $SCP_USER@$SCP_HOST:$SCP_REMOTEDIR
echo ""
echo "$BACKUP_TYPE Backup has been uploaded via scp to $SCP_HOST"
else
echo "SCP not turned on"
fi
#
if [ $USE_FTP = y ] ; then
echo "Preforming Remote Backup vis FTP"
echo ""
curl -T $BACKUP_DEST/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2 $FTP_SERVER/$SERVER_HOSTNAME-$USER_DIR--$DATENAME.tar.bz2
echo "$BACKUP_TYPE  backup has been uploaded via FTP to $FTP_SERVER"
else
echo ""
echo "FTP not turned on"
echo ""
fi
#
####################
#  Backup Results  #
####################
#
echo "The current $BACKUP_TYPE backup for $SERVER_HOSTNAME has completed and is located in: $BACKUP_DEST"
echo "" 
echo "Total disk space used for the entire $BACKUP_TYPE backup."
echo "Size - Location"
echo "`du -hs "$BACKUP_DIR"`"
echo "" 
echo "Total disk space used for the current $BACKUP_TYPE backup."
echo "Size - Location"
echo "`du -hs "$BACKUP_DEST"`"
#
###################
#   Email Results #
###################
#
if [ $EMAIL_INFO = y ] ; then
echo "$SERVER_HOSTNAME $BACKUP_TYPE Backup Summary" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Backup Start Time: $START_TIME" >> /tmp/emailmsg.txt
echo "Backup End Time: $END_TIME" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Rsync="$USE_RSYNC >> /tmp/emailmsg.txt
echo "SCP="$USE_SCP >> /tmp/emailmsg.txt
echo "FTP="$USE_FTP >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Total disk space used for the entire $BACKUP_TYPE backup." >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Size - Location" >> /tmp/emailmsg.txt
echo "`du -hs "$BACKUP_DIR"`" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Total disk space used for the current $BACKUP_TYPE backup." >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
echo "Size - Location" >> /tmp/emailmsg.txt
echo "`du -hs "$BACKUP_DEST"`" >> /tmp/emailmsg.txt
echo "" >> /tmp/emailmsg.txt
#
cat /tmp/emailmsg.txt | mail -s"$BACKUP_TYPE Backup Executed for $SERVER_HOSTNAME at $END_TIME" $BACKUP_EMAIL
rm -f /tmp/emailmsg.txt
else
echo "E-Mail not turned on"
fi
exit

HowTos/Rotational backup with remote backup options (last edited 2007-05-17 12:44:37 by DanielDeKok)