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

Contents of /trunk/bin/BackupPC_incPartsUpdate

Parent Directory Parent Directory | Revision Log Revision Log


Revision 230 - (show 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 #!/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 use BackupPC::Attrib qw/:all/;
10 use Data::Dumper;
11 use Time::HiRes qw/time/;
12 use POSIX qw/strftime/;
13 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);
29 $path =~ s#/[^/]+$#/#;
30 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
31
32 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
33
34 my $bin;
35 foreach my $c (qw/gzip split/) {
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;
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 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 open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
102 binmode($fh);
103 my $tar = Archive::Tar::Streamed->new($fh);
104
105 print STDERR ", tar" if ($debug);
106 my @tar_files;
107 while(my $entry = $tar->next) {
108 push @tar_files, $entry->name;
109 }
110 @tar_files = sort @tar_files;
111 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 print " ",($same ? 'ok' : 'DIFFERENT');
151 print STDERR " }} " if ($debug);
152
153 return $same;
154 }
155
156
157 #----- main
158
159 my $sth = $dbh->prepare( qq{
160
161 select
162 backups.id as backup_id,
163 hosts.name as host,
164 shares.name as share,
165 backups.num as num,
166 inc_size,
167 parts
168 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 where not inc_deleted
173 order by backups.date
174
175 } );
176
177 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });
178 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
179
180
181 $sth->execute();
182 my $num_backups = $sth->rows;
183 my $curr_backup = 1;
184
185 while (my $row = $sth->fetchrow_hashref) {
186 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
187
188 # this will return -1 if file doesn't exist
189 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
190
191 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
192 $curr_backup++;
193
194 my $t = time();
195
196 # re-create archive?
197 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 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 print STDERR "## $cmd\n" if ($debug);
203
204 system($cmd) == 0 or die "failed: $?";
205
206 rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
207
208 $size = (stat( "$tar_dir/$tar_file" ))[7];
209 }
210
211 if ($size > $MIN_TAR_SIZE) {
212
213 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchiveSize parametar";
214 $max_size *= 1024; # convert to bytes
215
216 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 # maximum file size on ISO image is 4Gb
225 # this will require Linux kernel 2.6.8 or newer
226 if ( $max_size > $max_file_size ) {
227 $max_size = $max_file_size;
228 }
229
230 my $parts = int( ($size + $max_size - 1) / $max_size );
231
232 if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
233 print " join";
234 tar_join("$tar_dir/$tar_file");
235 }
236
237 if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
238 print " split/$parts";
239 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
244 my $suffix_len = length("$parts");
245 system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
246 unlink $out || die "can't unlink $out: $!";
247 }
248
249 $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
250 $sth_inc_deleted->execute(0, $row->{'backup_id'});
251
252 printf(" %1.2f MB", ($size / 1024 / 1024));
253
254 } 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 print ", dur: ",fmt_time(time() - $t), "\n";
260
261 $dbh->commit;
262
263 }
264
265 undef $sth;
266 $dbh->disconnect;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26