--- psinib.pl 2003/10/27 19:07:32 1.19 +++ psinib.pl 2003/11/08 00:31:14 1.20 @@ -65,10 +65,12 @@ my $use_ping = 1; # default: use syn tcp ping to verify that host is up my $verbose = 1; # default verbosity level my $quiet = 0; +my $email; my $result = GetOptions( "ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, "verbose+" => \$verbose, "quiet+" => \$quiet, + "email=s" => \$email, ); $verbose -= $quiet; @@ -77,6 +79,39 @@ 'mountscript'; # die "usage: $0 mountscript"; +my $basedir = $0; +$basedir =~ s,/?[^/]+$,,g; + +# default subject for e-mail messages +my @subjects = ('Backup needs your attention!'); +my $sub_nr = 0; +my $email_body; + +if ($email) { + # It will use (and require) Tie::File only if --email=foo@bar.com + # arguement is used! + use Tie::File; + tie @subjects, 'Tie::File', "$basedir/subjects.txt" || xlog("CONFIG","Can't find $basedir/subjects.txt... using default (only one)"); + chdir; # this will change directory to HOME + if (open(SN,".psinib.subject")) { + $sub_nr = ; + chomp($sub_nr); + close(SN); + } + $sub_nr++; + # skip comments in subjects.txt + while($subjects[$sub_nr] =~ m/^#/) { + $sub_nr++; + } + $sub_nr = 0 if (! $subjects[$sub_nr]); + + if (open(SN,"> .psinib.subject")) { + print SN "$sub_nr\n"; + close (SN); + } else { + xlog("CONFIG","Can't open .psinib.subject -- I can't cycle subjects..."); + }; +} my @in_backup; # shares which are backeduped this run @@ -174,7 +209,12 @@ } close(M); -xlog("","$backup_ok backups completed of total ".($#in_backup+1)." this time (".int($backup_ok*100/($#in_backup+1))." %)"); +my $total = ($#in_backup + 1) || 0; +my $pcnt = ""; +$pcnt = "(".int($backup_ok*100/$total)." %)" if ($total > 0); +xlog("","$backup_ok backups completed of total $total this time".$pcnt); + +send_email(); 1; @@ -193,6 +233,18 @@ } } +# send e-mail with all messages +sub send_email { + return if (! $email || $email eq "" || !$email_body); + require Mail::Send; + my $msg = new Mail::Send; + $msg->to($email); + $msg->subject($subjects[$sub_nr]); + my $fn=$msg->open; + print $fn $email_body; + $fn->close; +} + # write entry to screen and log sub xlog { @@ -201,13 +253,20 @@ my $m = shift || '[no log entry]'; my $l = shift; $l = 1 if (! defined $l); # default verbosity is 1 - print STDERR $m,"\n" if ($verbose >= $l); + if ($verbose >= $l) { + if (! $email) { + print STDERR $m,"\n"; + # don't e-mail mesages with verbosity < 1 + } elsif ($l < 1) { + $email_body .= $m."\n"; + } + } print L "$t $share\t$m\n"; } # dump warn and dies into log -BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; warn $_[0] } } -BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; die $_[0] } } +BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; exit 1 } } +BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; exit 1 } } # split share name to host, dir and currnet date dir @@ -623,6 +682,10 @@ Decrease verbosity level +=item C<--email=email@domain> + +Send e-mails instead of dumping errors to STDERR. Useful for cron jobs. + =back This script in current version support just backup of Samba (or Micro$oft @@ -749,6 +812,12 @@ If you don't get any output, your samba might not listen to correct interface (see interfaces in smb.conf). +=head2 Aren't backups boring? + +No! If you have subjects.txt in same directory as C you can get +various funny subjects in your mail. They change over time as long as you +ignore your backup. + =head1 AUTHOR Dobrica Pavlinusic