added custom DumpPostFinishCmd command
[BackupPC.git] / bin / BackupPC_tarCreate
1 #!/usr/bin/perl
2 #============================================================= -*-perl-*-
3 #
4 # BackupPC_tarCreate: create a tar archive of an existing dump
5 # for restore on a client.
6 #
7 # DESCRIPTION
8 #  
9 #   Usage: BackupPC_tarCreate [options] files/directories...
10 #
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       output write buffer size in 512-byte blocks (default 20; same as tar)
25 #       -w readBufSz    buffer size for reading files (default 1048576 = 1MB)
26 #       -e charset      charset for encoding file names (default: value of
27 #                       $Conf{ClientCharset} when backup was done)
28 #       -l              just print a file listing; don't generate an archive
29 #       -L              just print a detailed file listing; don't generate an archive
30 #
31 #     The -h, -n and -s options specify which dump is used to generate
32 #     the tar archive.  The -r and -p options can be used to relocate
33 #     the paths in the tar archive so extracted files can be placed
34 #     in a location different from their original location.
35 #
36 # AUTHOR
37 #   Craig Barratt  <cbarratt@users.sourceforge.net>
38 #
39 # COPYRIGHT
40 #   Copyright (C) 2001-2009  Craig Barratt
41 #
42 #   This program is free software; you can redistribute it and/or modify
43 #   it under the terms of the GNU General Public License as published by
44 #   the Free Software Foundation; either version 2 of the License, or
45 #   (at your option) any later version.
46 #
47 #   This program is distributed in the hope that it will be useful,
48 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
49 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50 #   GNU General Public License for more details.
51 #
52 #   You should have received a copy of the GNU General Public License
53 #   along with this program; if not, write to the Free Software
54 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
55 #
56 #========================================================================
57 #
58 # Version 3.2.0, released 31 Jul 2010.
59 #
60 # See http://backuppc.sourceforge.net.
61 #
62 #========================================================================
63
64 use strict;
65 no  utf8;
66 use lib "/usr/local/BackupPC/lib";
67 use File::Path;
68 use Getopt::Std;
69 use Encode qw/from_to/;
70 use BackupPC::Lib;
71 use BackupPC::Attrib qw(:all);
72 use BackupPC::FileZIO;
73 use BackupPC::View;
74
75 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
76
77 my %opts;
78
79 if ( !getopts("Llte:h:n:p:r:s:b:w:i", \%opts) || @ARGV < 1 ) {
80     print STDERR <<EOF;
81 usage: $0 [options] files/directories...
82   Required options:
83      -h host         host from which the tar archive is created
84      -n dumpNum      dump number from which the tar archive is created
85                      A negative number means relative to the end (eg -1
86                      means the most recent dump, -2 2nd most recent etc).
87      -s shareName    share name from which the tar archive is created
88
89   Other options:
90      -t              print summary totals
91      -r pathRemove   path prefix that will be replaced with pathAdd
92      -p pathAdd      new path prefix
93      -b BLOCKS       output write buffer size in 512-byte blocks (default 20; same as tar)
94      -w readBufSz    buffer size for reading files (default 1048576 = 1MB)
95      -e charset      charset for encoding file names (default: value of
96                      \$Conf{ClientCharset} when backup was done)
97      -l              just print a file listing; don't generate an archive
98      -L              just print a detailed file listing; don't generate an archive
99      -i              create incremental tar dump with just new files
100 EOF
101     exit(1);
102 }
103
104 if ( $opts{h} !~ /^([\w\.\s-]+)$/
105         || $opts{h} =~ m{(^|/)\.\.(/|$)} ) {
106     print(STDERR "$0: bad host name '$opts{h}'\n");
107     exit(1);
108 }
109 my $Host = $opts{h};
110
111 if ( $opts{n} !~ /^(-?\d+)$/ ) {
112     print(STDERR "$0: bad dump number '$opts{n}'\n");
113     exit(1);
114 }
115 my $Num = $opts{n};
116
117 my @Backups = $bpc->BackupInfoRead($Host);
118 my $FileCnt = 0;
119 my $ByteCnt = 0;
120 my $DirCnt = 0;
121 my $SpecialCnt = 0;
122 my $ErrorCnt = 0;
123
124 my $i;
125 $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
126 for ( $i = 0 ; $i < @Backups ; $i++ ) {
127     last if ( $Backups[$i]{num} == $Num );
128 }
129 if ( $i >= @Backups ) {
130     print(STDERR "$0: bad backup number $Num for host $Host\n");
131     exit(1);
132 }
133
134 my $Charset = $Backups[$i]{charset};
135 $Charset = $opts{e} if ( $opts{e} ne "" );
136
137 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
138 my $PathAdd    = $1 if ( $opts{p} =~ /(.+)/ );
139 if ( $opts{s} =~ m{(^|/)\.\.(/|$)} ) {
140     print(STDERR "$0: bad share name '$opts{s}'\n");
141     exit(1);
142 }
143
144 our $ShareName = $opts{s};
145 our $view = BackupPC::View->new($bpc, $Host, \@Backups, $opts{i} ? { only_first => 1 } : {});
146
147 #
148 # This constant and the line of code below that uses it are borrowed
149 # from Archive::Tar.  Thanks to Calle Dybedahl and Stephen Zander.
150 # See www.cpan.org.
151 #
152 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
153 #                 Copyright 1998 Stephen Zander. All rights reserved.
154 #
155 my $tar_pack_header
156     = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
157 my $tar_header_length = 512;
158
159 my $BufSize    = $opts{w} || 1048576;     # 1MB or 2^20
160 my $WriteBuf   = "";
161 my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
162
163 my(%UidCache, %GidCache);
164 my(%HardLinkExtraFiles, @HardLinks);
165
166 #
167 # Write out all the requested files/directories
168 #
169 binmode(STDOUT);
170 my $fh = *STDOUT;
171 if ( $ShareName eq "*" ) {
172     my $PathRemoveOrig = $PathRemove;
173     my $PathAddOrig    = $PathAdd;
174     foreach $ShareName ( $view->shareList($Num) ) {
175         #print(STDERR "Doing share ($ShareName)\n");
176         $PathRemove = "/" if ( !defined($PathRemoveOrig) );
177         ($PathAdd = "/$ShareName/$PathAddOrig") =~ s{//+}{/}g;
178         foreach my $dir ( @ARGV ) {
179             archiveWrite($fh, $dir);
180         }
181         archiveWriteHardLinks($fh);
182     }
183 } else {
184     foreach my $dir ( @ARGV ) {
185         archiveWrite($fh, $dir);
186     }
187     archiveWriteHardLinks($fh);
188 }
189
190 if ( !$opts{l} && !$opts{L} ) {
191     #
192     # Finish with two null 512 byte headers, and then round out a full
193     # block.
194     # 
195     my $data = "\0" x ($tar_header_length * 2);
196     TarWrite($fh, \$data);
197     TarWrite($fh, undef);
198 }
199
200 #
201 # print out totals if requested
202 #
203 if ( $opts{t} ) {
204     print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
205                  " $SpecialCnt specials, $ErrorCnt errors\n";
206 }
207 if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
208     #
209     # Got errors, with no files or directories; exit with non-zero
210     # status
211     #
212     exit(1);
213 }
214 exit(0);
215
216 ###########################################################################
217 # Subroutines
218 ###########################################################################
219
220 sub archiveWrite
221 {
222     my($fh, $dir, $tarPathOverride) = @_;
223
224     if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
225         print(STDERR "$0: bad directory '$dir'\n");
226         $ErrorCnt++;
227         return;
228     }
229     $dir = "/" if ( $dir eq "." );
230     #print(STDERR "calling find with $Num, $ShareName, $dir\n");
231     if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
232                 $fh, $tarPathOverride) < 0 ) {
233         print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
234         $ErrorCnt++;
235         return;
236     }
237 }
238
239 #
240 # Write out any hardlinks (if any)
241 #
242 sub archiveWriteHardLinks
243 {
244     my($fh) = @_;
245     foreach my $hdr ( @HardLinks ) {
246         $hdr->{size} = 0;
247         my $name = $hdr->{linkname};
248         $name =~ s{^\./}{/};
249         if ( defined($HardLinkExtraFiles{$name}) ) {
250             $hdr->{linkname} = $HardLinkExtraFiles{$name};
251         }
252         if ( defined($PathRemove)
253               && substr($hdr->{linkname}, 0, length($PathRemove)+1)
254                         eq ".$PathRemove" ) {
255             substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
256         }
257         TarWriteFileInfo($fh, $hdr);
258     }
259     @HardLinks = ();
260     %HardLinkExtraFiles = ();
261 }
262
263 sub UidLookup
264 {
265     my($uid) = @_;
266
267     $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
268     return $UidCache{$uid};
269 }
270
271 sub GidLookup
272 {
273     my($gid) = @_;
274
275     $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
276     return $GidCache{$gid};
277 }
278
279 sub TarWrite
280 {
281     my($fh, $dataRef) = @_;
282
283     if ( !defined($dataRef) ) {
284         #
285         # do flush by padding to a full $WriteBufSz
286         #
287         my $data = "\0" x ($WriteBufSz - length($WriteBuf));
288         $dataRef = \$data;
289     }
290     if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
291         #
292         # just buffer and return
293         #
294         $WriteBuf .= $$dataRef;
295         return;
296     }
297     my $done = $WriteBufSz - length($WriteBuf);
298     if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
299                                 != $WriteBufSz ) {
300         print(STDERR "Unable to write to output file ($!)\n");
301         exit(1);
302     }
303     while ( $done + $WriteBufSz <= length($$dataRef) ) {
304         if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
305                             != $WriteBufSz ) {
306             print(STDERR "Unable to write to output file ($!)\n");
307             exit(1);
308         }
309         $done += $WriteBufSz;
310     }
311     $WriteBuf = substr($$dataRef, $done);
312 }
313
314 sub TarWritePad
315 {
316     my($fh, $size) = @_;
317
318     if ( $size % $tar_header_length ) {
319         my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
320         TarWrite($fh, \$data);
321     }
322 }
323
324 sub TarWriteHeader
325 {
326     my($fh, $hdr) = @_;
327
328     $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
329     $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
330     my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
331                                              : "";
332     my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
333                                              : "";
334     my $sizeStr;
335     if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
336         #
337         # GNU extension for files >= 8GB: send size in big-endian binary
338         #
339         $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
340                                   $hdr->{size} / (65536 * 65536),
341                                   $hdr->{size} % (65536 * 65536));
342     } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
343         #
344         # sprintf octal only handles up to 2^32 - 1
345         #
346         $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
347                  . sprintf("%08o", $hdr->{size} % (1 << 24));
348     } else {
349         $sizeStr = sprintf("%011o", $hdr->{size});
350     }
351     my $data = pack($tar_pack_header,
352                      substr($hdr->{name}, 0, 99),
353                      sprintf("%07o", $hdr->{mode}),
354                      sprintf("%07o", $hdr->{uid}),
355                      sprintf("%07o", $hdr->{gid}),
356                      $sizeStr,
357                      sprintf("%011o", $hdr->{mtime}),
358                      "",        #checksum field - space padded by pack("A8")
359                      $hdr->{type},
360                      substr($hdr->{linkname}, 0, 99),
361                      $hdr->{magic} || 'ustar ',
362                      $hdr->{version} || ' ',
363                      $hdr->{uname},
364                      $hdr->{gname},
365                      $devmajor,
366                      $devminor,
367                      ""         # prefix is empty
368                  );
369     substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
370     TarWrite($fh, \$data);
371 }
372
373 sub TarWriteFileInfo
374 {
375     my($fh, $hdr) = @_;
376
377     #
378     # Convert path names to requested (eg: client) charset
379     #
380     if ( $Charset ne "" ) {
381         from_to($hdr->{name},     "utf8", $Charset);
382         from_to($hdr->{linkname}, "utf8", $Charset);
383     }
384
385     if ( $opts{l} ) {
386         print($hdr->{name} . "\n");
387         return;
388     } elsif ( $opts{L} ) {
389         my $owner = "$hdr->{uid}/$hdr->{gid}";
390
391         my $name = $hdr->{name};
392
393         if ( $hdr->{type} == BPC_FTYPE_SYMLINK
394                 || $hdr->{type} == BPC_FTYPE_HARDLINK ) {
395             $name .= " -> $hdr->{linkname}";
396         }
397         $name =~ s/\n/\\n/g;
398
399         printf("%6o %9s %11.0f %s\n",
400                                     $hdr->{mode},
401                                     $owner,
402                                     $hdr->{size},
403                                     $name);
404         return;
405     }
406
407     #
408     # Handle long link names (symbolic links)
409     #
410     if ( length($hdr->{linkname}) > 99 ) {
411         my %h;
412         my $data = $hdr->{linkname} . "\0";
413         $h{name} = "././\@LongLink";
414         $h{type} = "K";
415         $h{size} = length($data);
416         TarWriteHeader($fh, \%h);
417         TarWrite($fh, \$data);
418         TarWritePad($fh, length($data));
419     }
420
421     #
422     # Handle long file names
423     #
424     if ( length($hdr->{name}) > 99 ) {
425         my %h;
426         my $data = $hdr->{name} . "\0";
427         $h{name} = "././\@LongLink";
428         $h{type} = "L";
429         $h{size} = length($data);
430         TarWriteHeader($fh, \%h);
431         TarWrite($fh, \$data);
432         TarWritePad($fh, length($data));
433     }
434     TarWriteHeader($fh, $hdr);
435 }
436
437 my $Attr;
438 my $AttrDir;
439
440 sub TarWriteFile
441 {
442     my($hdr, $fh, $tarPathOverride) = @_;
443
444     my $tarPath = $hdr->{relPath};
445     $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
446
447     $tarPath =~ s{//+}{/}g;
448     if ( defined($PathRemove)
449             && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
450         substr($tarPath, 0, length($PathRemove)) = $PathAdd;
451     }
452     $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
453     $tarPath =~ s{//+}{/}g;
454     $hdr->{name} = $tarPath;
455
456     if ( $hdr->{type} == BPC_FTYPE_DIR ) {
457         #
458         # Directory: just write the header
459         #
460         $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
461         TarWriteFileInfo($fh, $hdr);
462         $DirCnt++;
463     } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
464         my($data, $size);
465         #
466         # Regular file: write the header and file
467         #
468         my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
469         if ( !defined($f) ) {
470             print(STDERR "Unable to open file $hdr->{fullPath}\n");
471             $ErrorCnt++;
472             return;
473         }
474         TarWriteFileInfo($fh, $hdr);
475         if ( $opts{l} || $opts{L} ) {
476             $size = $hdr->{size};
477         } else {
478             while ( $f->read(\$data, $BufSize) > 0 ) {
479                 if ( $size + length($data) > $hdr->{size} ) {
480                     print(STDERR "Error: truncating $hdr->{fullPath} to"
481                                . " $hdr->{size} bytes\n");
482                     $data = substr($data, 0, $hdr->{size} - $size);
483                     $ErrorCnt++;
484                 }
485                 TarWrite($fh, \$data);
486                 $size += length($data);
487             }
488             $f->close;
489             if ( $size != $hdr->{size} ) {
490                 print(STDERR "Error: padding $hdr->{fullPath} to $hdr->{size}"
491                            . " bytes from $size bytes\n");
492                 $ErrorCnt++;
493                 while ( $size < $hdr->{size} ) {
494                     my $len = $hdr->{size} - $size;
495                     $len = $BufSize if ( $len > $BufSize );
496                     $data = "\0" x $len;
497                     TarWrite($fh, \$data);
498                     $size += $len;
499                 }
500             }
501             TarWritePad($fh, $size);
502         }
503         $FileCnt++;
504         $ByteCnt += $size;
505     } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
506         #
507         # Hardlink file: either write a hardlink or the complete file
508         # depending upon whether the linked-to file will be written
509         # to the archive.
510         #
511         # Start by reading the contents of the link.
512         #
513         my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
514         if ( !defined($f) ) {
515             print(STDERR "Unable to open file $hdr->{fullPath}\n");
516             $ErrorCnt++;
517             return;
518         }
519         my $data;
520         while ( $f->read(\$data, $BufSize) > 0 ) {
521             $hdr->{linkname} .= $data;
522         }
523         $f->close;
524         #
525         # Check @ARGV and the list of hardlinked files we have explicity
526         # dumped to see if we have dumped this file or not
527         #
528         my $done = 0;
529         my $name = $hdr->{linkname};
530         $name =~ s{^\./}{/};
531         if ( defined($HardLinkExtraFiles{$name}) ) {
532             $done = 1;
533         } else {
534             foreach my $arg ( @ARGV ) {
535                 $arg = "/" if ( $arg eq "." );
536                 $arg =~ s{^\./+}{/};
537                 $arg =~ s{/+$}{};
538                 $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// || $arg eq "" );
539             }
540         }
541         if ( $done ) {
542             #
543             # Target file will be or was written, so just remember
544             # the hardlink so we can dump it later.
545             #
546             push(@HardLinks, $hdr);
547             $SpecialCnt++;
548         } else {
549             #
550             # Have to dump the original file.  Just call the top-level
551             # routine, so that we save the hassle of dealing with
552             # mangling, merging and attributes.
553             #
554             my $name = $hdr->{linkname};
555             $name =~ s{^\./}{/};
556             $HardLinkExtraFiles{$name} = $hdr->{name};
557             archiveWrite($fh, $name, $hdr->{name});
558         }
559     } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
560         #
561         # Symbolic link: read the symbolic link contents into the header
562         # and write the header.
563         #
564         my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
565         if ( !defined($f) ) {
566             print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
567             $ErrorCnt++;
568             return;
569         }
570         my $data;
571         while ( $f->read(\$data, $BufSize) > 0 ) {
572             $hdr->{linkname} .= $data;
573         }
574         $f->close;
575         $hdr->{size} = 0;
576         TarWriteFileInfo($fh, $hdr);
577         $SpecialCnt++;
578     } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
579            || $hdr->{type} == BPC_FTYPE_BLOCKDEV
580            || $hdr->{type} == BPC_FTYPE_FIFO ) {
581         #
582         # Special files: for char and block special we read the
583         # major and minor numbers from a plain file.
584         #
585         if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
586             my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
587                                                 $hdr->{compress});
588             my $data;
589             if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
590                 print(STDERR "Unable to open/read char/block special file"
591                            . " $hdr->{fullPath}\n");
592                 $f->close if ( defined($f) );
593                 $ErrorCnt++;
594                 return;
595             }
596             $f->close;
597             if ( $data =~ /(\d+),(\d+)/ ) {
598                 $hdr->{devmajor} = $1;
599                 $hdr->{devminor} = $2;
600             }
601         }
602         $hdr->{size} = 0;
603         TarWriteFileInfo($fh, $hdr);
604         $SpecialCnt++;
605     } elsif ( $hdr->{type} == BPC_FTYPE_SOCKET
606            || $hdr->{type} == BPC_FTYPE_UNKNOWN ) {
607         #
608         # ignore these two file types - these are dynamic file types created
609         # by applications as needed
610         #
611     } else {
612         print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
613         $ErrorCnt++;
614     }
615 }