/[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 158 by dpavlin, Mon Oct 10 13:39:10 2005 UTC revision 254 by dpavlin, Mon Dec 12 16:07:27 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/;
 use File::Pid;  
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 19  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 = 1;  my $bin;
35    foreach my $c (qw/gzip md5sum/) {
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 56  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            if ($debug) {
79                    print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
80            } else {
81                    print " check";
82            }
83    
84            my @tar_parts;
85    
86            if (-d "$tar_dir/$filename") {
87                    print STDERR " multi-part" if ($opt{d});
88                    opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
89                    @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
90                    closedir($dir);
91            } else {
92                    push @tar_parts, "${filename}.tar.gz";
93            }
94    
95            my @tar_files;
96    
97            print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
98    
99            print " reading";
100    
101            foreach my $tarfilename (@tar_parts) {
102    
103                    print STDERR " $tarfilename" if ($debug);
104    
105                    my $path = "$tar_dir/$tarfilename";
106                    my $md5 = $path;
107                    $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";
108                    if (! -e $md5) {
109                            print ", creating md5";
110                            system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";
111                    }
112    
113                    open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
114                    binmode($fh);
115                    my $tar = Archive::Tar::Streamed->new($fh);
116    
117                    while(my $entry = $tar->next) {
118                            push @tar_files, $entry->name;
119                    }
120            }
121    
122            @tar_files = sort @tar_files;
123            print STDERR " ",($#tar_files + 1), " files" if ($debug);
124    
125            print STDERR ", database" if ($debug);
126    
127            my $sth = $dbh->prepare(qq{
128                    SELECT path,type
129                    FROM files
130                    JOIN shares on shares.id = shareid
131                    JOIN hosts on hosts.id = shares.hostid
132                    WHERE hosts.name = ? and shares.name = ? and backupnum = ?
133            });
134            $sth->execute($host, $share, $num);
135            my @db_files;
136            while( my $row = $sth->fetchrow_hashref ) {
137    
138                    my $path = $row->{'path'} || die "no path?";
139                    $path =~ s#^/#./#;
140                    $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
141                    push @db_files, $path;
142            }
143    
144            print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
145    
146            @db_files = sort @db_files;
147    
148            my $same = 1;
149            if ($#tar_files != $#db_files) {
150                    $same = 0;
151                    print STDERR " NUMBER" if ($debug);
152            } else {
153                    my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
154                    while ( $diff->Next() ) {
155                            next if $diff->Same();
156                            $same = 0;
157                            print "< $_\n" for $diff->Items(1);
158                            print "> $_\n" for $diff->Items(2);
159                    }
160            }
161    
162            print " ",($same ? 'ok' : 'DIFFERENT');
163            print STDERR " }} " if ($debug);
164    
165            return $same;
166    }
167    
168    
169  #----- main  #----- main
170    
171  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 64  select Line 174  select
174          backups.id as backup_id,          backups.id as backup_id,
175          hosts.name as host,          hosts.name as host,
176          shares.name as share,          shares.name as share,
177          backups.num as num          backups.num as num,
178            inc_size,
179            parts
180  from backups  from backups
181          join shares on backups.hostid = shares.hostid          join shares on backups.hostid = shares.hostid
182                  and shares.id = backups.shareid                  and shares.id = backups.shareid
183          join hosts on shares.hostid = hosts.id          join hosts on shares.hostid = hosts.id
184  where inc_size < 0 and not inc_deleted  where not inc_deleted
185  order by backups.date  order by backups.date
186    
187  } );  } );
188    
189  $sth->execute();  $sth->execute();
190    my $num_backups = $sth->rows;
191  my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ? where id = ? });  my $curr_backup = 1;
   
 %BackupPC::SearchLib::Conf = %Conf;  
192    
193  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
194          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
         print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";  
195    
196          my $t = time();          # this will return -1 if file doesn't exist
197            my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
198    
199          my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
200          print STDERR "## $cmd\n" if ($debug);  
201            if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size && ( $check && tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) || 1) ) {
202                    next;
203            }
204    
205          system($cmd) == 0 or die "failed: $?";          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
206            $curr_backup++;
207    
208          my $size = (stat( "$tar_dir/$tar_file" ))[7];          my $t = time();
209    
210            # re-create archive?
211            my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
212            print STDERR "## $cmd\n" if ($debug);
213    
214          print fmt_time(time() - $t)," $size bytes\n";          if (system($cmd) != 0) {
215                    print STDERR " FAILED";
216            }
217    
218          $sth_inc_size->execute($size, $row->{'backup_id'});          print ", dur: ",fmt_time(time() - $t), "\n";
219    
220          $dbh->commit;          $dbh->commit;
221    

Legend:
Removed from v.158  
changed lines
  Added in v.254

  ViewVC Help
Powered by ViewVC 1.1.26