LAMP Rotating Backup

 You have probably spent countless hours building and updating your website, but probably have not given much thought about how you will backup or restore your work in the event of a disaster. Most people assume their hosting provider will take care of that for them, but what if they go out of business or simply make a mistake?

Of course there are hundreds of ways to backup a LAMP website, but this is how I do it for a point in time restore. I am not a fan of incremental backups because they take more work to restore and are not really necessary since disk space is cheap nowadays. The goal of this script is to allow you to restore your LAMP website or a single file to a point in time of your choosing without much effort. Automating full, rotating backups of your LAMP website is easy to do with this simple shell script. This script accomplishes the following:

Installation and use:

1.) Download the script and unzip with the gunzip command (gunzip

2.) Make the script executeable (type chmod +x and copy the script to your scripts directory; /root/scripts or/home/userx/scripts, etc.

2.) Modify the variables to match your website; such as, source (www directory), destination, MySQL admin, days to keep the archives, etc.

3.) Create the root level of the destination directory; /home/backup/snapshots

4.) Test the script to make sure it works. (keep in mind that this script is going to delete directories and files under the directory you specified in the$DEST variable that are older than the number of days you specified in the $DAYSOLD variable. You may want to comment those function until you are sure of what you are doing.)

5.) Add the following line to your crontab to run daily at midnight. Userx is your account name provided by your hosting provider. The path is specific to Linux, so adjust accordingly for other *nix boxes. Change the location if you are running the command from a directory other than /root/scripts


0 0 * * * sh /root/scripts/ >> /var/dailybackup.log 2>&1



# What: Self rotating backup script for LAMP websites
# Author: Chris Updegrove
# Website:
# email: chrisup(AT)

# Adds the date to the file name in the Year-Month-Day fashion
DATE=$(date '+%Y-%m-%d')

# This is were you will save the snapsots on your server. Do not store them under your www directory!

# The name of your mysql administrator account.

# The password you use with your mysql administrator account

# The mysql database scheme

#The default mysql database

# The name of your website database

# Backup the www directory

# Backup the config files and misc files
MISC='/etc/yum* /etc/sudoers /etc/webmin /etc/webalizer.conf /etc/ssh'

# Number of days you want to keep the backups

# ----------------------------------------------------

# Start by cleaning up the old backups
find $DEST -maxdepth 1 -type f -mtime $DAYSOLD -exec echo "Removing these files => {}" \; -exec rm {} \; -print
find $DEST -maxdepth 1 -type d -mtime $DAYSOLD -exec echo "Removing these directories => {}" \; -exec rm -Rf {} \; -print

# Check the directotry for a failed backup first

if [ -e $DEST/$DATE ]
then echo "WARNING: $DEST/$DATE exists - renaming the file with a random number and a .bak extension"

if [ -e $DEST/$DATE.$HOSTNAME.tgz ]
then echo "WARNING: $DEST/$DATE.$HOSTNAME.tgz exists - renaming the file with a random number and a .bak extension"

# Prepare the destinatioin directory
mkdir $DEST/$DATE
mkdir $DEST/$DATE/mysql
mkdir $DEST/$DATE/config
mkdir $DEST/$DATE/config/etc
mkdir $DEST/$DATE/config/etc/apache

echo "Backing-up the $WWWDIR and $APACHECONF and $MISC to $DEST"
# Copy stuff we want to backup
# Apache data and config files, misc config files, scripts and bin files
cp -a $APACHECONF $DEST/$DATE/config/etc/apache/
cp -a $APACHECONFD $DEST/$DATE/config/etc/apache/
cp -a $MISC $DEST/$DATE/config/
# UNCOMMENT IF NEEDED cp -a /root/scripts $DEST/$DATE/
# UNCOMMENT IF NEEDED cp -a /usr/local/bin $DEST/$DATE/

echo "Dumping the MySQL databases"
# Backup MySQL
mysqldump --user=$MYSQLADMIN --password=$MYSQLPASSWD $WEBDB > $DEST/$DATE/mysql/$WEBDB.sql
mysqldump --user=$MYSQLADMIN --password=$MYSQLPASSWD $MYSQLDB > $DEST/$DATE/mysql/$MYSQLDB.sql
mysqldump --user=$MYSQLADMIN --password=$MYSQLPASSWD $SCHEMA > $DEST/$DATE/mysql/$SCHEMA.sql

echo "Storing the archive in $DEST/$DATE as $DATE.$HOSTNAME.tgz"
# Make one big archive with comperssion and keep permissions on files and folders
tar -zpcvf /$DEST/$DATE.$HOSTNAME.tgz $DEST/$DATE

echo "Cleanup the $DEST/$DATE directory"
# Cleanup the temporary destination directory
rm -Rf $DEST/$DATE




Back To Articles


None Found

Add Comment