Forum Moderators: phranque

Message Too Old, No Replies

Backup up your webserver to your home computer.

Safe, inexpensive and fun! or not.

         

wheel

3:48 am on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I recently set up a system to backup my webserver to a computer at my house. It's a good way to get the warm fuzzy feeling that comes from automated offsite backups for cheap - as well as having the data handy to burn CD/DVD archive copies and all that other fun stuff.

There's nothing new here, a rehash of a million blogs, but the subject of backups is worthy of yet one more post.

First, you'll need to set up your home connection. You'll need:
- a high speed always on connection. Broadband DSL, cable, etc are all fine.
- a static IP. Your ISP for your home connection will probably charge you $20 or so. Pay it (it's good for other things)
- a junker computer. Most anything will do. Speed and ram aren't so important. Most importantly you're going to want a decent sized hard drive, and maybe a cd/dvd burner. Ideally, set up your operating system on a small hard drive, then drop in a second hard drive - something like a 250g IDE or similiar.

Grab a linux distribution and install it on this computer. Mandrake, Redhat, whatever. when you partition the drives (it'll ask you this during the install) mount the second drive at /backup. That way any files you copy to the /backup directory end up on the second drive.

When you set up the network on that machine, give it a static IP address on your internal network.

Next, on whatever you're using for a firewall/router/modem you'll need to set things up so that port 53 is 'port forwarded' to the static IP address you just gave your new backup computer. That means an external call to port 53 will get transparently sent to the home computer. We need this so that each night when your webserver calls your home computer on port 53 for the backups that it ends up at the right computer.

Drop the machine into your basement/closet/whatever. Turn it on and make sure it's connected.

Now, you need to make sure your web server can login to this computer every night without being asked for a password. Log in to your webserver and issue the following command:
ssh-keygen -t dsa
That will ask you a couple of questions - leave the password blank. At the end, you'll have a directory called /.ssh. Inside that directory will be at least two files - one that has an extension of .pub (this is your public key).

Take the contents of that public key file - cut and paste somehow and copy it to your home backup computer. Look in the /~user/.ssh directory for a file called authorized_keys. If it doesn't exist, create it with the one line contents of the .pub file from your server. If it does exist, paste in the line from the .pub key on your server as the last line of authorized_keys on your home machine.

Now - you should be able to login to your home backup computer without a password. Login to your webserver. Then go 'ssh 123.123.123.123' where 123. is your static IP of your home connection. You should log in to your home computer without any password requirement.

Only two more steps - set up the automated transfer, then set up a compression utility on your home computer.

Create a file on your webserver called 'backup.sh' or something. Inside it, we want the following line:
rsync -e ssh -azv /home/~user/directory_to_backup 123.123.123.123:/backup/webserver

That will synchronize the directory from your webserver to a 'webserver' folder on the 'backup' drive at home. You can add things like --exclude "/home/logs" or other directories to exclude specific areas like your log files.

save the file, make sure you do a chmod 755 so you can execute the file. Try running it - it should backup to your home computer. Be aware that the first time you run it it will transfer everything (albeit compressed over ssh) so it'll take a while. after that it will update only changed files.

Now you need to add a cron job so this runs automatically on your web server everynight. Run the command:
crontab -e
and enter the line
0 3 * * * /path/to/backup_script/backup.sh

That'll sync the two machines at 3 am each morning.

Last step, we probably want compressed archives of the backup. So we're going to add an automated job to do this everyday after the backup.

On the home computer, create a file called 'compress.sh' (make sure you chmod 755 so it can be executed) In it, add these two lines:
cd /backup
tar -czf $(date +%Y%m%d)webserver-backup.tgz webserver

and then run this command on your home computer:
crontabe -e
to which you want to add the line:
0 8 * * * /path/to/compress_script/compress.sh

That will tarball and gzip your last night's backup and give it a name of today's date - e.g. 20050517webserver-backup.tgz. Once it's in the crontab, you'll get a new file every day. Getting a snapshot of any day is as easy as finding the appropriately named file and unzipping it. Voila - automated backups and archives.

Bonus points 1)
I also backup my mysql tables like this (file backup.sh):

rsync -e ssh -azv /home 10.10.10.10:/backup/webserver
service mysqld stop
rsync -e ssh -azv /var/lib/mysql 10.10.10.10:/backup/webserver
service mysqld start

that shuts down the mysql database server so that I get a complete backup without tables being changed in the middle of things. Another way to do it would be to do something like this in your backup.sh file:
mysqldump -u username -ppassword databasename -a -B>/home/~user/backupmysql.dump

Which will create a file called 'backupmysql.dump' on your webserver. If you place backupmysql.dump in the directory tree on your webserver that's getting backup up in the rsync command, then all you need to do is put the msyqldump command into backup.sh prior to the rsync and the database will get backed up along with everything else.(in other words, you dump your database to a text file that gets backed up along with everything else).

Bonus points 2)
on my home computer I actually have three drives. one has the operating system. The second has my current backup. On the third, I create the compressed daily files over to that drive instead of leaving it on the same drive as the backup. Just in case the drive ever got full, it won't affect my nightly backups.

Bonus points 3)
with all that in place, you can easily install K3B which is a linux version of cd/dvd burning - kinda like Nero for MS. Once a week, burn your archived files to CD or DVD and label them. There you have it - offsite backups and archives.

AFAIC, this setup is pretty professional. It covers offsite backups, nightly archives and archiving to DVD giving me a pretty good history should I ever need it. Just for giggles, I set it up so that my business desktop gets backed up to the machine in the basement every night too - one less thing to worry about!

make sure you back up *everything* you can think of. Once it's running, you only actually copy changed files over the web so you might as well eat the big initial transfer and copy your server config filesand the rest of it.

Make sure after you set it up you let it run for a day, then try and restore a file.

Make sure after you set it up you let it run for a day then try and restore a file.

The technical here can find some flaws in the methodology - but afaic, it's as robust a backup procedure as most commercially available - and far better than what most people are doing. Plus it's cheap - for the cost of a junk computer and a static IP address, and you've got all the backups at home you could want. (I actually augment this with a second hard drive on my server that I do a similiar procedure with so I have onsite backups as well).

Relax, knowing you're pretty well backed up.

HTH.

jim_w

7:24 am on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a small site, less than 200 Meg. So I just use WS_FTP95 and one month I put the backup on my XP box and the next month I put in on my Win98 box on the LAN. The probability that both hard drives would not be recoverable in case of a server’s catastrophic failure is very remote and all I have to do to backup is highlight the files and sub’s and click a button. It creates the directories and everything for me.