📄 amserverconfig.pl
字号:
#!@PERL@## Copyright (c) 2006 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# by the Free Software Foundation.## This program is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License# for more details.## You should have received a copy of the GNU General Public License along# 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#use Getopt::Long;use Time::Local;use File::Copy;use Socket; # for gethostbynamemy $confdir="@CONFIG_DIR@";my $prefix="@prefix@";my $tmpdir="@AMANDA_DBGDIR@";$prefix=$prefix; # avoid warnings about possible typomy $exec_prefix="@exec_prefix@";$exec_prefix=$exec_prefix; # dittomy $sbindir="@sbindir@";my $localstatedir="@localstatedir@";my $amandahomedir="$localstatedir/lib/amanda";my $datadir="$amandahomedir/template.d"; #rpm install template files heremy $def_tapedev="file:/$amandahomedir/vtapes";my $amanda_user="@CLIENT_LOGIN@";my $def_config="@DEFAULT_CONFIG@";my $def_dtimeout="1800";my $def_ctimeout="30";my $def_etimeout="300";my $def_perm=0750;my $amanda_conf_perm=0600;my $def_tapecycle=10;my $config;my $vtape_err=0;my $holding_err=0;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@';}#usagesub usage { print "$0 $suf\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"; print "\t\t[--tapedev <tapedev>] [--changerfile <changerfile>]\n"; print "\t\t[--changerdev <changerdev>] [--labelstr <labelstr>] \n"; print "\t\t[--mailto <mailto>] [--dumpcycle <dumpcycle> (ex: 5days, 1week or 2weeks)]\n"; print "\t\t[--runspercycle <runspercycle>] [--runtapes <runtapes>]\n"; print "\t\t[--tapecycle <tapecycle>]\n"; print "\t\t[--help]\n";}#print and logsub mprint { for $fh ( STDOUT, LOG ) { print $fh @_; }}sub log_and_die { my ($err, $cleanup) = @_; print LOG $err; # clean up $config directory if cleanup=1 # if error in creating vtape or holding disk, # advise user to create manually, no need to cleanup if ( $cleanup && defined $config && -e "$confdir/$config" ) { print LOG "cleaning up $confdir/$config\n"; if ( -e "$confdir/$config/amanda.conf" ) { unlink "$confdir/$config/amanda.conf" || print LOG "unlink $confdir/$config/amanda.conf failed: $!\n"; } if ( -e "$confdir/$config/advanced.conf" ) { unlink "$confdir/$config/advanced.conf" || print LOG "unlink $confdir/$config/advanced.conf failed: $!\n"; } if ( -e "$confdir/$config/tapelist" ) { unlink "$confdir/$config/tapelist" || print LOG "unlink $confdir/$config/tapelist failed: $!\n"; } if ( -e "$confdir/$config/curinfo" ) { rmdir "$confdir/$config/curinfo" || print LOG "rmdir $confdir/$config failed: $!\n"; } if ( -e "$confdir/$config/index" ) { rmdir "$confdir/$config/index" || print LOG "rmdir $confdir/$config/index failed: $!\n"; } rmdir "$confdir/$config" || print LOG "rmdir $confdir/$config failed: $!\n"; } die $err;}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 theresub 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); }}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 } 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); &mprint ("$confdir/template.d directory created\n"); }}sub copy_template_file { my $tplate = $_[0]; unless ($tplate) { &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(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") || &log_and_die ("ERROR: Cannot set amanda.conf file access permission: $!\n", 1); while (<CONF>) { $_ =~ s/$def_config/$config/; print NEWCONF $_; } close(CONF); close(NEWCONF); &mprint ("$confdir/$config/amanda.conf created and updated\n");}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); &mprint ("curinfo and index directory created\n");}sub touch_list_files { open (TLIST, ">$confdir/$config/tapelist") || &log_and_die ("ERROR: Cannot create tapelist file: $!\n", 1); close (TLIST); &mprint ("tapelist file created\n"); open (DLIST, ">$confdir/$config/disklist") || &log_and_die ("ERROR: Cannot create disklist file: $!\n", 1); close (DLIST); &mprint ("disklist file created\n");}# create holding disk directory, check disk space firstsub create_holding { if ( -d "$amandahomedir/holdings/$config" ) { my $uid = (stat("$amandahomedir/holdings/$config"))[4]; my $owner = (getpwuid($uid))[0]; unless ( $owner eq $amanda_user ) { &mprint ("WARNING: holding disk directory exists and is not owned by $amanda_user\n"); $holding_err++; } return; } my $div=1; my $out = `df -k $amandahomedir`; my @dfout = split(" " , $out); unless ( $#dfout == 12 ) { # df should output 12 elem &mprint ("WARNING: df failed, holding disk directory not created\n"); $holding_err++; return; } unless (( $dfout[1] eq "1K-blocks" ) || ( $dfout[1] eq "kbytes")) { $div=2; # 512-blocks displayed by df } 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 ); } mkdir ( "$amandahomedir/holdings/$config", $def_perm) || (&mprint ("WARNING: mkdir $amandahomedir/holdings/$config failed: $!\n"), $holding_err++, return) ; }}#create default tape dirsub create_deftapedir{ unless ( -e "$amandahomedir/vtapes" ) { mkdir ( "$amandahomedir/vtapes", $def_perm) || ( &mprint ("WARNING: mkdir $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 ); } $parentdir="$amandahomedir/vtapes/$config";}# create and label vtapesub create_vtape { &mprint ("creating vtape directory\n"); if ($template_only==0){ # check $template mode $mylabelprefix=$labelstr; #set labelstr if ($tapedev eq "$def_tapedev/$config"){ &create_deftapedir; } else { $tapedev=~/^(file:\/)/; $parentdir=$'; } } else { $mylabelprefix=$config; &create_deftapedir; } unless ( -e $parentdir){ &mprint ("WARNING: tapedev $parentdir does not exists, vtapes creation failed!\n"); &mprint ("Please create $parentdir and $confdir/$config and rerun the same command or else create vtapes manually.\n"); $vtape_err++; return; } chdir ("$parentdir") || ( &mprint("WARNING: chdir $parentdir failed: $!\n"), $vtape_err++, return ); my $i; &mprint ("amlabel vtapes\n"); 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"); $vtape_err++; return; } } else { $tp_cyclelimit=$def_tapecycle; } 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); } ( @amlabel_out = `$sbindir/amlabel -f $config $mylabelprefix-$i slot $i`) || ( &mprint ("WARNING: amlabel vtapes failed at slot $i: $!\n"), $vtape_err++, return); } foreach (@amlabel_out) { print LOG; } # reset tape to the first slot `$sbindir/amtape $config reset`;}sub create_customconf{ # now create a custom amanda.conf from user input unless ( $mailto ) { $mailto="$amanda_user"; } else { # untaint mailto which can be evil # reject mailto with the following * ( ) < > [ ] , ; : ! $ \ / " if ( $mailto =~ /^([^\*\(\)<>\[\]\,\;\:\!\$\\\/\"]+)$/ ) { $mailto = $1; # now untainted } else { &log_and_die ("ERROR: Invalid data in mailto.\n"); # log this somewhere } } unless ( $dumpcycle ) { $dumpcycle="1 week"; } unless ( $runspercycle ) { $runspercycle="5"; } unless ( $tapecycle ) { $tapecycle="10 tapes"; } unless ( $runtapes ) { $runtapes="1"; } unless ( $labelstr ) { if ($template eq "harddisk") { $labelstr="$config"; } else { $labelstr="^$config-[0-9][0-9]*\$"; } } if ((!(defined($template)))||($template eq "harddisk"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -