/[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 256 - (show annotations)
Mon Dec 12 20:59:53 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 5744 byte(s)
 r11642@llin:  dpavlin | 2005-12-12 22:53:37 +0100
 added check of total_size

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 my $path = abs_path($0);
26 $path =~ s#/[^/]+$#/#;
27 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
28
29 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
30
31 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;
43
44 my $start_t = time();
45
46 my $t_fmt = '%Y-%m-%d %H:%M:%S';
47
48 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
49 my $user = $Conf{SearchUser} || '';
50
51 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
52
53 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
54
55 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
56
57 #---- subs ----
58
59 sub fmt_time {
60 my $t = shift || return;
61 my $out = "";
62 my ($ss,$mm,$hh) = gmtime($t);
63 $out .= "${hh}h" if ($hh);
64 $out .= sprintf("%02d:%02d", $mm,$ss);
65 return $out;
66 }
67
68 sub curr_time {
69 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
183
184 my $sth = $dbh->prepare( qq{
185
186 select
187 backups.id as backup_id,
188 hosts.name as host,
189 shares.name as share,
190 backups.num as num,
191 inc_size,
192 parts
193 from backups
194 join shares on backups.hostid = shares.hostid
195 and shares.id = backups.shareid
196 join hosts on shares.hostid = hosts.id
197 where not inc_deleted
198 order by backups.date
199
200 } );
201
202 $sth->execute();
203 my $num_backups = $sth->rows;
204 my $curr_backup = 1;
205
206 while (my $row = $sth->fetchrow_hashref) {
207 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
208
209 # this will return -1 if file doesn't exist
210 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
211
212 print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
213
214 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 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
223 $curr_backup++;
224
225 my $t = time();
226
227 # re-create archive?
228 my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
229 print STDERR "## $cmd\n" if ($debug);
230
231 if (system($cmd) != 0) {
232 print STDERR " FAILED";
233 }
234
235 print ", dur: ",fmt_time(time() - $t), "\n";
236
237 $dbh->commit;
238
239 }
240
241 undef $sth;
242 $dbh->disconnect;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26