--- psinib.pl 2003/10/12 16:13:38 1.12 +++ psinib.pl 2003/10/26 12:55:56 1.16 @@ -24,17 +24,21 @@ use Fcntl qw(LOCK_EX LOCK_NB); use Digest::MD5; use File::Basename; +use Getopt::Long; # configuration my $LOG_TIME_FMT = '%Y-%m-%d %H:%M:%S'; # strftime format for logfile my $DIR_TIME_FMT = '%Y%m%d'; # strftime format for backup dir +# define timeout for ping +my $PING_TIMEOUT = 5; + my $LOG = '/var/log/backup.log'; # add path here... #$LOG = '/tmp/backup.log'; # store backups in which directory -#my $BACKUP_DEST = '/backup/isis_backup'; -my $BACKUP_DEST = '/tmp/backup/'; +my $BACKUP_DEST = '/backup/isis_backup'; +#my $BACKUP_DEST = '/tmp/backup/'; # files to ignore in backup my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt'); @@ -58,6 +62,12 @@ # taint path: nmblookup should be there! $ENV{'PATH'} = "/usr/bin:/bin"; +my $use_ping = 1; # deault: use ping to verify that host is up + +my $result = GetOptions( + "ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, +); + my $mounts = shift @ARGV || 'mountscript'; # die "usage: $0 mountscript"; @@ -65,9 +75,30 @@ my @in_backup; # shares which are backeduped this run -my $p = new Net::Ping->new("tcp", 2); -# ping will try tcp connect to netbios-ssn (139) -$p->{port_num} = getservbyname("netbios-ssn", "tcp"); +# init Net::Ping object +my $ping; +if ($use_ping) { + $ping = new Net::Ping->new("syn", 2); + # ping will try tcp connect to netbios-ssn (139) + $ping->{port_num} = getservbyname("netbios-ssn", "tcp"); +} + +# do syn ping to cifs port +sub host_up { + my $ping = shift || return; + my $host_ip = shift || xlog("host_up didn't get IP"); + my $timeout = shift; + return 1 if (! $use_ping); + + $ping->ping($host_ip,$timeout); + my $return = 0; + + while (my ($host,$rtt,$ip) = $ping->ack) { + xlog("","HOST: $host [$ip] ACKed in $rtt seconds"); + $return = 1 if ($ip eq $host_ip); + } + return $return; +} my $backup_ok = 0; @@ -129,7 +160,7 @@ if ($ip) { xlog($share,"IP is $ip"); - if ($p->ping($ip)) { + if (host_up($ping, $ip,$PING_TIMEOUT)) { if (snap_share($share,$user,$passwd,$workgroup)) { $backup_ok++; } @@ -259,6 +290,7 @@ my %file_atime; my %file_mtime; #my %file_md5; + %file_md5 = (); my @smb_files; my %smb_size; @@ -490,6 +522,7 @@ # remove files foreach (sort @files2erase) { unlink "$bc/$_" || warn "unlink $_: $!\n"; + delete $file_md5{$_}; } # remove not needed dirs (after files) @@ -502,6 +535,14 @@ unlink "$bc/$_/.md5sum" if (-e "$bc/$_/.md5sum"); } + # erase stale entries in .md5sum + my @md5_files = keys %file_md5; + $lc = List::Compare->new(\@md5_files, \@smb_files); + foreach my $file ($lc->get_Lonly) { + xlog("NOTICE","removing stale '$file' from .md5sum"); + delete $file_md5{$file}; + } + # create .md5sum my $last_dir = ''; my $md5; @@ -658,6 +699,19 @@ linking same files (other alternative would be to erase that dir and find second-oldest directory, but this seemed like more correct approach). +=head2 I can't connect to any share + +Please verify that nmblookup (which is part of samba package) is in /bin or +/usr/bin. Also verify that nmblookup returns IP address for your server +using: + + $ nmblookup tvhouse + querying tvhouse on 192.168.34.255 + 192.168.34.30 tvhouse<00> + +If you don't get any output, your samba might not listen to correct interface +(see interfaces in smb.conf). + =head1 AUTHOR Dobrica Pavlinusic