/[psinib]/psinib.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /psinib.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by dpavlin, Tue Mar 4 21:08:43 2003 UTC revision 1.14 by dpavlin, Sun Oct 12 18:47:35 2003 UTC
# Line 24  use Filesys::SmbClient; Line 24  use Filesys::SmbClient;
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
# Line 34  my $LOG = '/var/log/backup.log';       # add p Line 35  my $LOG = '/var/log/backup.log';       # add p
35    
36  # store backups in which directory  # store backups in which directory
37  my $BACKUP_DEST = '/backup/isis_backup';  my $BACKUP_DEST = '/backup/isis_backup';
38    #my $BACKUP_DEST = '/tmp/backup/';
39    
40  # files to ignore in backup  # files to ignore in backup
41  my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt');  my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt');
# Line 50  my $c = 0; Line 52  my $c = 0;
52          sleep 1;          sleep 1;
53          redo if ++$c < 10;          redo if ++$c < 10;
54          # no response for 10 sec, bail out          # no response for 10 sec, bail out
55          print STDERR "can't take lock on $LOG -- another $0 running?\n";          xlog("ABORT","can't take lock on $LOG -- another $0 running?");
56          exit 1;          exit 1;
57  }  }
58    
59  # taint path: nmblookup should be there!  # taint path: nmblookup should be there!
60  $ENV{'PATH'} = "/usr/bin:/bin";  $ENV{'PATH'} = "/usr/bin:/bin";
61    
62    my $use_ping = 1;       # deault: use ping to verify that host is up
63    
64    my $result = GetOptions(
65            "ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST,
66    );
67    
68  my $mounts = shift @ARGV ||  my $mounts = shift @ARGV ||
69          'mountscript';          'mountscript';
70  #       die "usage: $0 mountscript";  #       die "usage: $0 mountscript";
# Line 64  my $mounts = shift @ARGV || Line 72  my $mounts = shift @ARGV ||
72    
73  my @in_backup;  # shares which are backeduped this run  my @in_backup;  # shares which are backeduped this run
74    
75  my $p = new Net::Ping->new("tcp", 2);  my $ping;
76  # ping will try tcp connect to netbios-ssn (139)  if ($use_ping) {
77  $p->{port_num} = getservbyname("netbios-ssn", "tcp");          $ping = new Net::Ping->new("tcp", 2);
78            # ping will try tcp connect to netbios-ssn (139)
79            $ping->{port_num} = getservbyname("netbios-ssn", "tcp");
80    }
81    
82  my $backup_ok = 0;  my $backup_ok = 0;
83    
# Line 81  while(<M>) { Line 92  while(<M>) {
92          next if !/^\s*smbmount\s/;          next if !/^\s*smbmount\s/;
93          my (undef,$share,undef,$opt) = split(/\s+/,$_,4);          my (undef,$share,undef,$opt) = split(/\s+/,$_,4);
94    
95          my ($user,$passwd,$workgroup);          my ($user,$passwd,$workgroup,$ip);
96    
97          foreach (split(/,/,$opt)) {          foreach (split(/,/,$opt)) {
98                  my ($n,$v) = split(/=/,$_,2);                  my ($n,$v) = split(/=/,$_,2);
# Line 97  while(<M>) { Line 108  while(<M>) {
108                          }                          }
109                  } elsif ($n =~ m#workgroup#i) {                  } elsif ($n =~ m#workgroup#i) {
110                          $workgroup = $v;                          $workgroup = $v;
111                    } elsif ($n =~ m#ip#i) {
112                            $ip = $v;
113                  }                  }
114          }          }
115    
# Line 107  while(<M>) { Line 120  while(<M>) {
120          my $bl = "$BACKUP_DEST/$host/$dir/latest";      # latest backup          my $bl = "$BACKUP_DEST/$host/$dir/latest";      # latest backup
121          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";   # current one          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";   # current one
122          my $real_bl;          my $real_bl;
123          if (-e $bl) {          if (-l $bl) {
124                  $real_bl=readlink($bl) || die "can't read link $bl: $!";                  $real_bl=readlink($bl) || die "can't read link $bl: $!";
125                  $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 "/");
126                  if (-e $bc && $real_bl eq $bc) {                  if (-l $bc && $real_bl eq $bc) {
127                          print "$share allready backuped...\n";                          print "$share allready backuped...\n";
128                          $backup_ok++;                          $backup_ok++;
129                          next;                          next;
# Line 121  while(<M>) { Line 134  while(<M>) {
134    
135          print "working on $share\n";          print "working on $share\n";
136    
137            # try to nmblookup IP
138          my $ip = get_ip($share);          $ip = get_ip($share) if (! $ip);
139    
140          if ($ip) {          if ($ip) {
141                  xlog($share,"IP is $ip");                  xlog($share,"IP is $ip");
142                  if ($p->ping($ip)) {                  if (($use_ping && $ping->ping($ip)) || 1) {
143                          snap_share($share,$user,$passwd,$workgroup);                          if (snap_share($share,$user,$passwd,$workgroup)) {
144                          $backup_ok++;                                  $backup_ok++;
145                            }
146                  }                  }
147          }          }
148  }  }
# Line 204  sub snap_share { Line 218  sub snap_share {
218          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";
219    
220          my $real_bl;          my $real_bl;
221          if (-e $bl) {          if (-l $bl) {
222                  $real_bl=readlink($bl) || die "can't read link $bl: $!";                  $real_bl=readlink($bl) || die "can't read link $bl: $!";
223                  $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 "/");
224          } else {          } else {
# Line 225  sub snap_share { Line 239  sub snap_share {
239                  }                  }
240          }          }
241    
242          if (-e $bc && $real_bl && $real_bl eq $bc) {          if (-l $bc && $real_bl && $real_bl eq $bc) {
243                  print "$share allready backuped...\n";                  print "$share allready backuped...\n";
244                  return;                  return 1;
245          }          }
246    
247          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);
# Line 255  sub snap_share { Line 269  sub snap_share {
269          my %file_atime;          my %file_atime;
270          my %file_mtime;          my %file_mtime;
271          #my %file_md5;          #my %file_md5;
272            %file_md5 = ();
273    
274          my @smb_files;          my @smb_files;
275          my %smb_size;          my %smb_size;
# Line 285  sub snap_share { Line 300  sub snap_share {
300                                  push @ignore,norm_dir("$d/$_");                                  push @ignore,norm_dir("$d/$_");
301                          }                          }
302                          close(I);                          close(I);
303  print STDERR "ignore: ",join("|",@ignore),"\n";  #print STDERR "ignore: ",join("|",@ignore),"\n";
304                          link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" ||                          link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" ||
305                                  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";
306                  }                  }
# Line 316  print STDERR "ignore: ",join("|",@ignore Line 331  print STDERR "ignore: ",join("|",@ignore
331                                  } elsif (-d $pf) {                                  } elsif (-d $pf) {
332                                          push @dirs,$pr;                                          push @dirs,$pr;
333                                  } else {                                  } else {
334                                          print STDERR "unknown type: $pf\n";                                          print STDERR "not file or directory: $pf\n";
335                                  }                                  }
336                          } else {                          } else {
337                                  print STDERR "ignored: $pr\n";                                  print STDERR "ignored: $pr\n";
# Line 324  print STDERR "ignore: ",join("|",@ignore Line 339  print STDERR "ignore: ",join("|",@ignore
339                  }                  }
340          }          }
341    
342          xlog($share,($#files+1)." files and ".($#dirs+1)." dirs on local disk before backup");          # local dir always include /
343            xlog($share,($#files+1)." files and ".($#dirs)." dirs on local disk before backup");
344    
345          # read smb filesystem          # read smb filesystem
346    
# Line 335  print STDERR "ignore: ",join("|",@ignore Line 351  print STDERR "ignore: ",join("|",@ignore
351    
352          $di = 0;          $di = 0;
353          while ($di <= $#smb_dirs) {          while ($di <= $#smb_dirs) {
354                  my $d=$smb_dirs[$di++];                  my $d=$smb_dirs[$di];
355                  my $pf = norm_dir($d,"smb:$share/");    # path full                  my $pf = norm_dir($d,"smb:$share/");    # path full
356                  my $D = $smb->opendir($pf) || warn "smb->opendir($pf): $!\n";                  my $D = $smb->opendir($pf);
357                    if (! $D) {
358                            xlog($share,"FATAL: $share [$pf]: $!");
359                            # remove failing dir
360                            delete $smb_dirs[$di];
361                            return 0;                       # failed
362                    }
363                    $di++;
364    
365                  my @clutter = $smb->readdir_struct($D);                  my @clutter = $smb->readdir_struct($D);
366                  foreach my $item (@clutter) {                  foreach my $item (@clutter) {
# Line 355  print STDERR "ignore: ",join("|",@ignore Line 378  print STDERR "ignore: ",join("|",@ignore
378                                  } elsif ($item->[0] == main::SMBC_DIR) {                                  } elsif ($item->[0] == main::SMBC_DIR) {
379                                          push @smb_dirs,$pr;                                          push @smb_dirs,$pr;
380                                  } else {                                  } else {
381                                          print STDERR "unknown type: $pf\n";                                          print STDERR "not file or directory [",$item->[0],"]: $pf\n";
382                                  }                                  }
383                          } else {                          } else {
384                                  print STDERR "smb ignored: $pr\n";                                  print STDERR "smb ignored: $pr\n";
# Line 363  print STDERR "ignore: ",join("|",@ignore Line 386  print STDERR "ignore: ",join("|",@ignore
386                  }                  }
387          }          }
388    
389          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");
390    
391          # sync dirs          # sync dirs
392          my $lc = List::Compare->new(\@dirs, \@smb_dirs);          my $lc = List::Compare->new(\@dirs, \@smb_dirs);
# Line 395  print STDERR "ignore: ",join("|",@ignore Line 418  print STDERR "ignore: ",join("|",@ignore
418                                    
419                  foreach my $f (@_) {                  foreach my $f (@_) {
420  #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;  
                         }  
   
421                          my $md5 = Digest::MD5->new;                          my $md5 = Digest::MD5->new;
422    
423                          my $fd = $smb->open("$from/$f");                          my $fd = $smb->open("$from/$f");
424                          if (! $fd) {                          if (! $fd) {
425                                  print STDERR "can't open smb file $from/$f: $!\n";                                  xlog("WARNING","can't open smb file $from/$f: $!");
426                                    next;
427                            }
428    
429                            if (! open(F,"> $to/$f")) {
430                                    xlog("WARNING","can't open new file $to/$f: $!");
431                                  next;                                  next;
432                          }                          }
433    
# Line 478  print STDERR "ignore: ",join("|",@ignore Line 501  print STDERR "ignore: ",join("|",@ignore
501          # remove files          # remove files
502          foreach (sort @files2erase) {          foreach (sort @files2erase) {
503                  unlink "$bc/$_" || warn "unlink $_: $!\n";                  unlink "$bc/$_" || warn "unlink $_: $!\n";
504                    delete $file_md5{$_};
505          }          }
506    
507          # remove not needed dirs (after files)          # remove not needed dirs (after files)
# Line 490  print STDERR "ignore: ",join("|",@ignore Line 514  print STDERR "ignore: ",join("|",@ignore
514                  unlink "$bc/$_/.md5sum" if (-e "$bc/$_/.md5sum");                  unlink "$bc/$_/.md5sum" if (-e "$bc/$_/.md5sum");
515          }          }
516    
517            # erase stale entries in .md5sum
518            my @md5_files = keys %file_md5;
519            $lc = List::Compare->new(\@md5_files, \@smb_files);
520            foreach my $file ($lc->get_Lonly) {
521                    xlog("NOTICE","removing stale '$file' from .md5sum");
522                    delete $file_md5{$file};
523            }
524    
525          # create .md5sum          # create .md5sum
526          my $last_dir = '';          my $last_dir = '';
527          my $md5;          my $md5;
# Line 505  print STDERR "ignore: ",join("|",@ignore Line 537  print STDERR "ignore: ",join("|",@ignore
537                  }                  }
538                  print $md5 $file_md5{$f},"  $file\n";                  print $md5 $file_md5{$f},"  $file\n";
539          }          }
540          close($md5);          close($md5) if ($md5);
541    
542          # create leatest link          # create leatest link
543  #print "ln -s $bc $real_bl\n";  #print "ln -s $bc $real_bl\n";
544          if (-e $bl) {          if (-l $bl) {
545                  unlink $bl || warn "can't remove old latest symlink $bl: $!\n";                  unlink $bl || warn "can't remove old latest symlink $bl: $!\n";
546          }          }
547          symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n";          symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n";
548    
549          # FIX: sanity check -- remove for speedup          # FIX: sanity check -- remove for speedup
550          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);
551    
552          xlog($share,"backup completed...");          xlog($share,"backup completed...");
 }  
553    
554            return 1;
555    }
556  __END__  __END__
557  #-------------------------------------------------------------------------  #-------------------------------------------------------------------------
558    

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.14

  ViewVC Help
Powered by ViewVC 1.1.26