--- psinib.pl 2003/10/12 16:13:38 1.12 +++ psinib.pl 2003/10/27 18:58:41 1.18 @@ -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,17 @@ # taint path: nmblookup should be there! $ENV{'PATH'} = "/usr/bin:/bin"; +my $use_ping = 1; # default: use syn tcp ping to verify that host is up +my $verbose = 1; # default verbosity level +my $quiet = 0; + +my $result = GetOptions( + "ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, + "verbose+" => \$verbose, "quiet+" => \$quiet, +); + +$verbose -= $quiet; + my $mounts = shift @ARGV || 'mountscript'; # die "usage: $0 mountscript"; @@ -65,9 +80,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; @@ -114,7 +150,7 @@ $real_bl=readlink($bl) || die "can't read link $bl: $!"; $real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); if (-l $bc && $real_bl eq $bc) { - print "$share allready backuped...\n"; + xlog($share,"allready backuped..."); $backup_ok++; next; } @@ -122,14 +158,14 @@ } - print "working on $share\n"; + xlog($share,"working on $share..."); # try to nmblookup IP $ip = get_ip($share) if (! $ip); 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++; } @@ -163,13 +199,15 @@ my $share = shift; my $t = strftime $LOG_TIME_FMT, localtime; my $m = shift || '[no log entry]'; - print STDERR $m,"\n"; + my $l = shift; + $l = 1 if (! defined $l); # default verbosity is 1 + print STDERR $m,"\n" if ($verbose >= $l); print L "$t $share\t$m\n"; } # dump warn and dies into log -BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0]) ; warn $_[0] } } -BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0]) ; die $_[0] } } +BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; warn $_[0] } } +BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; die $_[0] } } # split share name to host, dir and currnet date dir @@ -182,7 +220,7 @@ $dir =~ s/^_+//; $dir =~ s/_+$//; } else { - print "Can't parse share $share into host and directory!\n"; + xlog($share,"Can't parse share $share into host and directory!",1); return; } return ($host,$dir,strftime $DIR_TIME_FMT, localtime); @@ -211,13 +249,15 @@ if (-l $bl) { $real_bl=readlink($bl) || die "can't read link $bl: $!"; $real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); - } else { - print "no old backup, trying to find last backup, "; + undef $real_bl if (! -e $real_bl); + } + if (! $real_bl) { + xlog($share,"no old backup, trying to find last backup,"); if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR); closedir(BL_DIR); $real_bl=pop @bl_dirs; - print "using $real_bl as latest...\n"; + xlog($share,"using $real_bl as latest..."); $real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); if ($real_bl eq $bc) { xlog($share,"latest from today (possible partial backup)"); @@ -225,12 +265,12 @@ $real_bl .= ".partial"; } } else { - print "this is first run...\n"; + xlog($share,"this is first run..."); } } if (-l $bc && $real_bl && $real_bl eq $bc) { - print "$share allready backuped...\n"; + xlog($share,"allready backuped..."); return 1; } @@ -238,12 +278,12 @@ if (! -e "$BACKUP_DEST/$host") { mkdir "$BACKUP_DEST/$host" || die "can't make dir for host $host, $BACKUP_DEST/$host: $!"; - print "created host directory $BACKUP_DEST/$host...\n"; + xlog($share,"created host directory $BACKUP_DEST/$host..."); } if (! -e "$BACKUP_DEST/$host/$dir") { mkdir "$BACKUP_DEST/$host/$dir" || die "can't make dir for share $share, $BACKUP_DEST/$host/$dir $!"; - print "created dir for share $share, $BACKUP_DEST/$host/$dir...\n"; + xlog($share,"created dir for this share $BACKUP_DEST/$host/$dir..."); } mkdir $bc || die "can't make dir for current backup $bc: $!"; @@ -259,6 +299,7 @@ my %file_atime; my %file_mtime; #my %file_md5; + %file_md5 = (); my @smb_files; my %smb_size; @@ -320,10 +361,10 @@ } elsif (-d $pf) { push @dirs,$pr; } else { - print STDERR "not file or directory: $pf\n"; + xlog($share,"not file or directory: $pf",0); } } else { - print STDERR "ignored: $pr\n"; + xlog($share,"ignored: $pr"); } } } @@ -344,7 +385,7 @@ my $pf = norm_dir($d,"smb:$share/"); # path full my $D = $smb->opendir($pf); if (! $D) { - xlog($share,"FATAL: $share [$pf]: $!"); + xlog($share,"FATAL: $share [$pf] as $param{username}/$param{workgroup}: $!",0); # remove failing dir delete $smb_dirs[$di]; return 0; # failed @@ -367,10 +408,10 @@ } elsif ($item->[0] == main::SMBC_DIR) { push @smb_dirs,$pr; } else { - print STDERR "not file or directory [",$item->[0],"]: $pf\n"; + xlog($share,"not file or directory [".$item->[0]."]: $pf",0); } } else { - print STDERR "smb ignored: $pr\n"; + xlog($share,"smb ignored: $pr"); } } } @@ -490,6 +531,7 @@ # remove files foreach (sort @files2erase) { unlink "$bc/$_" || warn "unlink $_: $!\n"; + delete $file_md5{$_}; } # remove not needed dirs (after files) @@ -502,6 +544,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; @@ -543,10 +593,34 @@ =head1 SYNOPSIS -./psinib.pl +./psinib.pl [OPTION]... [mount script] =head1 DESCRIPTION +Option can be one of more of following: + +=over 8 + +=item C<--backupdest=dir> + +Specify backup destination directory (defaults is /data/ + +=item C<--noping> + +Don't use ping to check if host is up (default is ti use tcp syn to cifs +port) + +=item C<--verbose -v> + +Increase verbosity level. Defailt is 1 which prints moderate amount of data +on STDOUT and STDERR. + +=item C<--quiet -q> + +Decrease verbosity level + +=back + This script in current version support just backup of Samba (or Micro$oft Winblowz) shares to central disk space. Central disk space is organized in multiple directories named after: @@ -658,11 +732,24 @@ 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 -L +LEwww.rot13.orgE~dpavlinE> =head1 LICENSE