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