This is a quick guide to setting up a simple backup process for a dedicated Linux server, with the aim of getting all critical information down to a home server or other storage.
This provides a good degree of protection from data loss, and avoids having to pay for ISPs backup space, which can cost nearly as much as servers in some cases.
It uses some scripting on the server, and an rsync job, which will take a while when first run, but will be a lot quicker on subsequent incremental backups.
First off, dump all your config and databases to one place. I use a shell script to save them all compressed to my rsync user's home directory. I also use dates in the filename, with automatic clearup of files older than 5 days. This allows you to retrieve data that may have been deleted before a problem was reported. Save the script to your cron.daily folder and make it executable.
To create the rsync user just newuser -d /home/rsync then passwd as normal.
#!/bin/sh
NOW=`date +%Y%m%d`
# Delete files older than 5 days
find /home/rsync/backup/* -mtime +5 -delete
# dump local db, all databases
DB_IGNORE_LIST="^information_schema$"
USER=[USER]
PASS=[PASS]
DB_LIST=`mysql -u $USER --password=$PASS -e "SHOW DATABASES" -B --skip-column-names | egrep -v "$DB_IGNORE_LIST"`
for db in $DB_LIST
do
echo Backing up $db
mysqldump -h 127.0.0.1 -u $USER --password=$PASS $db > "/home/rsync/backup/mysql/$db.$NOW.sql"
done
# Zip the sql dumps
find /home/rsync/backup/mysql/*.sql -exec gzip -f '{}' \;
# Zip entire httpd conf folder
tar -zcpf "/home/rsync/backup/conf/httpdconf.$NOW.tar.gz" /etc/httpd/conf*
# Zip all conf files under /etc/
find /etc/* -name '*.conf'|xargs tar -zcpf "/home/rsync/backup/conf/conf.$NOW.tar.gz"
You then need to get these files off, along with your main web site files.
To do this, set up an rsync server on the remote host.
Add an init script to /etc/init.d
#!/bin/sh
. /etc/rc.d/init.d/functions
[ -f /usr/bin/rsync ] || exit 0
case "$1" in
start)
action "Starting rsyncd: " /usr/bin/rsync --daemon
;;
stop)
action "Stopping rsyncd: " killall rsync
;;
*)
echo "Usage: rsyncd {start|stop}"
exit 1
esac
exit 0
You may also want to create a link for this in the relevant runlevel startup dir (rc3.d normally)
Create a config for your rsync daemon at /etc/rsyncd.conf
motd file = /etc/rsyncd.motd log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock [home] path = /home comment = Home uid = rsync gid = rsync read only = yes list = yes auth users = rsync secrets file = /etc/rsyncd.scrt
And a password file at /etc/rsyncd.scrt
rsync:[password]
This creates an rsync module pointing at your home folder. You can create more specific ones if you like, though all the data you want to back up should now be in the home folder after the cron script is run.
Next, on your local home machine, you need to create the rsync job that does the work. I am on Windows so I use cwRsync. You could also use the full version of Cygwin.
Create a batch file containing something like this, adjusting paths and required args as appropriate:
rsync --no-iconv --exclude-from=exclude.txt --recursive --progress --verbose --delete --stats --compress --times --links --log-file=//nas1/backup/rsync.log rsync@[your server address]::home //nas1/Backup/[your server name]/ < rsync.pass
I am passing the password in from a separate file (rsync.pass) so it works as a batch. If like me you are on Windows, you may have trouble setting the required chmod 600 on the password file, which is why I am doing it this way rather than using the --password-file argument.
NOTE: The more secure way to do this, and one that does not leave password files lying around your system, is to set up ssh public and private keys and run the the rsync job over ssh. I didn't have time to get this working on Vista though.
POSTSCRIPT: This works OK for me, though I know it's a bit of a hack job. Any data loss you experience using these techniques is entirely your own fault. Any comments and improvements would be welcome