PDA

View Full Version : How to auto rsync with ssh passwordless


patsissons
August 18th, 2006, 02:08 AM
This HOWTO is designed to help those who are looking to backup a directory from their local computer to a remote computer using the power of rsync, the security of ssh, and the ease of automation. Your local computer will make use of a cron job that will execute the backup as often as you want, behind the scene so that you don't have to worry about your data's saftey.

There will be a quick-start rundown of the steps at the end of this howto.

First we start off by making a public key on the local machine
ssh-keygen -t rsa
use -t rsa unless you plan on accessing a older machine, or rather a machine whose version of openSSH is older. In most cases you will not have a problem with -t rsa. However, if you do have a problem you can try leaving it out, this will create a DSA key instead. ssh-keygen will ask you first where to store the public key. The default location is usually fine, that is unless you want to manage multiple public keys. Unless multiple public keys is what you need, simply hit enter to accept the default location. Next you will be asked for a password. You must hit enter twice without typing in a password or else every time you use this public key you will have to enter that password instead. This should result in the creation of the key and the public key pair. If you chose the default values, ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.

Now we need to copy our public key to the remote machine so the remote machine can add it to its list of authorized keys.
ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host
Of course, you should use your custom public key if you did not use the default name in the previous step. Also as you would expect, username and remote_host should be replaced by their respective values. After executing this, it will ask you for your password, this is just the ssh password to the remote machine for the username that you used. Upon completion, there should be a file on the remote machine ~/.ssh/authorized_keys that contains the public key that you just generated.

You can test out if you were successful now by ssh'ing to the remote machine, you should no longer be asked for a password. If this is the case, you are in the clear, otherwise, something has gone wrong.

To setup rsync you will need to make a script that looks something like this.

#!/bin/sh
rsync -e 'ssh -p 22' -avzp /some/dir remote_host:/var/backups/some_host

The -e 'ssh -p 22' is not completely necessary (in fact its very redundant), however, if you are connecting to ssh on a non standard port, you will need to change the 22. Otherwise, you can just use -e ssh instead. Again, remember to change the remote_host to the actual hostname of the remote server. Now depending on how often you want to run this backup, you can either setup a specific entry in the /etc/crontab file (this is a little more complicated), or you can just use the pre-built cron directories in ubuntu (cron.hourly, cron.daily, cron.weekly, cron.monthly). I recommend the second option, since it makes for a very easy setup. Simply save the script you created inside the desired cron directory, then chmod +x the filename that you save it as. If you wish to test it out, simply execute the script from the console.

Finally, make sure that the directory that you are sending your backups to on the remote server actually exists, otherwise rsync will error out and you will not backup anything.


Quick-Start
On the local machine:

ssh-keygen -t rsa
# hit return three times

ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host
# enter your password for username on remote_host

cat > /etc/cron.daily/remote_backup
#!/bin/sh
rsync -e 'ssh -p 22' -avzp /some/dir remote_host:/var/backups/some_host
^D

chmod +x /etc/cron.daily/remote_backup

ssh username@remote_host mkdir /var/backups/some_host




Final Thoughts
I am no rsync expert, nor ssh or cron. Actually, I learnt most of this stuff in the last couple of hours. But since there was no tutorial on ubuntu forums, I decided to make my own. This means that there may very well be a better way to do this. I wouldn't doubt it for a second. Also, there may be better flags to include in the rsync script. If anyone knows anything better to add, please do so as I think this is a somewhat important topic to understand. To everyone else, I hope this how to is helpful in making your backup automations a breeze :)


--
Pat

JonRohan
August 30th, 2006, 05:19 PM
Just what im after. Thanks :)

claypole
September 9th, 2006, 01:09 PM
Thanks, this is exactly what I needed too! :D

carlossousa
September 11th, 2006, 06:15 PM
hello,

I noticed your nice rsync how-to but I wonder if you could help me out with something. I have 3 servers, pc1, pc2 and pc3.
pc1 has the data, pc2 has a rsync of pc1 done at 11pm every night and I wanted pc3 to only have a "update" of what happens to be different from pc1 and pc2 every night.
What rsync option do I have to activate on pc3 to allow me to store, on a directory name basis of the type "home[dayN-monthM-yearO]" so I would have the likes of a "incremental" daily backup of pc1?

Can you help?
Thanks,

Carlos Sousa

plusbryan
April 17th, 2008, 11:31 AM
My remote server is running on another port, and ssh-copy-id uses a somewhat unintuitive format for specifying ports:

# ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 222 root@server"

novakyu
November 17th, 2008, 11:18 AM
BTW, I just wanted to say that this setup is potentially very insecure, especially if you created a passwordless private key.

With a passwordless private key, if anyone ever gets a hold of the private key, he will have complete root access to your other server as well.

There's no way to make a passwordless backup completely secure, but at the very least, you should limit the key on the remote server end to allow connection from only the IPs that need to use the key and limit it to only the command it needs to use (rsync). Check the ssh manpage for authorized_keys for more information.

CodeAlias
December 9th, 2008, 08:57 AM
Great post,

I would like to point out to this howto :

Rsync configuration for password-less mirroring over ssh (http://www.codealias.info/technotes/synchronizing_cvs_repositories_with_rsync)

that also describes server side setup.

wrt security issues, I agree with novakyu. An alternative and a bit more secure way to perform password-less rsync would be through kerberized ssh connections.
Instead of using public keys, kerberos allows the rsync client to use renewable tickets to authenticate its self to the rsync server. If an attacker steals the ticket, he will be able to use it only for a short period of time (depending on your configuration), also he will not be able to use it from another machine.

dbrine
June 27th, 2009, 10:03 AM
great guide but I'm getting error (password prompts actually). I followed the guide and created keys on the local and copied the pub key to the server. this is the error I get. Where am I going wrong???

dbrine@VM-UServer2:~/.ssh$ sudo ssh-copy-id -i ~/.ssh/id_rsa.pub dbrine@192.168.1.107
dbrine@192.168.1.107's password:
Now try logging into the machine, with "ssh 'dbrine@192.168.1.107'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

dbrine@VM-UServer2:~/.ssh$ ssh dbrine@192.168.1.107
Enter passphrase for key '/home/dbrine/.ssh/id_rsa':
dbrine@192.168.1.107's password: