How to rsync files between two remotes?How to rsync files between two remote servers?How do I sync two ubuntu folders which are in different servers configured in aws?How to copy between two remote hosts using tar piped into SSH from remote server when behind a firewall?How to rsync files between two remotes with an explicit port and different usernames?Trying to run rsync between two remote servers via reverse port forwarding with ssh keyscp -3 no longer worksrsync, delete files on receiving side that were deleted on sending side. (But don't delete everything)Is rsync really bidirectional or more unidirectional?Smarter filetransfers than rsync?rsync, find, and other useful commands for synchronizing a large quantity of dataHow to rsync files between two remotes with an explicit port and different usernames?Does rsync require both source host and destination host to run rsync as client, server, or daemon?rsync to cifs: “change” time not updatedHow to rsync files between two remote servers?Set permissions and owner's group for destination folder with rsyncInitial rsync backup of large data slow to RAID 5

How old can references or sources in a thesis be?

Maximum likelihood parameters deviate from posterior distributions

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?

RSA: Danger of using p to create q

I'm flying to France today and my passport expires in less than 2 months

Can I make popcorn with any corn?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Fully-Firstable Anagram Sets

Why is Minecraft giving an OpenGL error?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Does an object always see its latest internal state irrespective of thread?

What would happen to a modern skyscraper if it rains micro blackholes?

Was any UN Security Council vote triple-vetoed?

Malcev's paper "On a class of homogeneous spaces" in English

Character reincarnated...as a snail

Has there ever been an airliner design involving reducing generator load by installing solar panels?

Why are electrically insulating heatsinks so rare? Is it just cost?

Why can't we play rap on piano?

A case of the sniffles

Why doesn't H₄O²⁺ exist?

How much of data wrangling is a data scientist's job?

If human space travel is limited by the G force vulnerability, is there a way to counter G forces?



How to rsync files between two remotes?


How to rsync files between two remote servers?How do I sync two ubuntu folders which are in different servers configured in aws?How to copy between two remote hosts using tar piped into SSH from remote server when behind a firewall?How to rsync files between two remotes with an explicit port and different usernames?Trying to run rsync between two remote servers via reverse port forwarding with ssh keyscp -3 no longer worksrsync, delete files on receiving side that were deleted on sending side. (But don't delete everything)Is rsync really bidirectional or more unidirectional?Smarter filetransfers than rsync?rsync, find, and other useful commands for synchronizing a large quantity of dataHow to rsync files between two remotes with an explicit port and different usernames?Does rsync require both source host and destination host to run rsync as client, server, or daemon?rsync to cifs: “change” time not updatedHow to rsync files between two remote servers?Set permissions and owner's group for destination folder with rsyncInitial rsync backup of large data slow to RAID 5






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








48















I would like to transfer files between two remote hosts using on local shell, but it seems rsync doesn't support synchronisation if two remotes are specified as follow:



$ rsync -vuar host1:/var/www host2:/var/www
The source and destination cannot both be remote.


What other workarounds/commands I could use to achieve similar results?










share|improve this question






















  • serverfault.com/questions/411552/rsync-remote-to-remote

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 9 '16 at 17:11






  • 1





    Actually you can rsync between 2 remote hosts by leveraging sshfs on a 3rd host. Just use sshfs to mount host1 and host2 on host 3. Then rsync between 1 and 2.

    – William Legg
    Oct 19 '17 at 18:07











  • @WilliamLegg the disadvantage with using sshfs is that then rsync sees the source and destination filesystems both as local, so it disables its delta algorithm. At that point you almost might as well just use cp -p. See the answer that proposes this and its subsequent comments.

    – roaima
    Feb 9 '18 at 8:59


















48















I would like to transfer files between two remote hosts using on local shell, but it seems rsync doesn't support synchronisation if two remotes are specified as follow:



$ rsync -vuar host1:/var/www host2:/var/www
The source and destination cannot both be remote.


What other workarounds/commands I could use to achieve similar results?










share|improve this question






















  • serverfault.com/questions/411552/rsync-remote-to-remote

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 9 '16 at 17:11






  • 1





    Actually you can rsync between 2 remote hosts by leveraging sshfs on a 3rd host. Just use sshfs to mount host1 and host2 on host 3. Then rsync between 1 and 2.

    – William Legg
    Oct 19 '17 at 18:07











  • @WilliamLegg the disadvantage with using sshfs is that then rsync sees the source and destination filesystems both as local, so it disables its delta algorithm. At that point you almost might as well just use cp -p. See the answer that proposes this and its subsequent comments.

    – roaima
    Feb 9 '18 at 8:59














48












48








48


21






I would like to transfer files between two remote hosts using on local shell, but it seems rsync doesn't support synchronisation if two remotes are specified as follow:



$ rsync -vuar host1:/var/www host2:/var/www
The source and destination cannot both be remote.


What other workarounds/commands I could use to achieve similar results?










share|improve this question














I would like to transfer files between two remote hosts using on local shell, but it seems rsync doesn't support synchronisation if two remotes are specified as follow:



$ rsync -vuar host1:/var/www host2:/var/www
The source and destination cannot both be remote.


What other workarounds/commands I could use to achieve similar results?







shell ssh rsync






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 7 '15 at 15:43









kenorbkenorb

9,041374112




9,041374112












  • serverfault.com/questions/411552/rsync-remote-to-remote

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 9 '16 at 17:11






  • 1





    Actually you can rsync between 2 remote hosts by leveraging sshfs on a 3rd host. Just use sshfs to mount host1 and host2 on host 3. Then rsync between 1 and 2.

    – William Legg
    Oct 19 '17 at 18:07











  • @WilliamLegg the disadvantage with using sshfs is that then rsync sees the source and destination filesystems both as local, so it disables its delta algorithm. At that point you almost might as well just use cp -p. See the answer that proposes this and its subsequent comments.

    – roaima
    Feb 9 '18 at 8:59


















  • serverfault.com/questions/411552/rsync-remote-to-remote

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 9 '16 at 17:11






  • 1





    Actually you can rsync between 2 remote hosts by leveraging sshfs on a 3rd host. Just use sshfs to mount host1 and host2 on host 3. Then rsync between 1 and 2.

    – William Legg
    Oct 19 '17 at 18:07











  • @WilliamLegg the disadvantage with using sshfs is that then rsync sees the source and destination filesystems both as local, so it disables its delta algorithm. At that point you almost might as well just use cp -p. See the answer that proposes this and its subsequent comments.

    – roaima
    Feb 9 '18 at 8:59

















serverfault.com/questions/411552/rsync-remote-to-remote

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Mar 9 '16 at 17:11





serverfault.com/questions/411552/rsync-remote-to-remote

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Mar 9 '16 at 17:11




1




1





Actually you can rsync between 2 remote hosts by leveraging sshfs on a 3rd host. Just use sshfs to mount host1 and host2 on host 3. Then rsync between 1 and 2.

– William Legg
Oct 19 '17 at 18:07





Actually you can rsync between 2 remote hosts by leveraging sshfs on a 3rd host. Just use sshfs to mount host1 and host2 on host 3. Then rsync between 1 and 2.

– William Legg
Oct 19 '17 at 18:07













@WilliamLegg the disadvantage with using sshfs is that then rsync sees the source and destination filesystems both as local, so it disables its delta algorithm. At that point you almost might as well just use cp -p. See the answer that proposes this and its subsequent comments.

– roaima
Feb 9 '18 at 8:59






@WilliamLegg the disadvantage with using sshfs is that then rsync sees the source and destination filesystems both as local, so it disables its delta algorithm. At that point you almost might as well just use cp -p. See the answer that proposes this and its subsequent comments.

– roaima
Feb 9 '18 at 8:59











10 Answers
10






active

oldest

votes


















48














As you have discovered you cannot use rsync with a remote source and a remote destination. Assuming the two servers can't talk directly to each other, it is possible to use ssh to tunnel via your local machine.



Instead of



rsync -vuar host1:/var/www host2:/var/www


you can use this



ssh -R localhost:50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /var/www localhost:/var/www'


In case you're wondering, the -R option sets up a reverse channel from port 50000 on host1 that maps (via your local machine) to port 22 on host2. There is no direct connection from host1 to host2.






share|improve this answer


















  • 1





    I like @roaima's solution, but I couldn't get it to work for me due to various reasons. In the end I used sshfs to mount both remote directories locally, then used rsync across the two locally mounted directories.

    – aidan
    Oct 26 '15 at 2:07











  • Possible to see an example where a key is used? Having trouble figuring out how to use -i to specify the keys needed for the ssh commands.

    – onassar
    Mar 31 '16 at 14:27











  • @onassar add the -i key... parameter inside the quotes after the ssh command. If that doesn't help you please feel free to ask a New Question, referencing this answer for context

    – roaima
    Mar 31 '16 at 17:41






  • 1





    the reverse connect does not read the ~/.ssh/config on the local side - need to use something that can be resolved as if there were no SSH config file

    – Florenz Kley
    Apr 29 '16 at 10:40






  • 1





    'Assuming the two servers can't talk directly to each other'. This solution does work around a firewall or NAT issue that prevents a direct SSH connection. However, it doesn't address the case where for security reasons the source user (on host1) has no key or credentials or insufficient write permissions on the destination. For that see Kevin Cox's solution, or resort to an indirect connection using a script or scp -3.

    – Cedric Knight
    Aug 21 '17 at 8:15


















17














You didn't say why you didn't want to log into one host and then copy to the other so I will share one of my reasons and solutions.



I couldn't log into one machine then rsync to the other because neither host had a SSH key that could log into the other. I solved this by using SSH agent forwarding to allow the first host to use my SSH key while I was logged in.



WARNING: SSH forwarding allows the host to use your SSH key for the duration of your login. While they can't copy your key they can log into other machines with it. Make sure you understand the risks and don't use agent forwarding for machines you don't trust.



The following command will use SSH agent forwarding to open a direct connection from host1 to host2. This has the advantage that the machine running the command isn't bottlenecking the transfer.



ssh -A host1 rsync -vuar /var/www host2:/var/www





share|improve this answer




















  • 3





    +1 for explaining a valid use case (where the remote user on host1 has no permissions on the destination server); for the important security caveat (use port forwarding -D rather than -A to get around network rather than key restrictions); for explaining the advantage; for the command being short; and it actually working. Note that you may need to specify username@host1 if it's different from local username. Also that rsync performs host key verification when connecting to host2, so host1's key should already be in ~/.ssh/known_hosts on host2 or the command will fail.

    – Cedric Knight
    Aug 21 '17 at 8:21












  • Phenomenal answer, this helped me orchestrate some things in TeamCity that I wasn't able to do before (n.b. for other TeamCity users, you must add the "Build Feature" called "SSH agent" to your build config before using ssh -A, see confluence.jetbrains.com/display/TCD10/SSH+Agent).

    – John Zwinck
    Mar 7 '18 at 10:15


















11














I like roaima's answer, but the paths are the same in both examples, obscuring which is which. We've established that the following doesn't work:



rsync -vuar host1:/host1/path host2:/host2/path


But this does (I omitted the explicit bind_address of localhost from the -R option since that's the default):



ssh -R 50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path'


Note that you'll have to have ssh keys set up correctly between the two remote hosts, with the private key on host1 and the public key on host2.



To debug the connection, break this into two parts and add verbose status:



localhost$ ssh -v -R 50000:host2:22 host1


If this works, you'll have a shell on host1. Now try the rsync command from host1. I recommend doing this in a different window so the verbose ssh info doesn't get mixed together with the rsync status info:



host1$ rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path





share|improve this answer

























  • In my example the paths are source, destination. The rsync is initiated on host1 with the target on host2. (You could have asked for clarification in a comment.)

    – roaima
    Jan 14 '16 at 0:12







  • 1





    I would have commented, but you can't comment on somebody else's post without a reputation of 50+.

    – jaybrau
    Jan 14 '16 at 15:20



















5














Reformatting answer by roaima in bash script syntax (and adding line continuation characters '' for clarity) I randomly picked port 22000...



SOURCE_USER=user1
SOURCE_HOST=hostname1
SOURCE_PATH=path1

TARGET_USER=user2
TARGET_HOST=host2
TARGET_PATH=path2

ssh -l $TARGET_USER -A -R localhost:22000:$TARGET_HOST:22
$SOURCE_USER@$SOURCE_HOST "rsync -e 'ssh -p 22000' -vuar $SOURCE_PATH
$TARGET_USER@localhost:$TARGET_PATH"





share|improve this answer


















  • 1





    it seems to me that all you've done is replace his arbitrary hostnames with variables?

    – Jeff Schaller
    Oct 20 '16 at 23:18






  • 2





    Yes, I did. It adds clarity for me on which is the source machine, which is the target, and where the source and target paths go. It took me awhile to work all that out and it wasn't obvious from simple placeholder hostnames.

    – David I.
    Oct 25 '16 at 15:23











  • Next time please feel free to improve someone else's answer directly by editing it.

    – roaima
    Apr 11 '17 at 7:30


















3














The Ideal way would be to run the rsync on one of those servers. But if you do not want to run a script on the remote server. You could run a script on your local system and do an ssh and execute the rsync there.



ssh user@$host1 <<ENDSSH >> /tmp/rsync.out 2>&1
rsync -vuar /var/www host2:/var/www
ENDSSH



Also, as you maybe aware rysnc does one way synchronization . If you'd like two way synchronization , you can look at osync (https://github.com/deajan/osync). I use it and found it to be helpful.






share|improve this answer






























    0














    Just as an additional info:



    If you use a jump host to connect the other two machines but they cannot reach each other directly, you can use sshfs as medium between these two machines like so (on the jump host):



    $ mkdir ~/sourcepath ~/destpath
    $ sshfs sourcehost:/target/dir ~/sourcepath
    $ sshfs desthost:/target/dir ~/destpath
    $ rsync -vua ~/sourcepath ~/desthpath


    SSHFS provides the two paths on the jump host and rsync manages the synchronisation of the files like always (just with the difference that it's virtually done locally).






    share|improve this answer


















    • 1





      Note that the performance will be terrible. This is because to detect changes rsync will read the file from the source server, transferring the whole thing over the network. That being said if you can't do a direct transfer you will have to eat this if you are using rsync. Also don't mount the destination, it is unnecessary and will cause rsync to change some defaults because it thinks it is talking to a local disk.

      – Kevin Cox
      Aug 22 '16 at 18:47


















    0














    You could run an rsyncd (server) on one of the computers.



    This is the approach I'm taking, since I do not want to use ssh to allow the 'source' (in rsync wording) access to the 'destination' as root without a password (as is required to use SSH tunneling with rsync in a script)



    In my case, I simply set up an rsyncd server on the destination computer with a single user allowed from the source pc and used rsync from the source side.



    Works great.






    share|improve this answer
































      0














      Try using this. It works for me.



      ssh src_user@src_host 'rsync -av /src/dir/location/ dest_user@dest_host:/dest/dir/loc/'





      share|improve this answer






























        0














        An easy to use script



        Over the years I've done this many times with more or less the same tricks as in every other answer here. However because it's very easy to get some detail wrong and spend a lot of time figuring out the issue I've come up with the script below that:



        1. Makes it easy to specify all the details (source, destination, options)

        2. Incrementally tests every single step and gives feedback if anything goes wrong, so that you know what to fix.

        3. Works around cases where ssh -A fails to propagate authentication data (don't know why this happens sometimes since the workaround was easier than finding the root cause)

        4. Finally does the job.

        How to use the script



        1. Make sure you can ssh to both hosts from localhost without typing a
          password.

        2. Set the variables in the first few lines of the script

        3. Execute it.

        How it works



        As I said it uses the same tricks as in every other answer here:



        • ssh's -R option to ssh from localhost to host1 while at the same time setting up a port forwarding that then allows host1 to connect via localhost to host2 (-R localhost:$FREE_PORT:$TARGET_ADDR_PORT)

        • ssh's -A option to allow easy authenticaion of the second ssh chanel

        My this IS complicated! Is there any easier way?



        When copying all or most bytes from source to destination it's FAR easier to use tar:



        ssh $SOURCE_HOST "tar czf - $SOURCE_PATH" 
        | ssh $TARGET_HOST "tar xzf - -C $TARGET_PATH/"


        The script



        #!/bin/bash
        #-------------------SET EVERYTHING BELOW-------------------
        # whatever you type after ssh to connect to SOURCE/TARGE host
        # (e.g. 1.2.3.4:22, user@host:22000, ssh_config_alias, etc)
        # So if you use "ssh foo" to connect to SOURCE then
        # you must set SOURCE_HOST=foo
        SOURCE_HOST=host1
        TARGET_HOST=host2
        # The IP address or hostname and ssh port of TARGET AS SEEN FROM LOCALHOST
        # So if ssh -p 5678 someuser@1.2.3.4 will connect you to TARGET then
        # you must set TARGET_ADDR_PORT=1.2.3.4:5678 and
        # you must set TARGET_USER=someuser
        TARGET_ADDR_PORT=1.2.3.4:5678
        TARGET_USER=someuser

        SOURCE_PATH=/mnt/foo # Path to rsync FROM
        TARGET_PATH=/mnt/bar # Path to rsync TO

        RSYNC_OPTS="-av --bwlimit=14M --progress" # rsync options
        FREE_PORT=54321 # just a free TCP port on localhost
        #---------------------------------------------------------

        echo -n "Test: ssh to $TARGET_HOST: "
        ssh $TARGET_HOST echo PASSED| grep PASSED || exit 2

        echo -n "Test: ssh to $SOURCE_HOST: "
        ssh $SOURCE_HOST echo PASSED| grep PASSED || exit 3

        echo -n "Verifying path in $SOURCE_HOST "
        ssh $SOURCE_HOST stat $SOURCE_PATH | grep "File:" || exit 5

        echo -n "Verifying path in $TARGET_HOST "
        ssh $TARGET_HOST stat $TARGET_PATH | grep "File:" || exit 5

        echo "configuring ssh from $SOURCE_HOST to $TARGET_HOST via locahost"
        ssh $SOURCE_HOST "echo "Host tmpsshrs; ControlMaster auto; ControlPath /tmp/%u_%r@%h:%p; hostname localhost; port $FREE_PORT; user $TARGET_USER" | tr ';' 'n' > /tmp/tmpsshrs"

        # The ssh options that will setup the tunnel
        TUNNEL="-R localhost:$FREE_PORT:$TARGET_ADDR_PORT"

        echo
        echo -n "Test: ssh to $SOURCE_HOST then to $TARGET_HOST: "
        if ! ssh -A $TUNNEL $SOURCE_HOST "ssh -A -F /tmp/tmpsshrs tmpsshrs echo PASSED" | grep PASSED ; then
        echo
        echo "Direct authentication failed, will use plan #B:"
        echo "Please open another terminal, execute the following command"
        echo "and leave the session running until rsync finishes"
        echo "(if you're asked for password use the one for $TARGET_USER@$TARGET_HOST)"
        echo " ssh -t -A $TUNNEL $SOURCE_HOST ssh -F /tmp/tmpsshrs tmpsshrs"
        read -p "Press [Enter] when done..."
        fi

        echo "Starting rsync"
        ssh -A $TUNNEL $SOURCE_HOST "rsync -e 'ssh -F /tmp/tmpsshrs' $RSYNC_OPTS $SOURCE_PATH tmpsshrs:$TARGET_PATH"

        echo
        echo "Cleaning up"
        ssh $SOURCE_HOST "rm /tmp/tmpsshrs"





        share|improve this answer

























        • tar is great when you've got a single (non-incremental) transfer and your transfer completes in a single pass. On the other hand, rsync with forwarding handles restarts and incremental transfers.

          – roaima
          yesterday











        • Of course @roaima -- I don't consider tar equivalent. I left this passing reference for the day when I will be reading this to solve a problem where rsync will not 100% necessary.

          – ndemou
          yesterday


















        -1














        It's possible to use tar via ssh to transfer the files:



        ssh -n user1@host1 'tar jcf - -C /var/www .' | ssh user2@host2 'tar jxvf - -C /var/www'


        Change j parameter (for tar) to z in two places if you wish to compress the archive with gzip, instead of bzip2. Usually bzip2 has the higher compression than gzip, but it's slower, so change it depending on your needs (see: bzip2 vs gzip).



        Related: How to copy between two remote hosts using tar piped into SSH from remote server when behind a firewall?




        Alternatively (to safe the bandwidth, because of the transparent compression) it's possible to use sshfs to mount remote file-system as local and use rsync as usual, e.g.



        $ sshfs user1@host1:/var/www /mnt
        $ rsync -vuar /mnt user2@host2:/var/www





        share|improve this answer




















        • 1





          Mounting the filesystem locally wont save any bandwidth between the source and local machine. It will however save bandwidth between the local and destination (as it isn't mounted locally). Using rsync this way only makes sense if you are trying to save bandwidth on the destination.

          – Kevin Cox
          Dec 3 '15 at 14:31











        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f183504%2fhow-to-rsync-files-between-two-remotes%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        10 Answers
        10






        active

        oldest

        votes








        10 Answers
        10






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        48














        As you have discovered you cannot use rsync with a remote source and a remote destination. Assuming the two servers can't talk directly to each other, it is possible to use ssh to tunnel via your local machine.



        Instead of



        rsync -vuar host1:/var/www host2:/var/www


        you can use this



        ssh -R localhost:50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /var/www localhost:/var/www'


        In case you're wondering, the -R option sets up a reverse channel from port 50000 on host1 that maps (via your local machine) to port 22 on host2. There is no direct connection from host1 to host2.






        share|improve this answer


















        • 1





          I like @roaima's solution, but I couldn't get it to work for me due to various reasons. In the end I used sshfs to mount both remote directories locally, then used rsync across the two locally mounted directories.

          – aidan
          Oct 26 '15 at 2:07











        • Possible to see an example where a key is used? Having trouble figuring out how to use -i to specify the keys needed for the ssh commands.

          – onassar
          Mar 31 '16 at 14:27











        • @onassar add the -i key... parameter inside the quotes after the ssh command. If that doesn't help you please feel free to ask a New Question, referencing this answer for context

          – roaima
          Mar 31 '16 at 17:41






        • 1





          the reverse connect does not read the ~/.ssh/config on the local side - need to use something that can be resolved as if there were no SSH config file

          – Florenz Kley
          Apr 29 '16 at 10:40






        • 1





          'Assuming the two servers can't talk directly to each other'. This solution does work around a firewall or NAT issue that prevents a direct SSH connection. However, it doesn't address the case where for security reasons the source user (on host1) has no key or credentials or insufficient write permissions on the destination. For that see Kevin Cox's solution, or resort to an indirect connection using a script or scp -3.

          – Cedric Knight
          Aug 21 '17 at 8:15















        48














        As you have discovered you cannot use rsync with a remote source and a remote destination. Assuming the two servers can't talk directly to each other, it is possible to use ssh to tunnel via your local machine.



        Instead of



        rsync -vuar host1:/var/www host2:/var/www


        you can use this



        ssh -R localhost:50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /var/www localhost:/var/www'


        In case you're wondering, the -R option sets up a reverse channel from port 50000 on host1 that maps (via your local machine) to port 22 on host2. There is no direct connection from host1 to host2.






        share|improve this answer


















        • 1





          I like @roaima's solution, but I couldn't get it to work for me due to various reasons. In the end I used sshfs to mount both remote directories locally, then used rsync across the two locally mounted directories.

          – aidan
          Oct 26 '15 at 2:07











        • Possible to see an example where a key is used? Having trouble figuring out how to use -i to specify the keys needed for the ssh commands.

          – onassar
          Mar 31 '16 at 14:27











        • @onassar add the -i key... parameter inside the quotes after the ssh command. If that doesn't help you please feel free to ask a New Question, referencing this answer for context

          – roaima
          Mar 31 '16 at 17:41






        • 1





          the reverse connect does not read the ~/.ssh/config on the local side - need to use something that can be resolved as if there were no SSH config file

          – Florenz Kley
          Apr 29 '16 at 10:40






        • 1





          'Assuming the two servers can't talk directly to each other'. This solution does work around a firewall or NAT issue that prevents a direct SSH connection. However, it doesn't address the case where for security reasons the source user (on host1) has no key or credentials or insufficient write permissions on the destination. For that see Kevin Cox's solution, or resort to an indirect connection using a script or scp -3.

          – Cedric Knight
          Aug 21 '17 at 8:15













        48












        48








        48







        As you have discovered you cannot use rsync with a remote source and a remote destination. Assuming the two servers can't talk directly to each other, it is possible to use ssh to tunnel via your local machine.



        Instead of



        rsync -vuar host1:/var/www host2:/var/www


        you can use this



        ssh -R localhost:50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /var/www localhost:/var/www'


        In case you're wondering, the -R option sets up a reverse channel from port 50000 on host1 that maps (via your local machine) to port 22 on host2. There is no direct connection from host1 to host2.






        share|improve this answer













        As you have discovered you cannot use rsync with a remote source and a remote destination. Assuming the two servers can't talk directly to each other, it is possible to use ssh to tunnel via your local machine.



        Instead of



        rsync -vuar host1:/var/www host2:/var/www


        you can use this



        ssh -R localhost:50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /var/www localhost:/var/www'


        In case you're wondering, the -R option sets up a reverse channel from port 50000 on host1 that maps (via your local machine) to port 22 on host2. There is no direct connection from host1 to host2.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 7 '15 at 18:22









        roaimaroaima

        46k758124




        46k758124







        • 1





          I like @roaima's solution, but I couldn't get it to work for me due to various reasons. In the end I used sshfs to mount both remote directories locally, then used rsync across the two locally mounted directories.

          – aidan
          Oct 26 '15 at 2:07











        • Possible to see an example where a key is used? Having trouble figuring out how to use -i to specify the keys needed for the ssh commands.

          – onassar
          Mar 31 '16 at 14:27











        • @onassar add the -i key... parameter inside the quotes after the ssh command. If that doesn't help you please feel free to ask a New Question, referencing this answer for context

          – roaima
          Mar 31 '16 at 17:41






        • 1





          the reverse connect does not read the ~/.ssh/config on the local side - need to use something that can be resolved as if there were no SSH config file

          – Florenz Kley
          Apr 29 '16 at 10:40






        • 1





          'Assuming the two servers can't talk directly to each other'. This solution does work around a firewall or NAT issue that prevents a direct SSH connection. However, it doesn't address the case where for security reasons the source user (on host1) has no key or credentials or insufficient write permissions on the destination. For that see Kevin Cox's solution, or resort to an indirect connection using a script or scp -3.

          – Cedric Knight
          Aug 21 '17 at 8:15












        • 1





          I like @roaima's solution, but I couldn't get it to work for me due to various reasons. In the end I used sshfs to mount both remote directories locally, then used rsync across the two locally mounted directories.

          – aidan
          Oct 26 '15 at 2:07











        • Possible to see an example where a key is used? Having trouble figuring out how to use -i to specify the keys needed for the ssh commands.

          – onassar
          Mar 31 '16 at 14:27











        • @onassar add the -i key... parameter inside the quotes after the ssh command. If that doesn't help you please feel free to ask a New Question, referencing this answer for context

          – roaima
          Mar 31 '16 at 17:41






        • 1





          the reverse connect does not read the ~/.ssh/config on the local side - need to use something that can be resolved as if there were no SSH config file

          – Florenz Kley
          Apr 29 '16 at 10:40






        • 1





          'Assuming the two servers can't talk directly to each other'. This solution does work around a firewall or NAT issue that prevents a direct SSH connection. However, it doesn't address the case where for security reasons the source user (on host1) has no key or credentials or insufficient write permissions on the destination. For that see Kevin Cox's solution, or resort to an indirect connection using a script or scp -3.

          – Cedric Knight
          Aug 21 '17 at 8:15







        1




        1





        I like @roaima's solution, but I couldn't get it to work for me due to various reasons. In the end I used sshfs to mount both remote directories locally, then used rsync across the two locally mounted directories.

        – aidan
        Oct 26 '15 at 2:07





        I like @roaima's solution, but I couldn't get it to work for me due to various reasons. In the end I used sshfs to mount both remote directories locally, then used rsync across the two locally mounted directories.

        – aidan
        Oct 26 '15 at 2:07













        Possible to see an example where a key is used? Having trouble figuring out how to use -i to specify the keys needed for the ssh commands.

        – onassar
        Mar 31 '16 at 14:27





        Possible to see an example where a key is used? Having trouble figuring out how to use -i to specify the keys needed for the ssh commands.

        – onassar
        Mar 31 '16 at 14:27













        @onassar add the -i key... parameter inside the quotes after the ssh command. If that doesn't help you please feel free to ask a New Question, referencing this answer for context

        – roaima
        Mar 31 '16 at 17:41





        @onassar add the -i key... parameter inside the quotes after the ssh command. If that doesn't help you please feel free to ask a New Question, referencing this answer for context

        – roaima
        Mar 31 '16 at 17:41




        1




        1





        the reverse connect does not read the ~/.ssh/config on the local side - need to use something that can be resolved as if there were no SSH config file

        – Florenz Kley
        Apr 29 '16 at 10:40





        the reverse connect does not read the ~/.ssh/config on the local side - need to use something that can be resolved as if there were no SSH config file

        – Florenz Kley
        Apr 29 '16 at 10:40




        1




        1





        'Assuming the two servers can't talk directly to each other'. This solution does work around a firewall or NAT issue that prevents a direct SSH connection. However, it doesn't address the case where for security reasons the source user (on host1) has no key or credentials or insufficient write permissions on the destination. For that see Kevin Cox's solution, or resort to an indirect connection using a script or scp -3.

        – Cedric Knight
        Aug 21 '17 at 8:15





        'Assuming the two servers can't talk directly to each other'. This solution does work around a firewall or NAT issue that prevents a direct SSH connection. However, it doesn't address the case where for security reasons the source user (on host1) has no key or credentials or insufficient write permissions on the destination. For that see Kevin Cox's solution, or resort to an indirect connection using a script or scp -3.

        – Cedric Knight
        Aug 21 '17 at 8:15













        17














        You didn't say why you didn't want to log into one host and then copy to the other so I will share one of my reasons and solutions.



        I couldn't log into one machine then rsync to the other because neither host had a SSH key that could log into the other. I solved this by using SSH agent forwarding to allow the first host to use my SSH key while I was logged in.



        WARNING: SSH forwarding allows the host to use your SSH key for the duration of your login. While they can't copy your key they can log into other machines with it. Make sure you understand the risks and don't use agent forwarding for machines you don't trust.



        The following command will use SSH agent forwarding to open a direct connection from host1 to host2. This has the advantage that the machine running the command isn't bottlenecking the transfer.



        ssh -A host1 rsync -vuar /var/www host2:/var/www





        share|improve this answer




















        • 3





          +1 for explaining a valid use case (where the remote user on host1 has no permissions on the destination server); for the important security caveat (use port forwarding -D rather than -A to get around network rather than key restrictions); for explaining the advantage; for the command being short; and it actually working. Note that you may need to specify username@host1 if it's different from local username. Also that rsync performs host key verification when connecting to host2, so host1's key should already be in ~/.ssh/known_hosts on host2 or the command will fail.

          – Cedric Knight
          Aug 21 '17 at 8:21












        • Phenomenal answer, this helped me orchestrate some things in TeamCity that I wasn't able to do before (n.b. for other TeamCity users, you must add the "Build Feature" called "SSH agent" to your build config before using ssh -A, see confluence.jetbrains.com/display/TCD10/SSH+Agent).

          – John Zwinck
          Mar 7 '18 at 10:15















        17














        You didn't say why you didn't want to log into one host and then copy to the other so I will share one of my reasons and solutions.



        I couldn't log into one machine then rsync to the other because neither host had a SSH key that could log into the other. I solved this by using SSH agent forwarding to allow the first host to use my SSH key while I was logged in.



        WARNING: SSH forwarding allows the host to use your SSH key for the duration of your login. While they can't copy your key they can log into other machines with it. Make sure you understand the risks and don't use agent forwarding for machines you don't trust.



        The following command will use SSH agent forwarding to open a direct connection from host1 to host2. This has the advantage that the machine running the command isn't bottlenecking the transfer.



        ssh -A host1 rsync -vuar /var/www host2:/var/www





        share|improve this answer




















        • 3





          +1 for explaining a valid use case (where the remote user on host1 has no permissions on the destination server); for the important security caveat (use port forwarding -D rather than -A to get around network rather than key restrictions); for explaining the advantage; for the command being short; and it actually working. Note that you may need to specify username@host1 if it's different from local username. Also that rsync performs host key verification when connecting to host2, so host1's key should already be in ~/.ssh/known_hosts on host2 or the command will fail.

          – Cedric Knight
          Aug 21 '17 at 8:21












        • Phenomenal answer, this helped me orchestrate some things in TeamCity that I wasn't able to do before (n.b. for other TeamCity users, you must add the "Build Feature" called "SSH agent" to your build config before using ssh -A, see confluence.jetbrains.com/display/TCD10/SSH+Agent).

          – John Zwinck
          Mar 7 '18 at 10:15













        17












        17








        17







        You didn't say why you didn't want to log into one host and then copy to the other so I will share one of my reasons and solutions.



        I couldn't log into one machine then rsync to the other because neither host had a SSH key that could log into the other. I solved this by using SSH agent forwarding to allow the first host to use my SSH key while I was logged in.



        WARNING: SSH forwarding allows the host to use your SSH key for the duration of your login. While they can't copy your key they can log into other machines with it. Make sure you understand the risks and don't use agent forwarding for machines you don't trust.



        The following command will use SSH agent forwarding to open a direct connection from host1 to host2. This has the advantage that the machine running the command isn't bottlenecking the transfer.



        ssh -A host1 rsync -vuar /var/www host2:/var/www





        share|improve this answer















        You didn't say why you didn't want to log into one host and then copy to the other so I will share one of my reasons and solutions.



        I couldn't log into one machine then rsync to the other because neither host had a SSH key that could log into the other. I solved this by using SSH agent forwarding to allow the first host to use my SSH key while I was logged in.



        WARNING: SSH forwarding allows the host to use your SSH key for the duration of your login. While they can't copy your key they can log into other machines with it. Make sure you understand the risks and don't use agent forwarding for machines you don't trust.



        The following command will use SSH agent forwarding to open a direct connection from host1 to host2. This has the advantage that the machine running the command isn't bottlenecking the transfer.



        ssh -A host1 rsync -vuar /var/www host2:/var/www






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 7 '18 at 13:40

























        answered Dec 3 '15 at 14:25









        Kevin CoxKevin Cox

        520411




        520411







        • 3





          +1 for explaining a valid use case (where the remote user on host1 has no permissions on the destination server); for the important security caveat (use port forwarding -D rather than -A to get around network rather than key restrictions); for explaining the advantage; for the command being short; and it actually working. Note that you may need to specify username@host1 if it's different from local username. Also that rsync performs host key verification when connecting to host2, so host1's key should already be in ~/.ssh/known_hosts on host2 or the command will fail.

          – Cedric Knight
          Aug 21 '17 at 8:21












        • Phenomenal answer, this helped me orchestrate some things in TeamCity that I wasn't able to do before (n.b. for other TeamCity users, you must add the "Build Feature" called "SSH agent" to your build config before using ssh -A, see confluence.jetbrains.com/display/TCD10/SSH+Agent).

          – John Zwinck
          Mar 7 '18 at 10:15












        • 3





          +1 for explaining a valid use case (where the remote user on host1 has no permissions on the destination server); for the important security caveat (use port forwarding -D rather than -A to get around network rather than key restrictions); for explaining the advantage; for the command being short; and it actually working. Note that you may need to specify username@host1 if it's different from local username. Also that rsync performs host key verification when connecting to host2, so host1's key should already be in ~/.ssh/known_hosts on host2 or the command will fail.

          – Cedric Knight
          Aug 21 '17 at 8:21












        • Phenomenal answer, this helped me orchestrate some things in TeamCity that I wasn't able to do before (n.b. for other TeamCity users, you must add the "Build Feature" called "SSH agent" to your build config before using ssh -A, see confluence.jetbrains.com/display/TCD10/SSH+Agent).

          – John Zwinck
          Mar 7 '18 at 10:15







        3




        3





        +1 for explaining a valid use case (where the remote user on host1 has no permissions on the destination server); for the important security caveat (use port forwarding -D rather than -A to get around network rather than key restrictions); for explaining the advantage; for the command being short; and it actually working. Note that you may need to specify username@host1 if it's different from local username. Also that rsync performs host key verification when connecting to host2, so host1's key should already be in ~/.ssh/known_hosts on host2 or the command will fail.

        – Cedric Knight
        Aug 21 '17 at 8:21






        +1 for explaining a valid use case (where the remote user on host1 has no permissions on the destination server); for the important security caveat (use port forwarding -D rather than -A to get around network rather than key restrictions); for explaining the advantage; for the command being short; and it actually working. Note that you may need to specify username@host1 if it's different from local username. Also that rsync performs host key verification when connecting to host2, so host1's key should already be in ~/.ssh/known_hosts on host2 or the command will fail.

        – Cedric Knight
        Aug 21 '17 at 8:21














        Phenomenal answer, this helped me orchestrate some things in TeamCity that I wasn't able to do before (n.b. for other TeamCity users, you must add the "Build Feature" called "SSH agent" to your build config before using ssh -A, see confluence.jetbrains.com/display/TCD10/SSH+Agent).

        – John Zwinck
        Mar 7 '18 at 10:15





        Phenomenal answer, this helped me orchestrate some things in TeamCity that I wasn't able to do before (n.b. for other TeamCity users, you must add the "Build Feature" called "SSH agent" to your build config before using ssh -A, see confluence.jetbrains.com/display/TCD10/SSH+Agent).

        – John Zwinck
        Mar 7 '18 at 10:15











        11














        I like roaima's answer, but the paths are the same in both examples, obscuring which is which. We've established that the following doesn't work:



        rsync -vuar host1:/host1/path host2:/host2/path


        But this does (I omitted the explicit bind_address of localhost from the -R option since that's the default):



        ssh -R 50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path'


        Note that you'll have to have ssh keys set up correctly between the two remote hosts, with the private key on host1 and the public key on host2.



        To debug the connection, break this into two parts and add verbose status:



        localhost$ ssh -v -R 50000:host2:22 host1


        If this works, you'll have a shell on host1. Now try the rsync command from host1. I recommend doing this in a different window so the verbose ssh info doesn't get mixed together with the rsync status info:



        host1$ rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path





        share|improve this answer

























        • In my example the paths are source, destination. The rsync is initiated on host1 with the target on host2. (You could have asked for clarification in a comment.)

          – roaima
          Jan 14 '16 at 0:12







        • 1





          I would have commented, but you can't comment on somebody else's post without a reputation of 50+.

          – jaybrau
          Jan 14 '16 at 15:20
















        11














        I like roaima's answer, but the paths are the same in both examples, obscuring which is which. We've established that the following doesn't work:



        rsync -vuar host1:/host1/path host2:/host2/path


        But this does (I omitted the explicit bind_address of localhost from the -R option since that's the default):



        ssh -R 50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path'


        Note that you'll have to have ssh keys set up correctly between the two remote hosts, with the private key on host1 and the public key on host2.



        To debug the connection, break this into two parts and add verbose status:



        localhost$ ssh -v -R 50000:host2:22 host1


        If this works, you'll have a shell on host1. Now try the rsync command from host1. I recommend doing this in a different window so the verbose ssh info doesn't get mixed together with the rsync status info:



        host1$ rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path





        share|improve this answer

























        • In my example the paths are source, destination. The rsync is initiated on host1 with the target on host2. (You could have asked for clarification in a comment.)

          – roaima
          Jan 14 '16 at 0:12







        • 1





          I would have commented, but you can't comment on somebody else's post without a reputation of 50+.

          – jaybrau
          Jan 14 '16 at 15:20














        11












        11








        11







        I like roaima's answer, but the paths are the same in both examples, obscuring which is which. We've established that the following doesn't work:



        rsync -vuar host1:/host1/path host2:/host2/path


        But this does (I omitted the explicit bind_address of localhost from the -R option since that's the default):



        ssh -R 50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path'


        Note that you'll have to have ssh keys set up correctly between the two remote hosts, with the private key on host1 and the public key on host2.



        To debug the connection, break this into two parts and add verbose status:



        localhost$ ssh -v -R 50000:host2:22 host1


        If this works, you'll have a shell on host1. Now try the rsync command from host1. I recommend doing this in a different window so the verbose ssh info doesn't get mixed together with the rsync status info:



        host1$ rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path





        share|improve this answer















        I like roaima's answer, but the paths are the same in both examples, obscuring which is which. We've established that the following doesn't work:



        rsync -vuar host1:/host1/path host2:/host2/path


        But this does (I omitted the explicit bind_address of localhost from the -R option since that's the default):



        ssh -R 50000:host2:22 host1 'rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path'


        Note that you'll have to have ssh keys set up correctly between the two remote hosts, with the private key on host1 and the public key on host2.



        To debug the connection, break this into two parts and add verbose status:



        localhost$ ssh -v -R 50000:host2:22 host1


        If this works, you'll have a shell on host1. Now try the rsync command from host1. I recommend doing this in a different window so the verbose ssh info doesn't get mixed together with the rsync status info:



        host1$ rsync -e "ssh -p 50000" -vuar /host1/path localhost:/host2/path






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 13 '16 at 16:42









        sam

        14.5k31627




        14.5k31627










        answered Jan 13 '16 at 16:28









        jaybraujaybrau

        211135




        211135












        • In my example the paths are source, destination. The rsync is initiated on host1 with the target on host2. (You could have asked for clarification in a comment.)

          – roaima
          Jan 14 '16 at 0:12







        • 1





          I would have commented, but you can't comment on somebody else's post without a reputation of 50+.

          – jaybrau
          Jan 14 '16 at 15:20


















        • In my example the paths are source, destination. The rsync is initiated on host1 with the target on host2. (You could have asked for clarification in a comment.)

          – roaima
          Jan 14 '16 at 0:12







        • 1





          I would have commented, but you can't comment on somebody else's post without a reputation of 50+.

          – jaybrau
          Jan 14 '16 at 15:20

















        In my example the paths are source, destination. The rsync is initiated on host1 with the target on host2. (You could have asked for clarification in a comment.)

        – roaima
        Jan 14 '16 at 0:12






        In my example the paths are source, destination. The rsync is initiated on host1 with the target on host2. (You could have asked for clarification in a comment.)

        – roaima
        Jan 14 '16 at 0:12





        1




        1





        I would have commented, but you can't comment on somebody else's post without a reputation of 50+.

        – jaybrau
        Jan 14 '16 at 15:20






        I would have commented, but you can't comment on somebody else's post without a reputation of 50+.

        – jaybrau
        Jan 14 '16 at 15:20












        5














        Reformatting answer by roaima in bash script syntax (and adding line continuation characters '' for clarity) I randomly picked port 22000...



        SOURCE_USER=user1
        SOURCE_HOST=hostname1
        SOURCE_PATH=path1

        TARGET_USER=user2
        TARGET_HOST=host2
        TARGET_PATH=path2

        ssh -l $TARGET_USER -A -R localhost:22000:$TARGET_HOST:22
        $SOURCE_USER@$SOURCE_HOST "rsync -e 'ssh -p 22000' -vuar $SOURCE_PATH
        $TARGET_USER@localhost:$TARGET_PATH"





        share|improve this answer


















        • 1





          it seems to me that all you've done is replace his arbitrary hostnames with variables?

          – Jeff Schaller
          Oct 20 '16 at 23:18






        • 2





          Yes, I did. It adds clarity for me on which is the source machine, which is the target, and where the source and target paths go. It took me awhile to work all that out and it wasn't obvious from simple placeholder hostnames.

          – David I.
          Oct 25 '16 at 15:23











        • Next time please feel free to improve someone else's answer directly by editing it.

          – roaima
          Apr 11 '17 at 7:30















        5














        Reformatting answer by roaima in bash script syntax (and adding line continuation characters '' for clarity) I randomly picked port 22000...



        SOURCE_USER=user1
        SOURCE_HOST=hostname1
        SOURCE_PATH=path1

        TARGET_USER=user2
        TARGET_HOST=host2
        TARGET_PATH=path2

        ssh -l $TARGET_USER -A -R localhost:22000:$TARGET_HOST:22
        $SOURCE_USER@$SOURCE_HOST "rsync -e 'ssh -p 22000' -vuar $SOURCE_PATH
        $TARGET_USER@localhost:$TARGET_PATH"





        share|improve this answer


















        • 1





          it seems to me that all you've done is replace his arbitrary hostnames with variables?

          – Jeff Schaller
          Oct 20 '16 at 23:18






        • 2





          Yes, I did. It adds clarity for me on which is the source machine, which is the target, and where the source and target paths go. It took me awhile to work all that out and it wasn't obvious from simple placeholder hostnames.

          – David I.
          Oct 25 '16 at 15:23











        • Next time please feel free to improve someone else's answer directly by editing it.

          – roaima
          Apr 11 '17 at 7:30













        5












        5








        5







        Reformatting answer by roaima in bash script syntax (and adding line continuation characters '' for clarity) I randomly picked port 22000...



        SOURCE_USER=user1
        SOURCE_HOST=hostname1
        SOURCE_PATH=path1

        TARGET_USER=user2
        TARGET_HOST=host2
        TARGET_PATH=path2

        ssh -l $TARGET_USER -A -R localhost:22000:$TARGET_HOST:22
        $SOURCE_USER@$SOURCE_HOST "rsync -e 'ssh -p 22000' -vuar $SOURCE_PATH
        $TARGET_USER@localhost:$TARGET_PATH"





        share|improve this answer













        Reformatting answer by roaima in bash script syntax (and adding line continuation characters '' for clarity) I randomly picked port 22000...



        SOURCE_USER=user1
        SOURCE_HOST=hostname1
        SOURCE_PATH=path1

        TARGET_USER=user2
        TARGET_HOST=host2
        TARGET_PATH=path2

        ssh -l $TARGET_USER -A -R localhost:22000:$TARGET_HOST:22
        $SOURCE_USER@$SOURCE_HOST "rsync -e 'ssh -p 22000' -vuar $SOURCE_PATH
        $TARGET_USER@localhost:$TARGET_PATH"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 20 '16 at 22:50









        David I.David I.

        16114




        16114







        • 1





          it seems to me that all you've done is replace his arbitrary hostnames with variables?

          – Jeff Schaller
          Oct 20 '16 at 23:18






        • 2





          Yes, I did. It adds clarity for me on which is the source machine, which is the target, and where the source and target paths go. It took me awhile to work all that out and it wasn't obvious from simple placeholder hostnames.

          – David I.
          Oct 25 '16 at 15:23











        • Next time please feel free to improve someone else's answer directly by editing it.

          – roaima
          Apr 11 '17 at 7:30












        • 1





          it seems to me that all you've done is replace his arbitrary hostnames with variables?

          – Jeff Schaller
          Oct 20 '16 at 23:18






        • 2





          Yes, I did. It adds clarity for me on which is the source machine, which is the target, and where the source and target paths go. It took me awhile to work all that out and it wasn't obvious from simple placeholder hostnames.

          – David I.
          Oct 25 '16 at 15:23











        • Next time please feel free to improve someone else's answer directly by editing it.

          – roaima
          Apr 11 '17 at 7:30







        1




        1





        it seems to me that all you've done is replace his arbitrary hostnames with variables?

        – Jeff Schaller
        Oct 20 '16 at 23:18





        it seems to me that all you've done is replace his arbitrary hostnames with variables?

        – Jeff Schaller
        Oct 20 '16 at 23:18




        2




        2





        Yes, I did. It adds clarity for me on which is the source machine, which is the target, and where the source and target paths go. It took me awhile to work all that out and it wasn't obvious from simple placeholder hostnames.

        – David I.
        Oct 25 '16 at 15:23





        Yes, I did. It adds clarity for me on which is the source machine, which is the target, and where the source and target paths go. It took me awhile to work all that out and it wasn't obvious from simple placeholder hostnames.

        – David I.
        Oct 25 '16 at 15:23













        Next time please feel free to improve someone else's answer directly by editing it.

        – roaima
        Apr 11 '17 at 7:30





        Next time please feel free to improve someone else's answer directly by editing it.

        – roaima
        Apr 11 '17 at 7:30











        3














        The Ideal way would be to run the rsync on one of those servers. But if you do not want to run a script on the remote server. You could run a script on your local system and do an ssh and execute the rsync there.



        ssh user@$host1 <<ENDSSH >> /tmp/rsync.out 2>&1
        rsync -vuar /var/www host2:/var/www
        ENDSSH



        Also, as you maybe aware rysnc does one way synchronization . If you'd like two way synchronization , you can look at osync (https://github.com/deajan/osync). I use it and found it to be helpful.






        share|improve this answer



























          3














          The Ideal way would be to run the rsync on one of those servers. But if you do not want to run a script on the remote server. You could run a script on your local system and do an ssh and execute the rsync there.



          ssh user@$host1 <<ENDSSH >> /tmp/rsync.out 2>&1
          rsync -vuar /var/www host2:/var/www
          ENDSSH



          Also, as you maybe aware rysnc does one way synchronization . If you'd like two way synchronization , you can look at osync (https://github.com/deajan/osync). I use it and found it to be helpful.






          share|improve this answer

























            3












            3








            3







            The Ideal way would be to run the rsync on one of those servers. But if you do not want to run a script on the remote server. You could run a script on your local system and do an ssh and execute the rsync there.



            ssh user@$host1 <<ENDSSH >> /tmp/rsync.out 2>&1
            rsync -vuar /var/www host2:/var/www
            ENDSSH



            Also, as you maybe aware rysnc does one way synchronization . If you'd like two way synchronization , you can look at osync (https://github.com/deajan/osync). I use it and found it to be helpful.






            share|improve this answer













            The Ideal way would be to run the rsync on one of those servers. But if you do not want to run a script on the remote server. You could run a script on your local system and do an ssh and execute the rsync there.



            ssh user@$host1 <<ENDSSH >> /tmp/rsync.out 2>&1
            rsync -vuar /var/www host2:/var/www
            ENDSSH



            Also, as you maybe aware rysnc does one way synchronization . If you'd like two way synchronization , you can look at osync (https://github.com/deajan/osync). I use it and found it to be helpful.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 7 '15 at 15:55









            rahulrahul

            1,025517




            1,025517





















                0














                Just as an additional info:



                If you use a jump host to connect the other two machines but they cannot reach each other directly, you can use sshfs as medium between these two machines like so (on the jump host):



                $ mkdir ~/sourcepath ~/destpath
                $ sshfs sourcehost:/target/dir ~/sourcepath
                $ sshfs desthost:/target/dir ~/destpath
                $ rsync -vua ~/sourcepath ~/desthpath


                SSHFS provides the two paths on the jump host and rsync manages the synchronisation of the files like always (just with the difference that it's virtually done locally).






                share|improve this answer


















                • 1





                  Note that the performance will be terrible. This is because to detect changes rsync will read the file from the source server, transferring the whole thing over the network. That being said if you can't do a direct transfer you will have to eat this if you are using rsync. Also don't mount the destination, it is unnecessary and will cause rsync to change some defaults because it thinks it is talking to a local disk.

                  – Kevin Cox
                  Aug 22 '16 at 18:47















                0














                Just as an additional info:



                If you use a jump host to connect the other two machines but they cannot reach each other directly, you can use sshfs as medium between these two machines like so (on the jump host):



                $ mkdir ~/sourcepath ~/destpath
                $ sshfs sourcehost:/target/dir ~/sourcepath
                $ sshfs desthost:/target/dir ~/destpath
                $ rsync -vua ~/sourcepath ~/desthpath


                SSHFS provides the two paths on the jump host and rsync manages the synchronisation of the files like always (just with the difference that it's virtually done locally).






                share|improve this answer


















                • 1





                  Note that the performance will be terrible. This is because to detect changes rsync will read the file from the source server, transferring the whole thing over the network. That being said if you can't do a direct transfer you will have to eat this if you are using rsync. Also don't mount the destination, it is unnecessary and will cause rsync to change some defaults because it thinks it is talking to a local disk.

                  – Kevin Cox
                  Aug 22 '16 at 18:47













                0












                0








                0







                Just as an additional info:



                If you use a jump host to connect the other two machines but they cannot reach each other directly, you can use sshfs as medium between these two machines like so (on the jump host):



                $ mkdir ~/sourcepath ~/destpath
                $ sshfs sourcehost:/target/dir ~/sourcepath
                $ sshfs desthost:/target/dir ~/destpath
                $ rsync -vua ~/sourcepath ~/desthpath


                SSHFS provides the two paths on the jump host and rsync manages the synchronisation of the files like always (just with the difference that it's virtually done locally).






                share|improve this answer













                Just as an additional info:



                If you use a jump host to connect the other two machines but they cannot reach each other directly, you can use sshfs as medium between these two machines like so (on the jump host):



                $ mkdir ~/sourcepath ~/destpath
                $ sshfs sourcehost:/target/dir ~/sourcepath
                $ sshfs desthost:/target/dir ~/destpath
                $ rsync -vua ~/sourcepath ~/desthpath


                SSHFS provides the two paths on the jump host and rsync manages the synchronisation of the files like always (just with the difference that it's virtually done locally).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 7 '16 at 13:25









                Roger LehmannRoger Lehmann

                1011




                1011







                • 1





                  Note that the performance will be terrible. This is because to detect changes rsync will read the file from the source server, transferring the whole thing over the network. That being said if you can't do a direct transfer you will have to eat this if you are using rsync. Also don't mount the destination, it is unnecessary and will cause rsync to change some defaults because it thinks it is talking to a local disk.

                  – Kevin Cox
                  Aug 22 '16 at 18:47












                • 1





                  Note that the performance will be terrible. This is because to detect changes rsync will read the file from the source server, transferring the whole thing over the network. That being said if you can't do a direct transfer you will have to eat this if you are using rsync. Also don't mount the destination, it is unnecessary and will cause rsync to change some defaults because it thinks it is talking to a local disk.

                  – Kevin Cox
                  Aug 22 '16 at 18:47







                1




                1





                Note that the performance will be terrible. This is because to detect changes rsync will read the file from the source server, transferring the whole thing over the network. That being said if you can't do a direct transfer you will have to eat this if you are using rsync. Also don't mount the destination, it is unnecessary and will cause rsync to change some defaults because it thinks it is talking to a local disk.

                – Kevin Cox
                Aug 22 '16 at 18:47





                Note that the performance will be terrible. This is because to detect changes rsync will read the file from the source server, transferring the whole thing over the network. That being said if you can't do a direct transfer you will have to eat this if you are using rsync. Also don't mount the destination, it is unnecessary and will cause rsync to change some defaults because it thinks it is talking to a local disk.

                – Kevin Cox
                Aug 22 '16 at 18:47











                0














                You could run an rsyncd (server) on one of the computers.



                This is the approach I'm taking, since I do not want to use ssh to allow the 'source' (in rsync wording) access to the 'destination' as root without a password (as is required to use SSH tunneling with rsync in a script)



                In my case, I simply set up an rsyncd server on the destination computer with a single user allowed from the source pc and used rsync from the source side.



                Works great.






                share|improve this answer





























                  0














                  You could run an rsyncd (server) on one of the computers.



                  This is the approach I'm taking, since I do not want to use ssh to allow the 'source' (in rsync wording) access to the 'destination' as root without a password (as is required to use SSH tunneling with rsync in a script)



                  In my case, I simply set up an rsyncd server on the destination computer with a single user allowed from the source pc and used rsync from the source side.



                  Works great.






                  share|improve this answer



























                    0












                    0








                    0







                    You could run an rsyncd (server) on one of the computers.



                    This is the approach I'm taking, since I do not want to use ssh to allow the 'source' (in rsync wording) access to the 'destination' as root without a password (as is required to use SSH tunneling with rsync in a script)



                    In my case, I simply set up an rsyncd server on the destination computer with a single user allowed from the source pc and used rsync from the source side.



                    Works great.






                    share|improve this answer















                    You could run an rsyncd (server) on one of the computers.



                    This is the approach I'm taking, since I do not want to use ssh to allow the 'source' (in rsync wording) access to the 'destination' as root without a password (as is required to use SSH tunneling with rsync in a script)



                    In my case, I simply set up an rsyncd server on the destination computer with a single user allowed from the source pc and used rsync from the source side.



                    Works great.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 21 '16 at 1:18









                    Wildcard

                    23.3k1067171




                    23.3k1067171










                    answered Feb 19 '16 at 23:10









                    RustyCarRustyCar

                    343




                    343





















                        0














                        Try using this. It works for me.



                        ssh src_user@src_host 'rsync -av /src/dir/location/ dest_user@dest_host:/dest/dir/loc/'





                        share|improve this answer



























                          0














                          Try using this. It works for me.



                          ssh src_user@src_host 'rsync -av /src/dir/location/ dest_user@dest_host:/dest/dir/loc/'





                          share|improve this answer

























                            0












                            0








                            0







                            Try using this. It works for me.



                            ssh src_user@src_host 'rsync -av /src/dir/location/ dest_user@dest_host:/dest/dir/loc/'





                            share|improve this answer













                            Try using this. It works for me.



                            ssh src_user@src_host 'rsync -av /src/dir/location/ dest_user@dest_host:/dest/dir/loc/'






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 28 '18 at 19:24









                            Nikhil PandeyNikhil Pandey

                            11




                            11





















                                0














                                An easy to use script



                                Over the years I've done this many times with more or less the same tricks as in every other answer here. However because it's very easy to get some detail wrong and spend a lot of time figuring out the issue I've come up with the script below that:



                                1. Makes it easy to specify all the details (source, destination, options)

                                2. Incrementally tests every single step and gives feedback if anything goes wrong, so that you know what to fix.

                                3. Works around cases where ssh -A fails to propagate authentication data (don't know why this happens sometimes since the workaround was easier than finding the root cause)

                                4. Finally does the job.

                                How to use the script



                                1. Make sure you can ssh to both hosts from localhost without typing a
                                  password.

                                2. Set the variables in the first few lines of the script

                                3. Execute it.

                                How it works



                                As I said it uses the same tricks as in every other answer here:



                                • ssh's -R option to ssh from localhost to host1 while at the same time setting up a port forwarding that then allows host1 to connect via localhost to host2 (-R localhost:$FREE_PORT:$TARGET_ADDR_PORT)

                                • ssh's -A option to allow easy authenticaion of the second ssh chanel

                                My this IS complicated! Is there any easier way?



                                When copying all or most bytes from source to destination it's FAR easier to use tar:



                                ssh $SOURCE_HOST "tar czf - $SOURCE_PATH" 
                                | ssh $TARGET_HOST "tar xzf - -C $TARGET_PATH/"


                                The script



                                #!/bin/bash
                                #-------------------SET EVERYTHING BELOW-------------------
                                # whatever you type after ssh to connect to SOURCE/TARGE host
                                # (e.g. 1.2.3.4:22, user@host:22000, ssh_config_alias, etc)
                                # So if you use "ssh foo" to connect to SOURCE then
                                # you must set SOURCE_HOST=foo
                                SOURCE_HOST=host1
                                TARGET_HOST=host2
                                # The IP address or hostname and ssh port of TARGET AS SEEN FROM LOCALHOST
                                # So if ssh -p 5678 someuser@1.2.3.4 will connect you to TARGET then
                                # you must set TARGET_ADDR_PORT=1.2.3.4:5678 and
                                # you must set TARGET_USER=someuser
                                TARGET_ADDR_PORT=1.2.3.4:5678
                                TARGET_USER=someuser

                                SOURCE_PATH=/mnt/foo # Path to rsync FROM
                                TARGET_PATH=/mnt/bar # Path to rsync TO

                                RSYNC_OPTS="-av --bwlimit=14M --progress" # rsync options
                                FREE_PORT=54321 # just a free TCP port on localhost
                                #---------------------------------------------------------

                                echo -n "Test: ssh to $TARGET_HOST: "
                                ssh $TARGET_HOST echo PASSED| grep PASSED || exit 2

                                echo -n "Test: ssh to $SOURCE_HOST: "
                                ssh $SOURCE_HOST echo PASSED| grep PASSED || exit 3

                                echo -n "Verifying path in $SOURCE_HOST "
                                ssh $SOURCE_HOST stat $SOURCE_PATH | grep "File:" || exit 5

                                echo -n "Verifying path in $TARGET_HOST "
                                ssh $TARGET_HOST stat $TARGET_PATH | grep "File:" || exit 5

                                echo "configuring ssh from $SOURCE_HOST to $TARGET_HOST via locahost"
                                ssh $SOURCE_HOST "echo "Host tmpsshrs; ControlMaster auto; ControlPath /tmp/%u_%r@%h:%p; hostname localhost; port $FREE_PORT; user $TARGET_USER" | tr ';' 'n' > /tmp/tmpsshrs"

                                # The ssh options that will setup the tunnel
                                TUNNEL="-R localhost:$FREE_PORT:$TARGET_ADDR_PORT"

                                echo
                                echo -n "Test: ssh to $SOURCE_HOST then to $TARGET_HOST: "
                                if ! ssh -A $TUNNEL $SOURCE_HOST "ssh -A -F /tmp/tmpsshrs tmpsshrs echo PASSED" | grep PASSED ; then
                                echo
                                echo "Direct authentication failed, will use plan #B:"
                                echo "Please open another terminal, execute the following command"
                                echo "and leave the session running until rsync finishes"
                                echo "(if you're asked for password use the one for $TARGET_USER@$TARGET_HOST)"
                                echo " ssh -t -A $TUNNEL $SOURCE_HOST ssh -F /tmp/tmpsshrs tmpsshrs"
                                read -p "Press [Enter] when done..."
                                fi

                                echo "Starting rsync"
                                ssh -A $TUNNEL $SOURCE_HOST "rsync -e 'ssh -F /tmp/tmpsshrs' $RSYNC_OPTS $SOURCE_PATH tmpsshrs:$TARGET_PATH"

                                echo
                                echo "Cleaning up"
                                ssh $SOURCE_HOST "rm /tmp/tmpsshrs"





                                share|improve this answer

























                                • tar is great when you've got a single (non-incremental) transfer and your transfer completes in a single pass. On the other hand, rsync with forwarding handles restarts and incremental transfers.

                                  – roaima
                                  yesterday











                                • Of course @roaima -- I don't consider tar equivalent. I left this passing reference for the day when I will be reading this to solve a problem where rsync will not 100% necessary.

                                  – ndemou
                                  yesterday















                                0














                                An easy to use script



                                Over the years I've done this many times with more or less the same tricks as in every other answer here. However because it's very easy to get some detail wrong and spend a lot of time figuring out the issue I've come up with the script below that:



                                1. Makes it easy to specify all the details (source, destination, options)

                                2. Incrementally tests every single step and gives feedback if anything goes wrong, so that you know what to fix.

                                3. Works around cases where ssh -A fails to propagate authentication data (don't know why this happens sometimes since the workaround was easier than finding the root cause)

                                4. Finally does the job.

                                How to use the script



                                1. Make sure you can ssh to both hosts from localhost without typing a
                                  password.

                                2. Set the variables in the first few lines of the script

                                3. Execute it.

                                How it works



                                As I said it uses the same tricks as in every other answer here:



                                • ssh's -R option to ssh from localhost to host1 while at the same time setting up a port forwarding that then allows host1 to connect via localhost to host2 (-R localhost:$FREE_PORT:$TARGET_ADDR_PORT)

                                • ssh's -A option to allow easy authenticaion of the second ssh chanel

                                My this IS complicated! Is there any easier way?



                                When copying all or most bytes from source to destination it's FAR easier to use tar:



                                ssh $SOURCE_HOST "tar czf - $SOURCE_PATH" 
                                | ssh $TARGET_HOST "tar xzf - -C $TARGET_PATH/"


                                The script



                                #!/bin/bash
                                #-------------------SET EVERYTHING BELOW-------------------
                                # whatever you type after ssh to connect to SOURCE/TARGE host
                                # (e.g. 1.2.3.4:22, user@host:22000, ssh_config_alias, etc)
                                # So if you use "ssh foo" to connect to SOURCE then
                                # you must set SOURCE_HOST=foo
                                SOURCE_HOST=host1
                                TARGET_HOST=host2
                                # The IP address or hostname and ssh port of TARGET AS SEEN FROM LOCALHOST
                                # So if ssh -p 5678 someuser@1.2.3.4 will connect you to TARGET then
                                # you must set TARGET_ADDR_PORT=1.2.3.4:5678 and
                                # you must set TARGET_USER=someuser
                                TARGET_ADDR_PORT=1.2.3.4:5678
                                TARGET_USER=someuser

                                SOURCE_PATH=/mnt/foo # Path to rsync FROM
                                TARGET_PATH=/mnt/bar # Path to rsync TO

                                RSYNC_OPTS="-av --bwlimit=14M --progress" # rsync options
                                FREE_PORT=54321 # just a free TCP port on localhost
                                #---------------------------------------------------------

                                echo -n "Test: ssh to $TARGET_HOST: "
                                ssh $TARGET_HOST echo PASSED| grep PASSED || exit 2

                                echo -n "Test: ssh to $SOURCE_HOST: "
                                ssh $SOURCE_HOST echo PASSED| grep PASSED || exit 3

                                echo -n "Verifying path in $SOURCE_HOST "
                                ssh $SOURCE_HOST stat $SOURCE_PATH | grep "File:" || exit 5

                                echo -n "Verifying path in $TARGET_HOST "
                                ssh $TARGET_HOST stat $TARGET_PATH | grep "File:" || exit 5

                                echo "configuring ssh from $SOURCE_HOST to $TARGET_HOST via locahost"
                                ssh $SOURCE_HOST "echo "Host tmpsshrs; ControlMaster auto; ControlPath /tmp/%u_%r@%h:%p; hostname localhost; port $FREE_PORT; user $TARGET_USER" | tr ';' 'n' > /tmp/tmpsshrs"

                                # The ssh options that will setup the tunnel
                                TUNNEL="-R localhost:$FREE_PORT:$TARGET_ADDR_PORT"

                                echo
                                echo -n "Test: ssh to $SOURCE_HOST then to $TARGET_HOST: "
                                if ! ssh -A $TUNNEL $SOURCE_HOST "ssh -A -F /tmp/tmpsshrs tmpsshrs echo PASSED" | grep PASSED ; then
                                echo
                                echo "Direct authentication failed, will use plan #B:"
                                echo "Please open another terminal, execute the following command"
                                echo "and leave the session running until rsync finishes"
                                echo "(if you're asked for password use the one for $TARGET_USER@$TARGET_HOST)"
                                echo " ssh -t -A $TUNNEL $SOURCE_HOST ssh -F /tmp/tmpsshrs tmpsshrs"
                                read -p "Press [Enter] when done..."
                                fi

                                echo "Starting rsync"
                                ssh -A $TUNNEL $SOURCE_HOST "rsync -e 'ssh -F /tmp/tmpsshrs' $RSYNC_OPTS $SOURCE_PATH tmpsshrs:$TARGET_PATH"

                                echo
                                echo "Cleaning up"
                                ssh $SOURCE_HOST "rm /tmp/tmpsshrs"





                                share|improve this answer

























                                • tar is great when you've got a single (non-incremental) transfer and your transfer completes in a single pass. On the other hand, rsync with forwarding handles restarts and incremental transfers.

                                  – roaima
                                  yesterday











                                • Of course @roaima -- I don't consider tar equivalent. I left this passing reference for the day when I will be reading this to solve a problem where rsync will not 100% necessary.

                                  – ndemou
                                  yesterday













                                0












                                0








                                0







                                An easy to use script



                                Over the years I've done this many times with more or less the same tricks as in every other answer here. However because it's very easy to get some detail wrong and spend a lot of time figuring out the issue I've come up with the script below that:



                                1. Makes it easy to specify all the details (source, destination, options)

                                2. Incrementally tests every single step and gives feedback if anything goes wrong, so that you know what to fix.

                                3. Works around cases where ssh -A fails to propagate authentication data (don't know why this happens sometimes since the workaround was easier than finding the root cause)

                                4. Finally does the job.

                                How to use the script



                                1. Make sure you can ssh to both hosts from localhost without typing a
                                  password.

                                2. Set the variables in the first few lines of the script

                                3. Execute it.

                                How it works



                                As I said it uses the same tricks as in every other answer here:



                                • ssh's -R option to ssh from localhost to host1 while at the same time setting up a port forwarding that then allows host1 to connect via localhost to host2 (-R localhost:$FREE_PORT:$TARGET_ADDR_PORT)

                                • ssh's -A option to allow easy authenticaion of the second ssh chanel

                                My this IS complicated! Is there any easier way?



                                When copying all or most bytes from source to destination it's FAR easier to use tar:



                                ssh $SOURCE_HOST "tar czf - $SOURCE_PATH" 
                                | ssh $TARGET_HOST "tar xzf - -C $TARGET_PATH/"


                                The script



                                #!/bin/bash
                                #-------------------SET EVERYTHING BELOW-------------------
                                # whatever you type after ssh to connect to SOURCE/TARGE host
                                # (e.g. 1.2.3.4:22, user@host:22000, ssh_config_alias, etc)
                                # So if you use "ssh foo" to connect to SOURCE then
                                # you must set SOURCE_HOST=foo
                                SOURCE_HOST=host1
                                TARGET_HOST=host2
                                # The IP address or hostname and ssh port of TARGET AS SEEN FROM LOCALHOST
                                # So if ssh -p 5678 someuser@1.2.3.4 will connect you to TARGET then
                                # you must set TARGET_ADDR_PORT=1.2.3.4:5678 and
                                # you must set TARGET_USER=someuser
                                TARGET_ADDR_PORT=1.2.3.4:5678
                                TARGET_USER=someuser

                                SOURCE_PATH=/mnt/foo # Path to rsync FROM
                                TARGET_PATH=/mnt/bar # Path to rsync TO

                                RSYNC_OPTS="-av --bwlimit=14M --progress" # rsync options
                                FREE_PORT=54321 # just a free TCP port on localhost
                                #---------------------------------------------------------

                                echo -n "Test: ssh to $TARGET_HOST: "
                                ssh $TARGET_HOST echo PASSED| grep PASSED || exit 2

                                echo -n "Test: ssh to $SOURCE_HOST: "
                                ssh $SOURCE_HOST echo PASSED| grep PASSED || exit 3

                                echo -n "Verifying path in $SOURCE_HOST "
                                ssh $SOURCE_HOST stat $SOURCE_PATH | grep "File:" || exit 5

                                echo -n "Verifying path in $TARGET_HOST "
                                ssh $TARGET_HOST stat $TARGET_PATH | grep "File:" || exit 5

                                echo "configuring ssh from $SOURCE_HOST to $TARGET_HOST via locahost"
                                ssh $SOURCE_HOST "echo "Host tmpsshrs; ControlMaster auto; ControlPath /tmp/%u_%r@%h:%p; hostname localhost; port $FREE_PORT; user $TARGET_USER" | tr ';' 'n' > /tmp/tmpsshrs"

                                # The ssh options that will setup the tunnel
                                TUNNEL="-R localhost:$FREE_PORT:$TARGET_ADDR_PORT"

                                echo
                                echo -n "Test: ssh to $SOURCE_HOST then to $TARGET_HOST: "
                                if ! ssh -A $TUNNEL $SOURCE_HOST "ssh -A -F /tmp/tmpsshrs tmpsshrs echo PASSED" | grep PASSED ; then
                                echo
                                echo "Direct authentication failed, will use plan #B:"
                                echo "Please open another terminal, execute the following command"
                                echo "and leave the session running until rsync finishes"
                                echo "(if you're asked for password use the one for $TARGET_USER@$TARGET_HOST)"
                                echo " ssh -t -A $TUNNEL $SOURCE_HOST ssh -F /tmp/tmpsshrs tmpsshrs"
                                read -p "Press [Enter] when done..."
                                fi

                                echo "Starting rsync"
                                ssh -A $TUNNEL $SOURCE_HOST "rsync -e 'ssh -F /tmp/tmpsshrs' $RSYNC_OPTS $SOURCE_PATH tmpsshrs:$TARGET_PATH"

                                echo
                                echo "Cleaning up"
                                ssh $SOURCE_HOST "rm /tmp/tmpsshrs"





                                share|improve this answer















                                An easy to use script



                                Over the years I've done this many times with more or less the same tricks as in every other answer here. However because it's very easy to get some detail wrong and spend a lot of time figuring out the issue I've come up with the script below that:



                                1. Makes it easy to specify all the details (source, destination, options)

                                2. Incrementally tests every single step and gives feedback if anything goes wrong, so that you know what to fix.

                                3. Works around cases where ssh -A fails to propagate authentication data (don't know why this happens sometimes since the workaround was easier than finding the root cause)

                                4. Finally does the job.

                                How to use the script



                                1. Make sure you can ssh to both hosts from localhost without typing a
                                  password.

                                2. Set the variables in the first few lines of the script

                                3. Execute it.

                                How it works



                                As I said it uses the same tricks as in every other answer here:



                                • ssh's -R option to ssh from localhost to host1 while at the same time setting up a port forwarding that then allows host1 to connect via localhost to host2 (-R localhost:$FREE_PORT:$TARGET_ADDR_PORT)

                                • ssh's -A option to allow easy authenticaion of the second ssh chanel

                                My this IS complicated! Is there any easier way?



                                When copying all or most bytes from source to destination it's FAR easier to use tar:



                                ssh $SOURCE_HOST "tar czf - $SOURCE_PATH" 
                                | ssh $TARGET_HOST "tar xzf - -C $TARGET_PATH/"


                                The script



                                #!/bin/bash
                                #-------------------SET EVERYTHING BELOW-------------------
                                # whatever you type after ssh to connect to SOURCE/TARGE host
                                # (e.g. 1.2.3.4:22, user@host:22000, ssh_config_alias, etc)
                                # So if you use "ssh foo" to connect to SOURCE then
                                # you must set SOURCE_HOST=foo
                                SOURCE_HOST=host1
                                TARGET_HOST=host2
                                # The IP address or hostname and ssh port of TARGET AS SEEN FROM LOCALHOST
                                # So if ssh -p 5678 someuser@1.2.3.4 will connect you to TARGET then
                                # you must set TARGET_ADDR_PORT=1.2.3.4:5678 and
                                # you must set TARGET_USER=someuser
                                TARGET_ADDR_PORT=1.2.3.4:5678
                                TARGET_USER=someuser

                                SOURCE_PATH=/mnt/foo # Path to rsync FROM
                                TARGET_PATH=/mnt/bar # Path to rsync TO

                                RSYNC_OPTS="-av --bwlimit=14M --progress" # rsync options
                                FREE_PORT=54321 # just a free TCP port on localhost
                                #---------------------------------------------------------

                                echo -n "Test: ssh to $TARGET_HOST: "
                                ssh $TARGET_HOST echo PASSED| grep PASSED || exit 2

                                echo -n "Test: ssh to $SOURCE_HOST: "
                                ssh $SOURCE_HOST echo PASSED| grep PASSED || exit 3

                                echo -n "Verifying path in $SOURCE_HOST "
                                ssh $SOURCE_HOST stat $SOURCE_PATH | grep "File:" || exit 5

                                echo -n "Verifying path in $TARGET_HOST "
                                ssh $TARGET_HOST stat $TARGET_PATH | grep "File:" || exit 5

                                echo "configuring ssh from $SOURCE_HOST to $TARGET_HOST via locahost"
                                ssh $SOURCE_HOST "echo "Host tmpsshrs; ControlMaster auto; ControlPath /tmp/%u_%r@%h:%p; hostname localhost; port $FREE_PORT; user $TARGET_USER" | tr ';' 'n' > /tmp/tmpsshrs"

                                # The ssh options that will setup the tunnel
                                TUNNEL="-R localhost:$FREE_PORT:$TARGET_ADDR_PORT"

                                echo
                                echo -n "Test: ssh to $SOURCE_HOST then to $TARGET_HOST: "
                                if ! ssh -A $TUNNEL $SOURCE_HOST "ssh -A -F /tmp/tmpsshrs tmpsshrs echo PASSED" | grep PASSED ; then
                                echo
                                echo "Direct authentication failed, will use plan #B:"
                                echo "Please open another terminal, execute the following command"
                                echo "and leave the session running until rsync finishes"
                                echo "(if you're asked for password use the one for $TARGET_USER@$TARGET_HOST)"
                                echo " ssh -t -A $TUNNEL $SOURCE_HOST ssh -F /tmp/tmpsshrs tmpsshrs"
                                read -p "Press [Enter] when done..."
                                fi

                                echo "Starting rsync"
                                ssh -A $TUNNEL $SOURCE_HOST "rsync -e 'ssh -F /tmp/tmpsshrs' $RSYNC_OPTS $SOURCE_PATH tmpsshrs:$TARGET_PATH"

                                echo
                                echo "Cleaning up"
                                ssh $SOURCE_HOST "rm /tmp/tmpsshrs"






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited yesterday

























                                answered 2 days ago









                                ndemoundemou

                                616717




                                616717












                                • tar is great when you've got a single (non-incremental) transfer and your transfer completes in a single pass. On the other hand, rsync with forwarding handles restarts and incremental transfers.

                                  – roaima
                                  yesterday











                                • Of course @roaima -- I don't consider tar equivalent. I left this passing reference for the day when I will be reading this to solve a problem where rsync will not 100% necessary.

                                  – ndemou
                                  yesterday

















                                • tar is great when you've got a single (non-incremental) transfer and your transfer completes in a single pass. On the other hand, rsync with forwarding handles restarts and incremental transfers.

                                  – roaima
                                  yesterday











                                • Of course @roaima -- I don't consider tar equivalent. I left this passing reference for the day when I will be reading this to solve a problem where rsync will not 100% necessary.

                                  – ndemou
                                  yesterday
















                                tar is great when you've got a single (non-incremental) transfer and your transfer completes in a single pass. On the other hand, rsync with forwarding handles restarts and incremental transfers.

                                – roaima
                                yesterday





                                tar is great when you've got a single (non-incremental) transfer and your transfer completes in a single pass. On the other hand, rsync with forwarding handles restarts and incremental transfers.

                                – roaima
                                yesterday













                                Of course @roaima -- I don't consider tar equivalent. I left this passing reference for the day when I will be reading this to solve a problem where rsync will not 100% necessary.

                                – ndemou
                                yesterday





                                Of course @roaima -- I don't consider tar equivalent. I left this passing reference for the day when I will be reading this to solve a problem where rsync will not 100% necessary.

                                – ndemou
                                yesterday











                                -1














                                It's possible to use tar via ssh to transfer the files:



                                ssh -n user1@host1 'tar jcf - -C /var/www .' | ssh user2@host2 'tar jxvf - -C /var/www'


                                Change j parameter (for tar) to z in two places if you wish to compress the archive with gzip, instead of bzip2. Usually bzip2 has the higher compression than gzip, but it's slower, so change it depending on your needs (see: bzip2 vs gzip).



                                Related: How to copy between two remote hosts using tar piped into SSH from remote server when behind a firewall?




                                Alternatively (to safe the bandwidth, because of the transparent compression) it's possible to use sshfs to mount remote file-system as local and use rsync as usual, e.g.



                                $ sshfs user1@host1:/var/www /mnt
                                $ rsync -vuar /mnt user2@host2:/var/www





                                share|improve this answer




















                                • 1





                                  Mounting the filesystem locally wont save any bandwidth between the source and local machine. It will however save bandwidth between the local and destination (as it isn't mounted locally). Using rsync this way only makes sense if you are trying to save bandwidth on the destination.

                                  – Kevin Cox
                                  Dec 3 '15 at 14:31















                                -1














                                It's possible to use tar via ssh to transfer the files:



                                ssh -n user1@host1 'tar jcf - -C /var/www .' | ssh user2@host2 'tar jxvf - -C /var/www'


                                Change j parameter (for tar) to z in two places if you wish to compress the archive with gzip, instead of bzip2. Usually bzip2 has the higher compression than gzip, but it's slower, so change it depending on your needs (see: bzip2 vs gzip).



                                Related: How to copy between two remote hosts using tar piped into SSH from remote server when behind a firewall?




                                Alternatively (to safe the bandwidth, because of the transparent compression) it's possible to use sshfs to mount remote file-system as local and use rsync as usual, e.g.



                                $ sshfs user1@host1:/var/www /mnt
                                $ rsync -vuar /mnt user2@host2:/var/www





                                share|improve this answer




















                                • 1





                                  Mounting the filesystem locally wont save any bandwidth between the source and local machine. It will however save bandwidth between the local and destination (as it isn't mounted locally). Using rsync this way only makes sense if you are trying to save bandwidth on the destination.

                                  – Kevin Cox
                                  Dec 3 '15 at 14:31













                                -1












                                -1








                                -1







                                It's possible to use tar via ssh to transfer the files:



                                ssh -n user1@host1 'tar jcf - -C /var/www .' | ssh user2@host2 'tar jxvf - -C /var/www'


                                Change j parameter (for tar) to z in two places if you wish to compress the archive with gzip, instead of bzip2. Usually bzip2 has the higher compression than gzip, but it's slower, so change it depending on your needs (see: bzip2 vs gzip).



                                Related: How to copy between two remote hosts using tar piped into SSH from remote server when behind a firewall?




                                Alternatively (to safe the bandwidth, because of the transparent compression) it's possible to use sshfs to mount remote file-system as local and use rsync as usual, e.g.



                                $ sshfs user1@host1:/var/www /mnt
                                $ rsync -vuar /mnt user2@host2:/var/www





                                share|improve this answer















                                It's possible to use tar via ssh to transfer the files:



                                ssh -n user1@host1 'tar jcf - -C /var/www .' | ssh user2@host2 'tar jxvf - -C /var/www'


                                Change j parameter (for tar) to z in two places if you wish to compress the archive with gzip, instead of bzip2. Usually bzip2 has the higher compression than gzip, but it's slower, so change it depending on your needs (see: bzip2 vs gzip).



                                Related: How to copy between two remote hosts using tar piped into SSH from remote server when behind a firewall?




                                Alternatively (to safe the bandwidth, because of the transparent compression) it's possible to use sshfs to mount remote file-system as local and use rsync as usual, e.g.



                                $ sshfs user1@host1:/var/www /mnt
                                $ rsync -vuar /mnt user2@host2:/var/www






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Apr 13 '17 at 12:37









                                Community

                                1




                                1










                                answered Feb 7 '15 at 16:52









                                kenorbkenorb

                                9,041374112




                                9,041374112







                                • 1





                                  Mounting the filesystem locally wont save any bandwidth between the source and local machine. It will however save bandwidth between the local and destination (as it isn't mounted locally). Using rsync this way only makes sense if you are trying to save bandwidth on the destination.

                                  – Kevin Cox
                                  Dec 3 '15 at 14:31












                                • 1





                                  Mounting the filesystem locally wont save any bandwidth between the source and local machine. It will however save bandwidth between the local and destination (as it isn't mounted locally). Using rsync this way only makes sense if you are trying to save bandwidth on the destination.

                                  – Kevin Cox
                                  Dec 3 '15 at 14:31







                                1




                                1





                                Mounting the filesystem locally wont save any bandwidth between the source and local machine. It will however save bandwidth between the local and destination (as it isn't mounted locally). Using rsync this way only makes sense if you are trying to save bandwidth on the destination.

                                – Kevin Cox
                                Dec 3 '15 at 14:31





                                Mounting the filesystem locally wont save any bandwidth between the source and local machine. It will however save bandwidth between the local and destination (as it isn't mounted locally). Using rsync this way only makes sense if you are trying to save bandwidth on the destination.

                                – Kevin Cox
                                Dec 3 '15 at 14:31

















                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f183504%2fhow-to-rsync-files-between-two-remotes%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                                Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                                NetworkManager fails with “Could not find source connection”Trouble connecting to VPN using network-manager, while command line worksHow can I be notified about state changes to a VPN adapterBacktrack 5 R3 - Refuses to connect to VPNFeed all traffic through OpenVPN for a specific network namespace onlyRun daemon on startup in Debian once openvpn connection establishedpfsense tcp connection between openvpn and lan is brokenInternet connection problem with web browsers onlyWhy does NetworkManager explicitly support tun/tap devices?Browser issues with VPNTwo IP addresses assigned to the same network card - OpenVPN issues?Cannot connect to WiFi with nmcli, although secrets are provided