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.0_CVS, released 3 Feb 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();
72 getopts("th:n:p:r:s:", \%opts);
75 print(STDERR "usage: $0 [-t] [-h host] [-n dumpNum] [-s shareName]"
76 . " [-r pathRemove] [-p pathAdd]"
77 . " files/directories...\n");
81 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
82 print(STDERR "$0: bad host name '$opts{h}'\n");
87 if ( $opts{n} !~ /^(\d+)$/ ) {
88 print(STDERR "$0: bad dump number '$opts{n}'\n");
93 my @Backups = $bpc->BackupInfoRead($Host);
101 for ( $i = 0 ; $i < @Backups ; $i++ ) {
102 last if ( $Backups[$i]{num} == $Num );
104 if ( $i >= @Backups ) {
105 print(STDERR "$0: bad backup number $Num for host $Host\n");
109 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
110 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
111 if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ ) {
112 print(STDERR "$0: bad share name '$opts{s}'\n");
115 my $ShareName = $opts{s};
118 # This constant and the line of code below that uses it are borrowed
119 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
122 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
123 # Copyright 1998 Stephen Zander. All rights reserved.
126 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
127 my $tar_header_length = 512;
129 my $BufSize = 1048576; # 1MB or 2^20
131 my $WriteBufSz = 20 * $tar_header_length;
133 my(%UidCache, %GidCache);
134 my(%HardLinkExtraFiles, @HardLinks);
137 # Write out all the requested files/directories
140 foreach my $dir ( @ARGV ) {
141 archiveWrite($fh, $dir);
145 # Write out any hardlinks (if any)
147 foreach my $hdr ( @HardLinks ) {
149 if ( defined($PathRemove)
150 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
151 eq ".$PathRemove" ) {
152 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
154 TarWriteFileInfo($fh, $hdr);
158 # Finish with two null 512 byte headers, and then round out a full
161 my $data = "\0" x ($tar_header_length * 2);
162 TarWrite($fh, \$data);
163 TarWrite($fh, undef);
166 # print out totals if requested
169 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
170 " $SpecialCnt specials, $ErrorCnt errors\n";
174 ###########################################################################
176 ###########################################################################
180 my($fh, $dir, $tarPathOverride) = @_;
182 my $view = BackupPC::View->new($bpc, $Host, \@Backups);
184 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
185 print(STDERR "$0: bad directory '$dir'\n");
189 $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
190 $fh, $tarPathOverride);
197 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
198 return $UidCache{$uid};
205 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
206 return $GidCache{$gid};
211 my($fh, $dataRef) = @_;
213 if ( !defined($dataRef) ) {
215 # do flush by padding to a full $WriteBufSz
217 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
220 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
222 # just buffer and return
224 $WriteBuf .= $$dataRef;
227 my $done = $WriteBufSz - length($WriteBuf);
228 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
230 print(STDERR "Unable to write to output file\n");
233 while ( $done + $WriteBufSz <= length($$dataRef) ) {
234 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
236 print(STDERR "Unable to write to output file\n");
239 $done += $WriteBufSz;
241 $WriteBuf = substr($$dataRef, $done);
248 if ( $size % $tar_header_length ) {
249 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
250 TarWrite($fh, \$data);
258 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
259 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
260 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
262 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
264 my $data = pack($tar_pack_header,
265 substr($hdr->{name}, 0, 99),
266 sprintf("%07o", $hdr->{mode}),
267 sprintf("%07o", $hdr->{uid}),
268 sprintf("%07o", $hdr->{gid}),
269 sprintf("%011o", $hdr->{size}),
270 sprintf("%011o", $hdr->{mtime}),
271 "", #checksum field - space padded by pack("A8")
273 substr($hdr->{linkname}, 0, 99),
274 $hdr->{magic} || 'ustar ',
275 $hdr->{version} || ' ',
282 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
283 TarWrite($fh, \$data);
291 # Handle long link names (symbolic links)
293 if ( length($hdr->{linkname}) > 99 ) {
295 my $data = $hdr->{linkname} . "\0";
296 $h{name} = "././\@LongLink";
298 $h{size} = length($data);
299 TarWriteHeader($fh, \%h);
300 TarWrite($fh, \$data);
301 TarWritePad($fh, length($data));
304 # Handle long file names
306 if ( length($hdr->{name}) > 99 ) {
308 my $data = $hdr->{name} . "\0";
309 $h{name} = "././\@LongLink";
311 $h{size} = length($data);
312 TarWriteHeader($fh, \%h);
313 TarWrite($fh, \$data);
314 TarWritePad($fh, length($data));
316 TarWriteHeader($fh, $hdr);
324 my($hdr, $fh, $tarPathOverride) = @_;
326 my $tarPath = $hdr->{relPath};
327 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
329 if ( defined($PathRemove)
330 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
331 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
333 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
334 $tarPath =~ s{//+}{/}g;
335 $hdr->{name} = $tarPath;
337 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
339 # Directory: just write the header
341 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
342 TarWriteFileInfo($fh, $hdr);
344 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
346 # Regular file: write the header and file
348 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
349 if ( !defined($f) ) {
350 print(STDERR "Unable to open file $hdr->{fullPath}\n");
354 TarWriteFileInfo($fh, $hdr);
356 while ( $f->read(\$data, $BufSize) > 0 ) {
357 TarWrite($fh, \$data);
358 $size += length($data);
361 TarWritePad($fh, $size);
364 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
366 # Hardlink file: either write a hardlink or the complete file
367 # depending upon whether the linked-to file will be written
370 # Start by reading the contents of the link.
372 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
373 if ( !defined($f) ) {
374 print(STDERR "Unable to open file $hdr->{fullPath}\n");
379 while ( $f->read(\$data, $BufSize) > 0 ) {
380 $hdr->{linkname} .= $data;
384 # Check @ARGV and the list of hardlinked files we have explicity
385 # dumped to see if we have dumped this file or not
388 my $name = $hdr->{linkname};
390 if ( $HardLinkExtraFiles{$name} ) {
393 foreach my $arg ( @ARGV ) {
396 $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// );
401 # Target file will be or was written, so just remember
402 # the hardlink so we can dump it later.
404 push(@HardLinks, $hdr);
408 # Have to dump the original file. Just call the top-level
409 # routine, so that we save the hassle of dealing with
410 # mangling, merging and attributes.
412 $HardLinkExtraFiles{$hdr->{linkname}} = 1;
413 archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
415 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
417 # Symbolic link: read the symbolic link contents into the header
418 # and write the header.
420 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
421 if ( !defined($f) ) {
422 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
427 while ( $f->read(\$data, $BufSize) > 0 ) {
428 $hdr->{linkname} .= $data;
432 TarWriteFileInfo($fh, $hdr);
434 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
435 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
436 || $hdr->{type} == BPC_FTYPE_FIFO ) {
438 # Special files: for char and block special we read the
439 # major and minor numbers from a plain file.
441 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
442 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
445 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
446 print(STDERR "Unable to open/read char/block special file"
447 . " $hdr->{fullPath}\n");
448 $f->close if ( defined($f) );
453 if ( $data =~ /(\d+),(\d+)/ ) {
454 $hdr->{devmajor} = $1;
455 $hdr->{devminor} = $2;
459 TarWriteFileInfo($fh, $hdr);
462 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");