2 #============================================================= -*-perl-*-
4 # BackupPC_tarCreate: create a tar archive of an existing dump
5 # for restore on a client.
9 # Usage: BackupPC_tarCreate [-t] [-h host] [-n dumpNum] [-s shareName]
10 # [-r pathRemove] [-p pathAdd] files/directories...
15 # -h host host from which the tar archive is created
16 # -n dumpNum dump number from which the tar archive is created
17 # -s shareName share name from which the tar archive is created
20 # -t print summary totals
21 # -r pathRemove path prefix that will be replaced with pathAdd
22 # -p pathAdd new path prefix
24 # The -h, -n and -s options specify which dump is used to generate
25 # the tar archive. The -r and -p options can be used to relocate
26 # the paths in the tar archive so extracted files can be placed
27 # in a location different from their original location.
30 # Craig Barratt <cbarratt@users.sourceforge.net>
33 # Copyright (C) 2001 Craig Barratt
35 # This program is free software; you can redistribute it and/or modify
36 # it under the terms of the GNU General Public License as published by
37 # the Free Software Foundation; either version 2 of the License, or
38 # (at your option) any later version.
40 # This program is distributed in the hope that it will be useful,
41 # but WITHOUT ANY WARRANTY; without even the implied warranty of
42 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 # GNU General Public License for more details.
45 # You should have received a copy of the GNU General Public License
46 # along with this program; if not, write to the Free Software
47 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 #========================================================================
51 # Version 2.0.0beta1, released 30 Mar 2003.
53 # See http://backuppc.sourceforge.net.
55 #========================================================================
58 use lib "/usr/local/BackupPC/lib";
62 use BackupPC::Attrib qw(:all);
63 use BackupPC::FileZIO;
66 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
67 my $TopDir = $bpc->TopDir();
68 my $BinDir = $bpc->BinDir();
69 my %Conf = $bpc->Conf();
73 if ( !getopts("th:n:p:r:s:", \%opts) || @ARGV < 1 ) {
74 print(STDERR "usage: $0 [-t] [-h host] [-n dumpNum] [-s shareName]"
75 . " [-r pathRemove] [-p pathAdd]"
76 . " files/directories...\n");
80 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
81 print(STDERR "$0: bad host name '$opts{h}'\n");
86 if ( $opts{n} !~ /^(\d+)$/ ) {
87 print(STDERR "$0: bad dump number '$opts{n}'\n");
92 my @Backups = $bpc->BackupInfoRead($Host);
100 for ( $i = 0 ; $i < @Backups ; $i++ ) {
101 last if ( $Backups[$i]{num} == $Num );
103 if ( $i >= @Backups ) {
104 print(STDERR "$0: bad backup number $Num for host $Host\n");
108 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
109 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
110 if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ ) {
111 print(STDERR "$0: bad share name '$opts{s}'\n");
114 my $ShareName = $opts{s};
117 # This constant and the line of code below that uses it are borrowed
118 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
121 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
122 # Copyright 1998 Stephen Zander. All rights reserved.
125 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
126 my $tar_header_length = 512;
128 my $BufSize = 1048576; # 1MB or 2^20
130 my $WriteBufSz = 20 * $tar_header_length;
132 my(%UidCache, %GidCache);
133 my(%HardLinkExtraFiles, @HardLinks);
136 # Write out all the requested files/directories
139 foreach my $dir ( @ARGV ) {
140 archiveWrite($fh, $dir);
144 # Write out any hardlinks (if any)
146 foreach my $hdr ( @HardLinks ) {
148 if ( defined($PathRemove)
149 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
150 eq ".$PathRemove" ) {
151 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
153 TarWriteFileInfo($fh, $hdr);
157 # Finish with two null 512 byte headers, and then round out a full
160 my $data = "\0" x ($tar_header_length * 2);
161 TarWrite($fh, \$data);
162 TarWrite($fh, undef);
165 # print out totals if requested
168 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
169 " $SpecialCnt specials, $ErrorCnt errors\n";
173 ###########################################################################
175 ###########################################################################
179 my($fh, $dir, $tarPathOverride) = @_;
181 my $view = BackupPC::View->new($bpc, $Host, \@Backups);
183 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
184 print(STDERR "$0: bad directory '$dir'\n");
188 $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
189 $fh, $tarPathOverride);
196 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
197 return $UidCache{$uid};
204 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
205 return $GidCache{$gid};
210 my($fh, $dataRef) = @_;
212 if ( !defined($dataRef) ) {
214 # do flush by padding to a full $WriteBufSz
216 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
219 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
221 # just buffer and return
223 $WriteBuf .= $$dataRef;
226 my $done = $WriteBufSz - length($WriteBuf);
227 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
229 print(STDERR "Unable to write to output file ($!)\n");
232 while ( $done + $WriteBufSz <= length($$dataRef) ) {
233 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
235 print(STDERR "Unable to write to output file ($!)\n");
238 $done += $WriteBufSz;
240 $WriteBuf = substr($$dataRef, $done);
247 if ( $size % $tar_header_length ) {
248 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
249 TarWrite($fh, \$data);
257 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
258 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
259 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
261 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
263 my $data = pack($tar_pack_header,
264 substr($hdr->{name}, 0, 99),
265 sprintf("%07o", $hdr->{mode}),
266 sprintf("%07o", $hdr->{uid}),
267 sprintf("%07o", $hdr->{gid}),
268 sprintf("%011o", $hdr->{size}),
269 sprintf("%011o", $hdr->{mtime}),
270 "", #checksum field - space padded by pack("A8")
272 substr($hdr->{linkname}, 0, 99),
273 $hdr->{magic} || 'ustar ',
274 $hdr->{version} || ' ',
281 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
282 TarWrite($fh, \$data);
290 # Handle long link names (symbolic links)
292 if ( length($hdr->{linkname}) > 99 ) {
294 my $data = $hdr->{linkname} . "\0";
295 $h{name} = "././\@LongLink";
297 $h{size} = length($data);
298 TarWriteHeader($fh, \%h);
299 TarWrite($fh, \$data);
300 TarWritePad($fh, length($data));
303 # Handle long file names
305 if ( length($hdr->{name}) > 99 ) {
307 my $data = $hdr->{name} . "\0";
308 $h{name} = "././\@LongLink";
310 $h{size} = length($data);
311 TarWriteHeader($fh, \%h);
312 TarWrite($fh, \$data);
313 TarWritePad($fh, length($data));
315 TarWriteHeader($fh, $hdr);
323 my($hdr, $fh, $tarPathOverride) = @_;
325 my $tarPath = $hdr->{relPath};
326 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
328 if ( defined($PathRemove)
329 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
330 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
332 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
333 $tarPath =~ s{//+}{/}g;
334 $hdr->{name} = $tarPath;
336 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
338 # Directory: just write the header
340 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
341 TarWriteFileInfo($fh, $hdr);
343 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
345 # Regular file: write the header and file
347 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
348 if ( !defined($f) ) {
349 print(STDERR "Unable to open file $hdr->{fullPath}\n");
353 TarWriteFileInfo($fh, $hdr);
355 while ( $f->read(\$data, $BufSize) > 0 ) {
356 TarWrite($fh, \$data);
357 $size += length($data);
360 TarWritePad($fh, $size);
363 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
365 # Hardlink file: either write a hardlink or the complete file
366 # depending upon whether the linked-to file will be written
369 # Start by reading the contents of the link.
371 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
372 if ( !defined($f) ) {
373 print(STDERR "Unable to open file $hdr->{fullPath}\n");
378 while ( $f->read(\$data, $BufSize) > 0 ) {
379 $hdr->{linkname} .= $data;
383 # Check @ARGV and the list of hardlinked files we have explicity
384 # dumped to see if we have dumped this file or not
387 my $name = $hdr->{linkname};
389 if ( $HardLinkExtraFiles{$name} ) {
392 foreach my $arg ( @ARGV ) {
395 $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// );
400 # Target file will be or was written, so just remember
401 # the hardlink so we can dump it later.
403 push(@HardLinks, $hdr);
407 # Have to dump the original file. Just call the top-level
408 # routine, so that we save the hassle of dealing with
409 # mangling, merging and attributes.
411 $HardLinkExtraFiles{$hdr->{linkname}} = 1;
412 archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
414 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
416 # Symbolic link: read the symbolic link contents into the header
417 # and write the header.
419 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
420 if ( !defined($f) ) {
421 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
426 while ( $f->read(\$data, $BufSize) > 0 ) {
427 $hdr->{linkname} .= $data;
431 TarWriteFileInfo($fh, $hdr);
433 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
434 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
435 || $hdr->{type} == BPC_FTYPE_FIFO ) {
437 # Special files: for char and block special we read the
438 # major and minor numbers from a plain file.
440 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
441 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
444 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
445 print(STDERR "Unable to open/read char/block special file"
446 . " $hdr->{fullPath}\n");
447 $f->close if ( defined($f) );
452 if ( $data =~ /(\d+),(\d+)/ ) {
453 $hdr->{devmajor} = $1;
454 $hdr->{devminor} = $2;
458 TarWriteFileInfo($fh, $hdr);
461 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");