~couchNerd ~serialOutrager ~puts2In42 ~makesPigsFly

TIP: Automating Website Backups

What can go wrong with your wonderfully running website / blog? Everything..

Your online abode is so difficult to keep secured. It can get hacked, you may install something wrong, a wrong configuration can wreak havoc, the server hard disk might crash, natural calamities, and what not. Your best option to secure yourself against all this is summarized in just one word “BACKUP”.

Now, a backup involves two most important things: “Database Backup” and “Files Backup”. Most webhosts give you the options to back these up for you but with a few catches:

  • You can backup but only through your control panel manually. No automation/scheduling of backups.

  • Automated Backing up is not free

  • Automated Backing up is free but restoring is not as they give you the backed up data only on request.

There are many plugins available for most CMS and blogging sw for backing up databases. But what about the files? Especially if you have more than one sites on one hosting account. Well, here I’ll tell you how to automate file backups in three simple steps.

Note: This guide is only for linux based hosts. But you can follow a similar approach for windows based hosting as well.

Step 1) Backup through command line.

You can use “tar” for this. With tar, you can create compressed archives quickly for a complete directory tree.

tar cvzf ~/backups/backup_date +%w.tgz ~/public_html
> > Explanation:

  • tar is the name of the command

  • c => compress v=> verbose z=>gzip for compression

  • ~/backups => backup directory where ur backups will be stored (~ is shortcut for your home directory). It is generally a good practice to store your backups in a directory not directly accessible through http, i.e., outside your public_html directory.

  • backupdate +%w.tgz => forms the name of the backup file in the form backup.tgz. This will form a circular buffer kind of scenario where backups more than 1 week later will automatically be overwritten by latest ones to avoid using up all your webspace. You can modify this according to your need and available webspace by taking a look at man page of “date” command. (Run “man date” on command line)

  • ~/public_html => is the directory you want to backup. Most probably your website will be located in this directory. If not sure, then check with your webhost.

Step 2) Mailing the backup to yourself through command line. (The server might develop some problem, hard disk might crash or your backups on server might be lost, so this is a good option to have)

mutt -s “Backup date” -a ~/backups/backup_date +%w.tgz [email protected]
> > Explanation:

  • mutt => mailing user agent that sends the mail (You can use others, but I prefer mutt)

  • -s “Backup date” => -s option is to specify the subject of the mail on command line. This line will specify the backup date as subject.

  • -a ~/backups/backup_date +%w.tgz => -a option specifies the file to be attached.

  • [email protected] => Specify the mail id t which the email has to be sent

  • The body of the email is empty. If you want some text to be here, you can form a file, say “email.txt” and write the contents in it and replace the above part with “<email.txt”

Step 3) Final Step. Automating it all. What you need to do is add the above commands into a script and then find a way to make it run automatically.

The script looks like:

#!/bin/bash > tar cvzf ~/backups/backup_date +%w.tgz ~/publichtml > mutt -s “Backup date” -a ~/backups/backupdate +%w.tgz [email protected]
> >

Save it as “backup.sh” and make it executable by running “chmod +x backup.sh”. We’ll assume that you save it in ~/backups directory. You can store it anywhere, just remember it.

Now, you need to tell the server to run it automatically at a specified time. We’ll use “cron” for this. (If you are not comfortable with command line, then login to your website control panel, and look for an option called cron, where you can make the schedule graphically) Run the following command:

crontab -e
> > This will open a file with your scheduled tasks info. Make sure that you don’t touch any of the existing lines. Add the following line at the end.

MAILTO=“myusername” > 00 22 * * * ~/backups/backup.sh
> >

The first line will make sure that the output of the script, any errors etc, are sent to the your local mail account, that you can check for debugging purposes.

The second line is of the format “# m h dom mon dow command” m=> minute of the hour h=>> hour of the day dom=> Day of month mon=> month of year dow=> day of week command=> command that you want to run So, the command mentioned above will run the command every day at 10:00 PM GMT. You can modify the line according to your needs.

That’s it. You are done. Just wait for the specified time, and then check your mailbox for the backup file. Let me know if you face any problems, have any issues, or want to ask any questions about cuztomizing it for yourself.