Howto: Use tar command through network over ssh session

Q. How do I use tar command over secure ssh session?

A. The GNU version of the tar archiving utility (and other old version of tar) can be use through network over ssh session. Do not use telnet command, it is insecure. You can use Unix/Linux pipes to create actives. Following command backups /wwwdata directory to dumpserver.nixcraft.in (IP 192.168.1.201) host over ssh session.

The default first SCSI tape drive under Linux is /dev/st0. You can read more about tape drives naming convention used under Linux here.

# tar zcvf - /wwwdata | ssh root@dumpserver.nixcraft.in "cat > /backup/wwwdata.tar.gz"OR# tar zcvf - /wwwdata | ssh root@192.168.1.201 "cat > /backup/wwwdata.tar.gz"

Output:

tar: Removing leading `/' from member names
/wwwdata/
/wwwdata/n/nixcraft.in/
/wwwdata/c/cyberciti.biz/
....
..
...
Password:

You can also use dd command for clarity purpose:# tar cvzf - /wwwdata | ssh ssh root@192.168.1.201 "dd of=/backup/wwwdata.tar.gz"It is also possible to dump backup to remote tape device:# tar cvzf - /wwwdata | ssh ssh root@192.168.1.201 "cat > /dev/nst0"OR you can use mt to rewind tape and then dump it using cat command:# tar cvzf - /wwwdata | ssh ssh root@192.168.1.201 $(mt -f /dev/nst0 rewind; cat > /dev/nst0)$You can restore tar backup over ssh session: # cd /
# ssh root@192.168.1.201 "cat /backup/wwwdata.tar.gz" | tar zxvf -
If you wish to use above command in cron job or scripts then consider SSH keys to get rid of the passwords.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 13 comments… read them below or add one }

1 Daniel K 10.26.06 at 7:02 am

Why use the ssh command twice, or is that a typo?

2 nixcraft 10.26.06 at 8:06 am

First one is with hostname and second one is with IP address.

3 mike 04.05.07 at 11:57 am

ssh ssh root@192.168.1.201 “cat /backup/wwwdata.tar.gz” | tar zxvf –

why use the ssh twice here? (I believe this was the original question, too.

4 nixcraft 04.05.07 at 12:58 pm

Daniel/Mike,

That was a typo. Thanks for heads up!

5 tafadzwa 10.02.07 at 2:06 pm

what is SQUID

6 Vincent 01.03.08 at 3:26 pm

The use of this and your examples seem rather untypical. Why pipe it through “ssh” if you’re just transfering a tar.gz to the other side. You could just create the tar.gz and scp it.

Also, the use of “cat” in your examples is completely unnecessary.

I came here hoping to find an example like this (i.e. transferring a directory recursively over ssh). So, for the next guy:

tar cvf – /data | ssh otherhost tar xvf -

7 Teknoenie 01.23.08 at 1:16 am

Hi Vincent,

You may want to do this to get around limitations in older implementations of SSH that do not allow for large file transfers (larger than 2GB). I had recently run into this problem and the only workable solution was to tar over ssh to get around it.

8 valor 04.15.08 at 10:15 am

Hi Vincent,

you could create a .tgz or whatever locally and then use scp. The problem with large amounts of data is that scp is awfully slow.

Cheers,

valor

9 james 08.24.08 at 3:09 pm

rsync -avzH -e’essh’ /wwwdata root@192.168.1.201:/backup/

10 Juan Cubillo 08.28.08 at 2:14 pm

The whole point of this command is to help you when you have a filesystem full and need to tar files but don’t have enough space to store the tars. You can pipe the tar through ssh so that later you may also delete the files and place the tar into the original filesystem.

11 poswer 11.05.08 at 8:41 am

i dont know how to use to tar on network i was used 192.168.200.178 machine i use this /mydata folder how to transer using tar over network destination system is 192.168.200.200. any one help me.

12 GDR! 01.05.09 at 11:35 pm

The opposite side – which is the more common case, where you want to pull data from server, as opposed to making the server initiate connection and pushing data:

ssh gdr@server.net "tar jcf - /srv/gdr/gdr.geekhood.net/gdrwpl" > gdrwpl_backup.tar.bz2

This might be useful if you are behind a firewall

13 thetrivialstuff 05.25.09 at 10:44 pm

Vincent:
The method of piping tar through SSH is faster than SCP not because SCP is slow (the transfer rate would theoretically be exactly the same), but because it saves a lot of time by parallelizing the tar.gz creation with the transfer. This is even more true if the source system only has one hard drive (or the only hard drive with enough free space to do the tar.gz is the same as the one you want data from).

If you have a few GB of loose files to copy into a .tar.gz on the remote side (say, for doing a backup), piping the output through ssh is faster because the source hard drive can just read continously the whole time and the destination can write at the same time. If you’re creating the .tar.gz on the same hard drive, you take a huge penalty for all the seeking it has to do; it as to read a bit, write it to the tar, read a bit more, write it to the tar, etc.

Even if you have a second hard drive (or a crapload of RAM), you’re still taking longer if you make the .tar.gz first because there’s creation + transfer time instead of just transfer time.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , , ,

Previous post: Where are the passwords of the users located in Linux?

Next post: Linux: Delete user password