/[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.6 by dpavlin, Tue Jan 21 21:30:19 2003 UTC revision 1.7 by dpavlin, Fri Jan 31 22:31:41 2003 UTC
# Line 64  my $mounts = shift @ARGV || Line 64  my $mounts = shift @ARGV ||
64    
65  my @in_backup;  # shares which are backeduped this run  my @in_backup;  # shares which are backeduped this run
66    
67  my $p = new Net::Ping->new();  my $p = new Net::Ping->new("tcp", 2);
68    # ping will try tcp connect to netbios-ssn (139)
69    $p->{port_num} = getservbyname("netbios-ssn", "tcp");
70    
71  my $backup_ok = 0;  my $backup_ok = 0;
72    
# Line 161  sub xlog { Line 163  sub xlog {
163          print L "$t $share\t$m\n";          print L "$t $share\t$m\n";
164  }  }
165    
166    # dump warn and dies into log
167    BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0]) ; warn $_[0] } }
168    BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0]) ; die $_[0] } }
169    
170    
171  # split share name to host, dir and currnet date dir  # split share name to host, dir and currnet date dir
172  sub share2host_dir {  sub share2host_dir {
# Line 202  sub snap_share { Line 208  sub snap_share {
208                  $real_bl=readlink($bl) || die "can't read link $bl: $!";                  $real_bl=readlink($bl) || die "can't read link $bl: $!";
209                  $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 "/");
210          } else {          } else {
211                  print "no old backup, this is first run...\n";                  print "no old backup, trying to find last backup, ";
212                    if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) {
213                            my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR);
214                            closedir(BL_DIR);
215                            $real_bl=pop @bl_dirs;
216                            print "using $real_bl as latest...\n";
217                            $real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/");
218                            if ($real_bl eq $bc) {
219                                    xlog($share,"latest from today (possible partial backup)");
220                                    rename $real_bl,$real_bl.".partial" || warn "can't reaname partial backup: $!";
221                                    $real_bl .= ".partial";
222                            }
223                    } else {
224                            print "this is first run...\n";
225                    }
226          }          }
227    
228          if (-e $bc && $real_bl && $real_bl eq $bc) {          if (-e $bc && $real_bl && $real_bl eq $bc) {
# Line 255  sub snap_share { Line 275  sub snap_share {
275          my $di = 0;          my $di = 0;
276          while ($di <= $#dirs && $real_bl) {          while ($di <= $#dirs && $real_bl) {
277                  my $d=$dirs[$di++];                  my $d=$dirs[$di++];
278                  opendir(DIR,"$bl/$d") || warn "opendir($bl/$d): $!\n";                  opendir(DIR,"$real_bl/$d") || warn "opendir($real_bl/$d): $!\n";
279    
280                  # read .backupignore if exists                  # read .backupignore if exists
281                  if (-f "$bl/$d/.backupignore") {                  if (-f "$real_bl/$d/.backupignore") {
282                          open(I,"$bl/$d/.backupignore");                          open(I,"$real_bl/$d/.backupignore");
283                          while(<I>) {                          while(<I>) {
284                                  chomp;                                  chomp;
285                                  push @ignore,norm_dir("$d/$_");                                  push @ignore,norm_dir("$d/$_");
286                          }                          }
287                          close(I);                          close(I);
288  print STDERR "ignore: ",join("|",@ignore),"\n";  print STDERR "ignore: ",join("|",@ignore),"\n";
289                          link "$bl/$d/.backupignore","$bc/$d/.backupignore" ||                          link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" ||
290                                  warn "can't copy $bl/$d/.backupignore to current backup dir: $!\n";                                  warn "can't copy $real_bl/$d/.backupignore to current backup dir: $!\n";
291                  }                  }
292    
293                  # read .md5sum if exists                  # read .md5sum if exists
294                  if (-f "$bl/$d/.md5sum") {                  if (-f "$real_bl/$d/.md5sum") {
295                          open(I,"$bl/$d/.md5sum");                          open(I,"$real_bl/$d/.md5sum");
296                          while(<I>) {                          while(<I>) {
297                                  chomp;                                  chomp;
298                                  my ($md5,$f) = split(/\s+/,$_,2);                                  my ($md5,$f) = split(/\s+/,$_,2);
# Line 286  print STDERR "ignore: ",join("|",@ignore Line 306  print STDERR "ignore: ",join("|",@ignore
306                          next if ($f eq '.');                          next if ($f eq '.');
307                          next if ($f eq '..');                          next if ($f eq '..');
308                          my $pr = norm_dir("$d/$f");     # path relative                          my $pr = norm_dir("$d/$f");     # path relative
309                          my $pf = norm_dir("$d/$f","$bl/");      # path full                          my $pf = norm_dir("$d/$f","$real_bl/"); # path full
310                          if (grep(/^\Q$pr\E$/,@ignore) == 0) {                          if (grep(/^\Q$pr\E$/,@ignore) == 0) {
311                                  if (-f $pf) {                                  if (-f $pf) {
312                                          push @files,$pr;                                          push @files,$pr;
# Line 452  print STDERR "ignore: ",join("|",@ignore Line 472  print STDERR "ignore: ",join("|",@ignore
472          xlog($share,"$transfer bytes transfered...");          xlog($share,"$transfer bytes transfered...");
473    
474          foreach (@ln_files) {          foreach (@ln_files) {
475                  link "$bl/$_","$bc/$_" || warn "link $bl/$_ -> $bc/$_: $!\n";                  link "$real_bl/$_","$bc/$_" || warn "link $real_bl/$_ -> $bc/$_: $!\n";
476          }          }
477    
478          # remove files          # remove files
# Line 473  print STDERR "ignore: ",join("|",@ignore Line 493  print STDERR "ignore: ",join("|",@ignore
493          # create .md5sum          # create .md5sum
494          my $last_dir = '';          my $last_dir = '';
495          my $md5;          my $md5;
496          foreach my $f (sort { $file_md5{$a}<=>$file_md5{$b} } keys %file_md5) {          foreach my $f (sort { $file_md5{$a} cmp $file_md5{$b} } keys %file_md5) {
497                  my $dir = dirname($f);                  my $dir = dirname($f);
498                  my $file = basename($f);                  my $file = basename($f);
499  print "$f -- $dir / $file<--\n";  #print "$f -- $dir / $file<--\n";
500                  if ($dir ne $last_dir) {                  if ($dir ne $last_dir) {
501                          close($md5) if ($md5);                          close($md5) if ($md5);
502                          open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!";                          open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!";
503                          $last_dir = $dir;                          $last_dir = $dir;
504  print STDERR "writing $last_dir/.md5sum\n";  #print STDERR "writing $last_dir/.md5sum\n";
505                  }                  }
506                  print $md5 $file_md5{$f},"  $file\n";                  print $md5 $file_md5{$f},"  $file\n";
507          }          }
508          close($md5);          close($md5);
509    
510          # create leatest link          # create leatest link
511  #       symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n";  #print "ln -s $bc $real_bl\n";
512            if (-e $bl) {
513                    unlink $bl || warn "can't remove old latest symlink $bl: $!\n";
514            }
515            symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n";
516    
517            # FIX: sanity check -- remove for speedup
518            xlog($share,"failed to create latest symlink...") if (readlink($bl) ne $bc || ! -e $bl);
519    
520          xlog($share,"backup completed...");          xlog($share,"backup completed...");
521  }  }
# Line 610  C<-mtime +11> part of command). Line 637  C<-mtime +11> part of command).
637    
638  I will leave that to you. I relay on GNU/Debian's C<logrotate> to do it for me.  I will leave that to you. I relay on GNU/Debian's C<logrotate> to do it for me.
639    
640    =head2 What are I<YYYYMMDD.partial> directories?
641    
642    If there isn't I<latest> symlink in snapshot directory, it's preatty safe to
643    assume that previous backup from that day failed. So, that directory will
644    be renamed to I<YYYYMMDD.partial> and snapshot will be performed again,
645    linking same files (other alternative would be to erase that dir and find
646    second-oldest directory, but this seemed like more correct approach).
647    
648  =head1 AUTHOR  =head1 AUTHOR
649    
650  Dobrica Pavlinusic <dpavlin@rot13.org>  Dobrica Pavlinusic <dpavlin@rot13.org>

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.26