HowTo

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 lampbackup.sh.gz).

2.) Make the script executeable (type chmod +x lampbackup.sh) 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

 

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=you@youremail.com
HOME=/home/userx 
0 0 * * * sh /root/scripts/lampbackup.sh >> /var/dailybackup.log 2>&1

 

 LAMP BACKUP SCRIPT:

#!/bin/bash
# What: Self rotating backup script for LAMP websites
# Author: Chris Updegrove
# Website: http://chaucer.homeunix.net
# email: chrisup(AT)gmail.com

# VARIABLES SECTION
# 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!
DEST=/home/backup/snapshots

# The name of your mysql administrator account.
MYSQLADMIN=sqladmin

# The password you use with your mysql administrator account
MYSQLPASSWD=password123

# The mysql database scheme
SCHEMA=information_schema

#The default mysql database
MYSQLDB=mysql

# The name of your website database
WEBDB=mywebsite

# Backup the www directory
WWWDIR=/var/www/html

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

# Number of days you want to keep the backups
DAYSOLD=+6

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

# 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"
mv $DEST/$DATE $DEST/$DATE.$RANDOM.bak
fi

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"
mv $DEST/$DATE.$HOSTNAME.tgz $DEST/$DATE.$HOSTNAME.$RANDOM.bak.tgz
fi

# 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 $WWWDIR $DEST/$DATE/
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



Comments

None Found

Add Comment