/[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 218 - (hide annotations)
Sun Oct 16 17:48:05 2005 UTC (18 years, 7 months ago) by dpavlin
File size: 6343 byte(s)
added MIN_TAR_SIZE cludge as variable

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26