Simple website backup script

I have used variation of this backup script for quick backups of typical LAMP stack websites (e.g. based on Drupal or WordPress). This saves the database along with the files into an archive…

#!/bin/bash
# Database connection information
dbname=database_name
dbhost=localhost
dbuser=database_user
dbpass=database_password
homedir=/var/www/www.domain.com
webrootdir=html
# Default TAR Output File Base Name
tarnamebase=website_backup-datestamp=`date +'%Y-%m-%d-%H-%M-%S'`
cd $homedir
# Temporary Directory
tempdir=tmpbckdir$datestamp
tarname=$tarnamebase$datestamp.tgz
mkdir -p $tempdir
tar cf $homedir/$tempdir/$webrootdir.tar $webrootdir
cd $homedir/$tempdir
mysqldump --password=$dbpass --user=$dbuser --host=$dbhost --add-drop-table $dbname > $dbname.sql
mkdir -p $homedir/website_backups
tar czf $homedir/website_backups/$tarname $webrootdir.tar $dbname.sql
cd $homedir
rm -r $tempdir

Depending on the website, there may be various non-web-accessible (private) directories to include in the script as well. Once the script is modified for a particular website, it can be put in a cron job and modified to copy the archive to a backup server.

Leave a Comment