62 |
# taint path: nmblookup should be there! |
# taint path: nmblookup should be there! |
63 |
$ENV{'PATH'} = "/usr/bin:/bin"; |
$ENV{'PATH'} = "/usr/bin:/bin"; |
64 |
|
|
65 |
my $use_ping = 1; # deault: use 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 |
67 |
|
my $quiet = 0; |
68 |
|
|
69 |
my $result = GetOptions( |
my $result = GetOptions( |
70 |
"ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, |
"ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, |
71 |
|
"verbose+" => \$verbose, "quiet+" => \$quiet, |
72 |
); |
); |
73 |
|
|
74 |
|
$verbose -= $quiet; |
75 |
|
|
76 |
my $mounts = shift @ARGV || |
my $mounts = shift @ARGV || |
77 |
'mountscript'; |
'mountscript'; |
78 |
# die "usage: $0 mountscript"; |
# die "usage: $0 mountscript"; |
150 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
151 |
$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 "/"); |
152 |
if (-l $bc && $real_bl eq $bc) { |
if (-l $bc && $real_bl eq $bc) { |
153 |
print "$share allready backuped...\n"; |
xlog($share,"allready backuped..."); |
154 |
$backup_ok++; |
$backup_ok++; |
155 |
next; |
next; |
156 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
|
|
161 |
print "working on $share\n"; |
xlog($share,"working on $share..."); |
162 |
|
|
163 |
# try to nmblookup IP |
# try to nmblookup IP |
164 |
$ip = get_ip($share) if (! $ip); |
$ip = get_ip($share) if (! $ip); |
199 |
my $share = shift; |
my $share = shift; |
200 |
my $t = strftime $LOG_TIME_FMT, localtime; |
my $t = strftime $LOG_TIME_FMT, localtime; |
201 |
my $m = shift || '[no log entry]'; |
my $m = shift || '[no log entry]'; |
202 |
print STDERR $m,"\n"; |
my $l = shift; |
203 |
|
$l = 1 if (! defined $l); # default verbosity is 1 |
204 |
|
print STDERR $m,"\n" if ($verbose >= $l); |
205 |
print L "$t $share\t$m\n"; |
print L "$t $share\t$m\n"; |
206 |
} |
} |
207 |
|
|
208 |
# dump warn and dies into log |
# dump warn and dies into log |
209 |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0]) ; warn $_[0] } } |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; warn $_[0] } } |
210 |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0]) ; die $_[0] } } |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; die $_[0] } } |
211 |
|
|
212 |
|
|
213 |
# split share name to host, dir and currnet date dir |
# split share name to host, dir and currnet date dir |
220 |
$dir =~ s/^_+//; |
$dir =~ s/^_+//; |
221 |
$dir =~ s/_+$//; |
$dir =~ s/_+$//; |
222 |
} else { |
} else { |
223 |
print "Can't parse share $share into host and directory!\n"; |
xlog($share,"Can't parse share $share into host and directory!",1); |
224 |
return; |
return; |
225 |
} |
} |
226 |
return ($host,$dir,strftime $DIR_TIME_FMT, localtime); |
return ($host,$dir,strftime $DIR_TIME_FMT, localtime); |
249 |
if (-l $bl) { |
if (-l $bl) { |
250 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
251 |
$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 "/"); |
252 |
} else { |
if (! -e $real_bl) { |
253 |
print "no old backup, trying to find last backup, "; |
xlog($share,"latest link $bl -> $real_bl not valid, removing it"); |
254 |
|
unlink $bl || die "can't remove link $bl: $!"; |
255 |
|
undef $real_bl; |
256 |
|
} |
257 |
|
} |
258 |
|
if (! $real_bl) { |
259 |
|
xlog($share,"no old backup, trying to find last backup"); |
260 |
if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { |
if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { |
261 |
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); |
262 |
closedir(BL_DIR); |
closedir(BL_DIR); |
263 |
$real_bl=pop @bl_dirs; |
$real_bl=pop @bl_dirs; |
264 |
print "using $real_bl as latest...\n"; |
xlog($share,"using $real_bl as latest..."); |
265 |
$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 "/"); |
266 |
if ($real_bl eq $bc) { |
if ($real_bl eq $bc) { |
267 |
xlog($share,"latest from today (possible partial backup)"); |
xlog($share,"latest from today (possible partial backup)"); |
269 |
$real_bl .= ".partial"; |
$real_bl .= ".partial"; |
270 |
} |
} |
271 |
} else { |
} else { |
272 |
print "this is first run...\n"; |
xlog($share,"this is first run..."); |
273 |
} |
} |
274 |
} |
} |
275 |
|
|
276 |
if (-l $bc && $real_bl && $real_bl eq $bc) { |
if (-l $bc && $real_bl && $real_bl eq $bc) { |
277 |
print "$share allready backuped...\n"; |
xlog($share,"allready backuped..."); |
278 |
return 1; |
return 1; |
279 |
} |
} |
280 |
|
|
282 |
|
|
283 |
if (! -e "$BACKUP_DEST/$host") { |
if (! -e "$BACKUP_DEST/$host") { |
284 |
mkdir "$BACKUP_DEST/$host" || die "can't make dir for host $host, $BACKUP_DEST/$host: $!"; |
mkdir "$BACKUP_DEST/$host" || die "can't make dir for host $host, $BACKUP_DEST/$host: $!"; |
285 |
print "created host directory $BACKUP_DEST/$host...\n"; |
xlog($share,"created host directory $BACKUP_DEST/$host..."); |
286 |
} |
} |
287 |
|
|
288 |
if (! -e "$BACKUP_DEST/$host/$dir") { |
if (! -e "$BACKUP_DEST/$host/$dir") { |
289 |
mkdir "$BACKUP_DEST/$host/$dir" || die "can't make dir for share $share, $BACKUP_DEST/$host/$dir $!"; |
mkdir "$BACKUP_DEST/$host/$dir" || die "can't make dir for share $share, $BACKUP_DEST/$host/$dir $!"; |
290 |
print "created dir for share $share, $BACKUP_DEST/$host/$dir...\n"; |
xlog($share,"created dir for this share $BACKUP_DEST/$host/$dir..."); |
291 |
} |
} |
292 |
|
|
293 |
mkdir $bc || die "can't make dir for current backup $bc: $!"; |
mkdir $bc || die "can't make dir for current backup $bc: $!"; |
365 |
} elsif (-d $pf) { |
} elsif (-d $pf) { |
366 |
push @dirs,$pr; |
push @dirs,$pr; |
367 |
} else { |
} else { |
368 |
print STDERR "not file or directory: $pf\n"; |
xlog($share,"not file or directory: $pf",0); |
369 |
} |
} |
370 |
} else { |
} else { |
371 |
print STDERR "ignored: $pr\n"; |
xlog($share,"ignored: $pr"); |
372 |
} |
} |
373 |
} |
} |
374 |
} |
} |
389 |
my $pf = norm_dir($d,"smb:$share/"); # path full |
my $pf = norm_dir($d,"smb:$share/"); # path full |
390 |
my $D = $smb->opendir($pf); |
my $D = $smb->opendir($pf); |
391 |
if (! $D) { |
if (! $D) { |
392 |
xlog($share,"FATAL: $share [$pf]: $!"); |
xlog($share,"FATAL: $share [$pf] as $param{username}/$param{workgroup}: $!",0); |
393 |
# remove failing dir |
# remove failing dir |
394 |
delete $smb_dirs[$di]; |
delete $smb_dirs[$di]; |
395 |
return 0; # failed |
return 0; # failed |
412 |
} elsif ($item->[0] == main::SMBC_DIR) { |
} elsif ($item->[0] == main::SMBC_DIR) { |
413 |
push @smb_dirs,$pr; |
push @smb_dirs,$pr; |
414 |
} else { |
} else { |
415 |
print STDERR "not file or directory [",$item->[0],"]: $pf\n"; |
xlog($share,"not file or directory [".$item->[0]."]: $pf",0); |
416 |
} |
} |
417 |
} else { |
} else { |
418 |
print STDERR "smb ignored: $pr\n"; |
xlog($share,"smb ignored: $pr"); |
419 |
} |
} |
420 |
} |
} |
421 |
} |
} |
597 |
|
|
598 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
599 |
|
|
600 |
./psinib.pl |
./psinib.pl [OPTION]... [mount script] |
601 |
|
|
602 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
603 |
|
|
604 |
|
Option can be one of more of following: |
605 |
|
|
606 |
|
=over 8 |
607 |
|
|
608 |
|
=item C<--backupdest=dir> |
609 |
|
|
610 |
|
Specify backup destination directory (defaults is /data/ |
611 |
|
|
612 |
|
=item C<--noping> |
613 |
|
|
614 |
|
Don't use ping to check if host is up (default is ti use tcp syn to cifs |
615 |
|
port) |
616 |
|
|
617 |
|
=item C<--verbose -v> |
618 |
|
|
619 |
|
Increase verbosity level. Defailt is 1 which prints moderate amount of data |
620 |
|
on STDOUT and STDERR. |
621 |
|
|
622 |
|
=item C<--quiet -q> |
623 |
|
|
624 |
|
Decrease verbosity level |
625 |
|
|
626 |
|
=back |
627 |
|
|
628 |
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 |
629 |
Winblowz) shares to central disk space. Central disk space is organized in |
Winblowz) shares to central disk space. Central disk space is organized in |
630 |
multiple directories named after: |
multiple directories named after: |
753 |
|
|
754 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |
755 |
|
|
756 |
L<http://www.rot13.org/~dpavlin/> |
L<http:E<sol>E<sol>www.rot13.orgE<sol>~dpavlinE<sol>> |
757 |
|
|
758 |
=head1 LICENSE |
=head1 LICENSE |
759 |
|
|