In a previous entry, I showed how to make backups in tape drives. While tape drives are still used these days (believe it or not), a lot of people would take most benefit of different approaches to make backups of their web applications, blogs, etc. Today I’ll be sharing short article about how to make backups of your website.
Quick Overview:
- Preparing everything for your backup
- Backing up your database and website files
- Configuring your backup script to run automatically
- Testing your backup
Preparing everything for your backup
As we need an automated backup solution for our websites, let us provide our script a way to connecting to the database without prompting for a password.
You can copy and edit the following text, then save it as .my.cnf in the home directory of your website. E.g.: if your home directory is “/home/my_website/”, then this file should be placed at “/home/my_website/.my.cnf” (note the leading dot ‘.’ in the file name)
[mysql]
user=your_mysql_user
password=your_mysql_password
host=localhost
database=website_db_name
[mysqldump]
user=your_mysql_user
password=your_mysql_password
host=localhost
Now, let us create a subdirectory called ‘bin‘ (from ‘binary’) in our home directory, that’s where our script will be placed. It’s possible that this directory already exists, in this case you can skip this section. The subdirectory should be created as ‘/home/my_website/bin‘.
Backing up your database and website files
Now, copy and edit this lines, and save it as “backup.sh” in your “/home/my_website/bin” directory. So after you have created it, it should reside in ‘/home/my_website/bin/backup.sh’
#!/bin/bash
#
# Copyright 2012, Pavel Espinal , SD, RD,
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Backup date string (to append to backup name)
# This is a date string that will be attached to your backup so you can
# know how recent it is
date_bk="`date +%F-[%A-%Hh-%Mm-%Ss]`"
# Which account to backup
# if your home directory is at "/home/my_website",
# then this should be "my_website"
user_bk="my_website"
# Which database to backup
# Name of the database you want to backup
database_bk="website_db_name"
# Backup recipient
# Email account where the backup should be sent to.
# I recommend you to put an email from your own website domain, then use a
# Gmail account to fetch these messages through POP3 :)
recipient_bk="backup@yourdomain.com"
# Name of files backup
filesBackupName_bk="${user_bk}Files-${date_bk}.tar.gz"
# Database backup name
dbBackupName_bk="${database_bk}DB-${date_bk}.sql.gz"
# DO NOT edit below this line (unless you know exactly what you are doing)
# Files backup
tar cf – /home/${user_bk}/public_html | gzip -c | \
uuencode ${filesBackupName_bk} | \
mail -s "Files Backup of ${date_bk}" ${recipient_bk}
# Database backup
mysqldump ${database_bk} | gzip -c | \
uuencode ${dbBackupName_bk} | \
mail -s "DB Backup of ${date_bk}" ${recipient_bk}
Configuring your backup script to run automatically
After you have created and uploaded the script to your ‘/home/my_website/bin’ directory, you must give the script execution rights. You can do this by modifiying the file permissions using any decent FTP client or through the command line:
chmod 744 /home/my_website/bin/backup.sh
Now that you have assigned the correct permissions to your file, you can go to your cPanel (assuming you have access to it) and configure a ‘cron job’ to run at the time you would like to backup your files. For example, If I want to to run my script every friday at 11:55 pm, I would add this line:
55 23 * * fri /home/my_website/bin/backup.sh 1>/dev/null
Testing your backup
After setting a backup mechanism, the first thing you must do (and continue doing regularly) is to get sure that it is working, and from time to time downloading a backup and getting sure it is functional, otherwise bad things could happen, very bad things.
You could test your new backup system executing it from the command line. If you don’t know how to do it, tell a friend to do it for you; but under any circumstances trust your files to a backup you have not tested.
Finally, if there’s something you think I could make clearer in this article, just let me know.