65 |
my $use_ping = 1; # default: use syn tcp ping to verify that host is up |
my $use_ping = 1; # default: use syn tcp ping to verify that host is up |
66 |
my $verbose = 1; # default verbosity level |
my $verbose = 1; # default verbosity level |
67 |
my $quiet = 0; |
my $quiet = 0; |
68 |
|
my $email; |
69 |
|
|
70 |
my $result = GetOptions( |
my $result = GetOptions( |
71 |
"ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, |
"ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, |
72 |
"verbose+" => \$verbose, "quiet+" => \$quiet, |
"verbose+" => \$verbose, "quiet+" => \$quiet, |
73 |
|
"email=s" => \$email, |
74 |
); |
); |
75 |
|
|
76 |
$verbose -= $quiet; |
$verbose -= $quiet; |
79 |
'mountscript'; |
'mountscript'; |
80 |
# die "usage: $0 mountscript"; |
# die "usage: $0 mountscript"; |
81 |
|
|
82 |
|
my $basedir = $0; |
83 |
|
$basedir =~ s,/?[^/]+$,,g; |
84 |
|
|
85 |
|
# default subject for e-mail messages |
86 |
|
my @subjects = ('Backup needs your attention!'); |
87 |
|
my $sub_nr = 0; |
88 |
|
my $email_body; |
89 |
|
|
90 |
|
if ($email) { |
91 |
|
# It will use (and require) Tie::File only if --email=foo@bar.com |
92 |
|
# arguement is used! |
93 |
|
use Tie::File; |
94 |
|
tie @subjects, 'Tie::File', "$basedir/subjects.txt" || xlog("CONFIG","Can't find $basedir/subjects.txt... using default (only one)"); |
95 |
|
chdir; # this will change directory to HOME |
96 |
|
if (open(SN,".psinib.subject")) { |
97 |
|
$sub_nr = <SN>; |
98 |
|
chomp($sub_nr); |
99 |
|
close(SN); |
100 |
|
} |
101 |
|
$sub_nr++; |
102 |
|
# skip comments in subjects.txt |
103 |
|
while($subjects[$sub_nr] =~ m/^#/) { |
104 |
|
$sub_nr++; |
105 |
|
} |
106 |
|
$sub_nr = 0 if (! $subjects[$sub_nr]); |
107 |
|
|
108 |
|
if (open(SN,"> .psinib.subject")) { |
109 |
|
print SN "$sub_nr\n"; |
110 |
|
close (SN); |
111 |
|
} else { |
112 |
|
xlog("CONFIG","Can't open .psinib.subject -- I can't cycle subjects..."); |
113 |
|
}; |
114 |
|
} |
115 |
|
|
116 |
my @in_backup; # shares which are backeduped this run |
my @in_backup; # shares which are backeduped this run |
117 |
|
|
209 |
} |
} |
210 |
close(M); |
close(M); |
211 |
|
|
212 |
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; |
213 |
|
my $pcnt = ""; |
214 |
|
$pcnt = "(".int($backup_ok*100/$total)." %)" if ($total > 0); |
215 |
|
xlog("","$backup_ok backups completed of total $total this time".$pcnt); |
216 |
|
|
217 |
|
send_email(); |
218 |
|
|
219 |
1; |
1; |
220 |
|
|
233 |
} |
} |
234 |
} |
} |
235 |
|
|
236 |
|
# send e-mail with all messages |
237 |
|
sub send_email { |
238 |
|
return if (! $email || $email eq "" || !$email_body); |
239 |
|
require Mail::Send; |
240 |
|
my $msg = new Mail::Send; |
241 |
|
$msg->to($email); |
242 |
|
$msg->subject($subjects[$sub_nr]); |
243 |
|
my $fn=$msg->open; |
244 |
|
print $fn $email_body; |
245 |
|
$fn->close; |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
# write entry to screen and log |
# write entry to screen and log |
250 |
sub xlog { |
sub xlog { |
253 |
my $m = shift || '[no log entry]'; |
my $m = shift || '[no log entry]'; |
254 |
my $l = shift; |
my $l = shift; |
255 |
$l = 1 if (! defined $l); # default verbosity is 1 |
$l = 1 if (! defined $l); # default verbosity is 1 |
256 |
print STDERR $m,"\n" if ($verbose >= $l); |
if ($verbose >= $l) { |
257 |
|
if (! $email) { |
258 |
|
print STDERR $m,"\n"; |
259 |
|
# don't e-mail mesages with verbosity < 1 |
260 |
|
} elsif ($l < 1) { |
261 |
|
$email_body .= $m."\n"; |
262 |
|
} |
263 |
|
} |
264 |
print L "$t $share\t$m\n"; |
print L "$t $share\t$m\n"; |
265 |
} |
} |
266 |
|
|
267 |
# dump warn and dies into log |
# dump warn and dies into log |
268 |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; warn $_[0] } } |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; exit 1 } } |
269 |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; die $_[0] } } |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; exit 1 } } |
270 |
|
|
271 |
|
|
272 |
# split share name to host, dir and currnet date dir |
# split share name to host, dir and currnet date dir |
682 |
|
|
683 |
Decrease verbosity level |
Decrease verbosity level |
684 |
|
|
685 |
|
=item C<--email=email@domain> |
686 |
|
|
687 |
|
Send e-mails instead of dumping errors to STDERR. Useful for cron jobs. |
688 |
|
|
689 |
=back |
=back |
690 |
|
|
691 |
This script in current version support just backup of Samba (or Micro$oft |
This script in current version support just backup of Samba (or Micro$oft |
812 |
If you don't get any output, your samba might not listen to correct interface |
If you don't get any output, your samba might not listen to correct interface |
813 |
(see interfaces in smb.conf). |
(see interfaces in smb.conf). |
814 |
|
|
815 |
|
=head2 Aren't backups boring? |
816 |
|
|
817 |
|
No! If you have subjects.txt in same directory as C<psinib.pl> you can get |
818 |
|
various funny subjects in your mail. They change over time as long as you |
819 |
|
ignore your backup. |
820 |
|
|
821 |
=head1 AUTHOR |
=head1 AUTHOR |
822 |
|
|
823 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |