Skip to content

Instantly share code, notes, and snippets.

@andrewjcurrie
Last active July 11, 2022 05:00
Show Gist options
  • Save andrewjcurrie/da545f4e608bc3e47970 to your computer and use it in GitHub Desktop.
Save andrewjcurrie/da545f4e608bc3e47970 to your computer and use it in GitHub Desktop.
Website Backup Shell Script
#!/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 "";
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment