/[BackupPC]/trunk/bin/BackupPC_incPartsUpdate
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 /trunk/bin/BackupPC_incPartsUpdate

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

revision 196 by dpavlin, Thu Oct 13 18:32:59 2005 UTC revision 253 by dpavlin, Mon Dec 12 13:41:08 2005 UTC
# Line 6  use lib "__INSTALLDIR__/lib"; Line 6  use lib "__INSTALLDIR__/lib";
6  use DBI;  use DBI;
7  use BackupPC::Lib;  use BackupPC::Lib;
8  use BackupPC::View;  use BackupPC::View;
9    use BackupPC::Attrib qw/:all/;
10  use Data::Dumper;  use Data::Dumper;
11  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
12  use POSIX qw/strftime/;  use POSIX qw/strftime/;
 use BackupPC::SearchLib;  
13  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
14    use File::Which;
15    use Archive::Tar::Streamed;
16    use Algorithm::Diff;
17    use Getopt::Std;
18    
19    my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
20    my %Conf = $bpc->Conf();
21    
22    use BackupPC::SearchLib;
23    %BackupPC::SearchLib::Conf = %Conf;
24    
25    # cludge: minimum .tar.gz size
26    my $MIN_TAR_SIZE = 80;
27    
28  my $path = abs_path($0);  my $path = abs_path($0);
29  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
# Line 18  my $tarIncCreate = $path .= 'BackupPC_ta Line 31  my $tarIncCreate = $path .= 'BackupPC_ta
31    
32  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
33    
34  my $debug = 0;  my $bin;
35    foreach my $c (qw/gzip/) {
36            $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
37    }
38    
39    my %opt;
40    getopts("cd", \%opt );
41    
42    my $debug = $opt{d};
43    my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
44    
45  $|=1;  $|=1;
46    
47  my $start_t = time();  my $start_t = time();
48    
49  my $t_fmt = '%Y-%m-%d %H:%M:%S';  my $t_fmt = '%Y-%m-%d %H:%M:%S';
50    
 my $hosts;  
 my $bpc = BackupPC::Lib->new || die;  
 my %Conf = $bpc->Conf();  
 my $TopDir = $bpc->TopDir();  
 my $beenThere = {};  
   
51  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
52  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
53    
# Line 55  sub curr_time { Line 72  sub curr_time {
72          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
73  }  }
74    
75    sub tar_check($$$$) {
76            my ($host,$share,$num,$filename) = @_;
77    
78            return 1;       # FIXME
79    
80            if ($debug) {
81                    print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
82            } else {
83                    print " check";
84            }
85    
86            if (-d $filename) {
87                    print STDERR ", joining";
88                    tar_join($filename);
89            }
90    
91            print STDERR ", opening" if ($debug);
92            open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
93            binmode($fh);
94            my $tar = Archive::Tar::Streamed->new($fh);
95    
96            print STDERR ", tar" if ($debug);
97            my @tar_files;
98            while(my $entry = $tar->next) {
99                    push @tar_files, $entry->name;
100            }
101            @tar_files = sort @tar_files;
102            print STDERR " ",($#tar_files + 1), " files" if ($debug);
103    
104            print STDERR ", database" if ($debug);
105    
106            my $sth = $dbh->prepare(qq{
107                    SELECT path,type
108                    FROM files
109                    JOIN shares on shares.id = shareid
110                    JOIN hosts on hosts.id = shares.hostid
111                    WHERE hosts.name = ? and shares.name = ? and backupnum = ?
112            });
113            $sth->execute($host, $share, $num);
114            my @db_files;
115            while( my $row = $sth->fetchrow_hashref ) {
116    
117                    my $path = $row->{'path'} || die "no path?";
118                    $path =~ s#^/#./#;
119                    $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
120                    push @db_files, $path;
121            }
122    
123            print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
124    
125            @db_files = sort @db_files;
126    
127            my $same = 1;
128            if ($#tar_files != $#db_files) {
129                    $same = 0;
130                    print STDERR " NUMBER" if ($debug);
131            } else {
132                    my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
133                    while ( $diff->Next() ) {
134                            next if $diff->Same();
135                            $same = 0;
136                            print "< $_\n" for $diff->Items(1);
137                            print "> $_\n" for $diff->Items(2);
138                    }
139            }
140    
141            print " ",($same ? 'ok' : 'DIFFERENT');
142            print STDERR " }} " if ($debug);
143    
144            return $same;
145    }
146    
147    
148  #----- main  #----- main
149    
150  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 76  order by backups.date Line 166  order by backups.date
166  } );  } );
167    
168  $sth->execute();  $sth->execute();
169    my $num_backups = $sth->rows;
170  my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });  my $curr_backup = 1;
 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });  
   
 %BackupPC::SearchLib::Conf = %Conf;  
171    
172  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
173          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
# Line 88  while (my $row = $sth->fetchrow_hashref) Line 175  while (my $row = $sth->fetchrow_hashref)
175          # this will return -1 if file doesn't exist          # this will return -1 if file doesn't exist
176          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
177    
178          print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
   
         my $t = time();  
   
         # re-create archive?  
         if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) {  
                 my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};  
                 print STDERR "## $cmd\n" if ($debug);  
179    
180                  system($cmd) == 0 or die "failed: $?";          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size && ( $check && tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file") || 1) ) {
181                            next;
                 $size = (stat( "$tar_dir/$tar_file" ))[7];  
182          }          }
183    
184          if ($size > 45) {          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
185            $curr_backup++;
                 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";  
                 $max_size *= 1024;      # convert to bytes  
   
                 my $parts = int( ($size + $max_size - 1) / $max_size );  
   
                 if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {  
                         print " join";  
   
                         my $in = my $out = "$tar_dir/$tar_file";  
                         $out .= '.tmp';  
   
                         # FIXME I should really order parts manually!  
                         system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";  
                 }  
186    
187                  if ($size > $max_size && ! -d "$tar_dir/$tar_file") {          my $t = time();
                         print " split/$parts";  
                         my $in = my $out = "$tar_dir/$tar_file";  
                         $out .= '.tmp';  
                         rename $in, $out || die "can't rename $in: $!";  
                         mkdir $in || die "can't mkdir $in: $!";  
   
                         my $suffix_len = length("$parts");  
                         system("split -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";  
                         unlink $out || die "can't unlink $out: $!";  
                 }  
   
                 $sth_inc_size->execute($size, $parts, $row->{'backup_id'});  
                 $sth_inc_deleted->execute(0, $row->{'backup_id'});  
188    
189                  printf(" %1.2f MB", ($size / 1024 / 1024));          # re-create archive?
190            my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} };
191            print STDERR "## $cmd\n" if ($debug);
192    
193          } else {          if (system($cmd) != 0) {
194                  $sth_inc_deleted->execute(1, $row->{'backup_id'});                  print STDERR " FAILED";
                 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";  
                 print " EMPTY";  
195          }          }
196    
197          print ", dur: ",fmt_time(time() - $t), "\n";          print ", dur: ",fmt_time(time() - $t), "\n";
198    
199          $dbh->commit;          $dbh->commit;

Legend:
Removed from v.196  
changed lines
  Added in v.253

  ViewVC Help
Powered by ViewVC 1.1.26