Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

tar from another server

by Anonymous Monk
on Dec 06, 2002 at 03:48 UTC ( [id://217979]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am kind of curious.

If there is a way I can create a tar file from a source server then 
extract it to a local machine in 1 command in perl using system().

from a unix command line I could do something like

# ssh me@server1 "cd /directory tar cf - " | tar xfv -

My attempt in using system()
my $CMD = "ssh me\@server1" ; my @args = qw( "cd /directory tar cf - " | tar xfv -) ; system("$CMD @args") == 0 or die "tar failed: $?" ;
It only allows me to ssh over to the server1 then exit out 
without going into the diretory create a tar file and untar. 

Any thought as to how I can accomplish this?
thanks,

Replies are listed 'Best First'.
Re: tar from another server
by graff (Chancellor) on Dec 06, 2002 at 04:44 UTC
    The following works for me. (I just have to answer ssh's "password:" prompt on my local keyboard, 'cuz I don't have those nifty ssh keys for the hosts I connect to from home, but that's no problem.)

    I decided to make a simple, general-purpose command line utility out of it, because I actually do this sort of transfer often enough myself to make it worthwhile (now I don't have to remember to put in all those parens, etc, on the command line).

    update: added more error checking and support for the "-l username" ssh option; also allow for "$src_path" to be empty (i.e., in login user's home directory).

    #!/usr/bin/perl # Program: ssh-tar.perl # Written by: dave graff # Purpose: make it easy to tar from remote-host to local-host use strict; my $Usage = "Usage: $0 hostname [-l username] path-to-tar local-dir-to +-save-to\n"; my $ssh_user = ""; if ( @ARGV > 2 && $ARGV[0] eq "-l" ) { $ssh_user = "$ARGV[0] $ARGV[1]"; shift; shift; } die $Usage unless ( @ARGV == 3 and -d $ARGV[2] and -w $ARGV[2] ); my $local_path = pop; chdir $local_path or die "can't chdir to $local_path: $!\n"; my $src_host = $ARGV[0]; my $divide = rindex( $ARGV[1], "/" ); my $src_path = ( $divide < 0 ) ? "" : substr( $ARGV[1], 0, $divide ); my $src_targ = substr( $ARGV[1], $divide +1 ); my $cmd = "ssh $ssh_user $src_host 'cd $src_path && tar cf - $src_targ +' | tar xf -"; print "command line is:\n\n $cmd\n\n"; system( $cmd );
      Thank you very very much :) I appreciate your answer!!!
      I think it is an excellent utility. I was able to tar from 
      the $src_host , however just when the job is about done.
      I got the following error. 
      
      tar: Unexpected EOF in archive
      tar: Unexpected EOF in archive
      tar: Error is not recoverable: exiting now
      
      and if I did 
      
      system( $cmd ) == 0 or die "tar failed"
      
      I always got the tar failed and die.
      Do you have any idea why?
      
      thanks!
      
        You may need to be careful about which version of tar is running on the remote host, and which version is running locally. If they're different (e.g. remote host uses a native solaris version, localhost uses gnu version, or whatever), this might cause the sort of problem you observed.

        Apart from that, does the local host get most of the content that you were hoping for? Can you isolate the particular file(s) that didn't get unpacked locally? (If, God forbid, one of the machines involved is using Microsoft text-mode semantics on the data transfer, that is bound to kill it prematurely.)

Re: tar from another server
by waswas-fng (Curate) on Dec 06, 2002 at 04:28 UTC
    This may point you in the right direction:
    tar -cf - <files> | (cd <target dir> ; tar -xvf - );

    Will tar and extract files in a diff location in one line.. add in ssh to do what you are looking to do.

    -Waswas

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://217979]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found