Backing up the whole site

Member for

1 year 5 months
Submitted by AlReaud on Wed, 01/18/2012 - 20:52

Revised: 2016-09-03, Al Reaud, Happy Cat Technologies

The following script integrates backing up the database with backing up the site code. Please note that descriptions place-hold for actual values in the script. Those actual values must be edited in, depending on your site configuration, for the script to work.

This script is adapted from the updated fullsitebackup.sh script created by Bristolguy on Drupal.org. This script is currently operational on Ubuntu 16.04, and has not been tested on other versions of Linux.

The shell script is available here for your convenience, demo.full_site_backup.sh.txt.

full_site_backup.sh
#! /bin/bash
#
# Al Reaud, Happy Cat Technologies
# Adapted from https://www.drupal.org/node/129202
# fullsitebackup.sh V1.2.20160903
# Full backup of website files and database content. Insure that this script IS NOT visible outside of the hosting account as it contains critical security information. A number of variables defining file location and database connection information must be set before this script will run. Files are tar'ed from the root directory of the website. All files are saved. The MySQL database tables are dumped without a database name and and with the option to drop and recreate the tables.
#
echo ""

# Configuration
_dbname='database name, may be the database user name also' # (e.g.: _dbname=drupaldb)
_dbuser='database user name, may be the database name also' # (e.g.: _dbuser=drupaluser)
_dbhostlocal='localhost'    # (usually what it is on the local server)
_dbhostremote='localhost'     # (also what it is on the remot server, but may be an IP address)
_dbpwd='database password'    # (don't let this slip away or so will your database...)
_docrootlocal='where the actual website resides on the local server'  # (e.g.: _docroot=html)
_docrootremote='where the actual website resides on the remote server' # (e.g.: _docroot=public_html)
# Include trailing slash, /, for _wwwbaselocal and _wwwbaseremote.
_wwwbaselocal='the full path name to the directory where the document root is on the local server'        # (e.g.: _wwwbaselocal=/var/www/)
_wwwbaseremote='the full path name to the directory where the document root is on the remote server'    # (e.g.: _wwwbaselocal=/chroot/user/)
_sitename='example.com'    # (e.g.: _sitename=example.com)
_tmpbckdir='temp'        # (can be anything but temp or tmp is prefered)

# This first test below checks whether we are on the local server or on the production server. In the document root of the local server, one creates an empty file called testing.server, such as with the bash command "touch testing.server" and then locks it read only.
if [ -e $_wwwbaselocal$_docrootlocal/testing.server ]; then
  echo "fullsitebackup.sh V1.2.20160903-$_sitename.test"
  _dbhost=$_dbhostlocal
  # Website Files
  _webrootdir=$_wwwbaselocal$_docrootlocal  # (e.g.: _webrootdir=/home/user/html)
  _backupdir=$_wwwbaselocal'backup'            # (the suggested name of the backup directory on the local server)
  # Default TAR Output File Base Name
  _tarnamebase=$_sitename'.test_backup-';
else
  echo "fullsitebackup.sh V1.2.20160903-$_sitename"
  _dbhost=$_dbhostremote
  # Website Files
  _webrootdir=$_wwwbaseremote$_docrootremote    # (e.g.: _webrootdir=/chroot/user/public_html)
  _backupdir=$_wwwbaseremote'backup'             # (the suggested name of the backup directory on the remote server)
  # Default TAR Output File Base Name
  _tarnamebase=$_sitename'_backup-';
fi

#
# Variables
#
echo " .. Setup"
# Form rest of name as a time stamp.
_datestamp=`date +'%Y%m%d-%H%M'`

# Get in Execution directory (script start point)
pushd $_backupdir
_startdir=`pwd`
# Temporary Directory
_tempdir=$_tmpbckdir$_datestamp
if [ ! -d $_tmpbckdir$_datestamp ]; then
   # Create temporary working directory
   mkdir $_tempdir
fi

#
# Input Parameter Check
#
if test "$1" = ""
  then
    _tarname=$_tarnamebase$_datestamp.tgz
  else
    _tarname=$1
fi
echo "    done"

#
# TAR website files
#
echo " .. TARing website files in $_webrootdir"
cd $_webrootdir
tar cf $_startdir/$_tempdir/filecontent.tar .
echo "    done"

#
# sqldump database information
#
echo " .. sqldump'ing database:"
echo "    user: $_dbuser; database: $_dbname; host: $_dbhost"
cd $_startdir/$_tempdir
mysqldump -p$_dbpwd --user=$_dbuser --host=$_dbhost --add-drop-table $_dbname > dbcontent.sql
echo "    done"

#
# Create final backup file
#
echo " .. Creating final compressed (tgz) TAR file: $_tarname"
tar czf $_startdir/$_tarname filecontent.tar dbcontent.sql
echo "    done"

#
# Cleanup
#
echo " .. Clean-up"
cd $_startdir'/'$_tempdir
# Make it safer...
if [ $? -eq 0 ]; then
    rm filecontent.tar dbcontent.sql
    cd $_startdir
    rmdir $_tempdir
fi
echo "    done"

#
# Exit banner
#
echo " .. Full site backup complete"
echo ""
popd

Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol type start> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <u> <s> <sup> <sub> <hr>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Image CAPTCHA
Enter the characters shown in the image.