/[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 230 - (hide annotations)
Tue Oct 25 09:30:52 2005 UTC (18 years, 7 months ago) by dpavlin
File size: 6780 byte(s)
 r8685@llin:  dpavlin | 2005-10-25 11:30:44 +0200
 implemented MaxArchiveFileSize in configuration and javascript 

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26