/[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

Annotation of /trunk/bin/BackupPC_incPartsUpdate

Parent Directory Parent Directory | Revision Log Revision Log


Revision 216 - (hide annotations)
Sun Oct 16 17:41:10 2005 UTC (18 years, 7 months ago) by dpavlin
File size: 6277 byte(s)
report status to STDOUT

1 dpavlin 157 #!/usr/local/bin/perl -w
2    
3     use strict;
4     use lib "__INSTALLDIR__/lib";
5    
6     use DBI;
7     use BackupPC::Lib;
8     use BackupPC::View;
9 dpavlin 214 use BackupPC::Attrib qw/:all/;
10 dpavlin 157 use Data::Dumper;
11     use Time::HiRes qw/time/;
12     use POSIX qw/strftime/;
13     use BackupPC::SearchLib;
14     use Cwd qw/abs_path/;
15 dpavlin 201 use File::Which;
16 dpavlin 215 use Archive::Tar::Streamed;
17 dpavlin 214 use Algorithm::Diff;
18     use Getopt::Std;
19 dpavlin 157
20     my $path = abs_path($0);
21     $path =~ s#/[^/]+$#/#;
22     my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
23    
24     die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
25    
26 dpavlin 201 my $bin;
27     foreach my $c (qw/gzip split/) {
28     $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
29     }
30    
31 dpavlin 214 my %opt;
32     getopts("cd", \%opt );
33 dpavlin 201
34 dpavlin 214 my $debug = $opt{d};
35     my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
36    
37 dpavlin 157 $|=1;
38    
39     my $start_t = time();
40    
41     my $t_fmt = '%Y-%m-%d %H:%M:%S';
42    
43     my $hosts;
44     my $bpc = BackupPC::Lib->new || die;
45     my %Conf = $bpc->Conf();
46     my $TopDir = $bpc->TopDir();
47     my $beenThere = {};
48    
49     my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
50     my $user = $Conf{SearchUser} || '';
51    
52     my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
53    
54     my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
55    
56     die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
57    
58     #---- subs ----
59    
60     sub fmt_time {
61     my $t = shift || return;
62     my $out = "";
63     my ($ss,$mm,$hh) = gmtime($t);
64     $out .= "${hh}h" if ($hh);
65     $out .= sprintf("%02d:%02d", $mm,$ss);
66     return $out;
67     }
68    
69     sub curr_time {
70     return strftime($t_fmt,localtime());
71     }
72    
73 dpavlin 214 sub tar_join($) {
74     my $filename = shift;
75    
76     my $in = my $out = $filename;
77     $out .= '.tmp';
78    
79     # FIXME I should really order parts manually!
80     system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
81    
82     }
83    
84     sub tar_check($$$$) {
85     my ($host,$share,$num,$filename) = @_;
86    
87     if ($debug) {
88     print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
89     } else {
90     print " check";
91     }
92    
93     if (-d $filename) {
94     print STDERR ", joining";
95     tar_join($filename);
96     }
97    
98     print STDERR ", opening" if ($debug);
99 dpavlin 215 open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
100     binmode($fh);
101     my $tar = Archive::Tar::Streamed->new($fh);
102 dpavlin 214
103     print STDERR ", tar" if ($debug);
104 dpavlin 215 my @tar_files;
105     while(my $entry = $tar->next) {
106     push @tar_files, $entry->name;
107     }
108     @tar_files = sort @tar_files;
109 dpavlin 214 print STDERR " ",($#tar_files + 1), " files" if ($debug);
110    
111     print STDERR ", database" if ($debug);
112    
113     my $sth = $dbh->prepare(qq{
114     SELECT path,type
115     FROM files
116     JOIN shares on shares.id = shareid
117     JOIN hosts on hosts.id = shares.hostid
118     WHERE hosts.name = ? and shares.name = ? and backupnum = ?
119     });
120     $sth->execute($host, $share, $num);
121     my @db_files;
122     while( my $row = $sth->fetchrow_hashref ) {
123    
124     my $path = $row->{'path'} || die "no path?";
125     $path =~ s#^/#./#;
126     $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
127     push @db_files, $path;
128     }
129    
130     print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
131    
132     @db_files = sort @db_files;
133    
134     my $same = 1;
135     if ($#tar_files != $#db_files) {
136     $same = 0;
137     print STDERR " NUMBER" if ($debug);
138     } else {
139     my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
140     while ( $diff->Next() ) {
141     next if $diff->Same();
142     $same = 0;
143     print "< $_\n" for $diff->Items(1);
144     print "> $_\n" for $diff->Items(2);
145     }
146     }
147    
148 dpavlin 216 print " ",($same ? 'ok' : 'DIFFERENT');
149 dpavlin 214 print STDERR " }} " if ($debug);
150    
151     return $same;
152     }
153    
154    
155 dpavlin 157 #----- main
156    
157     my $sth = $dbh->prepare( qq{
158    
159     select
160 dpavlin 158 backups.id as backup_id,
161 dpavlin 157 hosts.name as host,
162     shares.name as share,
163 dpavlin 192 backups.num as num,
164 dpavlin 196 inc_size,
165     parts
166 dpavlin 157 from backups
167     join shares on backups.hostid = shares.hostid
168     and shares.id = backups.shareid
169     join hosts on shares.hostid = hosts.id
170 dpavlin 192 where not inc_deleted
171 dpavlin 157 order by backups.date
172    
173     } );
174    
175 dpavlin 196 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });
176 dpavlin 163 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
177 dpavlin 158
178 dpavlin 157 %BackupPC::SearchLib::Conf = %Conf;
179    
180 dpavlin 213 $sth->execute();
181     my $num_backups = $sth->rows;
182     my $curr_backup = 1;
183    
184 dpavlin 157 while (my $row = $sth->fetchrow_hashref) {
185     my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
186 dpavlin 192
187 dpavlin 194 # this will return -1 if file doesn't exist
188     my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
189 dpavlin 192
190 dpavlin 213 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
191     $curr_backup++;
192 dpavlin 157
193     my $t = time();
194    
195 dpavlin 194 # re-create archive?
196 dpavlin 214 if ($row->{'inc_size'} == -1 || $size == -1 ||
197     $row->{'inc_size'} != $size ||
198     $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")
199     ) {
200 dpavlin 212 my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | $bin->{'gzip'} $Conf{GzipLevel} > ${tar_dir}/${tar_file}.tmp};
201 dpavlin 194 print STDERR "## $cmd\n" if ($debug);
202 dpavlin 157
203 dpavlin 194 system($cmd) == 0 or die "failed: $?";
204 dpavlin 212
205 dpavlin 213 rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
206 dpavlin 212
207 dpavlin 194 $size = (stat( "$tar_dir/$tar_file" ))[7];
208     }
209 dpavlin 157
210 dpavlin 194 if ($size > 45) {
211 dpavlin 157
212 dpavlin 194 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
213 dpavlin 196 $max_size *= 1024; # convert to bytes
214 dpavlin 158
215 dpavlin 196 my $parts = int( ($size + $max_size - 1) / $max_size );
216    
217     if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
218     print " join";
219 dpavlin 214 tar_join("$tar_dir/$tar_file");
220 dpavlin 196 }
221    
222 dpavlin 194 if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
223 dpavlin 196 print " split/$parts";
224 dpavlin 194 my $in = my $out = "$tar_dir/$tar_file";
225     $out .= '.tmp';
226     rename $in, $out || die "can't rename $in: $!";
227     mkdir $in || die "can't mkdir $in: $!";
228 dpavlin 196
229     my $suffix_len = length("$parts");
230 dpavlin 201 system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
231 dpavlin 194 unlink $out || die "can't unlink $out: $!";
232     }
233    
234 dpavlin 196 $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
235 dpavlin 168 $sth_inc_deleted->execute(0, $row->{'backup_id'});
236 dpavlin 194
237     printf(" %1.2f MB", ($size / 1024 / 1024));
238    
239 dpavlin 163 } else {
240     $sth_inc_deleted->execute(1, $row->{'backup_id'});
241     unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
242     print " EMPTY";
243     }
244 dpavlin 194 print ", dur: ",fmt_time(time() - $t), "\n";
245 dpavlin 158
246     $dbh->commit;
247    
248 dpavlin 157 }
249    
250     undef $sth;
251     $dbh->disconnect;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26