3 # Copyright (c) 2007,2008,2009 Zmanda, Inc. All Rights Reserved.
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.
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
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
18 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
19 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
22 use lib '@amperldir@';
26 use Socket; # for gethostbyname
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";
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";
41 my $amanda_conf_perm=0600;
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";
67 for $fh ( STDOUT, LOG ) {
73 my ($err, $cleanup) = @_;
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";
84 if ( -e "$confdir/$config/advanced.conf" ) {
85 unlink "$confdir/$config/advanced.conf" ||
86 print LOG "unlink $confdir/$config/advanced.conf failed: $!\n";
88 if ( -e "$confdir/$config/tapelist" ) {
89 unlink "$confdir/$config/tapelist" ||
90 print LOG "unlink $confdir/$config/tapelist failed: $!\n";
92 if ( -e "$confdir/$config/curinfo" ) {
93 rmdir "$confdir/$config/curinfo" ||
94 print LOG "rmdir $confdir/$config failed: $!\n";
96 if ( -e "$confdir/$config/index" ) {
97 rmdir "$confdir/$config/index" ||
98 print LOG "rmdir $confdir/$config/index failed: $!\n";
100 rmdir "$confdir/$config" ||
101 print LOG "rmdir $confdir/$config failed: $!\n";
110 ( $user eq $amanda_user ) ||
111 die ("ERROR: $0 must be run by $amanda_user\n", 0);
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");
121 mkdir ("$amandahomedir/gnutar-lists", $def_perm) ||
122 &log_and_die ("ERROR: mkdir:$amandahomedir/gnutar-lists failed: $!\n", 0);
126 sub create_conf_dir {
127 unless ( -e $confdir ) {
128 &log_and_die ("ERROR: $confdir does not exist\n", 0);
130 unless ( -e "$confdir/$config" ) {
131 mkdir ("$confdir/$config", $def_perm) ||
132 &log_and_die ("ERROR: mkdir: $confdir/$config failed: $!\n", 0); # $! = system error
134 &log_and_die ("ERROR: Configuration $config exists\n", 0);
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");
143 sub copy_template_file {
146 &log_and_die ("ERROR: template is missing\n", 1);
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);
156 $_ =~ s/$def_config/$config/;
161 &mprint ("$confdir/$config/amanda.conf created and updated\n");
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");
173 sub touch_list_files {
174 open (TLIST, ">$confdir/$config/tapelist")
175 || &log_and_die ("ERROR: Cannot create tapelist file: $!\n", 1);
177 &mprint ("tapelist file created\n");
179 open (DLIST, ">$confdir/$config/disklist")
180 || &log_and_die ("ERROR: Cannot create disklist file: $!\n", 1);
182 &mprint ("disklist file created\n");
185 # create holding disk directory, check disk space first
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");
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");
204 unless (( $dfout[1] eq "1K-blocks" ) || ( $dfout[1] eq "kbytes")) {
205 $div=2; # 512-blocks displayed by df
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 );
214 mkdir ( "$amandahomedir/holdings/$config", $def_perm) ||
215 (&mprint ("WARNING: mkdir $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ;
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 );
225 unless ( -e "$amandahomedir/vtapes/$config" ) {
226 mkdir ( "$amandahomedir/vtapes/$config", $def_perm) ||
227 ( &mprint ("WARNING: mkdir $amandahomedir/vtapes/$config failed: $!\n"), return );
229 $parentdir="$amandahomedir/vtapes/$config";
232 # create and label 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"){
241 $tapedev=~/^(file:\/)/;
246 $mylabelprefix=$config;
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");
256 chdir ("$parentdir") ||
257 ( &mprint("WARNING: chdir $parentdir failed: $!\n"), $vtape_err++, return );
259 &mprint ("amlabel vtapes\n");
260 if (defined $tapecycle) {
264 my $dfout =`df $parentdir`;
266 @dfdata=split(" ",$dfout);
267 unless ( $dfdata[1] eq "1K-blocks" ) {
268 $mul=512; # 512-blocks displayed by df
270 if (($dfdata[10]*$mul) < (($tp_cyclelimit*73728)+10240)){
271 &mprint ("WARNING: Not enough space for vtapes. Creation of vtapes failed\n");
277 $tp_cyclelimit=$def_tapecycle;
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);
285 ( @amlabel_out = `$sbindir/amlabel -f $config $mylabelprefix-$i slot $i`) ||
286 ( &mprint ("WARNING: amlabel vtapes failed at slot $i: $!\n"), $vtape_err++, return);
288 foreach (@amlabel_out) {
291 # reset tape to the first slot
292 `$sbindir/amtape $config reset`;
295 sub create_customconf{
296 # now create a custom amanda.conf from user input
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
304 &log_and_die ("ERROR: Invalid data in mailto.\n"); # log this somewhere
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") {
315 $labelstr="^$config-[0-9][0-9]*\$";
318 if ((!(defined($template)))||($template eq "harddisk"))
320 if (defined $tapedev){
321 $tapedev="file:/".$tapedev;
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"; }
329 elsif ($template eq "single-tape")
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";}
337 elsif ($template eq "tape-changer")
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";}
347 unless ($tpchanger){$tpchanger="chg-multi";}
348 unless ($changerfile){$changerfile="$confdir/$config/changer.conf";}
349 unless ($tapetype) {$tapetype="HP-DAT";}
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);
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";
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";
391 mprint ("custom amanda.conf created\n");
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");
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");
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\" ";
417 &mprint("$amandahomedir/.ssh/client_authorized_keys created. Please append to /var/lib/amanda/.ssh/authorized_keys file on Amanda clients\n");
422 sub copy_chg_manual_conf {
423 if ( $template eq "single-tape" && !defined $changerfile && !defined $tpchanger)
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");
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,
460 unless ( $#ARGV == 0 ) {
461 print STDERR "ERROR: config name is required.\n";
466 if ( "$ARGV[0]" =~ /^([-\@\w.]+)$/ ) {
467 $config = $1; # now untainted
469 die ("ERROR: Invalid data in config name.\n"); # log this somewhere
474 $oldPATH = $ENV{'PATH'};
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`;
480 my $logfile="$tmpdir/amserverconfig.$date.debug";
483 unless ( -e "$tmpdir" ) {
484 mkdir ("$tmpdir", $def_perm) ||
485 die ("ERROR: mkdir: $tmpdir failed: $!\n");
488 open (LOG, ">$logfile") || die ("ERROR: Cannot create logfile: $!\n");
489 print STDOUT "Logging to $logfile\n";
491 my $lhost=`hostname`;
493 # get our own canonical name, if possible (we don't sweat the IPv6 stuff here)
494 $host=(gethostbyname($lhost))[0];
497 $host = $lhost; #gethostbyname() failed, go with hostname output
501 my $need_changer = 0;
502 if ( defined $template ) {
504 # validate user input to template
507 @valid_templates = ( "harddisk", "single-tape", "tape-changer", "s3" );
508 foreach $elt (@valid_templates) {
509 if ( $elt eq lc($template) ) {
516 "valid inputs to --templates are harddisk, single-tape, tape-changer or S3\n";
521 # if tape-changer is chosen, check if mtx is installed
522 if ( $template eq "tape-changer" ) {
524 for $dir ( "/usr/sbin", "/usr/local/sbin", "/usr/local/bin",
525 "/usr/bin", "/bin", "/opt/csw/sbin", split( ":", $oldPATH ) )
527 if ( -e "$dir/mtx" ) {
534 "ERROR: mtx binary not found, tape-changer template will not work and is not installed.\n"
537 "ERROR: Please install mtx and rerun the same command.\n",
542 elsif ( $template eq "S3" ) {
551 unless ($changerfile) {
552 $changerfile = "$confdir/$config/changer.conf";
554 open( CCONF, ">$changerfile" )
555 || &log_and_die( "ERROR: Cannot create $changerfile: $!\n", 1 );
559 &check_gnutarlist_dir;
561 # copy dumptypes and tapetypes files if none exists.
562 my $dtype="$confdir/template.d/dumptypes";
563 my $ttype="$confdir/template.d/tapetypes";
565 unless ( -e $dtype ) {
566 copy("$templatedir/dumptypes", $dtype ) ||
567 &log_and_die ("ERROR: copy dumptypes failed: $!\n", 1);
571 unless ( -e $ttype ) {
572 copy("$templatedir/tapetypes", $ttype ) ||
573 &log_and_die ("ERROR: copy tapetypes file to $ttype failed: $!\n", 1);
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);
583 $_ =~ s/$def_config/$config/;
588 &mprint ("$confdir/$config/advanced.conf created and updated\n");
591 &create_curinfo_index_dir;
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");
602 if ( $template ne "harddisk" ) {
605 if (defined $labelstr) {
606 if ($labelstr=~/^([-\w.]+)$/) {
607 &create_vtape unless ( defined $novtape );
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");
616 $tapedev="$def_tapedev/$config";
617 ©_template_file($template);
618 if ($template ne "harddisk") {
619 unless ( -e "$amandahomedir/holdings/$config" ) {
622 } else { # harddisk and template only
623 unless ( -e "$amandahomedir/vtapes/$config" || defined $novtape ) {
628 ©_chg_manual_conf;
634 &build_amanda_ssh_key;
637 &mprint("Error in creating virtual tape, please check log and create virtual tape manually.\n");
641 if ( $holding_err ) {
642 &mprint("Error in creating holding disk, please check log and create holding disk manually.\n");
648 if ( $vtape_err==0 && $holding_err==0) {
654 $ENV{'PATH'} = $oldPATH;