Operating System - HP-UX
1826141 Members
5044 Online
109690 Solutions
New Discussion

How to tar a directory from one host to another using pipe

 

How to tar a directory from one host to another using pipe

Can anybody tell me the exact command line syntax to use tar in copying a directory and all its subdirectories and files preserving the permissions and links while transferring from one host to another host.
Hello
12 REPLIES 12
Patrick Wallek
Honored Contributor

Re: How to tar a directory from one host to another using pipe

The following should work well:

# cd /dir
# tar -cf - ./* | remsh dest_host "cd /dir; tar -xf -"

D. Jackson_1
Honored Contributor

Re: How to tar a directory from one host to another using pipe

On the source machine(file holder)

#>cd /"source_directory or files"

#>tar cvf - . | remsh "target_machine" '(cd /"source_directory"; tar - xvf -)'

Verify that the files have been transfered to the target host.

Hope this helps.

Re: How to tar a directory from one host to another using pipe

In the syntax you have provided will permissions and links be preserved.Also the directory matching the source directory has to exist on the target host .Is that right?
Hello
Rodney Hills
Honored Contributor

Re: How to tar a directory from one host to another using pipe

The above answers are correct, but they assume you have your .rhosts file setup correctly for this to work.

Do a "man rhosts" to learn more. You need to set it up to give permission for remote systems to connect in without a password.

Note- remsh,rlogin are very popular, but the push is to use the secure shell command instead "ssh". This provides encrypted connections between two machines.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: How to tar a directory from one host to another using pipe

Permissions and such are kept.

tar -cf - ./* | remsh dest_host "cd /dir; tar -xf -"

is known as a relative copy. tar backups up at the current directory, then "cd /dir" sets the current directory on the remote system and the following tar will restore folders/files relative to /dir, with all permissions, ownerships retained. Ownership is dependant that /etc/passwd has common entries between the two systems.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: How to tar a directory from one host to another using pipe

One thing I just realized

tar -cf - ./* | remsh dest_host "cd /dir; tar -xf -"

Will not copy files with "." prefixes. Shell considers them "hidden" files. Change command to-

tar -cf - ./* ./.* | remsh dest_host "cd /dir; tar -xf -"

to include ALL files.

HTH

-- Rod Hills
There be dragons...
Bill Douglass
Esteemed Contributor

Re: How to tar a directory from one host to another using pipe

If you specify the directory name in your tar command, it will pick up the hidden files (beginning with a ".") in directory and all sub-directories.

Using ssh, the following would work:

tar cvf - direcroty | ssh qe2n1 cd /new/directory/path\; tar xf -
Angus Crome
Honored Contributor

Re: How to tar a directory from one host to another using pipe

The above commands will work in most cases (as long as the caveats are met). However, you should generally force a block size on both the write and extract portions. It can be extremely important across a network, since tar on a given machine may not necessarily default to a blocksize of 20.

(ie;
cd
tar -cbf 64 - . | (remsh dest_host "cd /dir; tar -xbf 64 -"

You can also use the find command piped to cpio to do the same thing.

Both solutions do not handle files bigger than 2Gb. You will have to use fbackup to get those (or cp -p).

find ./ -depth | remsh dest_host "cpio -pmudlx /"

You will probably have to play with the remsh or ssh portion of the command to get cpio to work, as I have never tried it from machine A to machine B (except over "YIKES" NFS).
There are 10 types of people in the world, those who understand binary and those who don't - Author Unknown
Mariani Alberto
Frequent Advisor

Re: How to tar a directory from one host to another using pipe

I wonder why everyone uses a "cd" before "tar" to change the directory to which copy files.

How about using the -C tar option?


Mariani Alberto
Graham Cameron_1
Honored Contributor

Re: How to tar a directory from one host to another using pipe

On this site, we aren't allowed .rhosts so can't use remsh.
There is another way, use "rexec" instead of "remsh" - you are prompted for the remote password but otherwise the effect is exactly the same.
You can also use rexec -l to connect to the remote machine as someone else, if you have the password. (-l works for remsh too, if .rhosts is set up right).

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Seth Parker
Trusted Contributor

Re: How to tar a directory from one host to another using pipe

While not tar, here's the command I used to copy data from one machine to another during an upgrade to a new machine.

On the new machine, cd to the dest directory then execute:

date; remsh "cd ; find . -xdev|cpio -oa"|cpio -idmua; date

This will also let you know how long it took to complete.

As mentioned in other notes, you have to set up your .rhosts properly and enable remsh on the source machine, but it worked great for me.

Good luck!
Seth
Larry Basford
Regular Advisor

Re: How to tar a directory from one host to another using pipe

How about gzip to help avoid 2GB limit.

Maybe less time on a slow net

tar -cf - ./* ./.*| gzip > | remsh dest_host "cd /dir; gzcat tarfile.tar.gz | tar -xvf -"

Just idea syntax probably wrong.
Desaster recovery? Right !