Skip to content

Instantly share code, notes, and snippets.

@jimtalksdata
Created April 28, 2009 00:46
Show Gist options
  • Save jimtalksdata/102853 to your computer and use it in GitHub Desktop.
Save jimtalksdata/102853 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Report.pl
# This script scans for backups in the specified directories (@DIRINDEX)
# and generates log files for each cobian backup set detected in each
# of these directories. These log files are generated by the list command
# in 7z. Optionally password protected archives can be opened by
# specifying the password after the option -p (e.g. ./report.pl -p password).
#USER CONFIGURBLE PARAMETERS
# Directories to scan
# If scanning current directory, put .
my @DIRINDEX = (".","desktop","music");
# Path to 7Z/bin (absolute)
my $zippath = "/home/jimhsu/bin/7zip/bin";
# Path where reports go (relative to base)
my $reportdir = "reports";
print "Log file generator\n\n";
my $passphrase = '';
my $count = 0;
while ($param = shift(@ARGV)) {
if ($param eq '-p') {
$passphrase = shift(@ARGV);
}
}
$curpath = trim(`pwd`);
# Array of information to save:
# For cobian backups, organized like the following:
# Desktop and Documents.7z 2009-04-20 23;12;26.001
# Desktop and Documents.7z - backup type
# Space - separator
# 2009-04-20 23;12;26 - date/time
# . - separator
# 001 - Volume (# archive in set)
my %backupData;
my %backupDir;
my %results;
while ($curdir = shift(@DIRINDEX)) {
$cmd = "ls $curpath"."/"."$curdir";
$cmd = `$cmd`;
@files = split(/\n/,$cmd);
#print @files;
foreach $file (@files)
{
if($file =~ /(.+7z)\s(\S+\s\S+)\.([0-9]+)/) {
#print "Backup type: $1\nDate: $2\nVolume: $3\n";
$backupData{$1}{$2}{$3} = $file;
$backupDir{$1} = $curdir;
}
}
}
# Generate appropriate data
$grepcmd = "grep -v -e '[D|DR]\\.'"; # This suppresses the useless directory listings
if ($passphrase ne '') {
$passphrase = " -p$passphrase";
}
foreach $type (sort(keys %backupData)) {
foreach $date (sort(keys %{$backupData{$type}})) {
#print "$type | $date\n";
$fileone = $backupData{$type}{$date}{'001'};
$cmdexec = "$zippath/7z l '".$curpath."/".$backupDir{$type}."/".$fileone."'".$passphrase;
$cmdexec = "$cmdexec | $grepcmd";
$out = `$cmdexec`;
open(FILE,">$curpath/$reportdir/$type $date.txt") or die($!);
print FILE $out;
close(FILE);
$count++;
}
}
print "All done. Reports for $count backup sets have been generated in ./$reportdir.\n\n";
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
my $string = shift;
$string =~ s/\s+$//;
return $string;
}
sub print_r {
package print_r;
our $level;
our @level_index;
if ( ! defined $level ) { $level = 0 };
if ( ! defined @level_index ) { $level_index[$level] = 0 };
for ( @_ ) {
my $element = $_;
my $index = $level_index[$level];
print "\t" x $level . "[$index] => ";
if ( ref($element) eq 'ARRAY' ) {
my $array = $_;
$level_index[++$level] = 0;
print "(Array)\n";
for ( @$array ) {
main::print_r( $_ );
}
--$level if ( $level > 0 );
} elsif ( ref($element) eq 'HASH' ) {
my $hash = $_;
print "(Hash)\n";
++$level;
for ( keys %$hash ) {
$level_index[$level] = $_;
main::print_r( $$hash{$_} );
}
} else {
print "$element\n";
}
$level_index[$level]++;
}
} # End print_r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment