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 |
|
|
107 |
my $bl = "$BACKUP_DEST/$host/$dir/latest"; # latest backup |
my $bl = "$BACKUP_DEST/$host/$dir/latest"; # latest backup |
108 |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; # current one |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; # current one |
109 |
my $real_bl; |
my $real_bl; |
110 |
if (-e $bl) { |
if (-l $bl) { |
111 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
112 |
$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 "/"); |
113 |
if (-e $bc && $real_bl eq $bc) { |
if (-l $bc && $real_bl eq $bc) { |
114 |
print "$share allready backuped...\n"; |
print "$share allready backuped...\n"; |
115 |
$backup_ok++; |
$backup_ok++; |
116 |
next; |
next; |
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 |
|
|
204 |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; |
205 |
|
|
206 |
my $real_bl; |
my $real_bl; |
207 |
if (-e $bl) { |
if (-l $bl) { |
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 (-l $bc && $real_bl && $real_bl eq $bc) { |
229 |
print "$share allready backuped...\n"; |
print "$share allready backuped...\n"; |
230 |
return; |
return; |
231 |
} |
} |
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; |
335 |
|
|
336 |
$di = 0; |
$di = 0; |
337 |
while ($di <= $#smb_dirs) { |
while ($di <= $#smb_dirs) { |
338 |
my $d=$smb_dirs[$di++]; |
my $d=$smb_dirs[$di]; |
339 |
my $pf = norm_dir($d,"smb:$share/"); # path full |
my $pf = norm_dir($d,"smb:$share/"); # path full |
340 |
my $D = $smb->opendir($pf) || warn "smb->opendir($pf): $!\n"; |
my $D = $smb->opendir($pf); |
341 |
|
if (! $D) { |
342 |
|
xlog($share,"FATAL: $share: $!"); |
343 |
|
# remove failing dir |
344 |
|
delete $smb_dirs[$di]; |
345 |
|
next; |
346 |
|
} |
347 |
|
$di++; |
348 |
|
|
349 |
my @clutter = $smb->readdir_struct($D); |
my @clutter = $smb->readdir_struct($D); |
350 |
foreach my $item (@clutter) { |
foreach my $item (@clutter) { |
479 |
xlog($share,"$transfer bytes transfered..."); |
xlog($share,"$transfer bytes transfered..."); |
480 |
|
|
481 |
foreach (@ln_files) { |
foreach (@ln_files) { |
482 |
link "$bl/$_","$bc/$_" || warn "link $bl/$_ -> $bc/$_: $!\n"; |
link "$real_bl/$_","$bc/$_" || warn "link $real_bl/$_ -> $bc/$_: $!\n"; |
483 |
} |
} |
484 |
|
|
485 |
# remove files |
# remove files |
500 |
# create .md5sum |
# create .md5sum |
501 |
my $last_dir = ''; |
my $last_dir = ''; |
502 |
my $md5; |
my $md5; |
503 |
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) { |
504 |
my $dir = dirname($f); |
my $dir = dirname($f); |
505 |
my $file = basename($f); |
my $file = basename($f); |
506 |
print "$f -- $dir / $file<--\n"; |
#print "$f -- $dir / $file<--\n"; |
507 |
if ($dir ne $last_dir) { |
if ($dir ne $last_dir) { |
508 |
close($md5) if ($md5); |
close($md5) if ($md5); |
509 |
open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!"; |
open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!"; |
510 |
$last_dir = $dir; |
$last_dir = $dir; |
511 |
print STDERR "writing $last_dir/.md5sum\n"; |
#print STDERR "writing $last_dir/.md5sum\n"; |
512 |
} |
} |
513 |
print $md5 $file_md5{$f}," $file\n"; |
print $md5 $file_md5{$f}," $file\n"; |
514 |
} |
} |
515 |
close($md5); |
close($md5); |
516 |
|
|
517 |
# create leatest link |
# create leatest link |
518 |
# symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
#print "ln -s $bc $real_bl\n"; |
519 |
|
if (-l $bl) { |
520 |
|
unlink $bl || warn "can't remove old latest symlink $bl: $!\n"; |
521 |
|
} |
522 |
|
symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
523 |
|
|
524 |
|
# FIX: sanity check -- remove for speedup |
525 |
|
xlog($share,"failed to create latest symlink $bl -> $bc...") if (readlink($bl) ne $bc || ! -l $bl); |
526 |
|
|
527 |
xlog($share,"backup completed..."); |
xlog($share,"backup completed..."); |
528 |
} |
} |
581 |
over network) |
over network) |
582 |
|
|
583 |
=item - usage of C<.md5sum> files (compatible with command-line utility |
=item - usage of C<.md5sum> files (compatible with command-line utility |
584 |
C<md5sum> to keep file between snapshots hard-linked |
C<md5sum>) to keep file between snapshots hard-linked |
585 |
|
|
586 |
=back |
=back |
587 |
|
|
625 |
|
|
626 |
server/c_WinNT_fonts/yyyymmdd/.... |
server/c_WinNT_fonts/yyyymmdd/.... |
627 |
|
|
628 |
|
=head2 Won't I run out of disk space? |
629 |
|
|
630 |
|
Of course you will... Snapshots and logfiles will eventually fill-up your disk. |
631 |
|
However, you can do two things to stop that: |
632 |
|
|
633 |
|
=head3 Clean snapshort older than x days |
634 |
|
|
635 |
|
You can add following command to your C<root> crontab: |
636 |
|
|
637 |
|
find /backup/isis_backup -type d -mindepth 3 -maxdepth 3 -mtime +11 -exec rm -Rf {} \; |
638 |
|
|
639 |
|
I assume that C</backup/isis_backup> is directory in which are your snapshots |
640 |
|
and that you don't want to keep snapshots older than 11 days (that's |
641 |
|
C<-mtime +11> part of command). |
642 |
|
|
643 |
|
=head3 Rotate your logs |
644 |
|
|
645 |
|
I will leave that to you. I relay on GNU/Debian's C<logrotate> to do it for me. |
646 |
|
|
647 |
|
=head2 What are I<YYYYMMDD.partial> directories? |
648 |
|
|
649 |
|
If there isn't I<latest> symlink in snapshot directory, it's preatty safe to |
650 |
|
assume that previous backup from that day failed. So, that directory will |
651 |
|
be renamed to I<YYYYMMDD.partial> and snapshot will be performed again, |
652 |
|
linking same files (other alternative would be to erase that dir and find |
653 |
|
second-oldest directory, but this seemed like more correct approach). |
654 |
|
|
655 |
=head1 AUTHOR |
=head1 AUTHOR |
656 |
|
|