#!@PERL@
#
-# Copyright (c) 2006 Zmanda Inc. All Rights Reserved.
+# Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
-# Contact information: Zmanda Inc, 505 N Mathlida Ave, Suite 120
-# Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
+# Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
+# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
#
+use lib '@amperldir@';
use Getopt::Long;
use Time::Local;
use File::Copy;
+use File::Path;
use Socket; # for gethostbyname
+use Amanda::Paths;
+use Amanda::Util qw( :constants );
+use Amanda::Constants;
-my $confdir="@CONFIG_DIR@";
-my $prefix="@prefix@";
-my $tmpdir="@AMANDA_DBGDIR@";
-$prefix=$prefix; # avoid warnings about possible typo
-my $exec_prefix="@exec_prefix@";
-$exec_prefix=$exec_prefix; # ditto
-my $sbindir="@sbindir@";
-my $localstatedir="@localstatedir@";
+my $confdir="$CONFIG_DIR";
+my $tmpdir="$AMANDA_DBGDIR";
my $amandahomedir="$localstatedir/lib/amanda";
-my $datadir="$amandahomedir/template.d"; #rpm install template files here
-my $def_tapedev="file:/$amandahomedir/vtapes";
+my $templatedir="$amdatadir/template.d"; #rpm install template files here
+my $def_tapedev="file:$amandahomedir/vtapes";
-my $amanda_user="@CLIENT_LOGIN@";
-my $def_config="@DEFAULT_CONFIG@";
+my $amanda_user="$Amanda::Constants::CLIENT_LOGIN";
+my $def_config="$Amanda::Constants::DEFAULT_CONFIG";
my $def_dtimeout="1800";
my $def_ctimeout="30";
my $def_etimeout="300";
my $template_only=0;
my $parentdir;
my $host;
-# Get the version suffix.
-my $USE_VERSION_SUFFIXES = '@USE_VERSION_SUFFIXES@';
-my $suf = '';
-if ( $USE_VERSION_SUFFIXES =~ /^yes$/i ) {
- $suf='-@VERSION@';
-}
+my @pw = getpwuid($<);
+my $dumpuser = $pw[0];
#usage
sub usage {
- print "$0 $suf\n";
+ print "$0\n";
print "\t\t <config> [--template <template>]\n";
print "\t\t[--no-vtape] (do not create virtual tapes)\n";
print "\t\t[--tapetype <tapetype>] [--tpchanger <tpchanger>]\n";
}
-sub is_user_right {
- my $user = `whoami`;
- chomp($user);
- ( $user eq $amanda_user ) ||
- die ("ERROR: $0 must be run by $amanda_user\n", 0);
-}
-
-
# rpm installation should have taken care of these. Create one if it's not there
sub check_gnutarlist_dir {
if ( -e "$amandahomedir/gnutar-lists" ) {
&mprint ("$amandahomedir/gnutar-lists directory exists\n");
}
else {
- mkdir ("$amandahomedir/gnutar-lists", $def_perm) ||
- &log_and_die ("ERROR: mkdir:$amandahomedir/gnutar-lists failed: $!\n", 0);
+ mkpath ("$amandahomedir/gnutar-lists", $def_perm) ||
+ &log_and_die ("ERROR: mkpath:$amandahomedir/gnutar-lists failed: $!\n", 0);
}
}
sub create_conf_dir {
- unless ( -e $confdir ) {
- &log_and_die ("ERROR: $confdir does not exist\n", 0);
- }
unless ( -e "$confdir/$config" ) {
- mkdir ("$confdir/$config", $def_perm) ||
- &log_and_die ("ERROR: mkdir: $confdir/$config failed: $!\n", 0); # $! = system error
+ mkpath ("$confdir/$config", $def_perm) ||
+ &log_and_die ("ERROR: mkpath: $confdir/$config failed: $!\n", 0); # $! = system error
} else {
&log_and_die ("ERROR: Configuration $config exists\n", 0);
}
unless ( -e "$confdir/template.d" ) {
- mkdir ("$confdir/template.d", $def_perm) ||
- &log_and_die ("ERROR: mkdir: $confdir/template.d failed: $!\n", 0);
+ mkpath ("$confdir/template.d", $def_perm) ||
+ &log_and_die ("ERROR: mkpath: $confdir/template.d failed: $!\n", 0);
&mprint ("$confdir/template.d directory created\n");
}
}
&log_and_die ("ERROR: template is missing\n", 1);
}
# create and update amanda.conf
- open(CONF, "$datadir/amanda-$tplate.conf")
- || &log_and_die ("ERROR: Cannot open $datadir/amanda-$tplate.conf: $!\n", 1);
+ open(CONF, "$templatedir/amanda-$tplate.conf")
+ || &log_and_die ("ERROR: Cannot open $templatedir/amanda-$tplate.conf: $!\n", 1);
open(NEWCONF, ">$confdir/$config/amanda.conf") ||
&log_and_die ("ERROR: Cannot create $confdir/$config/amanda.conf: $!\n", 1);
chmod ($amanda_conf_perm, "$confdir/$config/amanda.conf") ||
sub create_curinfo_index_dir {
- mkdir("$confdir/$config/curinfo", $def_perm) ||
- &log_and_die ("ERROR: mkdir: $confdir/$config/curinfo failed: $!\n", 1);
- mkdir("$confdir/$config/index", $def_perm) ||
- &log_and_die ("ERROR: mkdir: $confdir/$config/index failed: $!\n", 1);
+ mkpath("$confdir/$config/curinfo", $def_perm) ||
+ &log_and_die ("ERROR: mkpath: $confdir/$config/curinfo failed: $!\n", 1);
+ mkpath("$confdir/$config/index", $def_perm) ||
+ &log_and_die ("ERROR: mkpath: $confdir/$config/index failed: $!\n", 1);
&mprint ("curinfo and index directory created\n");
}
if (( $dfout[10] / $div ) > 1024000 ) { # holding disk is defined 1000 MB
&mprint ("creating holding disk directory\n");
unless ( -d "$amandahomedir/holdings" ) {
- mkdir ( "$amandahomedir/holdings", $def_perm) ||
- (&mprint ("WARNING: mkdir $amandahomedir/holdings failed: $!\n"), $holding_err++, return );
+ mkpath ( "$amandahomedir/holdings", $def_perm) ||
+ (&mprint ("WARNING: mkpath $amandahomedir/holdings failed: $!\n"), $holding_err++, return );
}
- mkdir ( "$amandahomedir/holdings/$config", $def_perm) ||
- (&mprint ("WARNING: mkdir $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ;
+ mkpath ( "$amandahomedir/holdings/$config", $def_perm) ||
+ (&mprint ("WARNING: mkpath $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ;
}
}
#create default tape dir
sub create_deftapedir{
unless ( -e "$amandahomedir/vtapes" ) {
- mkdir ( "$amandahomedir/vtapes", $def_perm) ||
- ( &mprint ("WARNING: mkdir $amandahomedir/$config/vtapes failed: $!\n"), return );
+ mkpath ( "$amandahomedir/vtapes", $def_perm) ||
+ ( &mprint ("WARNING: mkpath $amandahomedir/$config/vtapes failed: $!\n"), return );
}
unless ( -e "$amandahomedir/vtapes/$config" ) {
- mkdir ( "$amandahomedir/vtapes/$config", $def_perm) ||
- ( &mprint ("WARNING: mkdir $amandahomedir/vtapes/$config failed: $!\n"), return );
+ mkpath ( "$amandahomedir/vtapes/$config", $def_perm) ||
+ ( &mprint ("WARNING: mkpath $amandahomedir/vtapes/$config failed: $!\n"), return );
}
$parentdir="$amandahomedir/vtapes/$config";
}
if ($template_only==0){ # check $template mode
$mylabelprefix=$labelstr; #set labelstr
if ($tapedev eq "$def_tapedev/$config"){
- &create_deftapedir;
+ &create_deftapedir;
}
else {
- $tapedev=~/^(file:\/)/;
- $parentdir=$';
+ $parentdir=$tapedev;
}
}
else {
if (defined $tapecycle) {
$tapecycle=~/^\d+/;
$tp_cyclelimit=$&;
- # check space
- my $dfout =`df $parentdir`;
- my $mul=1024;
- @dfdata=split(" ",$dfout);
- unless ( $dfdata[1] eq "1K-blocks" ) {
- $mul=512; # 512-blocks displayed by df
- }
- if (($dfdata[10]*$mul) < (($tp_cyclelimit*73728)+10240)){
- &mprint ("WARNING: Not enough space for vtapes. Creation of vtapes failed\n");
+
+ # check space
+ my $fsinfo = Amanda::Util::get_fs_usage($parentdir);
+ my $avail_bytes = $fsinfo->{'blocksize'} * $fsinfo->{'bavail'};
+
+ # mysteriously, we need at least 72k per slot, plus 10k overhead. The
+ # origin of these numbers is unknown.
+ my $needed_bytes = (($tp_cyclelimit*73728)+10240);
+ if ($avail_bytes < $needed_bytes){
+ &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");
$vtape_err++;
return;
}
for $i (1..$tp_cyclelimit) {
unless ( -e "slot$i"){
- mkdir ("slot$i", $def_perm) ||
- ( &mprint ("WARNING: mkdir $parentdir/slot$i failed: $!\n"), $vtape_err++, return);
+ mkpath ("slot$i", $def_perm) ||
+ ( &mprint ("WARNING: mkpath $parentdir/slot$i failed: $!\n"), $vtape_err++, return);
}
( @amlabel_out = `$sbindir/amlabel -f $config $mylabelprefix-$i slot $i`) ||
- ( &mprint ("WARNING: amlabel vtapes failed at slot $i: $!\n"), $vtape_err++, return);
+ ( &mprint ("WARNING: amlabel vtapes failed at slot $i: $!\n"), $vtape_err++, return);
}
foreach (@amlabel_out) {
print LOG;
$labelstr="^$config-[0-9][0-9]*\$";
}
}
+
+ open (CONF, ">$confdir/$config/amanda.conf") ||
+ &log_and_die ("ERROR: Cannot create amanda.conf file: $!\n", 1);
+ chmod ($amanda_conf_perm, "$confdir/$config/amanda.conf") ||
+ &log_and_die ("ERROR: Cannot set amanda.conf file access permission: $!\n", 1);
+
+ print CONF "org \"$config\"\t\t# your organization name for reports\n";
+ print CONF "dumpuser \"$dumpuser\"\t# the user to run dumps under\n";
+ print CONF "mailto \"$mailto\"\t# space separated list of operators at your site\n";
+ print CONF "dumpcycle $dumpcycle\t\t# the number of days in the normal dump cycle\n";
+ print CONF "runspercycle $runspercycle\t\t# the number of amdump runs in dumpcycle days\n";
+ print CONF "tapecycle $tapecycle\t# the number of tapes in rotation\n";
+ print CONF "runtapes $runtapes\t\t# number of tapes to be used in a single run of amdump\n";
+
if ((!(defined($template)))||($template eq "harddisk"))
{
- if (defined $tapedev){
- $tapedev="file:/".$tapedev;
- }
- unless ( $tpchanger ) { $tpchanger="chg-disk"; }
- unless ( $tapedev ) { $tapedev="$def_tapedev/$config"; }
- unless ( $changerfile ) { $changerfile="$confdir/$config/changer.conf"; }
- unless ( $changerdev ) { $changerdev="/dev/null";}
+ print CONF "\n";
+ print CONF "define changer my_vtapes {\n";
+ print CONF " tpchanger \"chg-disk:$tapedev\"\n";
+ print CONF " property \"num-slot\" \"10\"\n";
+ print CONF " property \"auto-create-slot\" \"yes\"\n";
+ print CONF "}\n";
+ print CONF "tpchanger \"my_vtapes\"\n\n";
unless ( $tapetype ) { $tapetype="HARDDISK"; }
}
elsif ($template eq "single-tape")
{
- unless ($tpchanger) {$tpchanger="chg-manual";}
- unless ($tapedev) {$tapedev="/dev/nst0";}
- unless ($changerfile) {$changerfile="$confdir/$config/chg-manual.conf";}
- unless ($changerdev) {$changerdev="/dev/null";}
+ print CONF "\n";
+ print CONF "define changer my_single {\n";
+ print CONF " tpchanger \"chg-single:$tapedev\"\n";
+ print CONF "}\n";
+ print CONF "tpchanger \"my_single\"\n\n";
unless ($tapetype) {$tapetype="HP-DAT";}
}
elsif ($template eq "tape-changer")
{
- unless ($tpchanger){$tpchanger="chg-zd-mtx";}
- unless ($tapedev){ $tapedev="/dev/nst0";}
- unless ($changerfile){$changerfile="$confdir/$config/changer.conf";}
- unless ($changerdev) {$changerdev="/dev/sg1";}
+ print CONF "\n";
+ print CONF "define changer my_robot {\n";
+ print CONF " tpchanger \"chg-robot:$changerdev\"\n";
+ print CONF " property \"tape-device\" \"0=$tapedev\"\n";
+ print CONF "}\n";
+ print CONF "tpchanger \"my_robot\"\n\n";
unless ($tapetype) {$tapetype="HP-DAT";}
}
else # S3 case
{
- unless ($tpchanger){$tpchanger="chg-multi";}
- unless ($changerfile){$changerfile="$confdir/$config/changer.conf";}
+ print CONF "\n";
+ print CONF "define changer my_s3 {\n";
+ print CONF " tpchanger \"chg-multi:$tapedev\"\n";
+ print CONF " device-property \"S3_ACCESS_KEY\" \"\"\n";
+ print CONF " device-property \"S3_SECRET_KEY\" \"\"\n";
+ print CONF " device-property \"NB_THREADS_BACKUP\" \"\"\n";
+ print CONF "}\n";
+ print CONF "tpchanger \"my_s3\"\n\n";
unless ($tapetype) {$tapetype="HP-DAT";}
}
-
- open (CONF, ">$confdir/$config/amanda.conf") ||
- &log_and_die ("ERROR: Cannot create amanda.conf file: $!\n", 1);
- chmod ($amanda_conf_perm, "$confdir/$config/amanda.conf") ||
- &log_and_die ("ERROR: Cannot set amanda.conf file access permission: $!\n", 1);
-
- print CONF "org \"$config\"\t\t# your organization name for reports\n";
- print CONF "mailto \"$mailto\"\t# space separated list of operators at your site\n";
- print CONF "dumpcycle $dumpcycle\t\t# the number of days in the normal dump cycle\n";
- print CONF "runspercycle $runspercycle\t\t# the number of amdump runs in dumpcycle days\n";
- print CONF "tapecycle $tapecycle\t# the number of tapes in rotation\n";
- print CONF "runtapes $runtapes\t\t# number of tapes to be used in a single run of amdump\n";
- print CONF "tpchanger \"$tpchanger\"\t# the tape-changer glue script\n";
- print CONF "tapedev \"$tapedev\"\t# the no-rewind tape device\n";
- print CONF "changerfile \"$changerfile\"\t# tape changer configuration parameter file\n";
- print CONF "changerdev \"$changerdev\"\t# tape changer configuration parameter device\n";
print CONF "tapetype $tapetype\t# what kind of tape it is\n";
print CONF "labelstr \"$labelstr\"\t# label constraint regex: all tapes must match\n";
print CONF "dtimeout $def_dtimeout\t# number of idle seconds before a dump is aborted\n";
}
close NEWAUTH;
close PUB;
- &mprint("$amandahomedir/.ssh/client_authorized_keys created. Please append to /var/lib/amanda/.ssh/authorized_keys file on Amanda clients\n");
+ &mprint("$amandahomedir/.ssh/client_authorized_keys created. Please append to $amandahomedir/.ssh/authorized_keys file on Amanda clients\n");
}
}
}
if ( $template eq "single-tape" && !defined $changerfile && !defined $tpchanger)
{
my $my_changerfile="$confdir/$config/chg-manual.conf";
- copy("$datadir/chg-manual.conf", $my_changerfile) ||
- &mprint ("copy $datadir/chg-manual.conf to $my_changerfile failed: $!\n");
+ copy("$templatedir/chg-manual.conf", $my_changerfile) ||
+ &mprint ("copy $templatedir/chg-manual.conf to $my_changerfile failed: $!\n");
}
}
chomp($date);
my $logfile="$tmpdir/amserverconfig.$date.debug";
-&is_user_right;
+Amanda::Util::setup_application("amserverconfig", "server", $CONTEXT_CMDLINE);
+Amanda::Util::finish_setup($RUNNING_AS_ANY);
+
unless ( -e "$tmpdir" ) {
- mkdir ("$tmpdir", $def_perm) ||
- die ("ERROR: mkdir: $tmpdir failed: $!\n");
+ mkpath ("$tmpdir", $def_perm) ||
+ die ("ERROR: mkpath: $tmpdir failed: $!\n");
}
open (LOG, ">$logfile") || die ("ERROR: Cannot create logfile: $!\n");
}
-&create_conf_dir;
-
+my $need_changer = 0;
if ( defined $template ) {
-# validate user input to template
+
+ # validate user input to template
chomp($template);
my $found = 0;
- @valid_templates = ( "harddisk", "single-tape", "tape-changer", "s3");
+ @valid_templates = ( "harddisk", "single-tape", "tape-changer", "s3" );
foreach $elt (@valid_templates) {
- if ($elt eq lc($template)) {
- $found = 1;
- last;
- }
+ if ( $elt eq lc($template) ) {
+ $found = 1;
+ last;
+ }
+ }
+ unless ($found) {
+ print STDERR
+ "valid inputs to --templates are harddisk, single-tape, tape-changer or S3\n";
+ &usage;
+ exit 1;
+ }
+
+ # if tape-changer is chosen, check if mtx is installed
+ if ( $template eq "tape-changer" ) {
+ my $ok = 0;
+ for $dir ( "/usr/sbin", "/usr/local/sbin", "/usr/local/bin",
+ "/usr/bin", "/bin", "/opt/csw/sbin", split( ":", $oldPATH ) )
+ {
+ if ( -e "$dir/mtx" ) {
+ $ok = 1;
+ last;
+ }
+ }
+ unless ($ok) {
+ &mprint(
+ "ERROR: mtx binary not found, tape-changer template will not work and is not installed.\n"
+ );
+ &log_and_die(
+ "ERROR: Please install mtx and rerun the same command.\n",
+ 0 );
+ }
+ $need_changer = 1;
+ }
+ elsif ( $template eq "S3" ) {
+ $need_changer = 1;
}
- unless ( $found ) {
- print STDERR "valid inputs to --templates are harddisk, single-tape, tape-changer or S3\n";
- &usage;
- exit 1;
-}
-# if tape-changer is chosen, check if mtx is installed
- if ($template eq "tape-changer") {
- my $ok = 0;
- for $dir ("/usr/sbin", "/usr/local/sbin", "/usr/local/bin", "/usr/bin", "/bin","/opt/csw/sbin",split(":",$oldPATH)) {
- if ( -e "$dir/mtx" ) {
- $ok = 1;
- last;
- }
- }
- unless ($ok) {
- &mprint ("ERROR: mtx binary not found, tape-changer template will not work and is not installed.\n");
- &log_and_die ("ERROR: Please install mtx and rerun the same command.\n", 0);
- }
- unless ($changerfile) {$changerfile="$confdir/$config/changer.conf";}
- open (CCONF, ">$changerfile")
- || &log_and_die ("ERROR: Cannot create $changerfile: $!\n", 1);
- close (CCONF);
- } elsif ($template eq "S3" ) {
- unless ($changerfile) {$changerfile="$confdir/$config/changer.conf";}
- open (CCONF, ">$changerfile")
- || &log_and_die ("ERROR: Cannot create $changerfile: $!\n", 1);
- close (CCONF);
+}
- }
+&create_conf_dir;
+if ($need_changer) {
+ unless ($changerfile) {
+ $changerfile = "$confdir/$config/changer.conf";
+ }
+ open( CCONF, ">$changerfile" )
+ || &log_and_die( "ERROR: Cannot create $changerfile: $!\n", 1 );
+ close(CCONF);
}
&check_gnutarlist_dir;
my $ttype="$confdir/template.d/tapetypes";
unless ( -e $dtype ) {
- copy("$datadir/dumptypes", $dtype ) ||
+ copy("$templatedir/dumptypes", $dtype ) ||
&log_and_die ("ERROR: copy dumptypes failed: $!\n", 1);
}
unless ( -e $ttype ) {
- copy("$datadir/tapetypes", $ttype ) ||
+ copy("$templatedir/tapetypes", $ttype ) ||
&log_and_die ("ERROR: copy tapetypes file to $ttype failed: $!\n", 1);
}
# update $def_config value to the specified config value in advanced.conf
- open(ADV, "$datadir/advanced.conf") || &log_and_die ("ERROR: Cannot open advanced.conf file: $!\n", 1);
+ open(ADV, "$templatedir/advanced.conf") || &log_and_die ("ERROR: Cannot open advanced.conf file: $!\n", 1);
open(NEWADV, ">$confdir/$config/advanced.conf") ||
&log_and_die ("ERROR: Cannot create advanced.conf file: $!\n", 1);
while (<ADV>) {
# THE END
+Amanda::Util::finish_application();