Website Backup Shell Script

Website Backup Shell Script

Website Backup Shell ScriptThe GitHub Gist below can be used to easily backup your PHP/MySQL websites. All you have to do is connect to your webserver over SSH, determine where you want to store the backup script, copy the contents of the Gist below to a new file named backup.sh or whatever name you like just make sure it ends with .sh. Before you save the file, go through it line by line and follow the included instructions.

You are going to need to define your website’s document root and your MySQL database name and your MySQL username and password to connect to said database. Once you have done all that save the file then don’t forget to issue a chmod +x backup.sh command (or whatever you called your file) to make it executable. After that you are ready to easily backup your website!

To run the backup script just type ./backup.sh and press enter while inside the directory where your script is stored. When it’s all said and done you’ll have one TAR archived backup labeled Website-Backup-(YEAR)-(MONTH)-(DAY).tgz. The year month and day are all the current month, day and year and the script is verbose so you will be notified of the backup status as it happens.

Once you extract the backup archive using tar -xvzf you will find your website’s entire document root compressed into a single archive labeled webroot.tar and your MySQL database will be stored in the database.sql MySQL file. I hope someone finds this script useful and if you have any tips or suggestions on how to improve it please feel free to add some comments. I plan on posting an update to this post sometime soon explaining how to completely automate your website backups with this script using a cronjob task. Thanks for reading and long live backups!

#!/bin/bash
#
# Website Backup Shell Script
# by Andrew Currie ([email protected])
#
# Performs a full backup of the specified database and document root.
# Be sure to edit the configuration options at the beginning of the file to match your environment prior to executing.
# The end result will be one TAR archive with the name 'website-backup-(current-datestamp)'.
# The backup includes the entire document root directory and also a 'database.sql' file containing a MySQL dump of the database.
#
# Begin Configuration Options (Specify which webroot and which database to backup.)
#
# Define The Website Document Root
webrootdir=/var/www/html/example.org
#
# Define The MySQL MySQL Database Name
dbname=DATABASE-NAME-HERE
#
# Define The MySQL Database Connection
dbhost=DATABASE-HOST-HERE
dbuser=DATABASE-USERNAME-HERE
dbpass=DATABASE-PASSWORD-HERE
#
# Define The Default Backup Filename
tarnamebase=Website-Backup-
datestamp=`date +'%Y-%m-%d'`
#
# Define The Script Execution Point
startdir=`pwd`
#
# Define The Temporary Working Directory
tempdir=TemporaryBackup-$datestamp
#
# End Configuration Options
#
# Begin The Input Parameters Check
#
if test "$1" = ""
then
tarname=$tarnamebase$datestamp.tgz
else
tarname=$1
fi
#
# Begin the backup process.
#
echo "";
echo "******************************************";
echo " PREPARING TO BACKUP THE WEBSITE ";
echo "******************************************";
echo "";
#
#
# Create The Temporary Working Directory
#
echo "";
echo "******************************************";
echo " SETTING UP A TEMPORARY BACKUP LOCATION ";
echo "******************************************";
echo "";
#
mkdir $tempdir
echo "";
echo "******************************************";
echo " TEMPORARY BACKUP LOCATION SETUP COMPLETE ";
echo "******************************************";
echo "";
#
#
# Compress The Specified Document Root Into a TAR Archive
echo "";
echo "**********************************************";
echo " COMPRESSING THE WEBSITE FILES IN $webrootdir ";
echo "**********************************************";
echo "";
cd $webrootdir
tar cf $startdir/$tempdir/webroot.tar .
echo "";
echo "**************************************************";
echo " WEBSITE FILES ARE NOW COMPRESSED INTO AN ARCHIVE ";
echo "**************************************************";
echo "";
#
# Peform mysqldump on the database specified in configuration options
#
echo "";
echo "***********************************************";
echo " PREPARING TO BACKUP THE DATABASE $dbname ";
echo "***********************************************";
echo "";
cd $startdir/$tempdir
mysqldump -p --user=$dbuser --pass=$dbpass --host=$dbhost --add-drop-table $dbname > database.sql
echo "";
echo "***********************************************";
echo " DATABASE BACKUP of $dbname COMPLETED ";
echo "***********************************************";
echo "";
#
# Append The Website Files Archive with The MySQL Database Backup
#
echo "";
echo "***********************************************************";
echo " MERGING THE WEBROOT AND DATABASE BACKUPS INTO ONE ARCHIVE ";
echo "***********************************************************";
echo "";
tar czf $startdir/$tarname webroot.tar database.sql
echo "";
echo "***********************************************************";
echo " WEBSITE BACKUP COMPLETED ($tarname) ";
echo "***********************************************************";
echo "";
#
# Cleanup Temporary Files
#
echo "";
echo "***********************************************************";
echo " PERFORMING SOME CLEANUP OPERATIONS ";
echo "***********************************************************";
echo "";
cd $startdir
rm -r $tempdir
echo "";
echo "***********************************************************";
echo " CLEANUP OPERATIONS COMPLETED ";
echo "***********************************************************";
echo "";
#
# Display The Confirmation Notice
#
echo "";
echo "***********************************************************";
echo " FULL WEBSITE BACKUP COMPLETED ";
echo "***********************************************************";
echo "";
#
DIGITAL PCI © 2018