3 # Copyright (c) 2005-2008 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 Mathlida 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;
49 # Get the version suffix.
50 my $USE_VERSION_SUFFIXES = '@USE_VERSION_SUFFIXES@';
52 if ( $USE_VERSION_SUFFIXES =~ /^yes$/i ) {
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";
73 for $fh ( STDOUT, LOG ) {
79 my ($err, $cleanup) = @_;
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";
90 if ( -e "$confdir/$config/advanced.conf" ) {
91 unlink "$confdir/$config/advanced.conf" ||
92 print LOG "unlink $confdir/$config/advanced.conf failed: $!\n";
94 if ( -e "$confdir/$config/tapelist" ) {
95 unlink "$confdir/$config/tapelist" ||
96 print LOG "unlink $confdir/$config/tapelist failed: $!\n";
98 if ( -e "$confdir/$config/curinfo" ) {
99 rmdir "$confdir/$config/curinfo" ||
100 print LOG "rmdir $confdir/$config failed: $!\n";
102 if ( -e "$confdir/$config/index" ) {
103 rmdir "$confdir/$config/index" ||
104 print LOG "rmdir $confdir/$config/index failed: $!\n";
106 rmdir "$confdir/$config" ||
107 print LOG "rmdir $confdir/$config failed: $!\n";
116 ( $user eq $amanda_user ) ||
117 die ("ERROR: $0 must be run by $amanda_user\n", 0);
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");
127 mkdir ("$amandahomedir/gnutar-lists", $def_perm) ||
128 &log_and_die ("ERROR: mkdir:$amandahomedir/gnutar-lists failed: $!\n", 0);
132 sub create_conf_dir {
133 unless ( -e $confdir ) {
134 &log_and_die ("ERROR: $confdir does not exist\n", 0);
136 unless ( -e "$confdir/$config" ) {
137 mkdir ("$confdir/$config", $def_perm) ||
138 &log_and_die ("ERROR: mkdir: $confdir/$config failed: $!\n", 0); # $! = system error
140 &log_and_die ("ERROR: Configuration $config exists\n", 0);
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");
149 sub copy_template_file {
152 &log_and_die ("ERROR: template is missing\n", 1);
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);
162 $_ =~ s/$def_config/$config/;
167 &mprint ("$confdir/$config/amanda.conf created and updated\n");
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");
179 sub touch_list_files {
180 open (TLIST, ">$confdir/$config/tapelist")
181 || &log_and_die ("ERROR: Cannot create tapelist file: $!\n", 1);
183 &mprint ("tapelist file created\n");
185 open (DLIST, ">$confdir/$config/disklist")
186 || &log_and_die ("ERROR: Cannot create disklist file: $!\n", 1);
188 &mprint ("disklist file created\n");
191 # create holding disk directory, check disk space first
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");
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");
210 unless (( $dfout[1] eq "1K-blocks" ) || ( $dfout[1] eq "kbytes")) {
211 $div=2; # 512-blocks displayed by df
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 );
220 mkdir ( "$amandahomedir/holdings/$config", $def_perm) ||
221 (&mprint ("WARNING: mkdir $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ;
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 );
231 unless ( -e "$amandahomedir/vtapes/$config" ) {
232 mkdir ( "$amandahomedir/vtapes/$config", $def_perm) ||
233 ( &mprint ("WARNING: mkdir $amandahomedir/vtapes/$config failed: $!\n"), return );
235 $parentdir="$amandahomedir/vtapes/$config";
238 # create and label 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"){
247 $tapedev=~/^(file:\/)/;
252 $mylabelprefix=$config;
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");
262 chdir ("$parentdir") ||
263 ( &mprint("WARNING: chdir $parentdir failed: $!\n"), $vtape_err++, return );
265 &mprint ("amlabel vtapes\n");
266 if (defined $tapecycle) {
270 my $dfout =`df $parentdir`;
272 @dfdata=split(" ",$dfout);
273 unless ( $dfdata[1] eq "1K-blocks" ) {
274 $mul=512; # 512-blocks displayed by df
276 if (($dfdata[10]*$mul) < (($tp_cyclelimit*73728)+10240)){
277 &mprint ("WARNING: Not enough space for vtapes. Creation of vtapes failed\n");
283 $tp_cyclelimit=$def_tapecycle;
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);
291 ( @amlabel_out = `$sbindir/amlabel -f $config $mylabelprefix-$i slot $i`) ||
292 ( &mprint ("WARNING: amlabel vtapes failed at slot $i: $!\n"), $vtape_err++, return);
294 foreach (@amlabel_out) {
297 # reset tape to the first slot
298 `$sbindir/amtape $config reset`;
301 sub create_customconf{
302 # now create a custom amanda.conf from user input
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
310 &log_and_die ("ERROR: Invalid data in mailto.\n"); # log this somewhere
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") {
321 $labelstr="^$config-[0-9][0-9]*\$";
324 if ((!(defined($template)))||($template eq "harddisk"))
326 if (defined $tapedev){
327 $tapedev="file:/".$tapedev;
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"; }
335 elsif ($template eq "single-tape")
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";}
343 elsif ($template eq "tape-changer")
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";}
353 unless ($tpchanger){$tpchanger="chg-multi";}
354 unless ($changerfile){$changerfile="$confdir/$config/changer.conf";}
355 unless ($tapetype) {$tapetype="HP-DAT";}
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);
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";
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";
397 mprint ("custom amanda.conf created\n");
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");
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");
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\" ";
423 &mprint("$amandahomedir/.ssh/client_authorized_keys created. Please append to /var/lib/amanda/.ssh/authorized_keys file on Amanda clients\n");
428 sub copy_chg_manual_conf {
429 if ( $template eq "single-tape" && !defined $changerfile && !defined $tpchanger)
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");
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,
466 unless ( $#ARGV == 0 ) {
467 print STDERR "ERROR: config name is required.\n";
472 if ( "$ARGV[0]" =~ /^([-\@\w.]+)$/ ) {
473 $config = $1; # now untainted
475 die ("ERROR: Invalid data in config name.\n"); # log this somewhere
480 $oldPATH = $ENV{'PATH'};
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`;
486 my $logfile="$tmpdir/amserverconfig.$date.debug";
489 unless ( -e "$tmpdir" ) {
490 mkdir ("$tmpdir", $def_perm) ||
491 die ("ERROR: mkdir: $tmpdir failed: $!\n");
494 open (LOG, ">$logfile") || die ("ERROR: Cannot create logfile: $!\n");
495 print STDOUT "Logging to $logfile\n";
497 my $lhost=`hostname`;
499 # get our own canonical name, if possible (we don't sweat the IPv6 stuff here)
500 $host=(gethostbyname($lhost))[0];
503 $host = $lhost; #gethostbyname() failed, go with hostname output
507 my $need_changer = 0;
508 if ( defined $template ) {
510 # validate user input to template
513 @valid_templates = ( "harddisk", "single-tape", "tape-changer", "s3" );
514 foreach $elt (@valid_templates) {
515 if ( $elt eq lc($template) ) {
522 "valid inputs to --templates are harddisk, single-tape, tape-changer or S3\n";
527 # if tape-changer is chosen, check if mtx is installed
528 if ( $template eq "tape-changer" ) {
530 for $dir ( "/usr/sbin", "/usr/local/sbin", "/usr/local/bin",
531 "/usr/bin", "/bin", "/opt/csw/sbin", split( ":", $oldPATH ) )
533 if ( -e "$dir/mtx" ) {
540 "ERROR: mtx binary not found, tape-changer template will not work and is not installed.\n"
543 "ERROR: Please install mtx and rerun the same command.\n",
548 elsif ( $template eq "S3" ) {
557 unless ($changerfile) {
558 $changerfile = "$confdir/$config/changer.conf";
560 open( CCONF, ">$changerfile" )
561 || &log_and_die( "ERROR: Cannot create $changerfile: $!\n", 1 );
565 &check_gnutarlist_dir;
567 # copy dumptypes and tapetypes files if none exists.
568 my $dtype="$confdir/template.d/dumptypes";
569 my $ttype="$confdir/template.d/tapetypes";
571 unless ( -e $dtype ) {
572 copy("$templatedir/dumptypes", $dtype ) ||
573 &log_and_die ("ERROR: copy dumptypes failed: $!\n", 1);
577 unless ( -e $ttype ) {
578 copy("$templatedir/tapetypes", $ttype ) ||
579 &log_and_die ("ERROR: copy tapetypes file to $ttype failed: $!\n", 1);
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);
589 $_ =~ s/$def_config/$config/;
594 &mprint ("$confdir/$config/advanced.conf created and updated\n");
597 &create_curinfo_index_dir;
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");
608 if ( $template ne "harddisk" ) {
611 if (defined $labelstr) {
612 if ($labelstr=~/^([-\w.]+)$/) {
613 &create_vtape unless ( defined $novtape );
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");
622 $tapedev="$def_tapedev/$config";
623 ©_template_file($template);
624 if ($template ne "harddisk") {
625 unless ( -e "$amandahomedir/holdings/$config" ) {
628 } else { # harddisk and template only
629 unless ( -e "$amandahomedir/vtapes/$config" || defined $novtape ) {
634 ©_chg_manual_conf;
640 &build_amanda_ssh_key;
643 &mprint("Error in creating virtual tape, please check log and create virtual tape manually.\n");
647 if ( $holding_err ) {
648 &mprint("Error in creating holding disk, please check log and create holding disk manually.\n");
654 if ( $vtape_err==0 && $holding_err==0) {
660 $ENV{'PATH'} = $oldPATH;