34 |
|
|
35 |
# store backups in which directory |
# store backups in which directory |
36 |
my $BACKUP_DEST = '/backup/isis_backup'; |
my $BACKUP_DEST = '/backup/isis_backup'; |
37 |
|
#my $BACKUP_DEST = '/tmp/backup/'; |
38 |
|
|
39 |
# files to ignore in backup |
# files to ignore in backup |
40 |
my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt'); |
my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt'); |
51 |
sleep 1; |
sleep 1; |
52 |
redo if ++$c < 10; |
redo if ++$c < 10; |
53 |
# no response for 10 sec, bail out |
# no response for 10 sec, bail out |
54 |
print STDERR "can't take lock on $LOG -- another $0 running?\n"; |
xlog("ABORT","can't take lock on $LOG -- another $0 running?"); |
55 |
exit 1; |
exit 1; |
56 |
} |
} |
57 |
|
|
82 |
next if !/^\s*smbmount\s/; |
next if !/^\s*smbmount\s/; |
83 |
my (undef,$share,undef,$opt) = split(/\s+/,$_,4); |
my (undef,$share,undef,$opt) = split(/\s+/,$_,4); |
84 |
|
|
85 |
my ($user,$passwd,$workgroup); |
my ($user,$passwd,$workgroup,$ip); |
86 |
|
|
87 |
foreach (split(/,/,$opt)) { |
foreach (split(/,/,$opt)) { |
88 |
my ($n,$v) = split(/=/,$_,2); |
my ($n,$v) = split(/=/,$_,2); |
98 |
} |
} |
99 |
} elsif ($n =~ m#workgroup#i) { |
} elsif ($n =~ m#workgroup#i) { |
100 |
$workgroup = $v; |
$workgroup = $v; |
101 |
|
} elsif ($n =~ m#ip#i) { |
102 |
|
$ip = $v; |
103 |
} |
} |
104 |
} |
} |
105 |
|
|
110 |
my $bl = "$BACKUP_DEST/$host/$dir/latest"; # latest backup |
my $bl = "$BACKUP_DEST/$host/$dir/latest"; # latest backup |
111 |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; # current one |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; # current one |
112 |
my $real_bl; |
my $real_bl; |
113 |
if (-e $bl) { |
if (-l $bl) { |
114 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
115 |
$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 "/"); |
116 |
if (-e $bc && $real_bl eq $bc) { |
if (-l $bc && $real_bl eq $bc) { |
117 |
print "$share allready backuped...\n"; |
print "$share allready backuped...\n"; |
118 |
$backup_ok++; |
$backup_ok++; |
119 |
next; |
next; |
124 |
|
|
125 |
print "working on $share\n"; |
print "working on $share\n"; |
126 |
|
|
127 |
|
# try to nmblookup IP |
128 |
my $ip = get_ip($share); |
$ip = get_ip($share) if (! $ip); |
129 |
|
|
130 |
if ($ip) { |
if ($ip) { |
131 |
xlog($share,"IP is $ip"); |
xlog($share,"IP is $ip"); |
132 |
if ($p->ping($ip)) { |
if ($p->ping($ip)) { |
133 |
snap_share($share,$user,$passwd,$workgroup); |
if (snap_share($share,$user,$passwd,$workgroup)) { |
134 |
$backup_ok++; |
$backup_ok++; |
135 |
|
} |
136 |
} |
} |
137 |
} |
} |
138 |
} |
} |
208 |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; |
209 |
|
|
210 |
my $real_bl; |
my $real_bl; |
211 |
if (-e $bl) { |
if (-l $bl) { |
212 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
213 |
$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 "/"); |
214 |
} else { |
} else { |
229 |
} |
} |
230 |
} |
} |
231 |
|
|
232 |
if (-e $bc && $real_bl && $real_bl eq $bc) { |
if (-l $bc && $real_bl && $real_bl eq $bc) { |
233 |
print "$share allready backuped...\n"; |
print "$share allready backuped...\n"; |
234 |
return; |
return 1; |
235 |
} |
} |
236 |
|
|
237 |
die "You should really create BACKUP_DEST [$BACKUP_DEST] by hand! " if (!-e $BACKUP_DEST); |
die "You should really create BACKUP_DEST [$BACKUP_DEST] by hand! " if (!-e $BACKUP_DEST); |
259 |
my %file_atime; |
my %file_atime; |
260 |
my %file_mtime; |
my %file_mtime; |
261 |
#my %file_md5; |
#my %file_md5; |
262 |
|
%file_md5 = (); |
263 |
|
|
264 |
my @smb_files; |
my @smb_files; |
265 |
my %smb_size; |
my %smb_size; |
290 |
push @ignore,norm_dir("$d/$_"); |
push @ignore,norm_dir("$d/$_"); |
291 |
} |
} |
292 |
close(I); |
close(I); |
293 |
print STDERR "ignore: ",join("|",@ignore),"\n"; |
#print STDERR "ignore: ",join("|",@ignore),"\n"; |
294 |
link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" || |
link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" || |
295 |
warn "can't copy $real_bl/$d/.backupignore to current backup dir: $!\n"; |
warn "can't copy $real_bl/$d/.backupignore to current backup dir: $!\n"; |
296 |
} |
} |
321 |
} elsif (-d $pf) { |
} elsif (-d $pf) { |
322 |
push @dirs,$pr; |
push @dirs,$pr; |
323 |
} else { |
} else { |
324 |
print STDERR "unknown type: $pf\n"; |
print STDERR "not file or directory: $pf\n"; |
325 |
} |
} |
326 |
} else { |
} else { |
327 |
print STDERR "ignored: $pr\n"; |
print STDERR "ignored: $pr\n"; |
329 |
} |
} |
330 |
} |
} |
331 |
|
|
332 |
xlog($share,($#files+1)." files and ".($#dirs+1)." dirs on local disk before backup"); |
# local dir always include / |
333 |
|
xlog($share,($#files+1)." files and ".($#dirs)." dirs on local disk before backup"); |
334 |
|
|
335 |
# read smb filesystem |
# read smb filesystem |
336 |
|
|
341 |
|
|
342 |
$di = 0; |
$di = 0; |
343 |
while ($di <= $#smb_dirs) { |
while ($di <= $#smb_dirs) { |
344 |
my $d=$smb_dirs[$di++]; |
my $d=$smb_dirs[$di]; |
345 |
my $pf = norm_dir($d,"smb:$share/"); # path full |
my $pf = norm_dir($d,"smb:$share/"); # path full |
346 |
my $D = $smb->opendir($pf) || warn "smb->opendir($pf): $!\n"; |
my $D = $smb->opendir($pf); |
347 |
|
if (! $D) { |
348 |
|
xlog($share,"FATAL: $share [$pf]: $!"); |
349 |
|
# remove failing dir |
350 |
|
delete $smb_dirs[$di]; |
351 |
|
return 0; # failed |
352 |
|
} |
353 |
|
$di++; |
354 |
|
|
355 |
my @clutter = $smb->readdir_struct($D); |
my @clutter = $smb->readdir_struct($D); |
356 |
foreach my $item (@clutter) { |
foreach my $item (@clutter) { |
368 |
} elsif ($item->[0] == main::SMBC_DIR) { |
} elsif ($item->[0] == main::SMBC_DIR) { |
369 |
push @smb_dirs,$pr; |
push @smb_dirs,$pr; |
370 |
} else { |
} else { |
371 |
print STDERR "unknown type: $pf\n"; |
print STDERR "not file or directory [",$item->[0],"]: $pf\n"; |
372 |
} |
} |
373 |
} else { |
} else { |
374 |
print STDERR "smb ignored: $pr\n"; |
print STDERR "smb ignored: $pr\n"; |
376 |
} |
} |
377 |
} |
} |
378 |
|
|
379 |
xlog($share,($#smb_files+1)." files and ".($#smb_dirs+1)." dirs on remote share"); |
xlog($share,($#smb_files+1)." files and ".($#smb_dirs)." dirs on remote share"); |
380 |
|
|
381 |
# sync dirs |
# sync dirs |
382 |
my $lc = List::Compare->new(\@dirs, \@smb_dirs); |
my $lc = List::Compare->new(\@dirs, \@smb_dirs); |
408 |
|
|
409 |
foreach my $f (@_) { |
foreach my $f (@_) { |
410 |
#print "smb_copy $from/$f -> $to/$f\n"; |
#print "smb_copy $from/$f -> $to/$f\n"; |
|
if (! open(F,"> $to/$f")) { |
|
|
print STDERR "can't open new file $to/$f: $!\n"; |
|
|
next; |
|
|
} |
|
|
|
|
411 |
my $md5 = Digest::MD5->new; |
my $md5 = Digest::MD5->new; |
412 |
|
|
413 |
my $fd = $smb->open("$from/$f"); |
my $fd = $smb->open("$from/$f"); |
414 |
if (! $fd) { |
if (! $fd) { |
415 |
print STDERR "can't open smb file $from/$f: $!\n"; |
xlog("WARNING","can't open smb file $from/$f: $!"); |
416 |
|
next; |
417 |
|
} |
418 |
|
|
419 |
|
if (! open(F,"> $to/$f")) { |
420 |
|
xlog("WARNING","can't open new file $to/$f: $!"); |
421 |
next; |
next; |
422 |
} |
} |
423 |
|
|
491 |
# remove files |
# remove files |
492 |
foreach (sort @files2erase) { |
foreach (sort @files2erase) { |
493 |
unlink "$bc/$_" || warn "unlink $_: $!\n"; |
unlink "$bc/$_" || warn "unlink $_: $!\n"; |
494 |
|
delete $file_md5{$_}; |
495 |
} |
} |
496 |
|
|
497 |
# remove not needed dirs (after files) |
# remove not needed dirs (after files) |
504 |
unlink "$bc/$_/.md5sum" if (-e "$bc/$_/.md5sum"); |
unlink "$bc/$_/.md5sum" if (-e "$bc/$_/.md5sum"); |
505 |
} |
} |
506 |
|
|
507 |
|
# erase stale entries in .md5sum |
508 |
|
my @md5_files = keys %file_md5; |
509 |
|
$lc = List::Compare->new(\@md5_files, \@smb_files); |
510 |
|
foreach my $file ($lc->get_Lonly) { |
511 |
|
xlog("NOTICE","removing stale '$file' from .md5sum"); |
512 |
|
delete $file_md5{$file}; |
513 |
|
} |
514 |
|
|
515 |
# create .md5sum |
# create .md5sum |
516 |
my $last_dir = ''; |
my $last_dir = ''; |
517 |
my $md5; |
my $md5; |
527 |
} |
} |
528 |
print $md5 $file_md5{$f}," $file\n"; |
print $md5 $file_md5{$f}," $file\n"; |
529 |
} |
} |
530 |
close($md5); |
close($md5) if ($md5); |
531 |
|
|
532 |
# create leatest link |
# create leatest link |
533 |
#print "ln -s $bc $real_bl\n"; |
#print "ln -s $bc $real_bl\n"; |
534 |
if (-e $bl) { |
if (-l $bl) { |
535 |
unlink $bl || warn "can't remove old latest symlink $bl: $!\n"; |
unlink $bl || warn "can't remove old latest symlink $bl: $!\n"; |
536 |
} |
} |
537 |
symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
538 |
|
|
539 |
# FIX: sanity check -- remove for speedup |
# FIX: sanity check -- remove for speedup |
540 |
xlog($share,"failed to create latest symlink...") if (readlink($bl) ne $bc || ! -e $bl); |
xlog($share,"failed to create latest symlink $bl -> $bc...") if (readlink($bl) ne $bc || ! -l $bl); |
541 |
|
|
542 |
xlog($share,"backup completed..."); |
xlog($share,"backup completed..."); |
|
} |
|
543 |
|
|
544 |
|
return 1; |
545 |
|
} |
546 |
__END__ |
__END__ |
547 |
#------------------------------------------------------------------------- |
#------------------------------------------------------------------------- |
548 |
|
|