/[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 167 by dpavlin, Mon Oct 10 14:05:09 2005 UTC revision 256 by dpavlin, Mon Dec 12 20:59:53 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  my $path = abs_path($0);  my $path = abs_path($0);
26  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
# Line 19  my $tarIncCreate = $path .= 'BackupPC_ta Line 28  my $tarIncCreate = $path .= 'BackupPC_ta
28    
29  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
30    
31  my $debug = 0;  my $bin;
32    foreach my $c (qw/gzip md5sum/) {
33            $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
34    }
35    
36    my %opt;
37    getopts("cd", \%opt );
38    
39    my $debug = $opt{d};
40    my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
41    
42  $|=1;  $|=1;
43    
44  my $start_t = time();  my $start_t = time();
45    
46  my $t_fmt = '%Y-%m-%d %H:%M:%S';  my $t_fmt = '%Y-%m-%d %H:%M:%S';
47    
 my $hosts;  
 my $bpc = BackupPC::Lib->new || die;  
 my %Conf = $bpc->Conf();  
 my $TopDir = $bpc->TopDir();  
 my $beenThere = {};  
   
48  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
49  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
50    
# Line 56  sub curr_time { Line 69  sub curr_time {
69          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
70  }  }
71    
72    sub tar_check($$$$) {
73            my ($host,$share,$num,$filename) = @_;
74    
75            if ($debug) {
76                    print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
77            } else {
78                    print " check";
79            }
80    
81            my @tar_parts;
82    
83            if (-d "$tar_dir/$filename") {
84                    print STDERR " multi-part" if ($opt{d});
85                    opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
86                    @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
87                    closedir($dir);
88            } else {
89                    push @tar_parts, "${filename}.tar.gz";
90            }
91    
92            print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
93    
94            my $same = 1;
95            my @tar_files;
96    
97            print " reading";
98    
99            foreach my $tarfilename (@tar_parts) {
100    
101                    print STDERR " $tarfilename" if ($debug);
102    
103                    my $path = "$tar_dir/$tarfilename";
104                    my $md5 = $path;
105                    $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";
106                    if (! -e $md5) {
107                            print ", creating md5";
108                            system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";
109                    }
110    
111                    open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
112                    binmode($fh);
113                    my $tar = Archive::Tar::Streamed->new($fh);
114    
115                    my $total_size = 0;
116    
117                    while(my $entry = $tar->next) {
118                            push @tar_files, $entry->name;
119                            $total_size += $entry->size;
120                    }
121    
122                    if ($total_size > $Conf{MaxArchiveFileSize}) {
123                            print STDERR " part too big $total_size > $Conf{MaxArchiveFileSize} }}" if ($debug);
124                            $same = 0;
125                            last;
126                    } elsif ($total_size > $Conf{MaxArchiveSize}) {
127                            print STDERR " part bigger than media $total_size > $Conf{MaxArchiveSize} }}" if ($debug);
128                            $same = 0;
129                            last;
130                    }
131            }
132    
133            # short-cut and exit;
134            return $same unless($same);
135    
136            @tar_files = sort @tar_files;
137            print STDERR " ",($#tar_files + 1), " files" if ($debug);
138    
139            print STDERR ", database" if ($debug);
140    
141            my $sth = $dbh->prepare(qq{
142                    SELECT path,type
143                    FROM files
144                    JOIN shares on shares.id = shareid
145                    JOIN hosts on hosts.id = shares.hostid
146                    WHERE hosts.name = ? and shares.name = ? and backupnum = ?
147            });
148            $sth->execute($host, $share, $num);
149            my @db_files;
150            while( my $row = $sth->fetchrow_hashref ) {
151    
152                    my $path = $row->{'path'} || die "no path?";
153                    $path =~ s#^/#./#;
154                    $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
155                    push @db_files, $path;
156            }
157    
158            print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
159    
160            @db_files = sort @db_files;
161    
162            if ($#tar_files != $#db_files) {
163                    $same = 0;
164                    print STDERR " NUMBER" if ($debug);
165            } else {
166                    my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
167                    while ( $diff->Next() ) {
168                            next if $diff->Same();
169                            $same = 0;
170                            print "< $_\n" for $diff->Items(1);
171                            print "> $_\n" for $diff->Items(2);
172                    }
173            }
174    
175            print " ",($same ? 'ok' : 'DIFFERENT');
176            print STDERR " }} " if ($debug);
177    
178            return $same;
179    }
180    
181    
182  #----- main  #----- main
183    
184  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 64  select Line 187  select
187          backups.id as backup_id,          backups.id as backup_id,
188          hosts.name as host,          hosts.name as host,
189          shares.name as share,          shares.name as share,
190          backups.num as num          backups.num as num,
191            inc_size,
192            parts
193  from backups  from backups
194          join shares on backups.hostid = shares.hostid          join shares on backups.hostid = shares.hostid
195                  and shares.id = backups.shareid                  and shares.id = backups.shareid
196          join hosts on shares.hostid = hosts.id          join hosts on shares.hostid = hosts.id
197  where inc_size < 0 and not inc_deleted  where not inc_deleted
198  order by backups.date  order by backups.date
199    
200  } );  } );
201    
202  $sth->execute();  $sth->execute();
203    my $num_backups = $sth->rows;
204  my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ? where id = ? });  my $curr_backup = 1;
 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });  
   
 %BackupPC::SearchLib::Conf = %Conf;  
205    
206  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
207          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 ";  
208    
209          my $t = time();          # this will return -1 if file doesn't exist
210            my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
211    
212          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});
         print STDERR "## $cmd\n" if ($debug);  
213    
214          system($cmd) == 0 or die "failed: $?";          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size) {
215                    if ($check) {
216                            tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
217                    } else {
218                            next;
219                    }
220            }
221    
222          my $size = (stat( "$tar_dir/$tar_file" ))[7];          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
223            $curr_backup++;
224    
225          print " dur: ",fmt_time(time() - $t)," $size bytes";          my $t = time();
226    
227          if ($size > 45) {          # re-create archive?
228                  $sth_inc_size->execute($size, $row->{'backup_id'});          my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
229          } else {          print STDERR "## $cmd\n" if ($debug);
230                  $sth_inc_deleted->execute(1, $row->{'backup_id'});  
231                  unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";          if (system($cmd) != 0) {
232                  print " EMPTY";                  print STDERR " FAILED";
233          }          }
234          print "\n";  
235            print ", dur: ",fmt_time(time() - $t), "\n";
236    
237          $dbh->commit;          $dbh->commit;
238    

Legend:
Removed from v.167  
changed lines
  Added in v.256

  ViewVC Help
Powered by ViewVC 1.1.26