Friday, October 10, 2014

Using tar on the fly to efficiently transfer file over ssh (wondertar)

Have you been in the situation where you want to transfer a big file, and decided to tar it before transferring but being limited by the disk space available on the machine?

Well, worry no more as I will show you how you can do a tar on the fly while ssh'ing, to overcome that limitation.

Method 1:

ssh foo@machine-to-keep-the-data "tar czpf - /data/to/be/transferred" | tar xzpf - -C /the/data/new/place

What this command will do is to create a tar file (tar czpf), and untar it at the other side of the ssh (tar xvpf) command, where c is for create tar, z is to use gzip, p is for preserving permission, f is for file which is to be zipped, - is for stdin or stdout and x is for extract


Method 2:

tar cpf - /data/to/be/transferred | ssh foo@machine-to-keep-the-data tar xpf - -C /the/data/new/place" 

This command will tar the file, and untar it at the other end, same as above, but just different command arrangement


Method 3 (this is useful if you want to tar it. and keep it that way on the other end, without untarring it):

tar czf - -C /data/to/be/transferred | ssh foo@machine-to-keep-the-data "cat - > /the/data/new/place/backup.tar.gz"


Method 4 (add pv to the middle of the pipes to monitor the transfer speed):

sudo apt-get install pv; ssh foo@machine-to-keep-the-data "tar czpf - /data/to/be/transferred" | pv | tar xzpf - -C /the/data/new/place



That's all, hope you will find these useful.


No comments: