Monday, November 28, 2011

Unix: Using ssh & tar to copy files/directories from or to remote hosts

Continuing the post Using Standard Input & Output With Tar: Copying File & Directory Trees, we cover the how-to of copying files over from or to a remote host using SSH and tar.

Suppose we wish to copy files/directories between these two hosts i.e. local & remote host. The remote host must have a SSH-server running while the local host should have a SSH-client.

There are two scenarios we will consider:
  1. copying from remote host to local host - download
  2. copying form local host to remote host - upload
In both scenarios, one typically employs scp to copy a bunch of files and directories (made into a tar-ball) remotely over an ssh connection. The disadvantages to this method are:
- If you interrupt the scp process of the tar file, you cannot access any of the files or portion of the tar-ball already copied.  All you get is a partially copied tar file.
- You have the additional step of creating the tar file prior to the actual copying process.

A better way of copying files between 2 machines is by exploiting ssh's ability to execute a remote command. This is much like what rsh is able to do.


To copy files/directories from remote host to local host, enter the command:

ssh -C user@host 'tar -cvf - <space_separated_list_of_files_&_directories> ' |  tar -xvf -

or

ssh -C user@host 'cd <directory>; tar -cvf - .' |  tar -xvf -

To copy everything from current directory in local host to remote host, use the command:

tar -cvf  -  .  |  ssh  -C  user@host  'cd <directory>; tar -xvf  -'


-C  option given for SSH, turns on compression for quicker transfer of data over the SSH tunnel

No comments:

Post a Comment