Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm looking for a solution for using rsync between 2 remote servers. It seems like its not possible. Does any one know why its not possible? I ask that because I think if I know the reason, maybe I could use another tool to make it possible.

Update: I have a hypervisor on my primary site with n vm's running on. I have another hypervisor on my secondary site which I want to be the backup server for my primary server. For keeping the file synced between these two, the best way I've found is using rsync. The problem is I don't want to run my code (rsync) on the VMs because I want my product to be agent less. In this case I need to add a third computer to do run the code. Now, I need to rsync between my primary and secondary site which I'm stuck with because rsync doesn't work for remote to remote servers.

share|improve this question
    
The reason is, the software wasn't written that way. –  Zoredache Jul 26 '12 at 19:06
    
What kind of access do you have to these servers? Have you considered simply mounting the remote filesystems with NFS/SMB/whatever? –  Zoredache Jul 26 '12 at 19:19
    
I wanna sync the files of virtual machines with another server. My client is gonna be a virtual machine on the same server that is running these vms. I could use ssh and ftp, but I prefer not to install any extra thing on the vms. –  AliBZ Jul 26 '12 at 19:25
    
Please take some time and update your question to actually include all your requirements that you have added after the fact in the comments. –  Zoredache Jul 26 '12 at 19:29
    
Done, I think its now more clear, thanx –  AliBZ Jul 26 '12 at 19:36

4 Answers 4

ssh user@server1 "rsync /files user@server2:/directory"
share|improve this answer
    
Of course this assumes that server1 has direct access to server2. If the two servers cannot reach each other for some reason this probably won't help. –  Zoredache Jul 26 '12 at 19:05
    
If neither server1 nor server2 can access the other, it's not likely that your server3 can access either server. The first thing to do is to make one or both servers actually accessible :) –  Michael Hampton Jul 26 '12 at 19:08
    
Just to make sure, you mean that I should first ssh, then run rsync on the first remote server? ssh server_address "do something" does something on the server after ssh connection is established? –  AliBZ Jul 26 '12 at 19:12
    
That's correct. In this example, the rsync command will be run directly on server1, and the communication goes directly from server1 to server2. –  Michael Hampton Jul 26 '12 at 19:15
1  
@AliBZ: even if you make rsync remote.server:/source /local/target you are running rsync on the remote server. Rsync to/from remote server will call rsync command on the server (Via ssh or using own protocol). So using ssh to explicitly connect to one of the servers does not change much. –  Jacek Konieczny Nov 13 '12 at 18:21

You can use ssh, with ssh public/private keys to do this securely with rsync.

ssh can authenticate with a password, or with a public/private keypair. In this answer I will cover how to setup an ssh public/private key pair. Describe ssh-agent forwarding, and how to use them with rsync to sync two remote machines.

SSH public/private key

You can generate an ssh key pair by using ssh-keygen, it will prompt you for a passphrase, you will want to enter something secure here. it will also generate two files $HOME/.ssh/id_rsa (don't share this), and $HOME/.ssh/id_rsa.pub.

The public key (id_rsa.pub) can be shared with anyone without fear of your account being compromised by them. If someone gets a hold of your private key then they can authenticate to any account/machine that is set up to accept your private key.

This is where the passphrase comes in, it makes it so if someone gets your private key they still need to know the passphrase.

authorized_keys

During login ssh looks for a file at $HOME/.ssh/authorized_keys. This file is a list of public keys that are authorized to be used for authentication. On your remote machines you will want to append your id_rsa.pub to this file.

During ssh attempts you will now be prompted for your passphrase to your private key instead of your password.

ssh-agent

You can avoid having to type your passphrase over and over by using ssh-agent bash, followed by ssh-add. This first runs bash with a "keyring" attached to it, then adds your key to the keyring. Now, while in this shell if ssh is called it will pull your private key from the keyring instead of prompting you for the passphrase.

agent-forwarding

Set AllowAgentForwarding yes in /etc/ssh/sshd_config on the remote hosts. This makes it so you can ssh from a machine you have sshed to, to another one that has your public key in authorized_keys and you won't be prompted for a password, or passphrase, as it will handle the communication back to the keychain at your source machine.

ssh commands

ssh lets you issue a command at the remote machine without giving you a shell, if you have the above setup you can do the following:

ssh user1@remote_host1 'hostname;id'

This will run the commands hostname, and id on remote_host1 and use your key without prompting you. It should return "remote_host1, user1"

You could also demonstrate the agent forwarding with

ssh user1@remote_host1 'ssh user2@remote_host2 "hostname;id"'

This will run ssh user2@remote_host2 "hostname;id" on remote_host1 wich will then ssh to remote_host2 and run the commands their. It hand back the authentication request to your machine as agent forwarding is on and use your key without prompting you. It should return "remote_host2, user2"

rsync

You can now do the following.

ssh remote_host1_user@remote_host1 "rsync -ave ssh  source_sync_dir remote_host2_user@remote_host2:target_sync_dir"

This tells remote_host1 to rsync the source on host_1 with the destination on host_2, and rsync has been told to use ssh, which will use your forwarded key.

share|improve this answer
    
Does AllowAgentForwarding yes allows you to ssh without password? –  AliBZ Jul 26 '12 at 19:27
    
If you have created an ssh key with ssh-keygen, and added the public key to the authorized_keys file on remote_host1 and remote_host2, then you can connect without the password as ssh will use your private key for authentication, and the forwarding will enable the first remote host to return authentication requests to the machine that the private key is on. –  Phillip Nordwall Jul 26 '12 at 19:35
    
I think its not a good way for privacy right? because in this case everybody could ssh to the second server without a password. –  AliBZ Jul 26 '12 at 19:37
    
It doesn't allow anyone to connect without a password. They must have the key, and know the passphrase. I have edited the answer to describe in more details what is going on. –  Phillip Nordwall Jul 26 '12 at 20:41

Assuming you're using *nix:

Add a cronjob that performs the synchronization to one of the servers.

If you want to sync everything from the /home/user/sync_dir from one computer to the other, and can't be bothered to set up rsync itself for the directory specifics, I prefer doing this over ssh. (You'll need to set up keys to login instead of passwords if running via cron).

rsync -ave "ssh" /home/user/sync_dir/ remote_user@remote_host:/home/user/remote_sync_dir/

share|improve this answer
    
Hi, what you suggest is for local to remote, i need remote to remote –  AliBZ Jul 26 '12 at 19:09
    
I don't want to write my rsync script on any of the systems. –  AliBZ Jul 26 '12 at 19:10
    
Then I do not believe this is possible with rsync. The software just doesn't support it (no better reason than that). –  Ryan Jul 26 '12 at 19:14
    
Do you know any other tool like rsync that I could use? –  AliBZ Jul 26 '12 at 19:15
gw_en_2_segmentos# ssh cuenta@ip_origen 'cd /carpeta_origen;star \
                           -acl -artype=exustar -z \
                           -c -f=- *' | ssh cuenta@ip_destino \
                                        'cd /carpeta_destino;star \
                                         -acl -artype=exustar -z -xv -f=-'

Will give you a remote-to-remote copy, with file permissions and POSIX ACLs

share|improve this answer
2  
As much as it may not seem fair, there is an official policy that questions (and answers) on Server Fault need to be in English. I've translated yours accoringly. –  voretaq7 Nov 13 '12 at 18:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.