/[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.4 by dpavlin, Sat Jan 4 15:59:14 2003 UTC revision 1.12 by dpavlin, Sun Oct 12 16:13:38 2003 UTC
# Line 30  my $LOG_TIME_FMT = '%Y-%m-%d %H:%M:%S'; Line 30  my $LOG_TIME_FMT = '%Y-%m-%d %H:%M:%S';
30  my $DIR_TIME_FMT = '%Y%m%d';            # strftime format for backup dir  my $DIR_TIME_FMT = '%Y%m%d';            # strftime format for backup dir
31    
32  my $LOG = '/var/log/backup.log';        # add path here...  my $LOG = '/var/log/backup.log';        # add path here...
33  $LOG = '/tmp/backup.log';  #$LOG = '/tmp/backup.log';
34    
35  # store backups in which directory  # store backups in which directory
36  my $BACKUP_DEST = '/data/isis_backup';  #my $BACKUP_DEST = '/backup/isis_backup';
37    my $BACKUP_DEST = '/tmp/backup/';
38    
39  # files to ignore in backup  # files to ignore in backup
40  my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt');  my @ignore = ('.md5sum', '.backupignore', 'backupignore.txt');
41    
42  # open log  # open log
43  open(L, "> $LOG") || die "can't open log $LOG: $!";  open(L, ">> $LOG") || die "can't open log $LOG: $!";
44  select((select(L), $|=1)[0]);   # flush output  select((select(L), $|=1)[0]);   # flush output
45    
46  # make a lock on logfile  # make a lock on logfile
# Line 50  my $c = 0; Line 51  my $c = 0;
51          sleep 1;          sleep 1;
52          redo if ++$c < 10;          redo if ++$c < 10;
53          # no response for 10 sec, bail out          # no response for 10 sec, bail out
54          print STDERR "can't take lock on $LOG -- another $0 running?\n";          xlog("ABORT","can't take lock on $LOG -- another $0 running?");
55          exit 1;          exit 1;
56  }  }
57    
# Line 64  my $mounts = shift @ARGV || Line 65  my $mounts = shift @ARGV ||
65    
66  my @in_backup;  # shares which are backeduped this run  my @in_backup;  # shares which are backeduped this run
67    
68  my $p = new Net::Ping->new();  my $p = new Net::Ping->new("tcp", 2);
69    # ping will try tcp connect to netbios-ssn (139)
70    $p->{port_num} = getservbyname("netbios-ssn", "tcp");
71    
72  my $backup_ok = 0;  my $backup_ok = 0;
73    
# Line 79  while(<M>) { Line 82  while(<M>) {
82          next if !/^\s*smbmount\s/;          next if !/^\s*smbmount\s/;
83          my (undef,$share,undef,$opt) = split(/\s+/,$_,4);          my (undef,$share,undef,$opt) = split(/\s+/,$_,4);
84    
85          my ($user,$passwd,$workgroup);          my ($user,$passwd,$workgroup,$ip);
86    
87          foreach (split(/,/,$opt)) {          foreach (split(/,/,$opt)) {
88                  my ($n,$v) = split(/=/,$_,2);                  my ($n,$v) = split(/=/,$_,2);
# Line 95  while(<M>) { Line 98  while(<M>) {
98                          }                          }
99                  } elsif ($n =~ m#workgroup#i) {                  } elsif ($n =~ m#workgroup#i) {
100                          $workgroup = $v;                          $workgroup = $v;
101                    } elsif ($n =~ m#ip#i) {
102                            $ip = $v;
103                  }                  }
104          }          }
105    
# Line 105  while(<M>) { Line 110  while(<M>) {
110          my $bl = "$BACKUP_DEST/$host/$dir/latest";      # latest backup          my $bl = "$BACKUP_DEST/$host/$dir/latest";      # latest backup
111          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";   # current one          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";   # current one
112          my $real_bl;          my $real_bl;
113          if (-e $bl) {          if (-l $bl) {
114                  $real_bl=readlink($bl) || die "can't read link $bl: $!";                  $real_bl=readlink($bl) || die "can't read link $bl: $!";
115                  $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 "/");
116                  if (-e $bc && $real_bl eq $bc) {                  if (-l $bc && $real_bl eq $bc) {
117                          print "$share allready backuped...\n";                          print "$share allready backuped...\n";
118                          $backup_ok++;                          $backup_ok++;
119                          next;                          next;
# Line 119  while(<M>) { Line 124  while(<M>) {
124    
125          print "working on $share\n";          print "working on $share\n";
126    
127            # try to nmblookup IP
128          my $ip = get_ip($share);          $ip = get_ip($share) if (! $ip);
129    
130          if ($ip) {          if ($ip) {
131                  xlog($share,"IP is $ip");                  xlog($share,"IP is $ip");
132                  if ($p->ping($ip)) {                  if ($p->ping($ip)) {
133                          snap_share($share,$user,$passwd,$workgroup);                          if (snap_share($share,$user,$passwd,$workgroup)) {
134                          $backup_ok++;                                  $backup_ok++;
135                            }
136                  }                  }
137          }          }
138  }  }
# Line 161  sub xlog { Line 167  sub xlog {
167          print L "$t $share\t$m\n";          print L "$t $share\t$m\n";
168  }  }
169    
170    # dump warn and dies into log
171    BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0]) ; warn $_[0] } }
172    BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0]) ; die $_[0] } }
173    
174    
175  # split share name to host, dir and currnet date dir  # split share name to host, dir and currnet date dir
176  sub share2host_dir {  sub share2host_dir {
# Line 186  sub snap_share { Line 196  sub snap_share {
196    
197          my %param = ( debug => 0 );          my %param = ( debug => 0 );
198    
199          $param{username} = shift;          $param{username} = shift || warn "can't find username for share $share";
200          $param{password} = shift;          $param{password} = shift || warn "can't find passwod for share $share";
201          $param{workgroup} = shift;          $param{workgroup} = shift || warn "can't find workgroup for share $share";
202    
203          my ($host,$dir,$date_dir) = share2host_dir($share);          my ($host,$dir,$date_dir) = share2host_dir($share);
204    
# Line 198  sub snap_share { Line 208  sub snap_share {
208          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";          my $bc = "$BACKUP_DEST/$host/$dir/$date_dir";
209    
210          my $real_bl;          my $real_bl;
211          if (-e $bl) {          if (-l $bl) {
212                  $real_bl=readlink($bl) || die "can't read link $bl: $!";                  $real_bl=readlink($bl) || die "can't read link $bl: $!";
213                  $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 "/");
214          } else {          } else {
215                  print "no old backup, this is first run...\n";                  print "no old backup, trying to find last backup, ";
216                    if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) {
217                            my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR);
218                            closedir(BL_DIR);
219                            $real_bl=pop @bl_dirs;
220                            print "using $real_bl as latest...\n";
221                            $real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/");
222                            if ($real_bl eq $bc) {
223                                    xlog($share,"latest from today (possible partial backup)");
224                                    rename $real_bl,$real_bl.".partial" || warn "can't reaname partial backup: $!";
225                                    $real_bl .= ".partial";
226                            }
227                    } else {
228                            print "this is first run...\n";
229                    }
230          }          }
231    
232          if (-e $bc && $real_bl && $real_bl eq $bc) {          if (-l $bc && $real_bl && $real_bl eq $bc) {
233                  print "$share allready backuped...\n";                  print "$share allready backuped...\n";
234                  return;                  return 1;
235          }          }
236    
237          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 279  sub snap_share {
279          my $di = 0;          my $di = 0;
280          while ($di <= $#dirs && $real_bl) {          while ($di <= $#dirs && $real_bl) {
281                  my $d=$dirs[$di++];                  my $d=$dirs[$di++];
282                  opendir(DIR,"$bl/$d") || warn "opendir($bl/$d): $!\n";                  opendir(DIR,"$real_bl/$d") || warn "opendir($real_bl/$d): $!\n";
283    
284                  # read .backupignore if exists                  # read .backupignore if exists
285                  if (-f "$bl/$d/.backupignore") {                  if (-f "$real_bl/$d/.backupignore") {
286                          open(I,"$bl/$d/.backupignore");                          open(I,"$real_bl/$d/.backupignore");
287                          while(<I>) {                          while(<I>) {
288                                  chomp;                                  chomp;
289                                  push @ignore,norm_dir("$d/$_");                                  push @ignore,norm_dir("$d/$_");
290                          }                          }
291                          close(I);                          close(I);
292  print STDERR "ignore: ",join("|",@ignore),"\n";  #print STDERR "ignore: ",join("|",@ignore),"\n";
293                          link "$bl/$d/.backupignore","$bc/$d/.backupignore" ||                          link "$real_bl/$d/.backupignore","$bc/$d/.backupignore" ||
294                                  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";
295                  }                  }
296    
297                  # read .md5sum if exists                  # read .md5sum if exists
298                  if (-f "$bl/$d/.md5sum") {                  if (-f "$real_bl/$d/.md5sum") {
299                          open(I,"$bl/$d/.md5sum");                          open(I,"$real_bl/$d/.md5sum");
300                          while(<I>) {                          while(<I>) {
301                                  chomp;                                  chomp;
302                                  my ($md5,$f) = split(/\s+/,$_,2);                                  my ($md5,$f) = split(/\s+/,$_,2);
# Line 286  print STDERR "ignore: ",join("|",@ignore Line 310  print STDERR "ignore: ",join("|",@ignore
310                          next if ($f eq '.');                          next if ($f eq '.');
311                          next if ($f eq '..');                          next if ($f eq '..');
312                          my $pr = norm_dir("$d/$f");     # path relative                          my $pr = norm_dir("$d/$f");     # path relative
313                          my $pf = norm_dir("$d/$f","$bl/");      # path full                          my $pf = norm_dir("$d/$f","$real_bl/"); # path full
314                          if (grep(/^\Q$pr\E$/,@ignore) == 0) {                          if (grep(/^\Q$pr\E$/,@ignore) == 0) {
315                                  if (-f $pf) {                                  if (-f $pf) {
316                                          push @files,$pr;                                          push @files,$pr;
# Line 296  print STDERR "ignore: ",join("|",@ignore Line 320  print STDERR "ignore: ",join("|",@ignore
320                                  } elsif (-d $pf) {                                  } elsif (-d $pf) {
321                                          push @dirs,$pr;                                          push @dirs,$pr;
322                                  } else {                                  } else {
323                                          print STDERR "unknown type: $pf\n";                                          print STDERR "not file or directory: $pf\n";
324                                  }                                  }
325                          } else {                          } else {
326                                  print STDERR "ignored: $pr\n";                                  print STDERR "ignored: $pr\n";
# Line 304  print STDERR "ignore: ",join("|",@ignore Line 328  print STDERR "ignore: ",join("|",@ignore
328                  }                  }
329          }          }
330    
331          xlog($share,($#files+1)." files and ".($#dirs+1)." dirs on local disk before backup");          # local dir always include /
332            xlog($share,($#files+1)." files and ".($#dirs)." dirs on local disk before backup");
333    
334          # read smb filesystem          # read smb filesystem
335    
# Line 315  print STDERR "ignore: ",join("|",@ignore Line 340  print STDERR "ignore: ",join("|",@ignore
340    
341          $di = 0;          $di = 0;
342          while ($di <= $#smb_dirs) {          while ($di <= $#smb_dirs) {
343                  my $d=$smb_dirs[$di++];                  my $d=$smb_dirs[$di];
344                  my $pf = norm_dir($d,"smb:$share/");    # path full                  my $pf = norm_dir($d,"smb:$share/");    # path full
345                  my $D = $smb->opendir($pf) || warn "smb->opendir($pf): $!\n";                  my $D = $smb->opendir($pf);
346                    if (! $D) {
347                            xlog($share,"FATAL: $share [$pf]: $!");
348                            # remove failing dir
349                            delete $smb_dirs[$di];
350                            return 0;                       # failed
351                    }
352                    $di++;
353    
354                  my @clutter = $smb->readdir_struct($D);                  my @clutter = $smb->readdir_struct($D);
355                  foreach my $item (@clutter) {                  foreach my $item (@clutter) {
# Line 335  print STDERR "ignore: ",join("|",@ignore Line 367  print STDERR "ignore: ",join("|",@ignore
367                                  } elsif ($item->[0] == main::SMBC_DIR) {                                  } elsif ($item->[0] == main::SMBC_DIR) {
368                                          push @smb_dirs,$pr;                                          push @smb_dirs,$pr;
369                                  } else {                                  } else {
370                                          print STDERR "unknown type: $pf\n";                                          print STDERR "not file or directory [",$item->[0],"]: $pf\n";
371                                  }                                  }
372                          } else {                          } else {
373                                  print STDERR "smb ignored: $pr\n";                                  print STDERR "smb ignored: $pr\n";
# Line 343  print STDERR "ignore: ",join("|",@ignore Line 375  print STDERR "ignore: ",join("|",@ignore
375                  }                  }
376          }          }
377    
378          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");
379    
380          # sync dirs          # sync dirs
381          my $lc = List::Compare->new(\@dirs, \@smb_dirs);          my $lc = List::Compare->new(\@dirs, \@smb_dirs);
# Line 375  print STDERR "ignore: ",join("|",@ignore Line 407  print STDERR "ignore: ",join("|",@ignore
407                                    
408                  foreach my $f (@_) {                  foreach my $f (@_) {
409  #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;  
                         }  
   
410                          my $md5 = Digest::MD5->new;                          my $md5 = Digest::MD5->new;
411    
412                          my $fd = $smb->open("$from/$f");                          my $fd = $smb->open("$from/$f");
413                          if (! $fd) {                          if (! $fd) {
414                                  print STDERR "can't open smb file $from/$f: $!\n";                                  xlog("WARNING","can't open smb file $from/$f: $!");
415                                    next;
416                            }
417    
418                            if (! open(F,"> $to/$f")) {
419                                    xlog("WARNING","can't open new file $to/$f: $!");
420                                  next;                                  next;
421                          }                          }
422    
# Line 452  print STDERR "ignore: ",join("|",@ignore Line 484  print STDERR "ignore: ",join("|",@ignore
484          xlog($share,"$transfer bytes transfered...");          xlog($share,"$transfer bytes transfered...");
485    
486          foreach (@ln_files) {          foreach (@ln_files) {
487                  link "$bl/$_","$bc/$_" || warn "link $bl/$_ -> $bc/$_: $!\n";                  link "$real_bl/$_","$bc/$_" || warn "link $real_bl/$_ -> $bc/$_: $!\n";
488          }          }
489    
490          # remove files          # remove files
# Line 473  print STDERR "ignore: ",join("|",@ignore Line 505  print STDERR "ignore: ",join("|",@ignore
505          # create .md5sum          # create .md5sum
506          my $last_dir = '';          my $last_dir = '';
507          my $md5;          my $md5;
508          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) {
509                  my $dir = dirname($f);                  my $dir = dirname($f);
510                  my $file = basename($f);                  my $file = basename($f);
511  print "$f -- $dir / $file<--\n";  #print "$f -- $dir / $file<--\n";
512                  if ($dir ne $last_dir) {                  if ($dir ne $last_dir) {
513                          close($md5) if ($md5);                          close($md5) if ($md5);
514                          open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!";                          open($md5, ">> $bc/$dir/.md5sum") || warn "can't create $bc/$dir/.md5sum: $!";
515                          $last_dir = $dir;                          $last_dir = $dir;
516  print STDERR "writing $last_dir/.md5sum\n";  #print STDERR "writing $last_dir/.md5sum\n";
517                  }                  }
518                  print $md5 $file_md5{$f},"  $file\n";                  print $md5 $file_md5{$f},"  $file\n";
519          }          }
520          close($md5);          close($md5) if ($md5);
521    
522          # create leatest link          # create leatest link
523  #       symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n";  #print "ln -s $bc $real_bl\n";
524            if (-l $bl) {
525                    unlink $bl || warn "can't remove old latest symlink $bl: $!\n";
526            }
527            symlink $bc,$bl || warn "can't create latest symlink $bl -> $bc: $!\n";
528    
529            # FIX: sanity check -- remove for speedup
530            xlog($share,"failed to create latest symlink $bl -> $bc...") if (readlink($bl) ne $bc || ! -l $bl);
531    
532          xlog($share,"backup completed...");          xlog($share,"backup completed...");
 }  
533    
534            return 1;
535    }
536  __END__  __END__
537  #-------------------------------------------------------------------------  #-------------------------------------------------------------------------
538    
# Line 547  transferring whole file over network (ju Line 587  transferring whole file over network (ju
587  over network)  over network)
588    
589  =item - usage of C<.md5sum> files (compatible with command-line utility  =item - usage of C<.md5sum> files (compatible with command-line utility
590  C<md5sum> to keep file between snapshots hard-linked  C<md5sum>) to keep file between snapshots hard-linked
591    
592  =back  =back
593    
# Line 591  After that you will get directories with Line 631  After that you will get directories with
631    
632          server/c_WinNT_fonts/yyyymmdd/....          server/c_WinNT_fonts/yyyymmdd/....
633    
634    =head2 Won't I run out of disk space?
635    
636    Of course you will... Snapshots and logfiles will eventually fill-up your disk.
637    However, you can do two things to stop that:
638    
639    =head3 Clean snapshort older than x days
640    
641    You can add following command to your C<root> crontab:
642    
643            find /backup/isis_backup -type d -mindepth 3 -maxdepth 3 -mtime +11 -exec rm -Rf {} \;
644    
645    I assume that C</backup/isis_backup> is directory in which are your snapshots
646    and that you don't want to keep snapshots older than 11 days (that's
647    C<-mtime +11> part of command).
648    
649    =head3 Rotate your logs
650    
651    I will leave that to you. I relay on GNU/Debian's C<logrotate> to do it for me.
652    
653    =head2 What are I<YYYYMMDD.partial> directories?
654    
655    If there isn't I<latest> symlink in snapshot directory, it's preatty safe to
656    assume that previous backup from that day failed. So, that directory will
657    be renamed to I<YYYYMMDD.partial> and snapshot will be performed again,
658    linking same files (other alternative would be to erase that dir and find
659    second-oldest directory, but this seemed like more correct approach).
660    
661  =head1 AUTHOR  =head1 AUTHOR
662    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.12

  ViewVC Help
Powered by ViewVC 1.1.26