ghacks Technology News

Back up your Apache web directory and database with this simple script


I administer a lot of web sites. And all of these web sites need backup solutions. Since most of those web sites use LAMP servers it only made sense to set up a backup system using the available, included open source tools. It didn’t take long to create a solid backup system and, with the help of cron, automate that system so that Apache’s document root and the website databases were backed up regularly and without user intervention.

The script made use of the following tools: date, cat, tar, mv, and rm. That’s it. The script will create backups with the date in the file name and then move the backups to a central location. Without further adieu, let’s get to the script.

#! /bin/sh

TMP=”/tmp/”

#Format the date in YEAR-MO-DY format
TODAY=`date +%F`

# Check to see if there is a lastbackup file in /tmp, if not create it,
# if so then set LAST equal to $TODAY
if [ -f /tmp/lastbackup ]; then
LAST=`cat /tmp/lastbackup`
else
LAST=$TODAY
fi

# Set the web directory backup name to the following
WEB_FILENAME=”inc-”$TODAY”-web.tar.gz”

# Set database backup name to the following
DB_FILENAME=”inc-”$TODAY”-db.tar.gz”

# this tars up my web directory into web.tar.gz tarball.
/bin/tar -czf $TMP$WEB_FILENAME –after-date=$LAST /var/www/html

# Move the web back to the backup directory
/bin/mv $TMP$WEB_FILENAME /data

# Remove web backup file from temp director
rm $TMP$WEB_FILENAME

# this tars up my database directory into $TODAY-db.tar.gz tarball.
/bin/tar -czf $TMP$DB_FILENAME –after-date=$LAST /var/lib/mysql

# Move the backup database to the backup directory
/bin/mv $TMP$DB_FILENAME /data

# Remove web backup file from temp directory
rm $TMP$DB_FILENAME

What I wanted this to do is create daily backups and move the backups to the /data directory on the drive housing the server. These backups will be saved for one month. After the month is completed i have a second script that deletes the months backups prior to running the next backup (so there is always a backup to fall to). How I made use of this script is simple. I save the script (called backup.sh) in the root user directory and create a second script called rm_backups.sh that looks like this:

#! /bin/sh

rm /data/*gz

With these two files in place I create two cron entries. The first cron entry is for running the backup.sh script and looks like:

0 23 * * *     ~/backup.sh

The second cron entry is for running the rm_backups.sh script and looks like:

0 20 1 * *     ~/rm_backups.sh

Both of the above cron jobs are created as the root user.

Final Thoughts

Naturally this solution could be easily modified (using such tools rsync) to set up an offsite backup solution. What should be obvious is that creating a simple, flexible server backup system on Linux is easy. With the help of a little ingenuity, you can create your own automated backup service.




Tags: , , , , , , , ,
Categories: Advice, Linux, Open Source, Tutorials Advanced, Tutorials Basic


Read Related Posts


Leave a Reply   Follow Ghacks   Subscribe To Comment Rss

© 2005-2009 Ghacks.net. All Rights Reserved. Privacy Policy - About Us