The goal, a simple perl script to backup a web directory, compress the archive and then ftp the file(s) to a remote server.
If anyone knows how to make this more robust, intuitive (by creating user defined variables etc.), & able to do better date checking please let me know.
I have used a few tutorials etc, but I learn better when someone steps through something I have already written and points out what can be improved etc.
Thanks in advance, jas
[start script] #!/usr/bin/perl # Backup web directory to remote server via FTP
# Declare modules to use use strict; use warnings; use POSIX qw(strftime); use Archive::Tar; use Compress::Zlib; use File::Find; use Net::FTP;
# Current date stamp my $date = strftime('%Y%m%d', localtime());
# Create tarball from /path/to/www/ directory my @files; find(sub { push @files,$File::Find::name },"/path/to/www/"); Archive::Tar->create_archive("www.tar",0,@files);
# Create Archive from recently created tarball my $gz = system('gzip -9 www.tar'); $gz = system('mv -f www.tar.gz ' . $date . '-www.tar.gz');
# Open connection to FTP server
my $ftp = Net::FTP->new("ftp.server.com", Debug => 1)or die "Could not open Connection to ftp.server.com";
$ftp->login("user-name", 'password')or die "Could not login to ftp.server.com: $@";
# Upload all archives in /path/to/backups/ directory foreach my $bup (glob('/path/to/backups/*-www.tar.gz')) { $ftp->put($bup) or die "Could not transfer files ", $ftp->message; }
# Create array of files on remote ftp server my @alst = $ftp->ls;
# Create empty variables my $year = ""; my $month = ""; my $day = ""; my $c_year = ""; my $c_month = ""; my $c_day = "";
# Extract individual variables from date variable if ($date =~ /^(\d{4})(\d{2})(\d{2})$/) { $c_year = $1; $c_month = $2; $c_day = $3; }
# Loop over array and check for matching files and anything older than 30 days
my @list;
foreach my $file(@alst) {
if ($file =~ /^\d{8}-www\.t(?:ar\.)?gz$/i) {
if ($file =~ /^(\d{4})(\d{2})(\d{2})-www\.t(?:ar\.)?gz$/i) {
$year = $1;
$month = $2;
$day = $3;
if($month != $c_month) {
push @list, $file;
} else {
warn "Skipping recent file: $file"; }
}
} else {
warn "Skipping improper file format: $file"; }
}
# Loop over new array of items flagged for deletion and remove from ftp server
foreach my $fle(@list) {
$ftp->delete($fle)or warn "Cannot delete remote file ($fle): " . $ftp->message; }
$ftp->quit; [/end script]
Wc -Sx- Jones wrote:
Jenda?
This is the partial original post -
Jas wrote:
Wouldn't this be better?
my $year = ""; my $month = ""; my $day = "";
if($file =~ /^(\d{4})(\d{2})(\d{2})-www.tar.gz$/) { $year = $1; $month = $2; $day = $3;
my $chk = "$month - 1";
Here - this is weird =)
"03" in numerical context is 3.
File name was 20040301 - collapses to 200431 - Not a match IMHO.
foreach($file ! $chk) { $ftp->delete($file); }
??? Not sure but I am going to test it out....
Maybe I am totally confused - I have been working on WebSphere. Yes, I plead complete insanity.
-Bill- __Sx__________________________________________ http://youve-reached-the.endoftheinternet.org/
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>