64 |
|
|
65 |
my @in_backup; # shares which are backeduped this run |
my @in_backup; # shares which are backeduped this run |
66 |
|
|
67 |
my $p = new Net::Ping->new(); |
my $p = new Net::Ping->new("tcp", 2); |
68 |
|
# ping will try tcp connect to netbios-ssn (139) |
69 |
|
$p->{port_num} = getservbyname("netbios-ssn", "tcp"); |
70 |
|
|
71 |
my $backup_ok = 0; |
my $backup_ok = 0; |
72 |
|
|
163 |
print L "$t $share\t$m\n"; |
print L "$t $share\t$m\n"; |
164 |
} |
} |
165 |
|
|
166 |
|
# dump warn and dies into log |
167 |
|
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0]) ; warn $_[0] } } |
168 |
|
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0]) ; die $_[0] } } |
169 |
|
|
170 |
|
|
171 |
# split share name to host, dir and currnet date dir |
# split share name to host, dir and currnet date dir |
172 |
sub share2host_dir { |
sub share2host_dir { |
192 |
|
|
193 |
my %param = ( debug => 0 ); |
my %param = ( debug => 0 ); |
194 |
|
|
195 |
$param{username} = shift; |
$param{username} = shift || warn "can't find username for share $share"; |
196 |
$param{password} = shift; |
$param{password} = shift || warn "can't find passwod for share $share"; |
197 |
$param{workgroup} = shift; |
$param{workgroup} = shift || warn "can't find workgroup for share $share"; |
198 |
|
|
199 |
my ($host,$dir,$date_dir) = share2host_dir($share); |
my ($host,$dir,$date_dir) = share2host_dir($share); |
200 |
|
|
208 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
209 |
$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 "/"); |
210 |
} else { |
} else { |
211 |
print "no old backup, this is first run...\n"; |
print "no old backup, trying to find last backup, "; |
212 |
|
if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { |
213 |
|
my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR); |
214 |
|
closedir(BL_DIR); |
215 |
|
$real_bl=pop @bl_dirs; |
216 |
|
print "using $real_bl as latest...\n"; |
217 |
|
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
218 |
|
if ($real_bl eq $bc) { |
219 |
|
xlog($share,"latest from today (possible partial backup)"); |
220 |
|
rename $real_bl,$real_bl.".partial" || warn "can't reaname partial backup: $!"; |
221 |
|
$real_bl .= ".partial"; |
222 |
|
} |
223 |
|
} else { |
224 |
|
print "this is first run...\n"; |
225 |
|
} |
226 |
} |
} |
227 |
|
|
228 |
if (-e $bc && $real_bl && $real_bl eq $bc) { |
if (-e $bc && $real_bl && $real_bl eq $bc) { |
275 |
my $di = 0; |
my $di = 0; |
276 |
while ($di <= $#dirs && $real_bl) { |
while ($di <= $#dirs && $real_bl) { |
277 |
my $d=$dirs[$di++]; |
my $d=$dirs[$di++]; |
278 |
opendir(DIR,"$bl/$d") || warn "opendir($bl/$d): $!\n"; |
opendir(DIR,"$real_bl/$d") || warn "opendir($real_bl/$d): $!\n"; |
279 |
|
|
280 |
# read .backupignore if exists |
# read .backupignore if exists |
281 |
if (-f "$bl/$d/.backupignore") { |
if (-f "$real_bl/$d/.backupignore") { |
282 |
open(I,"$bl/$d/.backupignore"); |
open(I,"$real_bl/$d/.backupignore"); |
283 |
while(<I>) { |
while(<I>) { |
284 |
chomp; |
chomp; |
285 |
push @ignore,norm_dir("$d/$_"); |
push @ignore,norm_dir("$d/$_"); |
286 |
} |
} |
287 |
close(I); |
close(I); |
288 |
print STDERR "ignore: ",join("|",@ignore),"\n"; |
print STDERR "ignore: ",join("|",@ignore),"\n"; |
289 |
link "$bl/$d/.backupignore","$bc/$d/.backupignore" || |
link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" || |
290 |
warn "can't copy $bl/$d/.backupignore to current backup dir: $!\n"; |
warn "can't copy $real_bl/$d/.backupignore to current backup dir: $!\n"; |
291 |
} |
} |
292 |
|
|
293 |
# read .md5sum if exists |
# read .md5sum if exists |
294 |
if (-f "$bl/$d/.md5sum") { |
if (-f "$real_bl/$d/.md5sum") { |
295 |
open(I,"$bl/$d/.md5sum"); |
open(I,"$real_bl/$d/.md5sum"); |
296 |
while(<I>) { |
while(<I>) { |
297 |
chomp; |
chomp; |
298 |
my ($md5,$f) = split(/\s+/,$_,2); |
my ($md5,$f) = split(/\s+/,$_,2); |
306 |
next if ($f eq '.'); |
next if ($f eq '.'); |
307 |
next if ($f eq '..'); |
next if ($f eq '..'); |
308 |
my $pr = norm_dir("$d/$f"); # path relative |
my $pr = norm_dir("$d/$f"); # path relative |
309 |
my $pf = norm_dir("$d/$f","$bl/"); # path full |
my $pf = norm_dir("$d/$f","$real_bl/"); # path full |
310 |
if (grep(/^\Q$pr\E$/,@ignore) == 0) { |
if (grep(/^\Q$pr\E$/,@ignore) == 0) { |
311 |
if (-f $pf) { |
if (-f $pf) { |
312 |
push @files,$pr; |
push @files,$pr; |
472 |
xlog($share,"$transfer bytes transfered..."); |
xlog($share,"$transfer bytes transfered..."); |
473 |
|
|
474 |
foreach (@ln_files) { |
foreach (@ln_files) { |
475 |
link "$bl/$_","$bc/$_" || warn "link $bl/$_ -> $bc/$_: $!\n"; |
link "$real_bl/$_","$bc/$_" || warn "link $real_bl/$_ -> $bc/$_: $!\n"; |
476 |
} |
} |
477 |
|
|
478 |
# remove files |
# remove files |
493 |
# create .md5sum |
# create .md5sum |
494 |
my $last_dir = ''; |
my $last_dir = ''; |
495 |
my $md5; |
my $md5; |
496 |
foreach my $f (sort { $file_md5{$a}<=>$file_md5{$b} } keys %file_md5) { |
foreach my $f (sort { $file_md5{$a} cmp $file_md5{$b} } keys %file_md5) { |
497 |
my $dir = dirname($f); |
my $dir = dirname($f); |
498 |
my $file = basename($f); |
my $file = basename($f); |
499 |
print "$f -- $dir / $file<--\n"; |
#print "$f -- $dir / $file<--\n"; |
500 |
if ($dir ne $last_dir) { |
if ($dir ne $last_dir) { |
501 |
close($md5) if ($md5); |
close($md5) if ($md5); |
502 |
open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!"; |
open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!"; |
503 |
$last_dir = $dir; |
$last_dir = $dir; |
504 |
print STDERR "writing $last_dir/.md5sum\n"; |
#print STDERR "writing $last_dir/.md5sum\n"; |
505 |
} |
} |
506 |
print $md5 $file_md5{$f}," $file\n"; |
print $md5 $file_md5{$f}," $file\n"; |
507 |
} |
} |
508 |
close($md5); |
close($md5); |
509 |
|
|
510 |
# create leatest link |
# create leatest link |
511 |
# symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
#print "ln -s $bc $real_bl\n"; |
512 |
|
if (-e $bl) { |
513 |
|
unlink $bl || warn "can't remove old latest symlink $bl: $!\n"; |
514 |
|
} |
515 |
|
symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
516 |
|
|
517 |
|
# FIX: sanity check -- remove for speedup |
518 |
|
xlog($share,"failed to create latest symlink...") if (readlink($bl) ne $bc || ! -e $bl); |
519 |
|
|
520 |
xlog($share,"backup completed..."); |
xlog($share,"backup completed..."); |
521 |
} |
} |
637 |
|
|
638 |
I will leave that to you. I relay on GNU/Debian's C<logrotate> to do it for me. |
I will leave that to you. I relay on GNU/Debian's C<logrotate> to do it for me. |
639 |
|
|
640 |
|
=head2 What are I<YYYYMMDD.partial> directories? |
641 |
|
|
642 |
|
If there isn't I<latest> symlink in snapshot directory, it's preatty safe to |
643 |
|
assume that previous backup from that day failed. So, that directory will |
644 |
|
be renamed to I<YYYYMMDD.partial> and snapshot will be performed again, |
645 |
|
linking same files (other alternative would be to erase that dir and find |
646 |
|
second-oldest directory, but this seemed like more correct approach). |
647 |
|
|
648 |
=head1 AUTHOR |
=head1 AUTHOR |
649 |
|
|
650 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |