Backing up the filesystem from a remote machine is easier than most people think. In the proccess you might as well backup your mysql datbase as well.

Terminologies that I will use
The server with the files you want to back up : live-server
The server you want to back-up to : backup-server

Assumptions
You have SSH access to the remote server
Your username for the live-server is USERNAME

OK so lets get going

1.Settting up the passwordless SSH login

Log into your live-server via the terminal (I use putty for this) and type

ssh-keygen -t dsa

This will produce a line saying something like “Generating public/private dsa key pair.” followed by “Enter file in which to save the key: (.ssh/id_dsa)”.

Press enter. It will then ask for a passphrase and to repeat the passphrase. Leave this blank by just pressing enter twice.

It will create a directory called .ssh and 2 files inside id_dsa and id_dsa.pub. NEVER let your id_dsa file get public. That will give free reign to your site. To help prevent this type

cd .ssh
 and
 chmod 600 id_dsa

You public keys are now created. The next step it to copy the id_dsa.pub file to the backup-server. To do this type

scp id_dsa.pub USERNAME@live-server:

(note the “:” ) and enter the password for the live-server. This copies the id_dsa.pub file to the live-server. Now log into the live-server by typing

ssh live-server -l USERNAME

There should be an id_dsa.pub in your home directory. You should append it to the last line of the file authorized_keys2 in your .ssh/ directory. Dont worry if you dont have an authorized_keys2 fíle, just type:

cat id_dsa.pub >> ./.ssh/authorized_keys2

The public key is now set up. To test if it works type

logout

You are now back on the backup-server. Now type

ssh live-server -l USERNAME

You should have logged into the live-server without having to type your password.

The steps without the explanations

backup-server: ssh-keygen -t dsa
backup-server: cd .ssh
backup-server: chmod 600 id_dsa
backup-server: scp id_dsa.pub USERNAME@live-server:
backup-server: ssh live-server -l USERNAME
live-server : cat id_dsa.pub >> ./.ssh/authorized_keys2
live-server : logout
backup-server: ssh live-server -l USERNAME

2. The backup script

I sometimes run the backup-script from a php file using the shell_exec() command. This gives me the flexibilty to to do some other things at the same time. For example I can make 7 folders and make a weeks backup. Or run the DB backup at the same time. That bit is up to you. Anyway here is the steps.

I assume that you want to make the backup in a folder called backupdir. So in the root on the backup-server type

mkdir backupdir

Now the actuell sync command. This will start the backup! If you want to run this from a shell script put it into the script file. If you want to run it form a php file save it in the with the shell_exec()

rsync -avz -e ssh USERNAME@live-server:/full/path/to/dir/ /full/path/backupdir/

If you don’t know the /full/path/ you can also type pwd (means “print working directory”)

If you want to backup your DB at the same type use the following. It can also go into shell_exec()

ssh USERNAME@liveserver 'mysqldump -q -u DB_USER --password=DB_PASS DB_NAME -h DB_HOST' > /full/path/backupdir/backup.sql

3. Automation

Now you just need to automate it with crontab, but that is not disscussed in this tutorial.
Here a few links to get started:

aota.net