6

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.

6
  • The reason is, the software wasn't written that way.
    – Zoredache
    Jul 26, 2012 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, 2012 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, 2012 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, 2012 at 19:29
  • 2

6 Answers 6

8
ssh user@server1 "rsync /files user@server2:/directory"
9
  • 7
    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, 2012 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 :) Jul 26, 2012 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, 2012 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. Jul 26, 2012 at 19:15
  • 2
    @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. Nov 13, 2012 at 18:21
5

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.

4
  • Does AllowAgentForwarding yes allows you to ssh without password?
    – AliBZ
    Jul 26, 2012 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. Jul 26, 2012 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, 2012 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. Jul 26, 2012 at 20:41
2

If server1 and server2 can connect to each other, the above solutions will work. Otherwise I don't have a solution for rsync, but an old tar trick will work. I'm doing it now.

Prep: create an ssh key with no passphrase and install it into the authorized_keys file of the account at each server. Then do this:

$ ssh -i .ssh/my_key bob@server1 'tar -cf - -C sourcedir' | \
ssh -i .sh/my_key carol@server2 'tar -xvpf - -C targetdir'

Note that you will end up with targetdir/sourcedir on server2.

Credit: The idea came from the Oreilly System Administration book with the armadillo on the cover. I just inserted a workstation in the middle since server1 can't talk directly to server2.

4
  • Does this really work for you? I had to remove the first -C option, and the minus in tar -cf
    – greg0ire
    Jul 19, 2016 at 8:02
  • Heh. My AT&T 3B1 running System V UNIX didn't like the hyphens either. My example works as given for GNU tar. If you're on Solaris or BSD instead of GNU/Linux, it's not surprising that you needed to adjust the options.
    – nortally
    Jul 21, 2016 at 17:48
  • I'm on Ubuntu… couldn't be more mainstream.
    – greg0ire
    Jul 21, 2016 at 21:01
  • No explanation then. On Ubuntu, tar --help presents tar -cf archive.tar foo bar as an example, works for me. Using the the -C option might require a little practice, I always try to use full paths.
    – nortally
    Jul 25, 2016 at 15:47
1

I want to sync files from serverA to serverB, but there's no direct connection between them, but I have serverC that can access both serverA and serverB. what worked for me is:
on serverC:
ssh user@serverA 'rsync -avP -e "ssh -ax user@serverC ssh " /source/files user@serverB:/des/tination'

0
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

1
-1

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/

5
  • Hi, what you suggest is for local to remote, i need remote to remote
    – AliBZ
    Jul 26, 2012 at 19:09
  • I don't want to write my rsync script on any of the systems.
    – AliBZ
    Jul 26, 2012 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, 2012 at 19:14
  • Do you know any other tool like rsync that I could use?
    – AliBZ
    Jul 26, 2012 at 19:15
  • This answer should be withdrawn, it doesn't answer the question. Mar 16, 2017 at 9:44

Not the answer you're looking for? Browse other questions tagged .