Tuesday, January 18, 2011

Automated Backup Script Using tar and FTP


I am posting here a script I made to automate backup via tar ang FTP. I hope that you may find this helpful. In the next post, I will try to explain each entries on the script. Thanks, take care and God Bless!!!

#!/bin/bash
# A Simple (Poor Man) disk based backup script
# Will backup all user home directories in seperate archives
# as a single /home backup file can be quite large and unwieldly.
# Does not need to be modified if users are added/deleted

# Step - 1 Create Timestamp and set up variables and functions
# BUDTSTAMP = Backup Date/Time Stamp
BUTDSTAMP=$(date +%Y%m%d)

# variable holding directories containing files to backup eg: BACKUPTHESE="/home /root /etc"
BACKUPTHESE="/var/www/html/adldap"

# Backup base place the back up base

B_BASE="/trynga"

cd $B_BASE

mkdir $B_BASE/vol
mkdir $B_BASE/vol/backup
mkdir $B_BASE/vol/backup/daily
mkdir $B_BASE/vol/backup/yesterday
mkdir $B_BASE/vol/backup/database

#directory containing today's backup
BKUPDIR="$B_BASE/vol/backup/daily"
#directory containing yesterday's backup
YDBKUPDIR="$B_BASE/vol/backup/yesterday"
#directory containing the day before yesterday's backup
DBBKUPDIR="$B_BASE/vol/backup/daybefore"
# DATABASE BACKUP DIR
DATABASEBACKUP="$B_BASE/vol/backup/database"
NCFTP="/usr/local/bin"

# device to monitor upon backup
DEVICE="/"

# recipient to send the backup report
#EMAILADD="backupadmin"
EMAILADD="rowell@mindragon.com"

# FTP SetUP
FTPS="192.168.1.118"
FTPU="rowell"
FTPP="mdi2005!"
NOW=$(date +"%d-%m-%Y")
BACKUP="$B_BASE/vol/backup/daily"
FTPD="serbackupfiles"

### MySQL Setup ###
MUSER="root"
MPASS="mdi2009!"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

# log files
LOG="/var/log/mail.txt"
ERRORLOG="/var/log/ERROR.txt"

# tag for backup file name
TAG="OpenSERbackup"

# function that quits and logs on error
exiterror()
{
# use this function by supplying $LINENO as first arg
echo "Fatal error caused by line ${1} of ${0}" >> $ERRORLOG
mail $EMAILADD -s "Backup Job ERROR" -v < $ERRORLOG
mail $EMAILADD -s "Backup Job Report" -v < $LOG
exit 1
}

# Step - 2 Start Email Message To Be Sent

# Remove mail message from previous backup
# I do this at the beginning of the script instead of the end
# in case the mail does not send for whatever reason or
# I need to debug it

rm $LOG > /dev/null 2>&1
echo "System Backup $BUTDSTAMP" >> $LOG

# The email sends a user friendly note showing the start and
# end time/dates. This is important so you can compare logs
# and see if a backup ran ok.

echo "Backup Began $(date)" >> $LOG

# Step - 3 Rotate Backups
# Simple three backupset rotation, keeps only last three
# Use directory /vol/backup as an example, make sure you change this path
# to fit your local settings. Have three directories in /vol/backup named daily,
# yesterday and daybefore.
# Make sure /vol/backup is on a different disk (preferably a different machine)
# than the files you are backing up

#looks specifically for backup files in case other files are kept in these directories
#for bkfile in $DBBKUPDIR/*; do
#echo $bkfile | grep $TAG >/dev/null &&
#(rm $bkfile || exiterror $LINENO )
#done
for bkfile in $YDBKUPDIR/*; do
echo $bkfile | grep $TAG >/dev/null &&
#(mv $bkfile $DBBKUPDIR/ || exiterror $LINENO )
(rm $bkfile || exiterror $LINENO )
done
for bkfile in $BKUPDIR/*; do
echo $bkfile | grep $TAG >/dev/null &&
(mv $bkfile $YDBKUPDIR/ || exiterror $LINENO )
done

# Step - 4 Archive Home Directories
# Creates a seperate tar file for each directory in the directories in $BACKUPTHESE


# change to dir to backup. Check in case cd failed for some reason.
cd $BACKUPTHIS || exiterror $LINENO
#backup all files listed in BACKUPTHESE
#for DIRTOBACKUP in $BACKUPTHESE; do
#for FOLDERNAME in $DIRTOBACKUP/*
#do
# Archives are created in the format someuser-$TAG-datetime.tar.gz
# basename is used here so as not to include absolute paths
# -p preserves permissions
#echo -e "-------------------\n>>>taring ${FOLDERNAME}\n\n" >> $LOG
#tar -czvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} >> $LOG || exiterror $LINENO
#done
#done

for FOLDERNAME in $BACKUPTHESE; do
echo -e "-------------------\n>>>taring ${FOLDERNAME}\n\n" >> $LOG
#tar -czvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} >> $LOG || exiterror $LINENO
tar -czvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} > /dev/null 2>&1 || exiterror $LINENO
done

# For MySQL backup database
echo -e "------------------\n>>>>back up the database\n\n" >> $LOG
### Start MySQL Backup ###
# Get all databases name

#mkdir $BACKUP/$NOW
mkdir $DATABASEBACKUP/$NOW

DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
echo "$DBS \n\n " >> $LOG
for db in $DBS
do
#FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
FILE=$DATABASEBACKUP/$NOW/mysql-$db.${BUTDSTAMP}.gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

# Step - 5 FTP backup start

echo -e "------------------\n>>>>Transfer backups to FTP server\n\n" >> $LOG

# not yet done Do an an FTP back up
# set the settings for FTP

### Dump backup using FTP ###
#Start FTP backup using ncftp
RETVAL=`$NCFTP/ncftp -u"$FTPU" -p"$FTPP" $FTPS <<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
lcd $DATABASEBACKUP/$NOW
mput *
quit
EOF`

echo -e "$RETVAL \n\n" >> $LOG

##### Let us use a a different version
#mkdir $FTPD
#mkdir $FTPD/$NOW
#ncftpput -m -z -u "$FTPU" -p "$FTPP" $FTPS


# Step - 6 Finish Email Report and Send

echo "Backup ended $(date)" >> $LOG

# df -h includes a human readable disk usage report of the media that /vol/backup
# is mounted on. Good to now if your backup disk is running out of space.
# Of course /dev/hdb1 is the device I use, modify it for your local settings

df -h $DEVICE >> $LOG
mail $EMAILADD -s "Backup Job Report" -v < $LOG

# Step - 7 All Done

No comments: