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

Jack Wallen
Jan 22, 2009
Updated • Nov 29, 2012
Backup, Linux
|
3

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.

Advertisement

Tutorials & Tips


Previous Post: «
Next Post: «

Comments

  1. Kenny N said on December 21, 2012 at 11:05 pm
    Reply

    This looks great. One thing to keep in mind though, is that this doesn’t account for there being changes to the Filesystem while the backup is running (which if the websites hosted, is extremely like to be the case). You may want to add a lock to the database, as well as use something like mysqldump. Here’s what I would do:

    mysqldump –all-databases > my_dump_file.sql

    Note that this will lock the tables (below copied from mysql site):
    =======
    –opt
    This option is shorthand. It is the same as specifying –add-drop-table –add-locks –create-options –disable-keys –extended-insert –lock-tables –quick –set-charset. It should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly.
    The –opt option is enabled by default. Use –skip-opt to disable it. See the discussion at the beginning of this section for information about selectively enabling or disabling a subset of the options affected by –opt.
    ===

    Also, before pulling the files, you probably would want to flush any pending writes to the filesystem. You can do this using:

    fsfreeze -f mountpoint
    #do the copy
    fsfreeze -u mountpoint

    1. Martin Brinkmann said on December 22, 2012 at 1:56 am
      Reply

      That’s a good idea, thanks for pointing that out.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.