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 |
|
my $home_dir=$ENV{'HOME'}; |
91 |
|
$home_dir = '/tmp' if (! -w $home_dir); |
92 |
|
|
93 |
|
if ($email) { |
94 |
|
# It will use (and require) Tie::File only if --email=foo@bar.com |
95 |
|
# arguement is used! |
96 |
|
use Tie::File; |
97 |
|
tie @subjects, 'Tie::File', "$basedir/subjects.txt" || xlog("CONFIG","Can't find $basedir/subjects.txt... using default (only one)"); |
98 |
|
chdir; # this will change directory to HOME |
99 |
|
if (open(SN,"$home_dir/.psinib.subject")) { |
100 |
|
$sub_nr = <SN>; |
101 |
|
chomp($sub_nr); |
102 |
|
close(SN); |
103 |
|
} |
104 |
|
$sub_nr++; |
105 |
|
# skip comments in subjects.txt |
106 |
|
while($subjects[$sub_nr] && $subjects[$sub_nr] =~ m/^#/) { |
107 |
|
$sub_nr++; |
108 |
|
} |
109 |
|
$sub_nr = 0 if (! $subjects[$sub_nr]); |
110 |
|
|
111 |
|
if (open(SN,"> $home_dir/.psinib.subject")) { |
112 |
|
print SN "$sub_nr\n"; |
113 |
|
close (SN); |
114 |
|
} else { |
115 |
|
xlog("CONFIG","Can't open $home_dir/.psinib.subject -- I can't cycle subjects..."); |
116 |
|
}; |
117 |
|
} |
118 |
|
|
119 |
my @in_backup; # shares which are backeduped this run |
my @in_backup; # shares which are backeduped this run |
120 |
|
|
212 |
} |
} |
213 |
close(M); |
close(M); |
214 |
|
|
215 |
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; |
216 |
|
my $pcnt = ""; |
217 |
|
$pcnt = "(".int($backup_ok*100/$total)." %)" if ($total > 0); |
218 |
|
xlog("","$backup_ok backups completed of total $total this time".$pcnt); |
219 |
|
|
220 |
|
send_email(); |
221 |
|
|
222 |
1; |
1; |
223 |
|
|
236 |
} |
} |
237 |
} |
} |
238 |
|
|
239 |
|
# send e-mail with all messages |
240 |
|
sub send_email { |
241 |
|
return if (! $email || $email eq "" || !$email_body); |
242 |
|
require Mail::Send; |
243 |
|
my $msg = new Mail::Send; |
244 |
|
$msg->to($email); |
245 |
|
$msg->subject($subjects[$sub_nr]); |
246 |
|
my $fn=$msg->open; |
247 |
|
print $fn $email_body; |
248 |
|
$fn->close; |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
# write entry to screen and log |
# write entry to screen and log |
253 |
sub xlog { |
sub xlog { |
256 |
my $m = shift || '[no log entry]'; |
my $m = shift || '[no log entry]'; |
257 |
my $l = shift; |
my $l = shift; |
258 |
$l = 1 if (! defined $l); # default verbosity is 1 |
$l = 1 if (! defined $l); # default verbosity is 1 |
259 |
print STDERR $m,"\n" if ($verbose >= $l); |
if ($verbose >= $l) { |
260 |
|
if (! $email) { |
261 |
|
print STDERR $m,"\n"; |
262 |
|
# don't e-mail mesages with verbosity < 1 |
263 |
|
} elsif ($l < 1) { |
264 |
|
$email_body .= $m."\n"; |
265 |
|
} |
266 |
|
} |
267 |
print L "$t $share\t$m\n"; |
print L "$t $share\t$m\n"; |
268 |
} |
} |
269 |
|
|
270 |
# dump warn and dies into log |
# dump warn and dies into log |
271 |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; warn $_[0] } } |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; exit 1 } } |
272 |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; die $_[0] } } |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; exit 1 } } |
273 |
|
|
274 |
|
|
275 |
# split share name to host, dir and currnet date dir |
# split share name to host, dir and currnet date dir |
311 |
if (-l $bl) { |
if (-l $bl) { |
312 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
313 |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
314 |
undef $real_bl if (! -e $real_bl); |
if (! -e $real_bl) { |
315 |
|
xlog($share,"latest link $bl -> $real_bl not valid, removing it"); |
316 |
|
unlink $bl || die "can't remove link $bl: $!"; |
317 |
|
undef $real_bl; |
318 |
|
} |
319 |
} |
} |
320 |
if (! $real_bl) { |
if (! $real_bl) { |
321 |
xlog($share,"no old backup, trying to find last backup,"); |
xlog($share,"no old backup, trying to find last backup"); |
322 |
if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { |
if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { |
323 |
my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR); |
my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR); |
324 |
closedir(BL_DIR); |
closedir(BL_DIR); |
685 |
|
|
686 |
Decrease verbosity level |
Decrease verbosity level |
687 |
|
|
688 |
|
=item C<--email=email@domain> |
689 |
|
|
690 |
|
Send e-mails instead of dumping errors to STDERR. Useful for cron jobs. |
691 |
|
|
692 |
=back |
=back |
693 |
|
|
694 |
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 |
815 |
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 |
816 |
(see interfaces in smb.conf). |
(see interfaces in smb.conf). |
817 |
|
|
818 |
|
=head2 Aren't backups boring? |
819 |
|
|
820 |
|
No! If you have subjects.txt in same directory as C<psinib.pl> you can get |
821 |
|
various funny subjects in your mail. They change over time as long as you |
822 |
|
ignore your backup. |
823 |
|
|
824 |
=head1 AUTHOR |
=head1 AUTHOR |
825 |
|
|
826 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |