Category: How to..., Installation, Linux Projects - Hands On — UbuntuLinuxHelp @ 9:39 am —

During the weekend (Saturday), UbuntuLinuxHelp was down for almost 12 hours.spacer_gif Automate Your Web Site Backup! Fortunately the hosting provider had data backups and there was no data loss. In any event, I also keep backups, so the added redundancy helps to protect the content. Up to now, the server has been configured to create a daily backup of databases and certain directories; and those (.gz files) are downloaded manually to another location later.spacer_gif Automate Your Web Site Backup!

But, what if there were no backups? What if your hosting provider cannot restore data at their end? To be blunt, you’d be back to square one! Developing a whole new site or blog from the beginning!spacer_gif Automate Your Web Site Backup! That’s a chilling thought, to lose everything and start again.spacer_gif Automate Your Web Site Backup!

For peace of mind and data (intellectual property) , today’s post will highlight some of the steps we’ve taken to fully automate the backup process. Hopefully this will help many of you who may encounter the same issues, or are simply looking for a proactive, automated backup system for your web sites, blogs, ecommerce sites, etc.

We’ll need 5 things to ensure this system works:spacer_gif Automate Your Web Site Backup!

  • The remote host (your web hosting server).spacer_gif Automate Your Web Site Backup!
  • The local host (your Ubuntu or other Linux based desktop).spacer_gif Automate Your Web Site Backup!
  • The open source Rsync package.spacer_gif Automate Your Web Site Backup!
  • OpenSSH.spacer_gif Automate Your Web Site Backup!
  • Cron.spacer_gif Automate Your Web Site Backup!

Let’s start with our desktop,spacer_gif Automate Your Web Site Backup! which is the ‘localhost’. In my case the desktop is Ubuntu Linux 7.10, but this can be any Linux based system. This could also be another Linux server, if you tweak this a bit more. ;) spacer_gif Automate Your Web Site Backup!

I know ‘cron’ is enabled (because it’s part of the default installation) of my Linux desktop. I also know SSH is installed (because it’s installed by default and I’ve used it), but I’m not sure if ‘rsych’ is there and if it works over SSH.spacer_gif Automate Your Web Site Backup!

Side note:spacer_gif Automate Your Web Site Backup! For those not familiar with Rsync, “rsync is an open source utility that provides fast incremental file transfer. rsync is freely available under the GNU General Public License and is currently being maintained by Wayne Davison.” Source: http://samba.anu.edu.au/rsync/

To see if rsych is installed, use the following terminal command:spacer_gif Automate Your Web Site Backup!

apt-cache search rsync

If you see it’s installed,spacer_gif Automate Your Web Site Backup! to determine if rsync works over SSH, open a terminal and type the following command (substituting your correct information):spacer_gif Automate Your Web Site Backup!

rsync -avz -e ssh Your Remote Username@Your Remote Server Host:/The Remote/dir /Your Local/dir/

Here is what the switches mean:spacer_gif Automate Your Web Site Backup!

a: Use ‘archive’ mode.spacer_gif Automate Your Web Site Backup!
v: Use ‘verbose’ output.spacer_gif Automate Your Web Site Backup!
z: Use ‘compression’ during file transfer.spacer_gif Automate Your Web Site Backup!
e: Specify the ‘command’ to run. In this case SSH.spacer_gif Automate Your Web Site Backup!

In my case the command could look something like this:spacer_gif Automate Your Web Site Backup!

rsync -avz -e ssh backupadmin@ubuntulinuxhelp.com :/backupdir/daily /home/ubplay/sitebackups

After entering the above command, I’m prompted to enter the password and the file transfer begins.spacer_gif Automate Your Web Site Backup!

In my case this is simple because the hosting provider uses ‘The’ industry standard software (Linux) as the standard applications, openssh, rsych, cron, etc.spacer_gif Automate Your Web Site Backup! And my local Linux system already had the tools installed. Now that I’ve determined it works, cron can automate the system. However, before moving to cron, make sure your server is configured to backup the files and databases on a daily (or other) schedule.spacer_gif Automate Your Web Site Backup!

If you’re using industry standard hosting services, you’ll be on a Linux box using cPanel.spacer_gif Automate Your Web Site Backup! Personally, I’ve tried several others including Plesk, ISPConfig, etc, however in my opinion, they don’t have the amount of flexibility or options that cPanel does. In terms of a LAN however, in my opinion nothing beats Webmin. Webmin has the greatest flexibility and options. However, I’m going off topic here, back to the subject at hand!…spacer_gif Automate Your Web Site Backup! Log into your hosting control panel and use the interface to configure your scheduled backups to occur during low-traffic periods. Make a note of the directory the backups are saved to. WHM/cPanel is great for this as it’s configured via a simple GUI, and is easy to use. :) In my case the server backs up the web site files and databases and stores them in /backupdir (so that my cron job can download any files in this directory later).spacer_gif Automate Your Web Site Backup! For privacy issues, I’m not going to post the script as it contains a username and password among other “exposures”.

Before moving to cron itself,spacer_gif Automate Your Web Site Backup! I needed to configure a script that will rsync over the SSH connection. Here are some example I found on the rsync site: Rsync Examples. Another great resource we found is here: resync-incr. On this site you’ll see another methodology and example scripts. And finally another great backup scripting resource here: Backup Script. I’m sure some of you have other great sites and resources listed, please comment below and add them. :) spacer_gif Automate Your Web Site Backup!

After you’ve set up your script, however you want it (there are hundreds of ways!),spacer_gif Automate Your Web Site Backup! use cron to run it. Setting up the cron job is not very difficult:spacer_gif Automate Your Web Site Backup!

0 2 * * * /home/ubplay/cron/rsync-ubuntulinuxhelp

This (above) downloads the backup at 2am every day. Remember to ensure that your server has finished creating its backup by this time.spacer_gif Automate Your Web Site Backup! Otherwise you’ll not be downloading the files you expect. In my case I use nano to create the file called “rsync-ubuntulinuxhelp” placed in the …/cron director. The file named rsync-ubuntulinuxhelp contains the actual bash script. To create the cron job itself (that calls the script), complete the following in a terminal:spacer_gif Automate Your Web Site Backup!

sudo crontab -e

and use the following parameters:spacer_gif Automate Your Web Site Backup!

* * * * * path to script/command to be executed and script/commandspacer_gif Automate Your Web Site Backup!
- - - - -
| | | | |
| | | | — Day of week (0 - 7)spacer_gif Automate Your Web Site Backup!
| | | ——- Month (1 - 12)spacer_gif Automate Your Web Site Backup!
| | ——— Day of month (1 - 31)spacer_gif Automate Your Web Site Backup!
| ———– Hour (0 - 23)spacer_gif Automate Your Web Site Backup!
————- Minute (0 - 59)spacer_gif Automate Your Web Site Backup!

(’*’ means ‘every’).spacer_gif Automate Your Web Site Backup!

Side note: to view your existing cron jobs, in a terminal, type:spacer_gif Automate Your Web Site Backup!

sudo cron -l

to delete a cron job:spacer_gif Automate Your Web Site Backup!

sudo cron -r

As usual, I hope this helps some of you!spacer_gif Automate Your Web Site Backup!

Enjoy :)

Technorati Tags: , , , , , , , , ,

There are 8 comment(s) added so far...

#1

Thank you very much. This was something I was looking for. I also had troubles with my hosting provider.

Kenney wrote on May 6, 2008 - 2:09 am
#2

@Kenney - Thanks. :) One thing I also realized is when cPanel makes the backups on the server, it already compresses them to .gz files so no need to re-compress using rsync (which will make the download slower). Therefore I removed the -z switch. ;)

UbuntuLinuxHelp wrote on May 6, 2008 - 6:32 am
#3

Would this work in reverse as well? I’ve been looking for away to back up my local data (photos, etc). My host has good redundancy, and my experience with ElephantDesktop was a travesty.

Arp wrote on May 6, 2008 - 8:28 am
#4

@Arp - I would think so! ;) There’s no reason why you can send stuff to your server.

UbuntuLinuxHelp wrote on May 6, 2008 - 8:31 am
#5

[…] But, what if there were no backups? What if your hosting provider cannot restore data at their end? To be blunt, you’d be back to square one! Developing a whole new site or blog from the beginning! Automate Your Web Site Backup! That’s a chilling thought, to lose everything and start again. Automate Your Web Site Backup! Read more at Ubuntu Linux Help […]

Automate Your Web Site Backup! : HowtoMatrix wrote on May 6, 2008 - 11:03 am
#6

Try rsnapshot!

It tankes advantage of the ext2/ext3 hardlinking features to save diskspace for unchanged files. It gives you “full backup” but does only require just a little more than the space of one full backup, plus incrementals.

It gives you the possibility to go back in time, restoring just a file, a directory and a lot more…

It is based on rsync.
http://www.rsnapshot.org/

Rolf wrote on May 23, 2008 - 10:42 am
#7

for rsync to work whith cron trough ssh, you “must” generate authkey without password, or your cron job may not connect to the server, because the ssh will require to enter pass, but cron cannot!

vladi wrote on May 26, 2008 - 3:29 am
#8

@vladi - The first time I connected manually during the set-up, I got the terminal prompt to add the key. After that it’s been running since, without the username and password (because it uses the key). So… as you mentioned, ssh has to be properly configured on the web server, if not, then it rsync will fail, in this case. ;) Thanks. :)

UbuntuLinuxHelp wrote on May 26, 2008 - 7:13 am
You can leave a response, or trackback from your own site.

Write Your Comment

Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs will be generated automatically.

You should have a name, right? 
Your email address, I promised I won't tell it to anyone. 
If you have a web site or blog, you can type the URL right here. 
This is where you type your comments. 
Remember my information for the next time I visit.