Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

tar help

by snyder (Initiate)
on Mar 20, 2006 at 13:43 UTC ( [id://538016]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Hello, I am new to perl and this is my first attempt. I am using Archive::Tar. I would like to tar a directory and ftp it to another server. I have the FTP going and can tar a "file" but am not successful at taring a "directory". Any help would be greatly appreciated. Thanks
#!/usr/bin/perl use Archive::Tar; my $tar = Archive::Tar->new; $tar->add_files('/var/lib/mysql/club -R'); $tar->write('club_db.tar'); use Net::FTP; print "putting the file on the server\n"; $ftpobj = Net::FTP -> new ("192.168.1.21"); $ftpobj -> login("my_name","my_pass"); $ftpobj -> binary; $ftpobj -> put ("club_db.tar"); $ftpobj -> quit;

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: tar help
by derby (Abbot) on Mar 20, 2006 at 14:25 UTC

    First step: code tags:

    #!/usr/bin/perl use Archive::Tar; my $tar = Archive::Tar->new; $tar->add_files('/var/lib/mysql/club -R'); $tar->write('club_db.tar'); use Net::FTP; print "putting the file on the server\n"; $ftpobj = Net::FTP -> new ("192.168.1.21"); $ftpobj -> login("my_name","my_pass"); $ftpobj -> binary; $ftpobj -> put ("club_db.tar"); $ftpobj -> quit;
    Second step: try it with one file:
    #!/usr/bin/perl use Archive::Tar; use Net::FTP; my $tar = Archive::Tar->new; $tar->add_files('/var/lib/mysql/club/foo'); $tar->write('club_db.tar'); print "putting the file on the server\n"; $ftpobj = Net::FTP->new ("192.168.1.21"); $ftpobj->login("my_name","my_pass"); $ftpobj->binary; $ftpobj->put ("club_db.tar"); $ftpobj->quit;
    third step, try it with two files:
    my $tar = Archive::Tar->new; $tar->add_files( '/var/lib/mysql/club/foo', '/var/lib/mysql/club/bar'); $tar->write('club_db.tar');
    fourth step ...

    figure out how to go from 2 files to n files. I would suggest first looking at IO::Dir if the directory contains no sub directories or File::Find if it does.

    -derby
      I have already achieved success with your first 3 recommendations. What I did find useful was your advice on the IO::Dir with the code below I can print to screen the files I wish to tar.
      #!/usr/bin/perl use IO::Dir; tie %dir, 'IO::Dir', "/var/lib/mysql/club"; foreach (keys %dir) { print $_, " " ,"\n"; }
      how would I use this to achieve my objective?
        my @files; tie %dir, 'IO::Dir', "/var/lib/mysql/club"; foreach (keys %dir) { next if $_ eq "."; next if $_ eq ".."; push( @files, "/var/lib/mysql/club/" . $_ ); } my $tar = Archive::Tar->new; $tar->add_files( @files ); $tar->write('club_db.tar');

        but .... don't forget to check for errors

        -derby
Re: tar help
by idsfa (Vicar) on Mar 20, 2006 at 15:01 UTC

    The ptar perl script which comes with Archive::Tar shows you how to do this using File::Find. Relevant fragment:

    if( $opts->{c} ) { my @files; find( sub { push @files, $File::Find::name; print $File::Find::name.$/ if $verbose }, @ARGV ); Archive::Tar->create_archive( $file, $compress, @files ); exit; }

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
      This looks like the path that will get me where I am trying to go. Thank you for your attention on this matter. For the record, I have a shell script to make the tarball and I have a script in ColdFusion to create the tarball and FTP it. I am attempting to learn some perl and that is why I am trying to write a script for something I have accomplished in another language. Expanding my knowledge with a point of reference.
Re: tar help
by theshz (Sexton) on Mar 20, 2006 at 15:41 UTC
    It looks like you're on some kind of linux/unix system, why not simply use the system command `tar -cf club_db.tar /var/lig/mysql/club`?
Re: tar help
by snyder (Initiate) on Mar 23, 2006 at 13:11 UTC
    Success.. Thanks to all monks for their help. I just needed to break the code down line by line and monkey with it until it worked. All I need now is a better understanding of the synatx. Did the "push" write the files to a list/variable named @files?
    #!/usr/bin/perl use IO::Dir; tie %dir, 'IO::Dir', "club"; foreach (keys %dir) { next if $_ eq "."; next if $_ eq ".."; push( @files, "club/" . $_ ); } use Archive::Tar; my $tar = Archive::Tar->new; $tar->add_files( @files ); $tar->write('club_db.tar'); use Net::FTP; print "putting the file on the server\n"; $ftpobj = Net::FTP -> new ("192.168.1.21"); $ftpobj -> login("username","password"); $ftpobj -> binary; $ftpobj -> put ("club_db.tar"); $ftpobj -> quit; print "file size ",-s "httpd.conf"," bytes\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538016]
Approved by kutsu
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.