2 #============================================================= -*-perl-*-
4 # configure.pl: Configuration and installation program for BackupPC
8 # This script should be run as root:
12 # The installation steps are described as the script runs.
15 # Craig Barratt <cbarratt@users.sourceforge.net>
18 # Copyright (C) 2001-2003 Craig Barratt
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write to the Free Software
32 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #========================================================================
36 # Version 2.0.0_CVS, released 18 Jan 2003.
38 # See http://backuppc.sourceforge.net.
40 #========================================================================
43 use vars qw(%Conf %OrigConf);
46 my @Packages = qw(ExtUtils::MakeMaker File::Path File::Spec File::Copy
47 DirHandle Digest::MD5 Data::Dumper Getopt::Std
48 BackupPC::Lib BackupPC::FileZIO);
50 foreach my $pkg ( @Packages ) {
55 BackupPC needs the package $pkg. Please install $pkg
56 before installing BackupPC.
64 This configure script should be run as root, rather than uid $<.
65 Provided uid $< has sufficient permissions to create the data and
66 install directories, then it should be ok to proceed. Otherwise,
67 please quit and restart as root.
70 exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
75 Is this a new installation or upgrade for BackupPC? If this is
76 an upgrade please tell me the full path of the existing BackupPC
77 configuration file (eg: /xxxx/conf/config.pl). Otherwise, just
83 # Check if this is an upgrade, in which case read the existing
84 # config file to get all the defaults.
88 $ConfigPath = prompt("--> Full path to existing conf/config.pl",
90 last if ( $ConfigPath eq ""
91 || ($ConfigPath =~ /^\// && -r $ConfigPath && -w $ConfigPath) );
92 my $problem = "is not an absolute path";
93 $problem = "is not writable" if ( !-w $ConfigPath );
94 $problem = "is not readable" if ( !-r $ConfigPath );
95 $problem = "doesn't exist" if ( !-f $ConfigPath );
96 print("The file '$ConfigPath' $problem.\n");
99 if ( $ConfigPath ne "" && -r $ConfigPath ) {
100 (my $topDir = $ConfigPath) =~ s{/[^/]+/[^/]+$}{};
101 die("BackupPC::Lib->new failed\n")
102 if ( !($bpc = BackupPC::Lib->new($topDir, ".")) );
103 %Conf = $bpc->Conf();
105 $Conf{TopDir} = $topDir;
106 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}, 1);
110 BackupPC is running on $Conf{ServerHost}. You need to stop BackupPC
111 before you can upgrade the code. Depending upon your installation,
112 you could run "/etc/init.d/backuppc stop".
120 # These are the programs whose paths we need to find
124 'gtar/tar' => "TarClientPath",
125 smbclient => "SmbClientPath",
126 nmblookup => "NmbLookupPath",
127 rsync => "RsyncClientPath",
130 'ssh/ssh2' => "SshPath",
131 sendmail => "SendmailPath",
132 hostname => "HostnamePath",
135 foreach my $prog ( sort(keys(%Programs)) ) {
137 foreach my $subProg ( split(/\//, $prog) ) {
138 $path ||= FindProgram("$ENV{PATH}:/bin:/usr/bin:/sbin:/usr/sbin",
141 $Conf{$Programs{$prog}} ||= $path;
147 I found the following locations for these programs:
150 foreach my $prog ( sort(keys(%Programs)) ) {
151 printf(" %-11s => %s\n", $prog, $Conf{$Programs{$prog}});
154 last if (prompt('--> Are these paths correct?', 'y') =~ /^y/i);
155 foreach my $prog ( sort(keys(%Programs)) ) {
156 $Conf{$Programs{$prog}} = prompt("--> $prog path",
157 $Conf{$Programs{$prog}});
161 my $Perl56 = system($Conf{PerlPath}
162 . q{ -e 'exit($^V && $^V ge v5.6.0 ? 1 : 0);'});
167 BackupPC needs perl version 5.6.0 or later. $Conf{PerlPath} appears
168 to be an older version. Please upgrade to a newer version of perl
169 and re-run this configure script.
177 Please tell me the hostname of the machine that BackupPC will run on.
180 chomp($Conf{ServerHost} = `$Conf{HostnamePath}`)
181 if ( defined($Conf{HostnamePath}) && !defined($Conf{ServerHost}) );
182 $Conf{ServerHost} = prompt("--> BackupPC will run on host", $Conf{ServerHost});
186 BackupPC should run as a dedicated user with limited privileges. You
187 need to create a user. This user will need read/write permission on
188 the main data directory and read/execute permission on the install
189 directory (these directories will be setup shortly).
191 The primary group for this user should also be chosen carefully.
192 By default the install directories will have group write permission.
193 The data directories and files will have group read permission but
197 my($name, $passwd, $Uid, $Gid);
199 $Conf{BackupPCUser} = prompt("--> BackupPC should run as user",
200 $Conf{BackupPCUser} || "backuppc");
201 ($name, $passwd, $Uid, $Gid) = getpwnam($Conf{BackupPCUser});
202 last if ( $name ne "" );
205 getpwnam() says that user $Conf{BackupPCUser} doesn't exist. Please check the
206 name and verify that this user is in the passwd file.
213 Please specify an install directory for BackupPC. This is where the
214 BackupPC scripts, library and documentation will be installed.
219 $Conf{InstallDir} = prompt("--> Install directory (full path)",
221 last if ( $Conf{InstallDir} =~ /^\// );
226 Please specify a data directory for BackupPC. This is where the
227 configuration files, LOG files and all the PC backups are stored.
228 This file system needs to be big enough to accommodate all the
229 PCs you expect to backup (eg: at least 1-2GB per machine).
234 $Conf{TopDir} = prompt("--> Data directory (full path)", $Conf{TopDir});
235 last if ( $Conf{TopDir} =~ /^\// );
238 if ( !defined($Conf{CompressLevel}) ) {
239 $Conf{CompressLevel} = BackupPC::FileZIO->compOk ? 3 : 0;
240 if ( $ConfigPath eq "" && $Conf{CompressLevel} ) {
243 BackupPC can compress pool files, providing around a 40% reduction in pool
244 size (your mileage may vary). Specify the compression level (0 turns
245 off compression, and 1 to 9 represent good/fastest to best/slowest).
246 The recommended values are 0 (off) or 3 (reasonable compression and speed).
247 Increasing the compression level to 5 will use around 20% more cpu time
248 and give perhaps 2-3% more compression.
251 } elsif ( $ConfigPath eq "" ) {
254 BackupPC can compress pool files, but it needs the Compress::Zlib
255 package installed (see www.cpan.org). Compression will provide around a
256 40% reduction in pool size, at the expense of cpu time. You can leave
257 compression off and run BackupPC without compression, in which case you
258 should leave the compression level at 0 (which means off). You could
259 install Compress::Zlib and turn compression on later, but read the
260 documentation first about how to do this. Or the better choice is
261 to quit, install Compress::Zlib, and re-run configure.pl.
264 } elsif ( $Conf{CompressLevel} ) {
265 $Conf{CompressLevel} = 0;
268 BackupPC now supports pool file compression. Since you are upgrading
269 BackupPC you probably have existing uncompressed backups. You have
270 several choices if you want to turn on compression. You can run
271 the script BackupPC_compressPool to convert everything to compressed
272 form. Or you can simply turn on compression, so that new backups
273 will be compressed. This will increase the pool storage requirement,
274 since both uncompressed and compressed copies of files will be stored.
275 But eventually the old uncompressed backups will expire, recovering
276 the pool storage. Please see the documentation for more details.
278 If you are not sure what to do, leave the Compression Level at 0,
279 which disables compression. You can always read the documentation
280 and turn it on later.
284 $Conf{CompressLevel} = 0;
287 BackupPC now supports pool file compression, but it needs the
288 Compress::Zlib module (see www.cpan.org). For now, leave
289 the compression level set at 0 to disable compression. If you
290 want you can install Compress::Zlib and turn compression on.
291 Please see the documentation for more details about converting
292 old backups to compressed form.
298 = prompt("--> Compression level", $Conf{CompressLevel});
299 last if ( $Conf{CompressLevel} =~ /^\d+$/ );
305 BackupPC has a powerful CGI perl interface that runs under Apache.
306 A single executable needs to be installed in a cgi-bin directory.
307 This executable needs to run as set-uid $Conf{BackupPCUser}, or
308 it can be run under mod_perl with Apache running as user $Conf{BackupPCUser}.
310 Leave this path empty if you don't want to install the CGI interface.
315 $Conf{CgiDir} = prompt("--> CGI bin directory (full path)", $Conf{CgiDir});
316 last if ( $Conf{CgiDir} =~ /^\// || $Conf{CgiDir} eq "" );
319 if ( $Conf{CgiDir} ne "" ) {
323 BackupPC's CGI script needs to display various GIF images that
324 should be stored where Apache can serve them. They should be
325 placed somewher under Apache's DocumentRoot. BackupPC also
326 needs to know the URL to access these images. Example:
328 Apache image directory: /usr/local/apache/htdocs/BackupPC
329 URL for image directory: /BackupPC
331 The URL for the image directory should start with a slash.
335 $Conf{CgiImageDir} = prompt("--> Apache image directory (full path)",
337 last if ( $Conf{CgiImageDir} =~ /^\// );
340 $Conf{CgiImageDirURL} = prompt("--> URL for image directory (omit http://host; starts with '/')",
341 $Conf{CgiImageDirURL});
342 last if ( $Conf{CgiImageDirURL} =~ /^\// );
350 - install the binaries, lib and docs in $Conf{InstallDir},
351 - create the data directory $Conf{TopDir},
352 - create/update the config.pl file $Conf{TopDir}/conf,
353 - optionally install the cgi-bin interface.
357 exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
360 # Create install directories
362 foreach my $dir ( qw(bin lib/BackupPC/Xfer lib/BackupPC/Zip
363 lib/BackupPC/Lang doc) ) {
364 next if ( -d "$Conf{InstallDir}/$dir" );
365 mkpath("$Conf{InstallDir}/$dir", 0, 0775);
366 if ( !-d "$Conf{InstallDir}/$dir"
367 || !chown($Uid, $Gid, "$Conf{InstallDir}/$dir") ) {
368 die("Failed to create or chown $Conf{InstallDir}/$dir\n");
370 print("Created $Conf{InstallDir}/$dir\n");
375 # Create CGI image directory
377 foreach my $dir ( ($Conf{CgiImageDir}) ) {
378 next if ( $dir eq "" || -d $dir );
379 mkpath($dir, 0, 0775);
380 if ( !-d $dir || !chown($Uid, $Gid, $dir) ) {
381 die("Failed to create or chown $dir");
383 print("Created $dir\n");
388 # Create $TopDir's top-level directories
390 foreach my $dir ( qw(. conf pool cpool pc trash log) ) {
391 mkpath("$Conf{TopDir}/$dir", 0, 0750) if ( !-d "$Conf{TopDir}/$dir" );
392 if ( !-d "$Conf{TopDir}/$dir"
393 || !chown($Uid, $Gid, "$Conf{TopDir}/$dir") ) {
394 die("Failed to create or chown $Conf{TopDir}/$dir\n");
396 print("Created $Conf{TopDir}/$dir\n");
400 printf("Installing binaries in $Conf{InstallDir}/bin\n");
401 foreach my $prog ( qw(BackupPC BackupPC_dump BackupPC_link BackupPC_nightly
402 BackupPC_sendEmail BackupPC_tarCreate BackupPC_trashClean
403 BackupPC_tarExtract BackupPC_compressPool BackupPC_zcat
404 BackupPC_restore BackupPC_serverMesg BackupPC_zipCreate ) ) {
405 InstallFile("bin/$prog", "$Conf{InstallDir}/bin/$prog", 0555);
409 # Remove unused binaries from older versions
411 unlink("$Conf{InstallDir}/bin/BackupPC_queueAll");
413 printf("Installing library in $Conf{InstallDir}/lib\n");
414 foreach my $lib ( qw(BackupPC/Lib.pm BackupPC/FileZIO.pm BackupPC/Attrib.pm
415 BackupPC/PoolWrite.pm BackupPC/View.pm BackupPC/Xfer/Tar.pm
416 BackupPC/Xfer/Smb.pm BackupPC/Xfer/Rsync.pm
417 BackupPC/Xfer/RsyncFileIO.pm BackupPC/Zip/FileMember.pm
418 BackupPC/Lang/en.pm BackupPC/Lang/fr.pm BackupPC/Lang/es.pm
420 InstallFile("lib/$lib", "$Conf{InstallDir}/lib/$lib", 0444);
423 if ( $Conf{CgiImageDir} ne "" ) {
424 printf("Installing images in $Conf{CgiImageDir}\n");
425 foreach my $img ( <images/*> ) {
426 (my $destImg = $img) =~ s{^images/}{};
427 InstallFile($img, "$Conf{CgiImageDir}/$destImg", 0444, 1);
431 printf("Making init.d scripts\n");
432 foreach my $init ( qw(linux-backuppc solaris-backuppc debian-backuppc) ) {
433 InstallFile("init.d/src/$init", "init.d/$init", 0444);
436 printf("Installing docs in $Conf{InstallDir}/doc\n");
437 foreach my $doc ( qw(BackupPC.pod BackupPC.html) ) {
438 InstallFile("doc/$doc", "$Conf{InstallDir}/doc/$doc", 0444);
441 printf("Installing config.pl and hosts in $Conf{TopDir}/conf\n");
442 InstallFile("conf/hosts", "$Conf{TopDir}/conf/hosts", 0644)
443 if ( !-f "$Conf{TopDir}/conf/hosts" );
446 # Now do the config file. If there is an existing config file we
447 # merge in the new config file, adding any new configuration
448 # parameters and deleting ones that are no longer needed.
450 my $dest = "$Conf{TopDir}/conf/config.pl";
451 my ($newConf, $newVars) = ConfigParse("conf/config.pl");
452 my ($oldConf, $oldVars);
454 ($oldConf, $oldVars) = ConfigParse($dest);
455 $newConf = ConfigMerge($oldConf, $oldVars, $newConf, $newVars);
457 $Conf{EMailFromUserName} ||= $Conf{BackupPCUser};
458 $Conf{EMailAdminUserName} ||= $Conf{BackupPCUser};
461 # Update various config parameters
465 # Guess $Conf{CgiURL}
467 if ( !defined($Conf{CgiURL}) ) {
468 if ( $Conf{CgiDir} =~ m{cgi-bin(/.*)} ) {
469 $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin$1/BackupPC_Admin'";
471 $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin/BackupPC_Admin'";
476 # The smbclient commands have moved from hard-coded to the config file.
477 # $Conf{SmbClientArgs} no longer exists, so merge it into the new
478 # commands if it still exists.
480 if ( defined($Conf{SmbClientArgs}) ) {
481 if ( $Conf{SmbClientArgs} ne "" ) {
482 foreach my $param ( qw(SmbClientRestoreCmd SmbClientFullCmd
483 SmbClientIncrCmd) ) {
484 $newConf->[$newVars->{$param}]{text}
485 =~ s/(-E\s+-N)/$1 $Conf{SmbClientArgs}/;
488 delete($Conf{SmbClientArgs});
492 # IncrFill should now be off
497 # Figure out sensible arguments for the ping command
499 if ( defined($Conf{PingArgs}) ) {
500 $Conf{PingCmd} = '$pingPath ' . $Conf{PingArgs};
501 } elsif ( !defined($Conf{PingCmd}) ) {
502 if ( $^O eq "solaris" || $^O eq "sunos" ) {
503 $Conf{PingCmd} = '$pingPath -s $host 56 1';
504 } elsif ( ($^O eq "linux" || $^O eq "openbsd" || $^O eq "netbsd")
505 && !system("$Conf{PingClientPath} -c 1 -w 3 localhost") ) {
506 $Conf{PingCmd} = '$pingPath -c 1 -w 3 $host';
508 $Conf{PingCmd} = '$pingPath -c 1 $host';
510 delete($Conf{PingArgs});
514 # Figure out sensible arguments for the df command
516 if ( !defined($Conf{DfCmd}) ) {
517 if ( $^O eq "solaris" || $^O eq "sunos" ) {
518 $Conf{DfCmd} = '$dfPath -k $topDir';
523 # $Conf{SmbClientTimeout} is now $Conf{ClientTimeout}
525 if ( defined($Conf{SmbClientTimeout}) ) {
526 $Conf{ClientTimeout} = $Conf{SmbClientTimeout};
527 delete($Conf{SmbClientTimeout});
530 my $confCopy = "$dest.pre-__VERSION__";
531 if ( -f $dest && !-f $confCopy ) {
533 # Make copy of config file, preserving ownership and modes
535 printf("Making backup copy of $dest -> $confCopy\n");
536 my @stat = stat($dest);
540 die("can't copy($dest, $confCopy)\n") unless copy($dest, $confCopy);
541 die("can't chown $uid, $gid $confCopy\n")
542 unless chown($uid, $gid, $confCopy);
543 die("can't chmod $mode $confCopy\n") unless chmod($mode, $confCopy);
545 open(OUT, ">$dest") || die("can't open $dest for writing\n");
547 foreach my $var ( @$newConf ) {
548 if ( length($blockComment)
549 && substr($var->{text}, 0, length($blockComment)) eq $blockComment ) {
550 $var->{text} = substr($var->{text}, length($blockComment));
551 $blockComment = undef;
553 $blockComment = $1 if ( $var->{text} =~ /^([\s\n]*#{70}.*#{70}[\s\n]+)/s );
554 $var->{text} =~ s/^\s*\$Conf\{(.*?)\}(\s*=\s*['"]?)(.*?)(['"]?\s*;)/
555 defined($Conf{$1}) && ref($Conf{$1}) eq ""
556 && $Conf{$1} ne $OrigConf{$1}
557 ? "\$Conf{$1}$2$Conf{$1}$4"
558 : "\$Conf{$1}$2$3$4"/emg;
559 print OUT $var->{text};
562 if ( !defined($oldConf) ) {
563 die("can't chmod 0640 mode $dest\n") unless chmod(0640, $dest);
564 die("can't chown $Uid, $Gid $dest\n") unless chown($Uid, $Gid, $dest);
567 if ( $Conf{CgiDir} ne "" ) {
568 printf("Installing cgi script BackupPC_Admin in $Conf{CgiDir}\n");
569 mkpath("$Conf{CgiDir}", 0, 0755);
570 InstallFile("cgi-bin/BackupPC_Admin", "$Conf{CgiDir}/BackupPC_Admin",
576 Ok, it looks like we are finished. There are several more things you
579 - Browse through the config file, $Conf{TopDir}/conf/config.pl,
580 and make sure all the settings are correct. In particular, you
581 will need to set the smb share password and user name, backup
582 policies and check the email message headers and bodies.
584 - Edit the list of hosts to backup in $Conf{TopDir}/conf/hosts.
586 - Read the documentation in $Conf{InstallDir}/doc/BackupPC.html.
587 Please pay special attention to the security section.
589 - Verify that the CGI script BackupPC_Admin runs correctly. You might
590 need to change the permissions or group ownership of BackupPC_Admin.
592 - BackupPC should be ready to start. Don't forget to run it
593 as user $Conf{BackupPCUser}! The installation also contains an
594 init.d/backuppc script that can be copied to /etc/init.d
595 so that BackupPC can auto-start on boot. See init.d/README.
600 ###########################################################################
602 ###########################################################################
606 my($prog, $dest, $mode, $binary) = @_;
608 my($uid, $gid) = ($Uid, $Gid);
612 # preserve ownership and modes of files that already exist
614 my @stat = stat($dest);
619 unlink($dest) if ( -f $dest );
621 die("can't copy($prog, $dest)\n") unless copy($prog, $dest);
623 open(PROG, $prog) || die("can't open $prog for reading\n");
624 open(OUT, ">$dest") || die("can't open $dest for writing\n");
626 s/__INSTALLDIR__/$Conf{InstallDir}/g;
627 s/__TOPDIR__/$Conf{TopDir}/g;
628 s/__BACKUPPCUSER__/$Conf{BackupPCUser}/g;
629 s/__CGIDIR__/$Conf{CgiDir}/g;
630 if ( $first && /^#.*bin\/perl/ ) {
633 # perl56 and later is taint ok
635 print OUT "#!$Conf{PerlPath} -T\n";
638 # prior to perl56, File::Find fails taint checks,
639 # so we run without -T. It's still safe.
641 print OUT "#!$Conf{PerlPath}\n";
651 die("can't chown $uid, $gid $dest") unless chown($uid, $gid, $dest);
652 die("can't chmod $mode $dest") unless chmod($mode, $dest);
657 my($path, $prog) = @_;
658 foreach my $dir ( split(/:/, $path) ) {
659 my $file = File::Spec->catfile($dir, $prog);
660 return $file if ( -x $file );
667 open(C, $file) || die("can't open $file");
668 my($out, @conf, $var);
677 $allVars->{$var} = @conf if ( defined($var) );
687 } elsif ( /^\s*\$Conf\{([^}]*)/ ) {
689 if ( defined($var) ) {
690 $allVars->{$var} = @conf if ( defined($var) );
705 $allVars->{$var} = @conf if ( defined($var) );
712 return (\@conf, $allVars);
717 my($old, $oldVars, $new, $newVars) = @_;
722 # Find which config parameters are not needed any longer
724 foreach my $var ( @$old ) {
725 next if ( !defined($var->{var}) || defined($newVars->{$var->{var}}) );
726 #print(STDERR "Deleting old config parameter $var->{var}\n");
730 # Find which config parameters are new
732 foreach my $var ( @$new ) {
733 next if ( !defined($var->{var}) );
734 if ( defined($oldVars->{$var->{var}}) ) {
735 $posn = $oldVars->{$var->{var}};
737 #print(STDERR "New config parameter $var->{var}: $var->{text}\n");
738 push(@{$old->[$posn]{new}}, $var);
742 # Create merged config file
744 foreach my $var ( @$old ) {
745 next if ( $var->{delete} );
747 foreach my $new ( @{$var->{new}} ) {