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