24 |
use Fcntl qw(LOCK_EX LOCK_NB); |
use Fcntl qw(LOCK_EX LOCK_NB); |
25 |
use Digest::MD5; |
use Digest::MD5; |
26 |
use File::Basename; |
use File::Basename; |
27 |
|
use Getopt::Long; |
28 |
|
|
29 |
# configuration |
# configuration |
30 |
my $LOG_TIME_FMT = '%Y-%m-%d %H:%M:%S'; # strftime format for logfile |
my $LOG_TIME_FMT = '%Y-%m-%d %H:%M:%S'; # strftime format for logfile |
31 |
my $DIR_TIME_FMT = '%Y%m%d'; # strftime format for backup dir |
my $DIR_TIME_FMT = '%Y%m%d'; # strftime format for backup dir |
32 |
|
|
33 |
|
# define timeout for ping |
34 |
|
my $PING_TIMEOUT = 5; |
35 |
|
|
36 |
my $LOG = '/var/log/backup.log'; # add path here... |
my $LOG = '/var/log/backup.log'; # add path here... |
37 |
#$LOG = '/tmp/backup.log'; |
#$LOG = '/tmp/backup.log'; |
38 |
|
|
39 |
# store backups in which directory |
# store backups in which directory |
40 |
my $BACKUP_DEST = '/backup/isis_backup'; |
my $BACKUP_DEST = '/backup/isis_backup'; |
41 |
|
#my $BACKUP_DEST = '/tmp/backup/'; |
42 |
|
|
43 |
# files to ignore in backup |
# files to ignore in backup |
44 |
my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt'); |
my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt'); |
55 |
sleep 1; |
sleep 1; |
56 |
redo if ++$c < 10; |
redo if ++$c < 10; |
57 |
# no response for 10 sec, bail out |
# no response for 10 sec, bail out |
58 |
print STDERR "can't take lock on $LOG -- another $0 running?\n"; |
xlog("ABORT","can't take lock on $LOG -- another $0 running?"); |
59 |
exit 1; |
exit 1; |
60 |
} |
} |
61 |
|
|
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 |
66 |
|
|
67 |
|
my $result = GetOptions( |
68 |
|
"ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, |
69 |
|
); |
70 |
|
|
71 |
my $mounts = shift @ARGV || |
my $mounts = shift @ARGV || |
72 |
'mountscript'; |
'mountscript'; |
73 |
# die "usage: $0 mountscript"; |
# die "usage: $0 mountscript"; |
75 |
|
|
76 |
my @in_backup; # shares which are backeduped this run |
my @in_backup; # shares which are backeduped this run |
77 |
|
|
78 |
my $p = new Net::Ping->new("tcp", 2); |
# init Net::Ping object |
79 |
# ping will try tcp connect to netbios-ssn (139) |
my $ping; |
80 |
$p->{port_num} = getservbyname("netbios-ssn", "tcp"); |
if ($use_ping) { |
81 |
|
$ping = new Net::Ping->new("syn", 2); |
82 |
|
# ping will try tcp connect to netbios-ssn (139) |
83 |
|
$ping->{port_num} = getservbyname("netbios-ssn", "tcp"); |
84 |
|
} |
85 |
|
|
86 |
|
# do syn ping to cifs port |
87 |
|
sub host_up { |
88 |
|
my $ping = shift || return; |
89 |
|
my $host_ip = shift || xlog("host_up didn't get IP"); |
90 |
|
my $timeout = shift; |
91 |
|
return 1 if (! $use_ping); |
92 |
|
|
93 |
|
$ping->ping($host_ip,$timeout); |
94 |
|
my $return = 0; |
95 |
|
|
96 |
|
while (my ($host,$rtt,$ip) = $ping->ack) { |
97 |
|
xlog("","HOST: $host [$ip] ACKed in $rtt seconds"); |
98 |
|
$return = 1 if ($ip eq $host_ip); |
99 |
|
} |
100 |
|
return $return; |
101 |
|
} |
102 |
|
|
103 |
my $backup_ok = 0; |
my $backup_ok = 0; |
104 |
|
|
113 |
next if !/^\s*smbmount\s/; |
next if !/^\s*smbmount\s/; |
114 |
my (undef,$share,undef,$opt) = split(/\s+/,$_,4); |
my (undef,$share,undef,$opt) = split(/\s+/,$_,4); |
115 |
|
|
116 |
my ($user,$passwd,$workgroup); |
my ($user,$passwd,$workgroup,$ip); |
117 |
|
|
118 |
foreach (split(/,/,$opt)) { |
foreach (split(/,/,$opt)) { |
119 |
my ($n,$v) = split(/=/,$_,2); |
my ($n,$v) = split(/=/,$_,2); |
129 |
} |
} |
130 |
} elsif ($n =~ m#workgroup#i) { |
} elsif ($n =~ m#workgroup#i) { |
131 |
$workgroup = $v; |
$workgroup = $v; |
132 |
|
} elsif ($n =~ m#ip#i) { |
133 |
|
$ip = $v; |
134 |
} |
} |
135 |
} |
} |
136 |
|
|
141 |
my $bl = "$BACKUP_DEST/$host/$dir/latest"; # latest backup |
my $bl = "$BACKUP_DEST/$host/$dir/latest"; # latest backup |
142 |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; # current one |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; # current one |
143 |
my $real_bl; |
my $real_bl; |
144 |
if (-e $bl) { |
if (-l $bl) { |
145 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
146 |
$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 "/"); |
147 |
if (-e $bc && $real_bl eq $bc) { |
if (-l $bc && $real_bl eq $bc) { |
148 |
print "$share allready backuped...\n"; |
print "$share allready backuped...\n"; |
149 |
$backup_ok++; |
$backup_ok++; |
150 |
next; |
next; |
155 |
|
|
156 |
print "working on $share\n"; |
print "working on $share\n"; |
157 |
|
|
158 |
|
# try to nmblookup IP |
159 |
my $ip = get_ip($share); |
$ip = get_ip($share) if (! $ip); |
160 |
|
|
161 |
if ($ip) { |
if ($ip) { |
162 |
xlog($share,"IP is $ip"); |
xlog($share,"IP is $ip"); |
163 |
if ($p->ping($ip)) { |
if (host_up($ping, $ip,$PING_TIMEOUT)) { |
164 |
snap_share($share,$user,$passwd,$workgroup); |
if (snap_share($share,$user,$passwd,$workgroup)) { |
165 |
$backup_ok++; |
$backup_ok++; |
166 |
|
} |
167 |
} |
} |
168 |
} |
} |
169 |
} |
} |
239 |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; |
my $bc = "$BACKUP_DEST/$host/$dir/$date_dir"; |
240 |
|
|
241 |
my $real_bl; |
my $real_bl; |
242 |
if (-e $bl) { |
if (-l $bl) { |
243 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
244 |
$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 "/"); |
245 |
} else { |
} else { |
260 |
} |
} |
261 |
} |
} |
262 |
|
|
263 |
if (-e $bc && $real_bl && $real_bl eq $bc) { |
if (-l $bc && $real_bl && $real_bl eq $bc) { |
264 |
print "$share allready backuped...\n"; |
print "$share allready backuped...\n"; |
265 |
return; |
return 1; |
266 |
} |
} |
267 |
|
|
268 |
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); |
290 |
my %file_atime; |
my %file_atime; |
291 |
my %file_mtime; |
my %file_mtime; |
292 |
#my %file_md5; |
#my %file_md5; |
293 |
|
%file_md5 = (); |
294 |
|
|
295 |
my @smb_files; |
my @smb_files; |
296 |
my %smb_size; |
my %smb_size; |
321 |
push @ignore,norm_dir("$d/$_"); |
push @ignore,norm_dir("$d/$_"); |
322 |
} |
} |
323 |
close(I); |
close(I); |
324 |
print STDERR "ignore: ",join("|",@ignore),"\n"; |
#print STDERR "ignore: ",join("|",@ignore),"\n"; |
325 |
link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" || |
link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" || |
326 |
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"; |
327 |
} |
} |
352 |
} elsif (-d $pf) { |
} elsif (-d $pf) { |
353 |
push @dirs,$pr; |
push @dirs,$pr; |
354 |
} else { |
} else { |
355 |
print STDERR "unknown type: $pf\n"; |
print STDERR "not file or directory: $pf\n"; |
356 |
} |
} |
357 |
} else { |
} else { |
358 |
print STDERR "ignored: $pr\n"; |
print STDERR "ignored: $pr\n"; |
360 |
} |
} |
361 |
} |
} |
362 |
|
|
363 |
xlog($share,($#files+1)." files and ".($#dirs+1)." dirs on local disk before backup"); |
# local dir always include / |
364 |
|
xlog($share,($#files+1)." files and ".($#dirs)." dirs on local disk before backup"); |
365 |
|
|
366 |
# read smb filesystem |
# read smb filesystem |
367 |
|
|
372 |
|
|
373 |
$di = 0; |
$di = 0; |
374 |
while ($di <= $#smb_dirs) { |
while ($di <= $#smb_dirs) { |
375 |
my $d=$smb_dirs[$di++]; |
my $d=$smb_dirs[$di]; |
376 |
my $pf = norm_dir($d,"smb:$share/"); # path full |
my $pf = norm_dir($d,"smb:$share/"); # path full |
377 |
my $D = $smb->opendir($pf) || warn "smb->opendir($pf): $!\n"; |
my $D = $smb->opendir($pf); |
378 |
|
if (! $D) { |
379 |
|
xlog($share,"FATAL: $share [$pf]: $!"); |
380 |
|
# remove failing dir |
381 |
|
delete $smb_dirs[$di]; |
382 |
|
return 0; # failed |
383 |
|
} |
384 |
|
$di++; |
385 |
|
|
386 |
my @clutter = $smb->readdir_struct($D); |
my @clutter = $smb->readdir_struct($D); |
387 |
foreach my $item (@clutter) { |
foreach my $item (@clutter) { |
399 |
} elsif ($item->[0] == main::SMBC_DIR) { |
} elsif ($item->[0] == main::SMBC_DIR) { |
400 |
push @smb_dirs,$pr; |
push @smb_dirs,$pr; |
401 |
} else { |
} else { |
402 |
print STDERR "unknown type: $pf\n"; |
print STDERR "not file or directory [",$item->[0],"]: $pf\n"; |
403 |
} |
} |
404 |
} else { |
} else { |
405 |
print STDERR "smb ignored: $pr\n"; |
print STDERR "smb ignored: $pr\n"; |
407 |
} |
} |
408 |
} |
} |
409 |
|
|
410 |
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"); |
411 |
|
|
412 |
# sync dirs |
# sync dirs |
413 |
my $lc = List::Compare->new(\@dirs, \@smb_dirs); |
my $lc = List::Compare->new(\@dirs, \@smb_dirs); |
439 |
|
|
440 |
foreach my $f (@_) { |
foreach my $f (@_) { |
441 |
#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; |
|
|
} |
|
|
|
|
442 |
my $md5 = Digest::MD5->new; |
my $md5 = Digest::MD5->new; |
443 |
|
|
444 |
my $fd = $smb->open("$from/$f"); |
my $fd = $smb->open("$from/$f"); |
445 |
if (! $fd) { |
if (! $fd) { |
446 |
print STDERR "can't open smb file $from/$f: $!\n"; |
xlog("WARNING","can't open smb file $from/$f: $!"); |
447 |
|
next; |
448 |
|
} |
449 |
|
|
450 |
|
if (! open(F,"> $to/$f")) { |
451 |
|
xlog("WARNING","can't open new file $to/$f: $!"); |
452 |
next; |
next; |
453 |
} |
} |
454 |
|
|
522 |
# remove files |
# remove files |
523 |
foreach (sort @files2erase) { |
foreach (sort @files2erase) { |
524 |
unlink "$bc/$_" || warn "unlink $_: $!\n"; |
unlink "$bc/$_" || warn "unlink $_: $!\n"; |
525 |
|
delete $file_md5{$_}; |
526 |
} |
} |
527 |
|
|
528 |
# remove not needed dirs (after files) |
# remove not needed dirs (after files) |
535 |
unlink "$bc/$_/.md5sum" if (-e "$bc/$_/.md5sum"); |
unlink "$bc/$_/.md5sum" if (-e "$bc/$_/.md5sum"); |
536 |
} |
} |
537 |
|
|
538 |
|
# erase stale entries in .md5sum |
539 |
|
my @md5_files = keys %file_md5; |
540 |
|
$lc = List::Compare->new(\@md5_files, \@smb_files); |
541 |
|
foreach my $file ($lc->get_Lonly) { |
542 |
|
xlog("NOTICE","removing stale '$file' from .md5sum"); |
543 |
|
delete $file_md5{$file}; |
544 |
|
} |
545 |
|
|
546 |
# create .md5sum |
# create .md5sum |
547 |
my $last_dir = ''; |
my $last_dir = ''; |
548 |
my $md5; |
my $md5; |
558 |
} |
} |
559 |
print $md5 $file_md5{$f}," $file\n"; |
print $md5 $file_md5{$f}," $file\n"; |
560 |
} |
} |
561 |
close($md5); |
close($md5) if ($md5); |
562 |
|
|
563 |
# create leatest link |
# create leatest link |
564 |
#print "ln -s $bc $real_bl\n"; |
#print "ln -s $bc $real_bl\n"; |
565 |
if (-e $bl) { |
if (-l $bl) { |
566 |
unlink $bl || warn "can't remove old latest symlink $bl: $!\n"; |
unlink $bl || warn "can't remove old latest symlink $bl: $!\n"; |
567 |
} |
} |
568 |
symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n"; |
569 |
|
|
570 |
# FIX: sanity check -- remove for speedup |
# FIX: sanity check -- remove for speedup |
571 |
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); |
572 |
|
|
573 |
xlog($share,"backup completed..."); |
xlog($share,"backup completed..."); |
|
} |
|
574 |
|
|
575 |
|
return 1; |
576 |
|
} |
577 |
__END__ |
__END__ |
578 |
#------------------------------------------------------------------------- |
#------------------------------------------------------------------------- |
579 |
|
|
699 |
linking same files (other alternative would be to erase that dir and find |
linking same files (other alternative would be to erase that dir and find |
700 |
second-oldest directory, but this seemed like more correct approach). |
second-oldest directory, but this seemed like more correct approach). |
701 |
|
|
702 |
|
=head2 I can't connect to any share |
703 |
|
|
704 |
|
Please verify that nmblookup (which is part of samba package) is in /bin or |
705 |
|
/usr/bin. Also verify that nmblookup returns IP address for your server |
706 |
|
using: |
707 |
|
|
708 |
|
$ nmblookup tvhouse |
709 |
|
querying tvhouse on 192.168.34.255 |
710 |
|
192.168.34.30 tvhouse<00> |
711 |
|
|
712 |
|
If you don't get any output, your samba might not listen to correct interface |
713 |
|
(see interfaces in smb.conf). |
714 |
|
|
715 |
=head1 AUTHOR |
=head1 AUTHOR |
716 |
|
|
717 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |