1752790 Members
6107 Online
108789 Solutions
New Discussion юеВ

Help perl scp script

 
SOLVED
Go to solution
Junior C.
Frequent Advisor

Help perl scp script

All,

I create a perl script that tar all files in directory.

Dev Server:
/dir/dir1/upload
/dir/dir2/upload
/dir/dir3/upload

No problem creating tar file in all directory above.

I want to scp the tar file from /dir/dir/upload
to PROD SRV. /dir/dir1, etc


my @upload_dir = ('/dir/dir1/upload',
'/dir/dir2/upload'
);

my @upload_server = ('prod_srv');

foreach my $dir (@upload_dir)
{
chdir ($dir);

system("tar cvf upload.tar *");
}


I appreciate all help.

JC
8 REPLIES 8
Steven Schweda
Honored Contributor

Re: Help perl scp script

Why drag perl into it, when a simple shell
script could do the job?

> system("tar cvf upload.tar *");

I'd use "tar cvf upload.tar .". For the
reason, see, for example:

http://forums12.itrc.hp.com/service/forums/questionanswer.do?threadId=1191368

If you insist on using perl, and you do the
"tar" using system(), then why not use
system() again to do the scp operation? If
you use a simple shell script, why not just
put the scp command into it?
Junior C.
Frequent Advisor

Re: Help perl scp script

Steven,

Shell or perl

I want to scp tar file from

SERVER_A SERVER_B
/abc/dir1/upload /abc/dir1
/efg/dir2/upload /efg/dir2
/hij/dir3/upload /hij/dir3
/xzy/dir4/upload /xyz/dir4

JC
Steven Schweda
Honored Contributor

Re: Help perl scp script

> I want to scp tar file [...]

Ok. Go ahead. "man scp"? "scp -h"?

Do you have ssh working between these two
systems?

Is the problem how to put an scp command
into a script, or how to make an scp command
work, or what?
Junior C.
Frequent Advisor

Re: Help perl scp script

Steven.

OK, lets start from the top.
I know how to scp a file from severA to severB

I need help in creating a script to scp tar files from

SERVER_A
/abc/dir1/XYZ/upload/upload.tar
/efg/dir2/DIR2/upload/upload.tar
/hij/dir3/ABC/upload/upload.tar
/xzy/dir4/upload/upload.tar
etc..
etc..
etc..
etc..


SERVER_B
/abc/dir1/XYZ
/efg/dir2/DIR2
/hij/dir3/ABC
/xyz/dir4
etc...
etc.

SERVER_C
/abc/dir1/XYZ
/efg/dir2/DIR2
/hij/dir3/ABC
/xyz/dir4
etc...
etc.

SERVER_A
scp /abc/dir1/XYZ/upload/upload.tar SERVER_B:/abc/dir1/XYZ

SERVER_A
scp /abc/dir1/XYZ/upload/upload.tar SERVER_C:/abc/dir1/XYZ

NOTE: That upload directory is just a holding area on SERVER_A I do not have a /abc/dir1/XYZ/upload/ on SERVER_B and SERVER_C

JC



Alexander Chuzhoy
Honored Contributor

Re: Help perl scp script

OK, then.
You will need to copy your public ssh key to allow passwordless login to server_a and server_b.


Then after you created the tarball, test if the directory structure exists on the destination server. If not create it, otherwise just copy the file to the destination directory:
destdir="/abc/dir1/XYZ"
for i in a b; do
ssh server_$i test -d $destdir
if [ $? -ne 0 ]; then
ssh server_${i} mkdir -p $destdir
scp server_${i}:$destdir
done



Junior C.
Frequent Advisor

Re: Help perl scp script

Alexander,

Thanks for the reply.

Following are my directory structure.

SERVER_A Example:
/usr/bin/test/upload
/etc/bin/home/upload
/sbin/home/help/upload
/var/sbin/home/bin/upload
..
..
/directory/ten/upload


In my perl script I loop/cd to /upload dir and tar all files in the directory, creating upload.tar file.

NEXT

After the upload.tar file is created on SERVER_A in /sbin/home/help/upload/upload.tar

I wan't scp the upload.tar file to SERVER_B and SERVER_C

scp /sbin/home/help/upload/upload.tar SERVER_B:/sbin/home/help

scp /sbin/home/help/upload/upload.tar SERVER_C:/sbin/home/help

THEN
create upload.tar file on SERVER_A in directory /var/sbin/home/bin/upload/upload.tar

scp /var/sbin/home/bin/upload/upload.tar SERVER_B:/var/sbin/home/bin/

scp /var/sbin/home/bin/upload/upload.tar SERVER_C:/var/sbin/home/bin/

etc,
etc,


-JC
Ralph Grothe
Honored Contributor

Re: Help perl scp script

You can omit the temporal local creation of the tar files if you are short on disk space
by directly piping into ssh.
Here is a code snippet of one way how this could be done.
This requires a previously created and distributed RSA key (i.e. the arg to -i).
Please, adapt to your users, hosts, file paths etc.

my $ruser = 'someone';
my $rhost = '123.123.123.123';
my $ldir = '/etc';
my $rdir = '/var/tmp/etc.bak';
my $rsa_key = "$ENV{HOME}/.ssh/id_rsa";
my $pid = open PSSH, '|-';
die "cannot open pipe\n" unless defined $pid;
if ($pid) {
chdir $ldir;
select((select(PSSH), $|=1)[0]);
print PSSH qx(/bin/tar cf - .);
} else {
exec qw(/usr/bin/ssh -o BatchMode=yes -q -C -T -i),
$rsa_key, "$ruser\@$rhost",
"mkdir -p $rdir && cd $rdir && tar xf -";
}
close PSSH;
Madness, thy name is system administration
Alexander Chuzhoy
Honored Contributor
Solution

Re: Help perl scp script

Ok, hope I understood now. I don't have a linux machine next to me to test the script,but it should be something like the following:

#!/usr/bin/perl
use strict;
my $upload="upload";
my @servers=( "10.10.10.10", "20.20.20.20");
my @dirs=("/usr/bin/test/","/etc/bin/home/","/sbin/home/help/","/var/sbin/home/bin/");

for my $dir (@dirs) {
print "Creating tar for $dir.\n";
my $localdir=$dir.$upload;
mkdir $localdir unless -d $localdir;
chdir $localdir;
system "tar cvf upload.tar ./*";
for my $server (@servers) {
print "Copying the tarball for $dir to $server.\n";
system "scp upload.tar $server:$dir";
}
}