Imported Upstream version 3.3.0
[debian/amanda] / server-src / amserverconfig.pl
1 #!@PERL@
2 #
3 # Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License version 2 as published
7 # by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
19 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 #
21
22 use lib '@amperldir@';
23 use Getopt::Long;
24 use Time::Local;
25 use File::Copy;
26 use File::Path;
27 use Socket;   # for gethostbyname
28 use Amanda::Paths;
29 use Amanda::Util qw( :constants );
30 use Amanda::Constants;
31
32 my $confdir="$CONFIG_DIR";
33 my $tmpdir="$AMANDA_DBGDIR";
34 my $amandahomedir="$localstatedir/lib/amanda";
35 my $templatedir="$amdatadir/template.d"; #rpm install template files here
36 my $def_tapedev="file:$amandahomedir/vtapes";
37
38 my $amanda_user="$Amanda::Constants::CLIENT_LOGIN";
39 my $def_config="$Amanda::Constants::DEFAULT_CONFIG";
40 my $def_dtimeout="1800";
41 my $def_ctimeout="30";
42 my $def_etimeout="300";
43 my $def_perm=0750;
44 my $amanda_conf_perm=0600;
45 my $def_tapecycle=10;
46 my $config;
47 my $vtape_err=0;
48 my $holding_err=0;
49 my $template_only=0;
50 my $parentdir;
51 my $host;
52 my @pw = getpwuid($<);
53 my $dumpuser = $pw[0];
54
55
56 #usage
57 sub usage {
58         print "$0\n";
59         print "\t\t <config> [--template <template>]\n";
60         print "\t\t[--no-vtape] (do not create virtual tapes)\n";
61         print "\t\t[--tapetype <tapetype>] [--tpchanger <tpchanger>]\n";
62         print "\t\t[--tapedev <tapedev>] [--changerfile <changerfile>]\n";
63         print "\t\t[--changerdev <changerdev>] [--labelstr <labelstr>] \n";
64         print "\t\t[--mailto <mailto>] [--dumpcycle <dumpcycle> (ex: 5days, 1week or 2weeks)]\n";
65         print "\t\t[--runspercycle <runspercycle>] [--runtapes <runtapes>]\n";
66         print "\t\t[--tapecycle <tapecycle>]\n";
67         print "\t\t[--help]\n";
68 }
69
70 #print and log
71 sub mprint {
72     for $fh ( STDOUT, LOG ) {
73         print $fh @_;
74     }
75 }
76
77 sub log_and_die { 
78     my ($err, $cleanup) = @_;
79     print LOG $err;
80     # clean up $config directory if cleanup=1
81     # if error in creating vtape or holding disk, 
82     # advise user to create manually, no need to cleanup
83     if ( $cleanup && defined $config  && -e "$confdir/$config" ) {
84         print LOG "cleaning up $confdir/$config\n";
85         if ( -e "$confdir/$config/amanda.conf" ) {
86             unlink "$confdir/$config/amanda.conf" || 
87             print LOG "unlink $confdir/$config/amanda.conf failed: $!\n";
88         }
89         if ( -e "$confdir/$config/advanced.conf" ) {
90             unlink "$confdir/$config/advanced.conf" || 
91             print LOG "unlink $confdir/$config/advanced.conf failed: $!\n";
92         }
93         if ( -e "$confdir/$config/tapelist" ) {
94             unlink "$confdir/$config/tapelist" || 
95             print LOG "unlink $confdir/$config/tapelist failed: $!\n";
96         }
97         if ( -e "$confdir/$config/curinfo" ) {
98             rmdir "$confdir/$config/curinfo" || 
99             print LOG "rmdir $confdir/$config failed: $!\n";
100         }
101         if ( -e "$confdir/$config/index" ) {
102             rmdir "$confdir/$config/index" || 
103             print LOG "rmdir $confdir/$config/index failed: $!\n";
104         }
105         rmdir "$confdir/$config" || 
106             print LOG "rmdir $confdir/$config failed: $!\n";
107     }
108     die $err;
109 }
110
111
112 # rpm installation should have taken care of these. Create one if it's not there
113 sub check_gnutarlist_dir {
114     if ( -e "$amandahomedir/gnutar-lists" ) {
115         &mprint ("$amandahomedir/gnutar-lists directory exists\n");
116     }
117     else {
118         mkpath ("$amandahomedir/gnutar-lists", $def_perm) ||
119             &log_and_die ("ERROR: mkpath:$amandahomedir/gnutar-lists failed: $!\n", 0);
120     }
121 }
122
123 sub create_conf_dir {
124   unless ( -e "$confdir/$config" ) {
125     mkpath ("$confdir/$config", $def_perm) ||
126       &log_and_die ("ERROR: mkpath: $confdir/$config failed: $!\n", 0); # $! = system error
127   } else {
128     &log_and_die ("ERROR: Configuration $config exists\n", 0);
129   }
130   unless ( -e "$confdir/template.d" ) {
131     mkpath ("$confdir/template.d", $def_perm)  ||
132       &log_and_die ("ERROR: mkpath: $confdir/template.d failed: $!\n", 0);
133     &mprint ("$confdir/template.d directory created\n");
134   }
135 }
136
137 sub copy_template_file {
138     my $tplate = $_[0];
139     unless ($tplate) {
140         &log_and_die ("ERROR: template is missing\n", 1);
141     }
142     # create and update amanda.conf
143     open(CONF, "$templatedir/amanda-$tplate.conf")
144         || &log_and_die ("ERROR: Cannot open $templatedir/amanda-$tplate.conf: $!\n", 1);
145     open(NEWCONF, ">$confdir/$config/amanda.conf") ||
146         &log_and_die ("ERROR: Cannot create $confdir/$config/amanda.conf: $!\n", 1);
147     chmod ($amanda_conf_perm, "$confdir/$config/amanda.conf") ||
148         &log_and_die ("ERROR: Cannot set amanda.conf file access permission: $!\n", 1);
149     while (<CONF>) {
150         $_ =~ s/$def_config/$config/;
151         print NEWCONF $_;
152     }
153     close(CONF);
154     close(NEWCONF);
155     &mprint ("$confdir/$config/amanda.conf created and updated\n");
156 }
157
158
159 sub create_curinfo_index_dir {
160     mkpath("$confdir/$config/curinfo", $def_perm) ||
161         &log_and_die ("ERROR: mkpath: $confdir/$config/curinfo failed: $!\n", 1);
162     mkpath("$confdir/$config/index", $def_perm) || 
163         &log_and_die ("ERROR: mkpath: $confdir/$config/index failed: $!\n", 1);
164     &mprint ("curinfo and index directory created\n");
165 }
166
167 sub touch_list_files {
168     open (TLIST, ">$confdir/$config/tapelist")
169         || &log_and_die ("ERROR: Cannot create tapelist file: $!\n", 1);
170     close (TLIST);
171     &mprint ("tapelist file created\n");
172
173     open (DLIST, ">$confdir/$config/disklist")
174         || &log_and_die ("ERROR: Cannot create disklist file: $!\n", 1);
175     close (DLIST);
176     &mprint ("disklist file created\n");
177 }
178
179 # create holding disk directory, check disk space first
180 sub create_holding { 
181   if ( -d "$amandahomedir/holdings/$config" ) {
182     my $uid = (stat("$amandahomedir/holdings/$config"))[4];
183     my $owner = (getpwuid($uid))[0];
184     unless ( $owner eq $amanda_user ) {
185       &mprint ("WARNING: holding disk directory exists and is not owned by $amanda_user\n");
186       $holding_err++;
187     }
188     return;
189   }
190     my $div=1;
191     my $out = `df -k $amandahomedir`;
192     my @dfout = split(" " , $out);
193     unless ( $#dfout == 12 ) {  # df should output 12 elem
194         &mprint ("WARNING: df failed, holding disk directory not created\n");
195         $holding_err++;
196         return;
197     }
198     unless (( $dfout[1] eq "1K-blocks" ) || ( $dfout[1] eq "kbytes")) {
199          $div=2;        # 512-blocks displayed by df
200      }
201     
202     if (( $dfout[10] / $div )  > 1024000 ) { # holding disk is defined 1000 MB
203         &mprint ("creating holding disk directory\n");
204         unless ( -d "$amandahomedir/holdings" ) { 
205         mkpath ( "$amandahomedir/holdings", $def_perm) ||
206             (&mprint ("WARNING: mkpath $amandahomedir/holdings failed: $!\n"), $holding_err++, return );
207     }
208         mkpath ( "$amandahomedir/holdings/$config", $def_perm) ||
209             (&mprint ("WARNING: mkpath $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ;
210     }
211 }
212
213 #create default tape dir
214 sub create_deftapedir{
215     unless ( -e "$amandahomedir/vtapes" ) { 
216         mkpath ( "$amandahomedir/vtapes", $def_perm) ||
217             ( &mprint ("WARNING: mkpath $amandahomedir/$config/vtapes failed: $!\n"), return );
218     }
219     unless ( -e "$amandahomedir/vtapes/$config" ) { 
220         mkpath ( "$amandahomedir/vtapes/$config", $def_perm) ||
221             ( &mprint ("WARNING: mkpath $amandahomedir/vtapes/$config failed: $!\n"), return );
222     }
223         $parentdir="$amandahomedir/vtapes/$config";
224 }
225
226 # create and label vtape
227 sub create_vtape {
228         &mprint ("creating vtape directory\n");
229         if ($template_only==0){ #  check $template mode
230                 $mylabelprefix=$labelstr;   #set labelstr
231                 if ($tapedev eq "$def_tapedev/$config"){
232                 &create_deftapedir;
233                 }
234                 else {
235                 $tapedev=~/^(file:\/)/;
236                 $parentdir=$';
237                 }
238         }
239         else {
240                 $mylabelprefix=$config;
241                 &create_deftapedir;     
242         }
243         unless ( -e $parentdir){
244                 &mprint ("WARNING: tapedev $parentdir does not exists, vtapes creation failed!\n");
245                 &mprint ("Please create $parentdir and $confdir/$config and rerun the same command or else create vtapes manually.\n");
246                 $vtape_err++;
247                 return;
248         }
249
250         chdir ("$parentdir") ||
251                 ( &mprint("WARNING: chdir $parentdir failed: $!\n"), $vtape_err++, return );
252     my $i;
253     &mprint ("amlabel vtapes\n");
254         if (defined $tapecycle) {
255                 $tapecycle=~/^\d+/; 
256                 $tp_cyclelimit=$&;
257
258                 # check space
259                 my $fsinfo = Amanda::Util::get_fs_usage($parentdir);
260                 my $avail_bytes = $fsinfo->{'blocksize'} * $fsinfo->{'bavail'};
261
262                 # mysteriously, we need at least 72k per slot, plus 10k overhead.  The
263                 # origin of these numbers is unknown.
264                 my $needed_bytes = (($tp_cyclelimit*73728)+10240);
265                 if ($avail_bytes < $needed_bytes){
266                         &mprint ("WARNING: Not enough space for vtapes. Need 72k per slot plus 10k ($needed_bytes bytes); have $avail_bytes available.  Creation of vtapes failed\n");
267                         $vtape_err++;
268                         return;
269                 }
270         }
271         else {
272                 $tp_cyclelimit=$def_tapecycle;
273         }
274
275         for $i (1..$tp_cyclelimit) {
276                 unless ( -e "slot$i"){
277                 mkpath ("slot$i", $def_perm) ||
278                 ( &mprint ("WARNING: mkpath $parentdir/slot$i failed: $!\n"), $vtape_err++, return);
279                 }
280                 ( @amlabel_out = `$sbindir/amlabel -f $config $mylabelprefix-$i slot $i`) ||
281             ( &mprint ("WARNING: amlabel vtapes failed at slot $i: $!\n"), $vtape_err++, return);
282     }
283         foreach (@amlabel_out) {
284           print LOG;
285         }
286         # reset tape to the first slot
287         `$sbindir/amtape $config reset`;
288 }
289
290 sub create_customconf{
291            # now create a custom amanda.conf from user input
292         unless ( $mailto ) 
293         { $mailto="$amanda_user"; }
294         else {  # untaint mailto which can be evil
295                 # reject mailto with the following * ( ) < > [ ] , ; : ! $ \ / "
296             if ( $mailto =~ /^([^\*\(\)<>\[\]\,\;\:\!\$\\\/\"]+)$/ ) {
297                 $mailto = $1;                      #  now untainted
298             } else {
299                 &log_and_die ("ERROR: Invalid data in mailto.\n");  # log this somewhere
300             }
301         }
302         unless ( $dumpcycle ) { $dumpcycle="1 week"; }
303         unless ( $runspercycle ) { $runspercycle="5"; }
304         unless ( $tapecycle ) { $tapecycle="10 tapes"; }
305         unless ( $runtapes ) { $runtapes="1"; }
306         unless ( $labelstr ) {
307           if ($template eq "harddisk") {
308             $labelstr="$config";
309           } else {
310             $labelstr="^$config-[0-9][0-9]*\$";
311           }
312         }
313         if ((!(defined($template)))||($template eq "harddisk")) 
314           {
315                 if (defined $tapedev){
316                 $tapedev="file:/".$tapedev;
317                 }
318                 unless ( $tpchanger ) { $tpchanger="chg-disk"; }
319                 unless ( $tapedev ) { $tapedev="$def_tapedev/$config"; }
320                 unless ( $changerfile ) { $changerfile="$confdir/$config/changer.conf"; }
321                 unless ( $changerdev ) { $changerdev="/dev/null";}
322                 unless ( $tapetype ) { $tapetype="HARDDISK"; }  
323           }
324         elsif ($template eq "single-tape")
325           {
326                 unless ($tpchanger) {$tpchanger="chg-manual";}
327                 unless ($tapedev)     {$tapedev="/dev/nst0";}
328                 unless ($changerfile) {$changerfile="$confdir/$config/chg-manual.conf";}
329                 unless ($changerdev) {$changerdev="/dev/null";}
330                 unless ($tapetype) {$tapetype="HP-DAT";}
331           }
332         elsif ($template eq "tape-changer") 
333           {
334                 unless ($tpchanger){$tpchanger="chg-zd-mtx";}
335                 unless ($tapedev){ $tapedev="/dev/nst0";}
336                 unless ($changerfile){$changerfile="$confdir/$config/changer.conf";}
337                 unless ($changerdev) {$changerdev="/dev/sg1";}
338                 unless ($tapetype)  {$tapetype="HP-DAT";}
339           }
340         else # S3 case
341           {
342             unless ($tpchanger){$tpchanger="chg-multi";}
343             unless ($changerfile){$changerfile="$confdir/$config/changer.conf";}
344             unless ($tapetype)  {$tapetype="HP-DAT";}
345           }
346
347
348         open (CONF, ">$confdir/$config/amanda.conf") ||
349             &log_and_die ("ERROR: Cannot create amanda.conf file: $!\n", 1);
350         chmod ($amanda_conf_perm, "$confdir/$config/amanda.conf") ||
351             &log_and_die ("ERROR: Cannot set amanda.conf file access permission: $!\n", 1);
352
353         print CONF "org \"$config\"\t\t# your organization name for reports\n";
354         print CONF "dumpuser \"$dumpuser\"\t# the user to run dumps under\n";
355         print CONF "mailto \"$mailto\"\t# space separated list of operators at your site\n";
356         print CONF "dumpcycle $dumpcycle\t\t# the number of days in the normal dump cycle\n";
357         print CONF "runspercycle $runspercycle\t\t# the number of amdump runs in dumpcycle days\n";
358         print CONF "tapecycle $tapecycle\t# the number of tapes in rotation\n"; 
359         print CONF "runtapes $runtapes\t\t# number of tapes to be used in a single run of amdump\n";
360         print CONF "tpchanger \"$tpchanger\"\t# the tape-changer glue script\n";
361         print CONF "tapedev \"$tapedev\"\t# the no-rewind tape device\n";
362         print CONF "changerfile \"$changerfile\"\t# tape changer configuration parameter file\n";
363         print CONF "changerdev \"$changerdev\"\t# tape changer configuration parameter device\n";
364         print CONF "tapetype $tapetype\t# what kind of tape it is\n";
365         print CONF "labelstr \"$labelstr\"\t# label constraint regex: all tapes must match\n";
366         print CONF "dtimeout $def_dtimeout\t# number of idle seconds before a dump is aborted\n";
367         print CONF "ctimeout $def_ctimeout\t# max number of secconds amcheck waits for each client\n";
368         print CONF "etimeout $def_etimeout\t# number of seconds per filesystem for estimates\n";
369         print CONF "define dumptype global {\n";
370         print CONF "       comment \"Global definitions\"\n";
371         print CONF "       auth \"bsdtcp\"\n}\n";
372         print CONF "define dumptype gui-base {\n";
373         print CONF "       global\n";
374         print CONF "       program \"GNUTAR\"\n";
375         print CONF "       comment \"gui base dumptype dumped with tar\"\n";
376         print CONF "       compress none\n";
377         print CONF "       index yes\n}\n";
378         if ($tapetype eq "HARDDISK") {
379           print CONF "define tapetype HARDDISK {\n";
380           print CONF "       comment \"Virtual Tapes\"\n";
381           print CONF "       length 5000 mbytes\n}\n";
382         }
383         print CONF "includefile \"advanced.conf\"\n";
384         print CONF "includefile \"$confdir/template.d/dumptypes\"\n";
385         print CONF "includefile \"$confdir/template.d/tapetypes\"\n";
386         close (CONF);
387         mprint ("custom amanda.conf created\n");
388   }
389
390
391 sub check_xinetd{
392     &mprint ("/var/lib/amanda/example/xinetd.amandaserver contains the latest Amanda server daemon configuration.\n");
393     &mprint ("Please merge it to /etc/xinetd.d/amandaserver.\n");
394 }
395
396
397 sub build_amanda_ssh_key{
398   if ( -e "$amandahomedir/.ssh/id_rsa_amdump.pub" ) {
399     if ( -e "$amandahomedir/.ssh/client_authorized_key" ) {
400       &mprint ("$amandahomedir/.ssh/client_authorized_keys exists.\n");
401     }
402     else {
403       open(NEWAUTH, ">$amandahomedir/.ssh/client_authorized_keys") ||
404         (&mprint("WARNING: open $amandahomedir/.ssh/client_authorized_key failed: $!\n"), return);
405       open(PUB, "$amandahomedir/.ssh/id_rsa_amdump.pub") ||
406         (&mprint("WARNING: open $amandahomedir/.ssh/id_rsa_amdump.pub failed: $!\n"), return);
407       print NEWAUTH "from=\"$host\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,command=\"/usr/lib/amanda/amandad -auth=ssh amdump\" ";
408       while (<PUB>) {
409       print NEWAUTH;
410     }
411       close NEWAUTH;
412       close PUB;
413       &mprint("$amandahomedir/.ssh/client_authorized_keys created. Please append to $amandahomedir/.ssh/authorized_keys file on Amanda clients\n");
414       }
415   }
416 }
417
418 sub copy_chg_manual_conf {
419   if ( $template eq "single-tape" && !defined $changerfile && !defined $tpchanger)
420     {
421       my $my_changerfile="$confdir/$config/chg-manual.conf";
422       copy("$templatedir/chg-manual.conf", $my_changerfile) ||
423         &mprint ("copy $templatedir/chg-manual.conf to $my_changerfile failed: $!\n");
424     }
425 }
426
427 #main
428 my $ret=0;
429
430 $ret = GetOptions ("template=s"=>\$template,
431                    "no-vtape!"=>\$novtape,
432               "tapetype=s"=>\$tapetype,
433               "tpchanger=s"=>\$tpchanger,
434               "tapedev=s"=>\$tapedev,
435               "changerfile=s"=>\$changerfile,
436               "changerdev=s"=>\$changerdev,
437               "labelstr=s"=>\$labelstr,
438               "mailto=s"=>\$mailto,
439               "dumpcycle=s"=>\$dumpcycle,
440               "runspercycle=i"=>\$runspercycle,
441               "runtapes=i"=>\$runtapes,
442               "tapecycle=i"=>\$tapecycle,
443               "help!"=>\$help
444               );
445
446 unless ( $ret ) {
447     &usage;
448     exit 1;
449 }
450
451 if($help) {
452     &usage;
453     exit 0;
454 }
455
456 unless ( $#ARGV == 0 ) {
457     print STDERR "ERROR: config name is required.\n";
458     &usage;
459     exit 1;
460 }
461 else {
462     if ( "$ARGV[0]" =~ /^([-\@\w.]+)$/ ) {
463         $config = $1;                   #  now untainted
464     } else {
465         die ("ERROR: Invalid data in config name.\n");  # log this somewhere
466     }
467 }
468
469
470 $oldPATH = $ENV{'PATH'};
471
472 $ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/bin:/usr/ucb"; # force known path
473 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
474 $date=`date +%Y%m%d%H%M%S`;
475 chomp($date);
476 my $logfile="$tmpdir/amserverconfig.$date.debug";
477
478 Amanda::Util::setup_application("amserverconfig", "server", $CONTEXT_CMDLINE);
479 Amanda::Util::finish_setup($RUNNING_AS_ANY);
480
481 unless ( -e "$tmpdir" ) {
482     mkpath ("$tmpdir", $def_perm) ||
483         die ("ERROR: mkpath: $tmpdir failed: $!\n");
484 }
485
486 open (LOG, ">$logfile") || die ("ERROR: Cannot create logfile: $!\n");
487 print STDOUT "Logging to $logfile\n";
488
489 my $lhost=`hostname`;
490 chomp($lhost);
491 # get our own canonical name, if possible (we don't sweat the IPv6 stuff here)
492 $host=(gethostbyname($lhost))[0];
493
494 unless ( $host ) {
495     $host = $lhost;  #gethostbyname() failed, go with hostname output
496 }
497
498
499 my $need_changer = 0;
500 if ( defined $template ) {
501
502     # validate user input to template
503     chomp($template);
504     my $found = 0;
505     @valid_templates = ( "harddisk", "single-tape", "tape-changer", "s3" );
506     foreach $elt (@valid_templates) {
507         if ( $elt eq lc($template) ) {
508             $found = 1;
509             last;
510         }
511     }
512     unless ($found) {
513         print STDERR
514             "valid inputs to --templates are harddisk, single-tape, tape-changer or S3\n";
515         &usage;
516         exit 1;
517     }
518
519     # if tape-changer is chosen, check if mtx is installed
520     if ( $template eq "tape-changer" ) {
521         my $ok = 0;
522         for $dir ( "/usr/sbin", "/usr/local/sbin", "/usr/local/bin",
523             "/usr/bin", "/bin", "/opt/csw/sbin", split( ":", $oldPATH ) )
524         {
525             if ( -e "$dir/mtx" ) {
526                 $ok = 1;
527                 last;
528             }
529         }
530         unless ($ok) {
531             &mprint(
532                 "ERROR: mtx binary not found, tape-changer template will not work and is not installed.\n"
533             );
534             &log_and_die(
535                 "ERROR: Please install mtx and rerun the same command.\n",
536                 0 );
537         }
538         $need_changer = 1;
539     }
540     elsif ( $template eq "S3" ) {
541         $need_changer = 1;
542     }
543
544 }
545
546 &create_conf_dir;
547
548 if ($need_changer) {
549     unless ($changerfile) {
550         $changerfile = "$confdir/$config/changer.conf";
551     }
552     open( CCONF, ">$changerfile" )
553         || &log_and_die( "ERROR: Cannot create $changerfile: $!\n", 1 );
554     close(CCONF);
555 }
556
557 &check_gnutarlist_dir;
558
559 # copy dumptypes and tapetypes files if none exists.
560 my $dtype="$confdir/template.d/dumptypes";
561 my $ttype="$confdir/template.d/tapetypes";
562
563 unless ( -e $dtype ) {
564     copy("$templatedir/dumptypes", $dtype ) ||
565     &log_and_die ("ERROR: copy dumptypes failed: $!\n", 1);
566 }
567
568
569 unless ( -e $ttype ) {
570     copy("$templatedir/tapetypes", $ttype ) ||
571     &log_and_die ("ERROR: copy tapetypes file to $ttype failed: $!\n", 1);
572 }
573
574
575
576 # update $def_config value to the specified config value in advanced.conf
577     open(ADV, "$templatedir/advanced.conf") || &log_and_die ("ERROR: Cannot open advanced.conf file: $!\n", 1);
578     open(NEWADV, ">$confdir/$config/advanced.conf") || 
579         &log_and_die ("ERROR: Cannot create advanced.conf file: $!\n", 1);
580     while (<ADV>) {
581         $_ =~ s/$def_config/$config/;
582         print NEWADV $_;
583     }
584     close(ADV);
585     close(NEWADV);
586     &mprint ("$confdir/$config/advanced.conf created and updated\n");
587
588
589 &create_curinfo_index_dir;
590 &touch_list_files;
591
592
593 if ( defined $template ) {
594 # if any other parameters are provided, create a workable custom config
595         if ( defined $tapetype || defined $tpchanger || defined $tapedev
596          || defined $changerdev || defined $labelstr || defined $mailto || defined $dumpcycle
597          || defined $runspercycle || defined $runtapes || defined $tapecycle ) {
598                 &mprint("Creating custom configuration using templates\n");
599                 create_customconf();
600                 if ( $template ne "harddisk" ) {
601                   &create_holding;
602                 } else {
603                   if (defined $labelstr) {
604                     if ($labelstr=~/^([-\w.]+)$/) {
605                       &create_vtape unless ( defined $novtape );
606                     } else {
607                       &mprint ("WARNING: Only alphanumeric string is supported in labelstr when using template to create vtapes. ");
608                       &mprint ("If you want to use regex in labelstr, please create vtapes manually.\n");
609                     }
610                   }
611                 }
612               } else {
613                 $template_only=1;
614                 $tapedev="$def_tapedev/$config";
615                 &copy_template_file($template);
616                 if ($template ne "harddisk") {
617                   unless ( -e "$amandahomedir/holdings/$config" ) {
618                     &create_holding;
619                   }
620                 } else {  # harddisk and template only
621                   unless ( -e "$amandahomedir/vtapes/$config" || defined $novtape ) {
622                     &create_vtape;
623                   }
624                 }
625               }
626         &copy_chg_manual_conf;
627       } else {
628 &create_customconf;
629 }
630
631 &check_xinetd;
632 &build_amanda_ssh_key;
633
634 if ( $vtape_err ) {
635   &mprint("Error in creating virtual tape, please check log and create virtual tape manually.\n");
636   exit 1;
637 }
638
639 if ( $holding_err ) {
640   &mprint("Error in creating holding disk, please check log and create holding disk manually.\n");
641   exit 1;
642 }
643
644
645
646 if ( $vtape_err==0 && $holding_err==0) {
647   &mprint("DONE.\n");
648   exit 0;
649 }
650
651
652 $ENV{'PATH'} = $oldPATH;
653
654
655 # THE END
656 Amanda::Util::finish_application();