WBITT

We Bring In Tomorrows Technology

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Backup script for your Web and MySQL db servers!

E-mail Print PDF
User Rating: / 7
PoorBest 

Assalam-u-alaikum,

This is another of the much asked material. The backup script. Below are two scripts. One for backing up the web site content and the other to backup your MySQL Databases.

Both scripts will push the backed up content to a FTP server for added availability. The scripts are pretty straight forward to use. You just need to adjust a few variables and rest is taken care of.

For these scripts to work, you will need to setup the following layout:

/backups

/backups/localdbs  # (this will contain the daily Database backups)

/backups/localsites # (this will contain the daily website content backups)

 

Each day, a directory of the format YYYYMMDD will be created in localdbs and localsites directories. Which, will in-turn contain the actual DB dump files (compressed) or the website content (tar.bz2) .

Update: The scripts are updated to automatically delete backups, older than 14 days (or whatever you configure), from both local systems and remote FTP location.

 

 

First the backup script to backup websites contents:-

sitesbackup.sh file on www.yourdomain.com:-

#!/bin/sh
###########################################################################################
# Filename:     sitesbackup.sh
# Purpose:      Backup web content hosted on this server.
# Author:       Muhammad Kamran Azeem (kamran _at_ wbitt _dot_ com)
# Created:      20080628
# Modified:     20080720, 20080628
# Note:         For FTP, make sure that the file ~/.netrc exists in the home directory
#               of the user running this script. The permissions must be 0600.
#               The syntax of .netrc file is:
#               machine ftpserver.somedomain.com
#               login ftpusername
#               password somepassword
#
# Note2:        This script must have permissions not more than 0750 . Because
#               this script contains username and password of your mysql admin user.
#
# Note 3:       You will need to frequently delete the old backups both from local disk,
#               as well as from the FTP server.
#
# Note 4:       Setup a cronjob as:-
#               0 2 * * * /backups/sitesbackup.sh
#
############################################################################################

######### User configuration. Start. #############################################

FTPSERVER=ftpserver.somedomain.com

# Backup Dest directory, change this if you have some other location
BACKUPDIR="/backups/localsites"
WEBCONTENTDIR="/var/www/vhosts"

# FTP client program to be used. LFTP is the best client. You will come to  know yourself!:
FTP=lftp


# This is the time period for which the backups will be retained.
# Backups before this date will be deleted! Make selection carefully.
# The value is number of days before $TODAY.
RETENTIONPERIOD=14

# Store list of sites
SITESLIST=""

# DO NOT BACKUP these sites / directories
# (these will be names of actual directory and file names you wish to skip)
IGNORESITES="test.com"


########## User configuration. End. ###############################################

# Paths to various utilities / programs ...
TODAY=$(date +%Y%m%d)

TAR="$(which tar)"
RSYNC="$(which rsync)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
BZIP2="$(which bzip2)"

# Main directory where today's backups will be stored
TODAYBACKUPDIR="$TODAY"
REMOVELASTBACKUPDATE=$(date --date="$RETENTIONPERIOD days ago" +%Y%m%d)


# Get hostname
HOST="$(hostname)"

echo "Here is the configuration, you specified:-"
echo "FTPSERVER=$FTPSERVER"

echo "WEBCONTENTDIR=$WEBCONTENTDIR"
echo "BACKUPDIR=$BACKUPDIR"
echo "TODAYBACKUPDIR=$TODAYBACKUPDIR"
echo "RETENTIONPERIOD=$RETENTIONPERIOD"
echo "REMOVELASTBACKUPDATE=$REMOVELASTBACKUPDATE"

echo "System Hostname was found automatically as: $HOST"

echo "Starting WebContent backup...."
[ ! -d $BACKUPDIR/$TODAYBACKUPDIR ] && mkdir -p $BACKUPDIR/$TODAYBACKUPDIR || :

# Get all websites list first
SITESLIST="$(ls $WEBCONTENTDIR)"

for SITE in $SITESLIST
do
skipsite=-1
if [ "$IGNORESITES" != "" ];
then
for i in $IGNORESITES
do
[ "$SITE" == "$i" ] && skipsite=1 || :
done
fi

if [ "$skipsite" == "-1" ] ; then
FILE="$BACKUPDIR/$TODAYBACKUPDIR/$SITE.$HOST.$TODAY.tar.bz2"
echo "Backing up site $SITE..... Creating $FILE..."
$TAR   cjf   $FILE    $WEBCONTENTDIR/$SITE
fi
done

echo "WebContent Backtup complete. Now starting FTP upload..."

cd $BACKUPDIR/$TODAYBACKUPDIR/

# Create a directory on ftp server with today's date. And upload the file to it.

$FTP $FTPSERVER << EOF
mkdir $TODAY
cd $TODAY
mput *.tar.bz2
ls -aL
EOF

cd -

echo "FTP Upload complete."

echo "Time to remove old backups from local system  and remote FTP server."
echo "First remove the directory $BACKUPDIR/$REMOVELASTBACKUPDATE from local system..."
echo "NOTE: Make sure you don't have any typing mistake in the next lines below,"
echo "as IT HAS POTENTIAL TO WIPE OFF YOUR SERVER HARD DISK CLEAN !!!"
echo "YOU HAVE BEEN WARNED."
echo "Un-comment the rm -fr lines below when you are SURE.".


cd $BACKUPDIR
# rm -fr $REMOVELASTBACKUPDATE

echo "Deleting $REMOVELASTBACKUPDATE from $FTPSERVER..."
$FTP $FTPSERVER << EOF
# rm -fr  $REMOVELASTBACKUPDATE
ls -aL
EOF

cd -

exit 0

Now the backup script for MySQL database server:-

dbbackup.sh file on www.yourdomain.com:-

#!/bin/sh
###########################################################################################
# Filename:    dbbackup.sh
# Purpose:    Backup MySQL databases hosted on this server.
# Author:       Muhammad Kamran Azeem (kamran _at_ wbitt _at_ com)
# Created:      20080424
# Modified:     20080720, 20080628
# Note:         For FTP, make sure that the file ~/.netrc exists in the home directory
#               of the user running this script. The permissions must be 0600.
#               The syntax of .netrc file is:
#               machine ftpserver.somedomain.com
#               login ftpusername
#               password somepassword
#
# Note2:        This script must have permissions not more than 0750 . Because
#               this script contains username and password of your mysql admin user.
#
# Note 3:       You will need to frequently delete the old backups both from local disk,
#               as well as from the FTP server.
#
# Note 4:       Setup a cronjob as:-
#               0 3 * * * /backups/dbbackup.sh
#
############################################################################################

########## User configuration. Start. ######################################################

FTPSERVER=ftpserver.somedomain.com

MYSQLUSER="root"     # MySQL root USERNAME
MYSQLPASS="mysqlrootpasswd"       # MySQL root PASSWORD
MYSQLHOST="localhost"          # MySQL Hostname

# Backup Dest directory, change this if you have someother location
BACKUPDIR="/backups/localdbs"

# This is the time period for which the backups will be retained.
# Backups before this date will be deleted! Make selection carefully.
# The value is number of days before $TODAY.
RETENTIONPERIOD=14


# Store list of databases
DBLIST=""

# DO NOT BACKUP these databases
IGNOREDBS="test"

# FTP client program to be used:
FTP=lftp


########## User configuration. End. ###############################################


TODAY=$(date +%Y%m%d)

REMOVELASTBACKUPDATE=$(date --date="$RETENTIONPERIOD days ago" +%Y%m%d)

# File to store current backup file
FILE=""

# Paths to various utlities / programs ...
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
BZIP2="$(which bzip2)"

# Main directory where today's backups will be stored
TODAYBACKUPDIR="$TODAY"

# Get hostname
HOST="$(hostname)"

echo "Here is the configuration, you specified:-"
echo "FTPSERVER=$FTPSERVER"
echo "MYSQLUSER=$MYSQLUSER"
echo "MYSQLPASS=[Edit this script file to see the password]!"
echo "MYSQLHOST=$MYSQLHOST"
echo "BACKUPDIR=$BACKUPDIR"
echo "TODAYBACKUPDIR=$TODAYBACKUPDIR"

echo "RETENTIONPERIOD=$RETENTIONPERIOD"
echo "REMOVELASTBACKUPDATE=$REMOVELASTBACKUPDATE"
echo "System Hostname was found automatically as: $HOST"

echo "Starting DB backup/dump...."
[ ! -d $BACKUPDIR/$TODAYBACKUPDIR ] && mkdir -p $BACKUPDIR/$TODAYBACKUPDIR || :

# Get all database list first
DBLIST="$($MYSQL -u $MYSQLUSER -h $MYSQLHOST -p$MYSQLPASS -Bse 'show databases')"

for DB in $DBLIST
do
skipdb=-1
if [ "$IGNOREDBS" != "" ];
then
for i in $IGNOREDBS
do
[ "$DB" == "$i" ] && skipdb=1 || :
done
fi

if [ "$skipdb" == "-1" ] ; then
FILE="$BACKUPDIR/$TODAYBACKUPDIR/$DB.$HOST.$TODAY.dump.bz2"
# do all in one job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
echo "Processing Database $DB..... Creating $FILE..."
$MYSQLDUMP -u $MYSQLUSER -h $MYSQLHOST -p$MYSQLPASS $DB | $BZIP2 -9 > $FILE
fi
done

echo "Database Dump complete. Now starting FTP upload..."

cd $BACKUPDIR/$TODAYBACKUPDIR/

# Create a directory on ftp server with today's date. And upload the backup files to it.

$FTP $FTPSERVER << EOF
mkdir $TODAY
cd $TODAY
mput *.dump.bz2
ls -aL
EOF

cd -

echo "FTP Upload complete."

echo "Time to remove old backups from local system  and remote FTP server."
echo "First remove the directory $BACKUPDIR/$REMOVELASTBACKUPDATE from local system..."
echo "NOTE: Make sure you don't have any typing mistake in the next lines below,"
echo "as IT HAS POTENTIAL TO WIPE OFF YOUR SERVER HARD DISK CLEAN !!!"
echo "YOU HAVE BEEN WARNED."
echo "Un-comment the rm -fr lines below when you are SURE.".


cd $BACKUPDIR
# rm -fr $REMOVELASTBACKUPDATE

echo "Deleting $REMOVELASTBACKUPDATE from $FTPSERVER..."
$FTP $FTPSERVER << EOF
# rm -fr  $REMOVELASTBACKUPDATE
ls -aL
EOF

cd -

exit 0

##########################################################

 

Alhumdulillah. DONE.

You are here How To / Tutorials Backup script for your Web and MySQL db servers!