How to Backup a Linux Box using SSH


Enable public key login by running the following on the source box as root

ssh-keygen -t rsa
Copy the resulting public key to the target box, again as root

scp .ssh/id_rsa.pub target_server:.ssh/authorized_keys2
Once the public key is installed you can test it by simply ssh'ing to the box as root you should be logged in without being prompted for a
password

ssh servername
To get backup going I made up this script very rudimentary....

I could have chosen to make one large remote tar ball but I chose to
split it up into functional tars

#!/bin/sh
# filename: /home/jamesm/bin/backup

# things to backup

# mail
# apache config
# website
# home drive
# svn db

REMOTE_DIR=/mnt/hde1/backup

MYMAIL=/home/vpopmail/domains/jmcd.dyndns.org/jamesm/Maildir
tar -czvf - $MYMAIL | ssh mx1 "cat > $REMOTE_DIR/amd-jamesm-mail.tar.gz"

APACHE_CONFIG="/etc/httpd/access \
/etc/httpd/conf \
/etc/httpd/conf.d"
tar -czvf - $APACHE_CONFIG | ssh mx1 "cat > $REMOTE_DIR/amd-apache.tar.gz"

SVN=/opt/subversion
tar -czvf - $SVN | ssh mx1 "cat > $REMOTE_DIR/amd-svn.tar.gz"

MYHOME="/home/jamesm/Docs \
/home/jamesm/.ssh \
# /home/jamesm/mp3 \ probably not needed to backup mp3 or oggs every day
# /home/jamesm/ogg \
/home/jamesm/bin"
tar -czvf - $MYHOME | ssh mx1 "cat > $REMOTE_DIR/amd-myhome.tar.gz"

MYWEB=/var/www/html
tar -czvf - $MYWEB | ssh mx1 "cat > $REMOTE_DIR/amd-web.tar.gz"

You can run the above manually but it's probably easier to put it in root's crontab

crontab -e

and add a line to get it to backup every day at 2:05 am to call the script

05 2 * * * /home/jamesm/bin/backup

James McDonald  1:44 PM 28/09/2005 AEST