Hi, I'm new to Perl.
I try to check Last Upload in perl on Amazon S3 but the script failed to print/recognize last successful upload date, here is the code:
#!/usr/bin/perl use DateTime; use Time::Local; $s3_bucket = 'some/folder/'; $state_failure = 1; @checkB = &checkBackup; $checkBT = checkBackup(latest); print "CHECKBT: $checkBT\n"; $dt = DateTime->now; $ymdh = $dt->ymd . "-" . $dt->hour; $ymdh = converttoEpoch($ymdh); if ($ymdh - $checkB[0] > 24){ print "ERROR\n"; $state_failure = 1; } else { print "SUCCESS"; $state_failure = 0; } sub checkBackup { local ($req) = @_; @last_backups = (); @backupList = (); @last_backups = `s3cmd --access_key=XXX --secret_key=xxx ls s3 +://$s3_bucket \| awk \'\{print \$1\" \"\$2\}\'`; $i = 0; foreach $lineB (@last_backups) { chomp $lineB; if ($i > 0){ #print "Before Convert: $lineB\n"; $lineBC = converttoEpoch($lineB); #print "After convert: $lineB\n"; push (@backupListB, $lineB); push (@backupList, $lineBC); } $i++; } @sortedList = sort {$b <=> $a} @backupList; @sortedListB = sort {$b <=> $a} @backupListB; if ($req eq "latest") { return $sortedListB[0]; } else { return @sortedList; } } sub converttoEpoch { local ($input) = @_; @all_num = $input =~ /(\d+)/g; $yearE = $all_num[0]; $monthE = $all_num[1]; $dayE = $all_num[2]; $hourE = $all_num[3]; $minuteE = 0; $secondE = 0; if (@all_num){ $dt = DateTime->new( year => $yearE, month => $mont +hE, day => $dayE, hour => $hourE, minute => 0, second => 0, nanosecond => 000000000, time_zon +e => 'local' ); $epoch_time = $dt->epoch; $epoch_time = $epoch_time / 3600; return $epoch_time; } } use Mail::Sender; $sender = new Mail::Sender { # smtp => 'some.mail.provider', on_errors => 'code', debug_level => 1, } or die "Can't create the Mail::Sender object: $Mail::Sender::Error\n +"; if ($state_failure == 1){ if ($sender->MailMsg( { to => 'account@some.mail.provider', subject => "NOTICE - No Additional Backup is c +reated on Amazon S3", msg => "There is no Backup activity more than +24 hour\nLast Backup Time: $checkBT\n", } ) < 0 ) { die "$Mail::Sender::Error\n"; } }
Result: I got email and the script print the first upload date (the oldest) file in Amazon S3, not the latest date (newest date).
Please help, thank you very much.
In reply to To Check Last Upload Date on Amazon S3 by babah
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |