5

I am trying to copy a large folder structure between machines. I want to maintain the ownership/rights during the copy as it is not reasonable to 'fix' the privs afterwards.

Therefore, I am using the following command to tar the file with privs intact and transfer the data to the destination machine. The same users exist on both machines.

tar cfzp - foldertocopy | ssh me@machine "cat > /applications/incoming/foldertocopy.tar.gz"

The transfer works fine, and the next step is to su to root on the remote machine and untar the file.

The problem is: there isn't enough disk space to store both the compressed and uncompressed data at the same time.

I could use rsync/recursive scp but my user doesn't have the rights to create the files with the correct privs itself and root can't log in remotely.

What are my options? The source machine is RHEL4 and the destination is RHEL5.

Rich
  • 4,529

3 Answers3

9

As root, set up a named pipe:

# mkfifo /tmp/fifo
# chmod o+w /tmp/fifo

Then, transfer your data as me:

$ tar cfzp - foldertocopy | ssh me@machine "cat > /tmp/fifo"

But read it as root:

# tar -xfzp /tmp/fifo
2

One solution to the problem is to have ssh run the untar directly:

tar cfzp - foldertocopy | ssh me@machine "cd rightplace; tar xzf -"
  • Will that work even though my user on the remote machine doesn't have the rights to create the files with the correct owner/privs? I can sudo su - on the remote machine, but I don't know if it's possible to do this from within the command executed by ssh...I have to specify a password when doing sudo su -. – Rich Jan 30 '12 at 13:41
  • This is the right solution, but you should do it backwards. SSH to the machine, write a shell script that logs back to your machine and uses tar cf ... | tar xf to transfer the data, and then sudo ./shellscript. You can make the shell script owned by root and only readable by root because your login password will be in it. And delete it after the transfer. – Michael Dillon Feb 01 '12 at 07:51
2

Why don't you use archival rsync?

rsync -avzHAX foldertocopy user@remoteserver:/file/path/to/copy

This way you can save every detail about the file, also with "z" rsync compresses the stream on the fly.

bayindirh
  • 979