httpremix.com

About the Author

Jonah Ellison lives in Seattle, Washington and works at a web firm developing websites and custom web applications for the LAMP solution stack. He enjoys optimization, front-end usability, databases, clean code and clever solutions. Contact him or learn more.

 

Topics

Automating Server Backups Using “rsync/ssh/cron” on a Windows or Linux Machine 2008 Oct 9

Topics: backups — by jonah ellison

Everyone knows backups are important, not just for restoring from a system failure, but also for fixing user/admin/developer mistakes. Here's an efficient way to automatically backup files on your Linux/CentOS server using a spare Windows or Linux machine. You should already have a basic grasp of the Linux shell prompt for this guide.

Installing Cygwin
(You can skip this if you're backing up on a Linux machine.)
The first task is installing Cygwin on your Windows box, which provides a Linux-like environment for running our backup tools (rsync+ssh and cron). When running setup.exe, you should select as many packages as you feel comfortable installing by clicking install arrows. Be sure to install all of "Admin," "Base," "Devel," and "Net." You can always run setup.exe later to update your existing Cygwin installation.

Creating SSH Public/Private Keys
Since we want to copy our files securely over SSH without being prompted for a password each time we login, we need to generate a public key for our backup machine. At the shell prompt, enter:

  1. ssh-keygen -t dsa

Ignore the questions by pressing "Enter" at any prompts, such as the file location or passphrase. Now open ~/.ssh/id_dsa.pub (e.g., c:/cgwin/home/(Windows Username)/.ssh/id_dsa.pub) file and copy the contents. Log in to your remote/Linux machine and paste this info into the file ~/.ssh/authorized_keys. If the file doesn't exist, just create a new one. You must also modify your SSH daemon to use allow Pubkey authentications. Edit /etc/ssh/sshd_config and uncomment:

    RSAAuthentication yes
    PubkeyAuthentication yes

On your backup machine, test your pubkey by typing (replacing username and remotehost):

  1. ssh username@remotehost

You should now be able to login into your Linux server without typing in a password!

The rsync Backup Script

"rsync is a software application for Unix systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. rsync can copy or display directory contents and copy files, optionally using compression and recursion." –rsync on Wikipedia

In other words, rsync is one of the best backup tools available.

Create your backup directory:

  1. mkdir /rsync

And create your backup script inside it, named backup.sh, making sure to replace username@localhost:

  1. #!/bin/bash
  2. rsync -avz -e ssh username@remote:/var/www/ /rsync/www/ --log-file=/rsync/rsync.log

/var/www/ is the full path on the remote machine. /rsync/www/ is where to save on your backup machine. If you use a non-standard SSH port, you can use "ssh -p portNum" in quotes.

You can also backup your MySQL database files, log files, code repositories, or whatever else you desire. If you don't have access to the MySQL files directory, you can setup a mysqldump script on the remote machine to save to a file.

You may also want to create "snapshots" of your backups, so these files don't get overwritten by unwanted changes. Here's a snapshot script (snapshot.sh) that gzips MySQL database files:

  1. #!/bin/bash
  2. archivedate=$(date +%Y-%m-%d)
  3. tar cvf - /rsync/mysql | gzip > /rsync/archive/mysql_$archivedate.tar.gz

Cygwin: Installing the "cron" service
Crontabs are used for automating our backup scripts. On Cygwin, cron must be setup as a Windows service in order to run in the background:

  1. chmod +x /bin/cygwin1.dll
  2. mkdir /etc/cron.d
  3. cygrunsrv --install cron -p /usr/sbin/cron -a -D
  4. cygrunsrv --start cron

To see if the cron service is running, type:

  1. ps -a | grep cron

Adding the Crontab:
On your backup server, type:

  1. crontab -e

Press "i" and add:

  1. 0 0 * * * /rsync/backup.sh

Press "ESC" then "w" and enter to save.

This will run the backup script at midnight every day. You can also add a line for the snapshot.sh script... replace the first "0" with "30" to run the script 30 minutes later.

That's it! Now just make sure your backup server has your hard drives in a RAID 1 (mirroring) or better configuration for additional data redundancy!


1 Comment »

1. Comment by Steph – 2009 January 25 @ 5:25 am

Many thanks for your great and really helpful article !


Leave a comment

RSS feed for comments on this post - TrackBack URL