Free remote backup: passwordless SSH + Rsync

If you’ve always wanted to backup your files to a remote server, but never really knew how – here’s a quick and easy way to do it:

1. Create a passwordless SSH setup
Login into your account on your [Linux, Unix, BSD, OS X] system. Type in

ssh-keygen -t dsa

Hit Enter three times.

This will create a .ssh directory in your home directory including some special encrypted files. Then type this – replacing the bits in bold with your own details

ssh user@thehost.com ‘test -d .ssh || mkdir -m 0700 .ssh ; cat >> .ssh/authorized_keys && chmod 0600 .ssh/*’ < ~/.ssh/id_dsa.pub

If you’re asked for a password, type in the password for user@thehost.com that you created.

Test your setup by typing:

ssh user@thehost.com

If it logs in without asking you for a password – you’re done with that part! If not, try googling or mail me and I’ll try to help you out.

If you want to add more authorized keys – like from other users, just add a number to the end of authorized_keys – like so:

ssh user2@thehost.com ‘test -d .ssh || mkdir -m 0700 .ssh ; cat >> .ssh/authorized_keys2 && chmod 0600 .ssh/*’ < ~/.ssh/id_dsa.pub

Test it out by typing:

ssh user2@thehost.com

2. Back it up

Once that’s done, you need to set up rsync to back up your files. Type something like this:

rsync -e ssh -avz –delete /mydir/to_backup_up/ user@myhost.com:/mydir/backup/ >> ~/backup.log

Replace (a) /mydir/to_backup_up/ with the directory you want to backup (b)user@myhost.com with your username and host details (c) /mydir/backup with the remote folder that you will be backing your data up to and (d) ~/backup.log with the name of the file you want to log the output to.

If you want to automate this, type crontab -e and then add this:

30 * * * * rsync -e ssh -avz –delete /mydir/to_backup_up/ user@myhost.com:/mydir/backup/ >> ~/backup.log

Replace all the bits in bold, save it and then this should backup your data every 30 mins.

Enjoy!!