Tar Over Ssh

3.4.3 tar-over-ssh
As mentioned in section 3.3.1, ssh can be used as transport to redirect input and output between
hosts. This ability makes it easy to transfer files using standard unix archiving utilities like tar
and cpio. These have advantages when you need to transfer a large numbers of file, preserve file
attributes exactly and copy hard or symbolic links.
The following example will copy all files and directories from /usr/share/games on host hachi
to /tmp on the local machine. Note that this will preserve the directory structure and attributes
including utimes, owner, group and permission information.
[root@roku root]# ssh hachi "cd /usr/share/games ; tar cf - ./a*" | > (cd /tmp ; tar xpvf -)
To copy local files to a remote destination, a symmetrical command may be used:
(cd /tmp ; tar cf - ./xyz*) | ssh hachi "cd /tmp ; tar xcvf -"
A slight modification to the above example makes it easy to obtain a local tar file of a remote set
of files (note the extra compression step):
ssh hachi "cd /tmp ; tar cvf - ./* | bzip2 -9" > tmp.tar.bz2
This technique is very useful for simple unattended backups, once password-less authentication
has been configured (section 4).


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License