I'm a Drupal PHP/MySQL web developer, living and working in Seattle, WA.
My professional interests are optimization, MySQL, front-end usability, efficient coding, and making Drupal do cool things.
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.
(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 . 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:
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:
On your backup machine, test your pubkey by typing (replacing username and remotehost):
You should now be able to login into your Linux server without typing in a password!
The rsync Backup Script
In other words, rsync is one of the best backup tools available.
Create your backup directory:
And create your backup script inside it, named
backup.sh, making sure to replace username@localhost:
#!/bin/bash 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:
#!/bin/bash archivedate=$(date +%Y-%m-%d) 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:
chmod +x /bin/cygwin1.dll mkdir /etc/cron.d cygrunsrv --install cron -p /usr/sbin/cron -a -D cygrunsrv --start cron
To see if the cron service is running, type:
ps -a | grep cron
Adding the Crontab:
On your backup server, type:
Press "i" and add:
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!