1 |
dpavlin |
234 |
#!/usr/bin/perl -w |
2 |
dpavlin |
100 |
#============================================================= -*-perl-*- |
3 |
|
|
# |
4 |
dpavlin |
109 |
# BackupPC_tarIncCreate: create a tar archive of an existing incremental dump |
5 |
|
|
# |
6 |
dpavlin |
100 |
# |
7 |
|
|
# DESCRIPTION |
8 |
|
|
# |
9 |
dpavlin |
112 |
# Usage: BackupPC_tarIncCreate [options] |
10 |
dpavlin |
100 |
# |
11 |
|
|
# Flags: |
12 |
|
|
# Required options: |
13 |
|
|
# |
14 |
|
|
# -h host Host from which the tar archive is created. |
15 |
|
|
# -n dumpNum Dump number from which the tar archive is created. |
16 |
|
|
# A negative number means relative to the end (eg -1 |
17 |
|
|
# means the most recent dump, -2 2nd most recent etc). |
18 |
|
|
# -s shareName Share name from which the tar archive is created. |
19 |
|
|
# |
20 |
|
|
# Other options: |
21 |
|
|
# -t print summary totals |
22 |
|
|
# -r pathRemove path prefix that will be replaced with pathAdd |
23 |
|
|
# -p pathAdd new path prefix |
24 |
|
|
# -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar) |
25 |
|
|
# -w writeBufSz write buffer size (default 1MB) |
26 |
|
|
# |
27 |
|
|
# The -h, -n and -s options specify which dump is used to generate |
28 |
|
|
# the tar archive. The -r and -p options can be used to relocate |
29 |
|
|
# the paths in the tar archive so extracted files can be placed |
30 |
|
|
# in a location different from their original location. |
31 |
|
|
# |
32 |
|
|
# AUTHOR |
33 |
|
|
# Craig Barratt <cbarratt@users.sourceforge.net> |
34 |
dpavlin |
112 |
# Ivan Klaric <iklaric@gmail.com> |
35 |
|
|
# Dobrica Pavlinusic <dpavlin@rot13.org> |
36 |
dpavlin |
100 |
# |
37 |
|
|
# COPYRIGHT |
38 |
|
|
# Copyright (C) 2001-2003 Craig Barratt |
39 |
|
|
# |
40 |
|
|
# This program is free software; you can redistribute it and/or modify |
41 |
|
|
# it under the terms of the GNU General Public License as published by |
42 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
43 |
|
|
# (at your option) any later version. |
44 |
|
|
# |
45 |
|
|
# This program is distributed in the hope that it will be useful, |
46 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
47 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
48 |
|
|
# GNU General Public License for more details. |
49 |
|
|
# |
50 |
|
|
# You should have received a copy of the GNU General Public License |
51 |
|
|
# along with this program; if not, write to the Free Software |
52 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
53 |
|
|
# |
54 |
|
|
#======================================================================== |
55 |
|
|
# |
56 |
|
|
# Version 2.1.0, released 20 Jun 2004. |
57 |
|
|
# |
58 |
|
|
# See http://backuppc.sourceforge.net. |
59 |
|
|
# |
60 |
|
|
#======================================================================== |
61 |
|
|
|
62 |
|
|
use strict; |
63 |
|
|
no utf8; |
64 |
|
|
use lib "__INSTALLDIR__/lib"; |
65 |
|
|
use File::Path; |
66 |
|
|
use Getopt::Std; |
67 |
|
|
use DBI; |
68 |
|
|
use BackupPC::Lib; |
69 |
|
|
use BackupPC::Attrib qw(:all); |
70 |
|
|
use BackupPC::FileZIO; |
71 |
|
|
use BackupPC::View; |
72 |
|
|
use BackupPC::SearchLib; |
73 |
dpavlin |
112 |
use Time::HiRes qw/time/; |
74 |
|
|
use POSIX qw/strftime/; |
75 |
dpavlin |
234 |
use File::Which; |
76 |
|
|
use File::Path; |
77 |
dpavlin |
235 |
use File::Slurp; |
78 |
dpavlin |
112 |
use Data::Dumper; ### FIXME |
79 |
dpavlin |
100 |
|
80 |
|
|
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); |
81 |
|
|
my $TopDir = $bpc->TopDir(); |
82 |
|
|
my $BinDir = $bpc->BinDir(); |
83 |
|
|
my %Conf = $bpc->Conf(); |
84 |
dpavlin |
234 |
%BackupPC::SearchLib::Conf = %Conf; |
85 |
dpavlin |
100 |
my %opts; |
86 |
dpavlin |
112 |
my $in_backup_increment; |
87 |
dpavlin |
100 |
|
88 |
dpavlin |
112 |
|
89 |
dpavlin |
252 |
if ( !getopts("th:n:p:r:s:b:w:vdf", \%opts) ) { |
90 |
dpavlin |
100 |
print STDERR <<EOF; |
91 |
dpavlin |
112 |
usage: $0 [options] |
92 |
dpavlin |
100 |
Required options: |
93 |
|
|
-h host host from which the tar archive is created |
94 |
|
|
-n dumpNum dump number from which the tar archive is created |
95 |
|
|
A negative number means relative to the end (eg -1 |
96 |
|
|
means the most recent dump, -2 2nd most recent etc). |
97 |
|
|
-s shareName share name from which the tar archive is created |
98 |
|
|
|
99 |
|
|
Other options: |
100 |
|
|
-t print summary totals |
101 |
|
|
-r pathRemove path prefix that will be replaced with pathAdd |
102 |
|
|
-p pathAdd new path prefix |
103 |
|
|
-b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar) |
104 |
|
|
-w writeBufSz write buffer size (default 1048576 = 1MB) |
105 |
dpavlin |
252 |
-f overwrite existing parts |
106 |
dpavlin |
156 |
-v verbose output |
107 |
dpavlin |
234 |
-d debug output |
108 |
dpavlin |
100 |
EOF |
109 |
|
|
exit(1); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) { |
113 |
dpavlin |
236 |
die "$0: bad host name '$opts{h}'\n"; |
114 |
dpavlin |
100 |
} |
115 |
|
|
my $Host = $opts{h}; |
116 |
|
|
|
117 |
|
|
if ( $opts{n} !~ /^(-?\d+)$/ ) { |
118 |
dpavlin |
236 |
die "$0: bad dump number '$opts{n}'\n"; |
119 |
dpavlin |
100 |
} |
120 |
|
|
my $Num = $opts{n}; |
121 |
|
|
|
122 |
dpavlin |
234 |
my $bin; |
123 |
|
|
foreach my $c (qw/gzip md5sum tee/) { |
124 |
|
|
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
125 |
|
|
} |
126 |
|
|
|
127 |
dpavlin |
100 |
my @Backups = $bpc->BackupInfoRead($Host); |
128 |
|
|
my $FileCnt = 0; |
129 |
|
|
my $ByteCnt = 0; |
130 |
|
|
my $DirCnt = 0; |
131 |
|
|
my $SpecialCnt = 0; |
132 |
|
|
my $ErrorCnt = 0; |
133 |
dpavlin |
234 |
my $current_tar_size = 0; |
134 |
dpavlin |
236 |
my $total_increment_size = 0; |
135 |
dpavlin |
100 |
|
136 |
|
|
my $i; |
137 |
|
|
$Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 ); |
138 |
|
|
for ( $i = 0 ; $i < @Backups ; $i++ ) { |
139 |
|
|
last if ( $Backups[$i]{num} == $Num ); |
140 |
|
|
} |
141 |
|
|
if ( $i >= @Backups ) { |
142 |
dpavlin |
236 |
die "$0: bad backup number $Num for host $Host\n"; |
143 |
dpavlin |
100 |
} |
144 |
|
|
|
145 |
|
|
my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ ); |
146 |
|
|
my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ ); |
147 |
|
|
if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) { |
148 |
dpavlin |
236 |
die "$0: bad share name '$opts{s}'\n"; |
149 |
dpavlin |
100 |
} |
150 |
|
|
our $ShareName = $opts{s}; |
151 |
|
|
our $view = BackupPC::View->new($bpc, $Host, \@Backups); |
152 |
|
|
|
153 |
dpavlin |
235 |
# database |
154 |
|
|
|
155 |
|
|
my $dsn = $Conf{SearchDSN}; |
156 |
|
|
my $db_user = $Conf{SearchUser} || ''; |
157 |
|
|
|
158 |
|
|
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 0} ); |
159 |
|
|
|
160 |
|
|
my $sth_inc_size = $dbh->prepare(qq{ |
161 |
|
|
update backups set |
162 |
|
|
inc_size = ?, |
163 |
|
|
parts = ?, |
164 |
|
|
inc_deleted = false |
165 |
dpavlin |
251 |
where id = ? |
166 |
|
|
}); |
167 |
dpavlin |
235 |
my $sth_backup_parts = $dbh->prepare(qq{ |
168 |
|
|
insert into backup_parts ( |
169 |
|
|
backup_id, |
170 |
|
|
part_nr, |
171 |
|
|
tar_size, |
172 |
|
|
size, |
173 |
|
|
md5, |
174 |
|
|
items |
175 |
|
|
) values (?,?,?,?,?,?) |
176 |
|
|
}); |
177 |
|
|
|
178 |
dpavlin |
100 |
# |
179 |
|
|
# This constant and the line of code below that uses it are borrowed |
180 |
|
|
# from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander. |
181 |
|
|
# See www.cpan.org. |
182 |
|
|
# |
183 |
|
|
# Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved. |
184 |
|
|
# Copyright 1998 Stephen Zander. All rights reserved. |
185 |
|
|
# |
186 |
|
|
my $tar_pack_header |
187 |
|
|
= 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12'; |
188 |
|
|
my $tar_header_length = 512; |
189 |
|
|
|
190 |
|
|
my $BufSize = $opts{w} || 1048576; # 1MB or 2^20 |
191 |
|
|
my $WriteBuf = ""; |
192 |
|
|
my $WriteBufSz = ($opts{b} || 20) * $tar_header_length; |
193 |
|
|
|
194 |
|
|
my(%UidCache, %GidCache); |
195 |
|
|
my(%HardLinkExtraFiles, @HardLinks); |
196 |
|
|
|
197 |
|
|
# |
198 |
|
|
# Write out all the requested files/directories |
199 |
|
|
# |
200 |
dpavlin |
112 |
|
201 |
dpavlin |
234 |
my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar"; |
202 |
|
|
|
203 |
|
|
my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}; |
204 |
|
|
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir); |
205 |
|
|
|
206 |
|
|
my $tar_file = BackupPC::SearchLib::getGzipName($Host, $ShareName, $Num) || die "can't getGzipName($Host, $ShareName, $Num)"; |
207 |
|
|
|
208 |
dpavlin |
251 |
my $tar_path_final = $tar_dir . '/' . $tar_file; |
209 |
|
|
my $tar_path = $tar_path_final . '.tmp'; |
210 |
|
|
|
211 |
dpavlin |
234 |
$tar_path =~ s#//#/#g; |
212 |
|
|
|
213 |
dpavlin |
235 |
my $sth = $dbh->prepare(qq{ |
214 |
|
|
SELECT |
215 |
|
|
backups.id |
216 |
|
|
FROM backups |
217 |
|
|
JOIN shares on shares.id = shareid |
218 |
|
|
JOIN hosts on hosts.id = shares.hostid |
219 |
|
|
WHERE hosts.name = ? and shares.name = ? and backups.num = ? |
220 |
|
|
}); |
221 |
|
|
$sth->execute($Host, $ShareName, $Num); |
222 |
|
|
my ($backup_id) = $sth->fetchrow_array; |
223 |
|
|
$sth->finish; |
224 |
dpavlin |
234 |
|
225 |
dpavlin |
257 |
|
226 |
|
|
# delete exising backup_parts |
227 |
|
|
my $sth_delete_backup_parts = $dbh->prepare(qq{ |
228 |
|
|
delete from backup_parts |
229 |
|
|
where backup_id = ? |
230 |
|
|
}); |
231 |
|
|
$sth_delete_backup_parts->execute($backup_id); |
232 |
|
|
|
233 |
|
|
|
234 |
dpavlin |
235 |
print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d}); |
235 |
|
|
|
236 |
dpavlin |
252 |
if (-e $tar_path_final) { |
237 |
|
|
if ($opts{f}) { |
238 |
|
|
rmtree $tar_path_final || die "can't remove $tar_path_final: $!"; |
239 |
|
|
} else { |
240 |
|
|
die "$tar_path_final allready exists\n"; |
241 |
|
|
} |
242 |
|
|
} |
243 |
dpavlin |
235 |
|
244 |
dpavlin |
234 |
my $fh; |
245 |
|
|
my $part = 0; |
246 |
|
|
my $no_files = 0; |
247 |
dpavlin |
235 |
my $items_in_part = 0; |
248 |
dpavlin |
234 |
|
249 |
|
|
sub new_tar_part { |
250 |
dpavlin |
236 |
my $arg = {@_}; |
251 |
|
|
|
252 |
dpavlin |
234 |
if ($fh) { |
253 |
|
|
return if ($current_tar_size == 0); |
254 |
|
|
|
255 |
dpavlin |
271 |
print STDERR "\n\t+ $part:"; |
256 |
dpavlin |
234 |
|
257 |
dpavlin |
236 |
# |
258 |
|
|
# Finish with two null 512 byte headers, |
259 |
|
|
# and then round out a full block. |
260 |
|
|
# |
261 |
dpavlin |
234 |
my $data = "\0" x ($tar_header_length * 2); |
262 |
|
|
TarWrite($fh, \$data); |
263 |
|
|
TarWrite($fh, undef); |
264 |
|
|
|
265 |
|
|
close($fh) || die "can't close archive part $part: $!"; |
266 |
dpavlin |
235 |
|
267 |
|
|
my $file = $tar_path . '/' . $part; |
268 |
|
|
|
269 |
|
|
my $md5 = read_file( $file . '.md5' ) || die "can't read md5sum file ${file}.md5"; |
270 |
|
|
$md5 =~ s/\s.*$//; |
271 |
|
|
|
272 |
|
|
my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz"; |
273 |
|
|
|
274 |
dpavlin |
271 |
print "$file, $size bytes, $items_in_part items"; |
275 |
|
|
|
276 |
dpavlin |
235 |
$sth_backup_parts->execute( |
277 |
|
|
$backup_id, |
278 |
|
|
$part, |
279 |
|
|
$current_tar_size, |
280 |
|
|
$size, |
281 |
|
|
$md5, |
282 |
|
|
$items_in_part, |
283 |
|
|
); |
284 |
|
|
|
285 |
dpavlin |
253 |
$total_increment_size += $size; |
286 |
dpavlin |
236 |
|
287 |
|
|
if ($arg->{close}) { |
288 |
dpavlin |
251 |
|
289 |
dpavlin |
252 |
sub move($$) { |
290 |
|
|
my ($from,$to) = @_; |
291 |
|
|
print STDERR "# rename $from -> $to\n" if ($opts{d}); |
292 |
|
|
rename $from, $to || die "can't move $from -> $to: $!\n"; |
293 |
|
|
} |
294 |
|
|
|
295 |
dpavlin |
251 |
if ($part == 1) { |
296 |
dpavlin |
254 |
print STDERR " single" if ($opts{v}); |
297 |
dpavlin |
252 |
move("${tar_path}/1.tar.gz", "${tar_path_final}.tar.gz"); |
298 |
|
|
move("${tar_path}/1.md5", "${tar_path_final}.md5"); |
299 |
|
|
rmtree $tar_path or die "can't remove temporary dir $tar_path: $!"; |
300 |
dpavlin |
251 |
} else { |
301 |
dpavlin |
254 |
print STDERR " [last]" if ($opts{v}); |
302 |
dpavlin |
252 |
move("${tar_path}", "${tar_path_final}"); |
303 |
dpavlin |
261 |
|
304 |
|
|
# if this archive was single part, remove it |
305 |
|
|
foreach my $suffix (qw/.tar.gz .md5/) { |
306 |
|
|
my $path = $tar_path_final . $suffix; |
307 |
|
|
unlink $path if (-e $path); |
308 |
|
|
} |
309 |
dpavlin |
251 |
} |
310 |
|
|
|
311 |
dpavlin |
236 |
$sth_inc_size->execute( |
312 |
|
|
$total_increment_size, |
313 |
|
|
$part, |
314 |
|
|
$backup_id |
315 |
|
|
); |
316 |
dpavlin |
251 |
|
317 |
dpavlin |
271 |
print "\n\ttotal $total_increment_size bytes"; |
318 |
dpavlin |
251 |
|
319 |
dpavlin |
236 |
return; |
320 |
|
|
} |
321 |
|
|
|
322 |
dpavlin |
234 |
} |
323 |
|
|
|
324 |
|
|
$part++; |
325 |
|
|
|
326 |
|
|
# if this is first part, create directory |
327 |
|
|
|
328 |
|
|
if ($part == 1) { |
329 |
dpavlin |
253 |
if (-e $tar_path) { |
330 |
dpavlin |
234 |
print STDERR "# deleting existing $tar_path\n" if ($opts{d}); |
331 |
|
|
rmtree($tar_path); |
332 |
|
|
} |
333 |
|
|
mkdir($tar_path) || die "can't create directory $tar_path: $!"; |
334 |
dpavlin |
236 |
|
335 |
|
|
sub abort_cleanup { |
336 |
|
|
print STDERR "ABORTED: cleanup temp dir"; |
337 |
|
|
rmtree($tar_path); |
338 |
|
|
$dbh->rollback; |
339 |
|
|
exit 1; |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
$SIG{'INT'} = \&abort_cleanup; |
343 |
|
|
$SIG{'QUIT'} = \&abort_cleanup; |
344 |
|
|
$SIG{'__DIE__'} = \&abort_cleanup; |
345 |
|
|
|
346 |
dpavlin |
234 |
} |
347 |
|
|
|
348 |
|
|
my $file = $tar_path . '/' . $part; |
349 |
|
|
|
350 |
|
|
# |
351 |
|
|
# create comprex pipe which will pass output through gzip |
352 |
|
|
# for compression, create file on disk using tee |
353 |
|
|
# and pipe same output to md5sum to create checksum |
354 |
|
|
# |
355 |
|
|
|
356 |
|
|
my $cmd = '| ' . $bin->{'gzip'} . ' ' . $Conf{GzipLevel} . ' ' . |
357 |
|
|
'| ' . $bin->{'tee'} . ' ' . $file . '.tar.gz' . ' ' . |
358 |
|
|
'| ' . $bin->{'md5sum'} . ' - > ' . $file . '.md5'; |
359 |
|
|
|
360 |
|
|
print STDERR "## $cmd\n" if ($opts{d}); |
361 |
|
|
|
362 |
|
|
open($fh, $cmd) or die "can't open $cmd: $!"; |
363 |
|
|
binmode($fh); |
364 |
dpavlin |
235 |
|
365 |
dpavlin |
234 |
$current_tar_size = 0; |
366 |
dpavlin |
235 |
$items_in_part = 0; |
367 |
dpavlin |
234 |
} |
368 |
|
|
|
369 |
|
|
new_tar_part(); |
370 |
|
|
|
371 |
dpavlin |
112 |
if (seedCache($Host, $ShareName, $Num)) { |
372 |
|
|
archiveWrite($fh, '/'); |
373 |
|
|
archiveWriteHardLinks($fh); |
374 |
dpavlin |
252 |
new_tar_part( close => 1 ); |
375 |
dpavlin |
100 |
} else { |
376 |
dpavlin |
170 |
print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v}); |
377 |
dpavlin |
252 |
# remove temporary files if there are no files |
378 |
dpavlin |
234 |
rmtree($tar_path); |
379 |
|
|
} |
380 |
|
|
|
381 |
dpavlin |
100 |
# |
382 |
|
|
# print out totals if requested |
383 |
|
|
# |
384 |
|
|
if ( $opts{t} ) { |
385 |
|
|
print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,", |
386 |
|
|
" $SpecialCnt specials, $ErrorCnt errors\n"; |
387 |
|
|
} |
388 |
|
|
if ( $ErrorCnt && !$FileCnt && !$DirCnt ) { |
389 |
|
|
# |
390 |
|
|
# Got errors, with no files or directories; exit with non-zero |
391 |
|
|
# status |
392 |
|
|
# |
393 |
dpavlin |
236 |
die "got errors or no files\n"; |
394 |
dpavlin |
100 |
} |
395 |
dpavlin |
234 |
|
396 |
dpavlin |
235 |
$sth_inc_size->finish; |
397 |
|
|
$sth_backup_parts->finish; |
398 |
|
|
|
399 |
|
|
$dbh->commit || die "can't commit changes to database"; |
400 |
|
|
$dbh->disconnect(); |
401 |
|
|
|
402 |
dpavlin |
236 |
exit; |
403 |
dpavlin |
100 |
|
404 |
|
|
########################################################################### |
405 |
|
|
# Subroutines |
406 |
|
|
########################################################################### |
407 |
|
|
|
408 |
|
|
sub archiveWrite |
409 |
|
|
{ |
410 |
|
|
my($fh, $dir, $tarPathOverride) = @_; |
411 |
|
|
|
412 |
|
|
if ( $dir =~ m{(^|/)\.\.(/|$)} ) { |
413 |
|
|
print(STDERR "$0: bad directory '$dir'\n"); |
414 |
|
|
$ErrorCnt++; |
415 |
|
|
return; |
416 |
|
|
} |
417 |
|
|
$dir = "/" if ( $dir eq "." ); |
418 |
|
|
#print(STDERR "calling find with $Num, $ShareName, $dir\n"); |
419 |
|
|
|
420 |
|
|
if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile, |
421 |
|
|
$fh, $tarPathOverride) < 0 ) { |
422 |
|
|
print(STDERR "$0: bad share or directory '$ShareName/$dir'\n"); |
423 |
|
|
$ErrorCnt++; |
424 |
|
|
return; |
425 |
|
|
} |
426 |
|
|
} |
427 |
|
|
|
428 |
|
|
# |
429 |
|
|
# Write out any hardlinks (if any) |
430 |
|
|
# |
431 |
|
|
sub archiveWriteHardLinks |
432 |
|
|
{ |
433 |
|
|
my $fh = @_; |
434 |
|
|
foreach my $hdr ( @HardLinks ) { |
435 |
|
|
$hdr->{size} = 0; |
436 |
|
|
if ( defined($PathRemove) |
437 |
|
|
&& substr($hdr->{linkname}, 0, length($PathRemove)+1) |
438 |
|
|
eq ".$PathRemove" ) { |
439 |
|
|
substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd"; |
440 |
|
|
} |
441 |
|
|
TarWriteFileInfo($fh, $hdr); |
442 |
|
|
} |
443 |
|
|
@HardLinks = (); |
444 |
|
|
%HardLinkExtraFiles = (); |
445 |
|
|
} |
446 |
|
|
|
447 |
|
|
sub UidLookup |
448 |
|
|
{ |
449 |
|
|
my($uid) = @_; |
450 |
|
|
|
451 |
|
|
$UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) ); |
452 |
|
|
return $UidCache{$uid}; |
453 |
|
|
} |
454 |
|
|
|
455 |
|
|
sub GidLookup |
456 |
|
|
{ |
457 |
|
|
my($gid) = @_; |
458 |
|
|
|
459 |
|
|
$GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) ); |
460 |
|
|
return $GidCache{$gid}; |
461 |
|
|
} |
462 |
|
|
|
463 |
|
|
sub TarWrite |
464 |
|
|
{ |
465 |
|
|
my($fh, $dataRef) = @_; |
466 |
|
|
|
467 |
dpavlin |
234 |
|
468 |
dpavlin |
100 |
if ( !defined($dataRef) ) { |
469 |
|
|
# |
470 |
|
|
# do flush by padding to a full $WriteBufSz |
471 |
|
|
# |
472 |
|
|
my $data = "\0" x ($WriteBufSz - length($WriteBuf)); |
473 |
|
|
$dataRef = \$data; |
474 |
|
|
} |
475 |
dpavlin |
234 |
|
476 |
|
|
# poor man's tell :-) |
477 |
|
|
$current_tar_size += length($$dataRef); |
478 |
|
|
|
479 |
dpavlin |
100 |
if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) { |
480 |
|
|
# |
481 |
|
|
# just buffer and return |
482 |
|
|
# |
483 |
|
|
$WriteBuf .= $$dataRef; |
484 |
|
|
return; |
485 |
|
|
} |
486 |
|
|
my $done = $WriteBufSz - length($WriteBuf); |
487 |
|
|
if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done)) |
488 |
|
|
!= $WriteBufSz ) { |
489 |
dpavlin |
236 |
die "Unable to write to output file ($!)\n"; |
490 |
dpavlin |
100 |
} |
491 |
|
|
while ( $done + $WriteBufSz <= length($$dataRef) ) { |
492 |
|
|
if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz)) |
493 |
|
|
!= $WriteBufSz ) { |
494 |
dpavlin |
236 |
die "Unable to write to output file ($!)\n"; |
495 |
dpavlin |
100 |
} |
496 |
|
|
$done += $WriteBufSz; |
497 |
|
|
} |
498 |
|
|
$WriteBuf = substr($$dataRef, $done); |
499 |
|
|
} |
500 |
|
|
|
501 |
|
|
sub TarWritePad |
502 |
|
|
{ |
503 |
|
|
my($fh, $size) = @_; |
504 |
|
|
|
505 |
|
|
if ( $size % $tar_header_length ) { |
506 |
|
|
my $data = "\0" x ($tar_header_length - ($size % $tar_header_length)); |
507 |
|
|
TarWrite($fh, \$data); |
508 |
|
|
} |
509 |
|
|
} |
510 |
|
|
|
511 |
|
|
sub TarWriteHeader |
512 |
|
|
{ |
513 |
|
|
my($fh, $hdr) = @_; |
514 |
|
|
|
515 |
|
|
$hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) ); |
516 |
|
|
$hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) ); |
517 |
|
|
my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor}) |
518 |
|
|
: ""; |
519 |
|
|
my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor}) |
520 |
|
|
: ""; |
521 |
|
|
my $sizeStr; |
522 |
|
|
if ( $hdr->{size} >= 2 * 65536 * 65536 ) { |
523 |
|
|
# |
524 |
|
|
# GNU extension for files >= 8GB: send size in big-endian binary |
525 |
|
|
# |
526 |
|
|
$sizeStr = pack("c4 N N", 0x80, 0, 0, 0, |
527 |
|
|
$hdr->{size} / (65536 * 65536), |
528 |
|
|
$hdr->{size} % (65536 * 65536)); |
529 |
|
|
} elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) { |
530 |
|
|
# |
531 |
|
|
# sprintf octal only handles up to 2^32 - 1 |
532 |
|
|
# |
533 |
|
|
$sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24)) |
534 |
|
|
. sprintf("%08o", $hdr->{size} % (1 << 24)); |
535 |
|
|
} else { |
536 |
|
|
$sizeStr = sprintf("%011o", $hdr->{size}); |
537 |
|
|
} |
538 |
|
|
my $data = pack($tar_pack_header, |
539 |
|
|
substr($hdr->{name}, 0, 99), |
540 |
|
|
sprintf("%07o", $hdr->{mode}), |
541 |
|
|
sprintf("%07o", $hdr->{uid}), |
542 |
|
|
sprintf("%07o", $hdr->{gid}), |
543 |
|
|
$sizeStr, |
544 |
|
|
sprintf("%011o", $hdr->{mtime}), |
545 |
|
|
"", #checksum field - space padded by pack("A8") |
546 |
|
|
$hdr->{type}, |
547 |
|
|
substr($hdr->{linkname}, 0, 99), |
548 |
|
|
$hdr->{magic} || 'ustar ', |
549 |
|
|
$hdr->{version} || ' ', |
550 |
|
|
$hdr->{uname}, |
551 |
|
|
$hdr->{gname}, |
552 |
|
|
$devmajor, |
553 |
|
|
$devminor, |
554 |
|
|
"" # prefix is empty |
555 |
|
|
); |
556 |
|
|
substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data)); |
557 |
|
|
TarWrite($fh, \$data); |
558 |
|
|
} |
559 |
|
|
|
560 |
|
|
sub TarWriteFileInfo |
561 |
|
|
{ |
562 |
|
|
my($fh, $hdr) = @_; |
563 |
|
|
|
564 |
|
|
# |
565 |
|
|
# Handle long link names (symbolic links) |
566 |
|
|
# |
567 |
|
|
if ( length($hdr->{linkname}) > 99 ) { |
568 |
|
|
my %h; |
569 |
|
|
my $data = $hdr->{linkname} . "\0"; |
570 |
|
|
$h{name} = "././\@LongLink"; |
571 |
|
|
$h{type} = "K"; |
572 |
|
|
$h{size} = length($data); |
573 |
|
|
TarWriteHeader($fh, \%h); |
574 |
|
|
TarWrite($fh, \$data); |
575 |
|
|
TarWritePad($fh, length($data)); |
576 |
|
|
} |
577 |
|
|
# |
578 |
|
|
# Handle long file names |
579 |
|
|
# |
580 |
|
|
if ( length($hdr->{name}) > 99 ) { |
581 |
|
|
my %h; |
582 |
|
|
my $data = $hdr->{name} . "\0"; |
583 |
|
|
$h{name} = "././\@LongLink"; |
584 |
|
|
$h{type} = "L"; |
585 |
|
|
$h{size} = length($data); |
586 |
|
|
TarWriteHeader($fh, \%h); |
587 |
|
|
TarWrite($fh, \$data); |
588 |
|
|
TarWritePad($fh, length($data)); |
589 |
|
|
} |
590 |
|
|
TarWriteHeader($fh, $hdr); |
591 |
|
|
} |
592 |
|
|
|
593 |
|
|
# |
594 |
dpavlin |
112 |
# seed cache of files in this increment |
595 |
dpavlin |
100 |
# |
596 |
dpavlin |
112 |
sub seedCache($$$) { |
597 |
|
|
my ($host, $share, $dumpNo) = @_; |
598 |
|
|
|
599 |
dpavlin |
251 |
print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v}); |
600 |
dpavlin |
112 |
my $sql = q{ |
601 |
dpavlin |
234 |
SELECT path,size |
602 |
dpavlin |
112 |
FROM files |
603 |
|
|
JOIN shares on shares.id = shareid |
604 |
|
|
JOIN hosts on hosts.id = shares.hostid |
605 |
|
|
WHERE hosts.name = ? and shares.name = ? and backupnum = ? |
606 |
|
|
}; |
607 |
dpavlin |
100 |
|
608 |
dpavlin |
112 |
my $sth = $dbh->prepare($sql); |
609 |
|
|
$sth->execute($host, $share, $dumpNo); |
610 |
|
|
my $count = $sth->rows; |
611 |
dpavlin |
251 |
print STDERR " $count items, parts:" if ($opts{v}); |
612 |
dpavlin |
112 |
while (my $row = $sth->fetchrow_arrayref) { |
613 |
dpavlin |
156 |
#print STDERR "+ ", $row->[0],"\n"; |
614 |
dpavlin |
234 |
$in_backup_increment->{ $row->[0] } = $row->[1]; |
615 |
dpavlin |
100 |
} |
616 |
dpavlin |
112 |
|
617 |
|
|
$sth->finish(); |
618 |
dpavlin |
100 |
|
619 |
dpavlin |
112 |
return $count; |
620 |
dpavlin |
100 |
} |
621 |
|
|
|
622 |
dpavlin |
234 |
# |
623 |
|
|
# calculate overhad for one file in tar |
624 |
|
|
# |
625 |
|
|
sub tar_overhead($) { |
626 |
|
|
my $name = shift || ''; |
627 |
|
|
|
628 |
|
|
# header, padding of file and two null blocks at end |
629 |
|
|
my $len = 4 * $tar_header_length; |
630 |
|
|
|
631 |
|
|
# if filename is longer than 99 chars subtract blocks for |
632 |
|
|
# long filename |
633 |
|
|
if ( length($name) > 99 ) { |
634 |
|
|
$len += int( ( length($name) + $tar_header_length ) / $tar_header_length ) * $tar_header_length; |
635 |
|
|
} |
636 |
|
|
|
637 |
|
|
return $len; |
638 |
|
|
} |
639 |
|
|
|
640 |
dpavlin |
100 |
my $Attr; |
641 |
|
|
my $AttrDir; |
642 |
|
|
|
643 |
|
|
sub TarWriteFile |
644 |
|
|
{ |
645 |
|
|
my($hdr, $fh, $tarPathOverride) = @_; |
646 |
|
|
|
647 |
|
|
my $tarPath = $hdr->{relPath}; |
648 |
|
|
$tarPath = $tarPathOverride if ( defined($tarPathOverride) ); |
649 |
|
|
|
650 |
|
|
$tarPath =~ s{//+}{/}g; |
651 |
dpavlin |
112 |
|
652 |
dpavlin |
234 |
#print STDERR "? $tarPath\n" if ($opts{d}); |
653 |
|
|
my $size = $in_backup_increment->{$tarPath}; |
654 |
|
|
return unless (defined($size)); |
655 |
dpavlin |
112 |
|
656 |
dpavlin |
234 |
# is this file too large to fit into MaxArchiveFileSize? |
657 |
|
|
|
658 |
|
|
if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) { |
659 |
dpavlin |
251 |
print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d}); |
660 |
dpavlin |
234 |
new_tar_part(); |
661 |
|
|
} |
662 |
|
|
|
663 |
dpavlin |
235 |
#print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d}); |
664 |
|
|
$items_in_part++; |
665 |
dpavlin |
234 |
|
666 |
dpavlin |
100 |
if ( defined($PathRemove) |
667 |
|
|
&& substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) { |
668 |
|
|
substr($tarPath, 0, length($PathRemove)) = $PathAdd; |
669 |
|
|
} |
670 |
|
|
$tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// ); |
671 |
|
|
$tarPath =~ s{//+}{/}g; |
672 |
|
|
$hdr->{name} = $tarPath; |
673 |
|
|
|
674 |
|
|
if ( $hdr->{type} == BPC_FTYPE_DIR ) { |
675 |
|
|
# |
676 |
|
|
# Directory: just write the header |
677 |
|
|
# |
678 |
|
|
$hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} ); |
679 |
dpavlin |
112 |
TarWriteFileInfo($fh, $hdr); |
680 |
|
|
$DirCnt++; |
681 |
dpavlin |
100 |
} elsif ( $hdr->{type} == BPC_FTYPE_FILE ) { |
682 |
|
|
# |
683 |
|
|
# Regular file: write the header and file |
684 |
|
|
# |
685 |
|
|
my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress}); |
686 |
|
|
if ( !defined($f) ) { |
687 |
|
|
print(STDERR "Unable to open file $hdr->{fullPath}\n"); |
688 |
|
|
$ErrorCnt++; |
689 |
|
|
return; |
690 |
|
|
} |
691 |
dpavlin |
234 |
# do we need to split file? |
692 |
|
|
if ($hdr->{size} < $max_file_size) { |
693 |
|
|
TarWriteFileInfo($fh, $hdr); |
694 |
|
|
my($data, $size); |
695 |
|
|
while ( $f->read(\$data, $BufSize) > 0 ) { |
696 |
|
|
TarWrite($fh, \$data); |
697 |
|
|
$size += length($data); |
698 |
|
|
} |
699 |
|
|
$f->close; |
700 |
|
|
TarWritePad($fh, $size); |
701 |
dpavlin |
100 |
$FileCnt++; |
702 |
|
|
$ByteCnt += $size; |
703 |
dpavlin |
234 |
} else { |
704 |
|
|
my $full_size = $hdr->{size}; |
705 |
|
|
my $orig_name = $hdr->{name}; |
706 |
|
|
my $max_part_size = $max_file_size - tar_overhead($hdr->{name}); |
707 |
|
|
|
708 |
|
|
my $parts = int(($full_size + $max_part_size - 1) / $max_part_size); |
709 |
|
|
print STDERR "# splitting $orig_name [$full_size bytes] into $parts parts\n" if ($opts{d}); |
710 |
|
|
foreach my $subpart ( 1 .. $parts ) { |
711 |
|
|
new_tar_part(); |
712 |
|
|
if ($subpart < $parts) { |
713 |
|
|
$hdr->{size} = $max_part_size; |
714 |
|
|
} else { |
715 |
|
|
$hdr->{size} = $full_size % $max_part_size; |
716 |
|
|
} |
717 |
|
|
$hdr->{name} = $orig_name . '/' . $subpart; |
718 |
|
|
print STDERR "## creating part $subpart ",$hdr->{name}, " [", $hdr->{size}," bytes]\n"; |
719 |
|
|
|
720 |
|
|
TarWriteFileInfo($fh, $hdr); |
721 |
|
|
my($data, $size); |
722 |
|
|
if (0) { |
723 |
|
|
for ( 1 .. int($hdr->{size} / $BufSize) ) { |
724 |
|
|
my $r_size = $f->read(\$data, $BufSize); |
725 |
|
|
die "expected $BufSize bytes read, got $r_size bytes!" if ($r_size != $BufSize); |
726 |
|
|
TarWrite($fh, \$data); |
727 |
|
|
$size += length($data); |
728 |
|
|
} |
729 |
|
|
} |
730 |
|
|
my $size_left = $hdr->{size} % $BufSize; |
731 |
|
|
my $r_size = $f->read(\$data, $size_left); |
732 |
|
|
die "expected $size_left bytes last read, got $r_size bytes!" if ($r_size != $size_left); |
733 |
|
|
|
734 |
|
|
TarWrite($fh, \$data); |
735 |
|
|
$size += length($data); |
736 |
|
|
TarWritePad($fh, $size); |
737 |
dpavlin |
235 |
|
738 |
|
|
$items_in_part++; |
739 |
dpavlin |
234 |
} |
740 |
|
|
$f->close; |
741 |
|
|
$FileCnt++; |
742 |
|
|
$ByteCnt += $full_size; |
743 |
|
|
new_tar_part(); |
744 |
|
|
} |
745 |
dpavlin |
100 |
} elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) { |
746 |
|
|
# |
747 |
|
|
# Hardlink file: either write a hardlink or the complete file |
748 |
dpavlin |
234 |
# depending upon whether the linked-to file will be written |
749 |
|
|
# to the archive. |
750 |
dpavlin |
100 |
# |
751 |
dpavlin |
234 |
# Start by reading the contents of the link. |
752 |
|
|
# |
753 |
dpavlin |
100 |
my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress}); |
754 |
|
|
if ( !defined($f) ) { |
755 |
|
|
print(STDERR "Unable to open file $hdr->{fullPath}\n"); |
756 |
|
|
$ErrorCnt++; |
757 |
|
|
return; |
758 |
|
|
} |
759 |
|
|
my $data; |
760 |
|
|
while ( $f->read(\$data, $BufSize) > 0 ) { |
761 |
|
|
$hdr->{linkname} .= $data; |
762 |
|
|
} |
763 |
dpavlin |
234 |
$f->close; |
764 |
|
|
my $done = 0; |
765 |
|
|
my $name = $hdr->{linkname}; |
766 |
|
|
$name =~ s{^\./}{/}; |
767 |
|
|
if ( $HardLinkExtraFiles{$name} ) { |
768 |
|
|
# |
769 |
|
|
# Target file will be or was written, so just remember |
770 |
|
|
# the hardlink so we can dump it later. |
771 |
|
|
# |
772 |
|
|
push(@HardLinks, $hdr); |
773 |
|
|
$SpecialCnt++; |
774 |
|
|
} else { |
775 |
|
|
# |
776 |
|
|
# Have to dump the original file. Just call the top-level |
777 |
|
|
# routine, so that we save the hassle of dealing with |
778 |
|
|
# mangling, merging and attributes. |
779 |
|
|
# |
780 |
|
|
$HardLinkExtraFiles{$hdr->{linkname}} = 1; |
781 |
|
|
archiveWrite($fh, $hdr->{linkname}, $hdr->{name}); |
782 |
|
|
} |
783 |
dpavlin |
100 |
} elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) { |
784 |
|
|
# |
785 |
|
|
# Symbolic link: read the symbolic link contents into the header |
786 |
|
|
# and write the header. |
787 |
|
|
# |
788 |
|
|
my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress}); |
789 |
|
|
if ( !defined($f) ) { |
790 |
|
|
print(STDERR "Unable to open symlink file $hdr->{fullPath}\n"); |
791 |
|
|
$ErrorCnt++; |
792 |
|
|
return; |
793 |
|
|
} |
794 |
|
|
my $data; |
795 |
|
|
while ( $f->read(\$data, $BufSize) > 0 ) { |
796 |
|
|
$hdr->{linkname} .= $data; |
797 |
|
|
} |
798 |
|
|
$f->close; |
799 |
|
|
$hdr->{size} = 0; |
800 |
|
|
TarWriteFileInfo($fh, $hdr); |
801 |
|
|
$SpecialCnt++; |
802 |
|
|
} elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV |
803 |
|
|
|| $hdr->{type} == BPC_FTYPE_BLOCKDEV |
804 |
|
|
|| $hdr->{type} == BPC_FTYPE_FIFO ) { |
805 |
|
|
# |
806 |
|
|
# Special files: for char and block special we read the |
807 |
|
|
# major and minor numbers from a plain file. |
808 |
|
|
# |
809 |
|
|
if ( $hdr->{type} != BPC_FTYPE_FIFO ) { |
810 |
|
|
my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, |
811 |
|
|
$hdr->{compress}); |
812 |
|
|
my $data; |
813 |
|
|
if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) { |
814 |
|
|
print(STDERR "Unable to open/read char/block special file" |
815 |
|
|
. " $hdr->{fullPath}\n"); |
816 |
|
|
$f->close if ( defined($f) ); |
817 |
|
|
$ErrorCnt++; |
818 |
|
|
return; |
819 |
|
|
} |
820 |
|
|
$f->close; |
821 |
|
|
if ( $data =~ /(\d+),(\d+)/ ) { |
822 |
|
|
$hdr->{devmajor} = $1; |
823 |
|
|
$hdr->{devminor} = $2; |
824 |
|
|
} |
825 |
|
|
} |
826 |
|
|
$hdr->{size} = 0; |
827 |
|
|
TarWriteFileInfo($fh, $hdr); |
828 |
|
|
$SpecialCnt++; |
829 |
|
|
} else { |
830 |
|
|
print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n"); |
831 |
|
|
$ErrorCnt++; |
832 |
|
|
} |
833 |
|
|
} |
834 |
dpavlin |
112 |
|
835 |
|
|
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
836 |
|
|
sub curr_time { |
837 |
|
|
return strftime($t_fmt,localtime()); |
838 |
|
|
} |