3 # Copyright (c) 2007-2012 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
26 use Socket; # for gethostbyname
28 my $confdir="@CONFIG_DIR@";
29 my $tmpdir="@AMANDA_DBGDIR@";
31 my $prefix="@prefix@";
32 my $localstatedir="@localstatedir@";
33 my $amandahomedir="$localstatedir/lib/amanda";
35 my $amanda_user="@CLIENT_LOGIN@";
36 my $amanda_group="disk";
37 my $def_root_user="root";
38 my $def_dumptype="user-tar";
44 print "\t\t--config <config> Required. Ex: DailySet1\n";
45 print "\t\t--client <FQDN-name> Required. Ex: server.zmanda.com\n";
46 print "\t\t--diskdev <directory> Required. Ex: /home\n";
47 print "\t\t--m Modify exisiting entry\n";
48 print "\t\t[--dumptype <dumptype> Default: user-tar]\n";
49 print "\t\t[--includefile <string> glob expression of file(s) to include]\n";
50 print "\t\t[--includelist <file> file contains glob expressions to include]\n";
51 print "\t\t[ specify either --includefile or --includelist ]\n";
52 print "\t\t[--excludefile <string> glob expression of file(s) to exclude]\n";
53 print "\t\t[--excludelist <file> file contains glob expressions to exclude]\n";
54 print "\t\t[ specify either --excludefile or --excludelist ]\n";
55 print "\t\t[--user <username> name of user running amrecover on the client]\n";
56 print "\t\t[--auth <string> authentication used when running amrecover]\n";
57 print "\t\t[--gnutar_list_dir <string> directory where gnutar keep its state file on the client]\n";
58 print "\t\t[--amandates <string> file where amanda keep the date of dumplevel on the client]\n";
59 print "\t\t[--batch batch mode used when copying file to client]\n";
60 print "\t\t[--no-client-update do not update files on the client";
61 print "\t\t[--help]\n";
65 for $fh ( STDOUT, LOG ) {
78 ( $user eq $amanda_user ) ||
79 die ("ERROR: $0 must be run by $amanda_user\n");
83 # alphabetics, numerics, and underscores,
84 # hyphen, at sign, dot and "/" are ok
87 if ( $arg =~ /^([-\@\/\w.]+)$/ ) {
90 return 1; # bad, tainted input
95 # modify existing entry.
96 # only got here if -m is used and entry is found.
98 my $open_seen=0; # '{' is seen
99 my $include_done=0; # original include line is parsed
100 my $exclude_done=0; # original exclude line is parsed
101 my $ok=0; # 1 if target entry is found
103 @ARGV = ("$confdir/$config/disklist");
104 $^I = ".tmp"; # allow inplace editing
106 my ($one, $two, $three ) = split(/\s+/, $_);
108 # if include or exclude is not previously there,
109 # take care of them here
112 if ( $include_done==0 && $ok ) {
113 print "include list \"$includelist\"\n" if ( $includelist );
114 print "include file \"$includefile\"\n" if ( $includefile );
116 if ( $exclude_done==0 && $ok ) {
117 print "exclude list \"$excludelist\"\n" if ( $excludelist );
118 print "exclude file \"$excludefile\"\n" if ( $excludefile );
120 $ok=0; # reset, done with one entry
123 # take care of entry that has '{'
124 if ( $open_seen==1 ) {
125 if ( !$two && !$three ) { # inside {, dumptype line has 1 field only
126 s/$one/$dumptype/ if ( $dumptype );
127 } elsif ( $two && $three ) { # inside {, include/exclude line
128 if ( $one eq "include" ) {
129 if ( $includelist ) {
130 s/$two.*$/list "$includelist"/;
131 } elsif ( $includefile ) {
132 s/$two.*$/file "$includefile"/;
136 if ( $one eq "exclude" ) {
137 if ( $excludelist ) {
138 s/$two.*$/list "$excludelist"/;
139 } elsif ( $excludefile ) {
140 s/$two.*$/file "$excludefile"/;
148 # entry which previously doesn't have include/exclude
149 if (( $one eq $client ) && ($two eq $diskdev) ) {
151 if ( $three && ($three ne "{") ) {
152 if ( $sp_diskfile==1 ) { #previously don't have include/exclude
153 $three = $dumptype if ( $dumptype );
154 $includeline="include list \"$includelist\"" if ( $includelist );
155 $includeline="include file \"$includefile\"" if ( $includefile );
156 $excludeline="exclude list \"$excludelist\"\n" if ( $excludelist );
157 $excludeline="exclude file \"$excludefile\"\n" if ( $excludefile );
158 s/$three/{\n$three\n$includeline\n$excludeline}/;
160 s/$three/$dumptype/ if ( $dumptype ); #easy one, just replace dumptype.
161 $ok=0; #done with one entry
169 unlink("$confdir/$config/disklist.tmp");
178 $ret= GetOptions ( "config=s"=>\$config,
179 "client=s"=>\$client,
180 "diskdev=s"=>\$diskdev,
181 "dumptype=s"=>\$dumptype,
182 "includefile=s"=>\$includefile,
183 "includelist=s"=>\$includelist,
184 "excludefile=s"=>\$excludefile,
185 "excludelist=s"=>\$excludelist,
186 "user=s"=>\$root_user,
188 "gnutar_list_dir=s"=>\$tarlist,
189 "amandates=s"=>\$amandates,
192 "no-client-update!"=>\$no_client_update,
208 unless (defined $config && defined $client && defined $diskdev ) {
209 print STDERR "--config, --client and --diskdev are required.\n";
214 die ("ERROR: Invalid data in config.\n") if is_tainted($config);
215 die ("ERROR: Invalid data in client.\n") if is_tainted($client);
219 if ( defined $includefile && defined $includelist ) {
220 print STDERR "Specify either --includefile or --includelist, not both.\n";
225 if ( defined $excludefile && defined $excludelist ) {
226 print STDERR "Specify either --excludefile or --excludelist, not both.\n";
231 $oldPATH = $ENV{'PATH'};
232 $ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/bin:/usr/ucb"; # force known path
233 $date=`date +%Y%m%d%H%M%S`;
235 my $logfile="$tmpdir/amaddclient.$date.debug";
238 open (LOG, ">$logfile") || die "ERROR: Cannot create logfile : $!\n";
239 print STDOUT "Logging to $logfile\n";
241 my $lhost=`hostname`;
243 # get our own canonical name, if possible (we don't sweat the IPv6 stuff here)
244 my $host=(gethostbyname($lhost))[0];
247 $host = $lhost; #gethostbyname() failed, go with hostname output
254 # make sure dumptype is defined in dumptypes or amanda.conf file
256 if ( defined $dumptype ) {
257 for $fhs ( "$confdir/template.d/dumptypes", "$confdir/$config/amanda.conf" ) {
258 open (DTYPE, $fhs) ||
259 &log_and_die ("ERROR: Cannot open $fhs file : $!\n");
261 if (/^\s*define\s*dumptype\s*$dumptype\s*{/) {
270 &log_and_die ("ERROR: $dumptype not defined in $confdir/template.d/dumptypes or $confdir/$config/amanda.conf\n");
274 # create disklist file
275 unless ( -e "$confdir/$config" ) {
276 &log_and_die ("ERROR: $confdir/$config not found\n");
279 if (defined $includefile || defined $includelist
280 || defined $excludefile || defined $excludelist) {
284 unless ( -e "$confdir/$config/disklist" ) { # create it if necessary
285 open (DLE, ">$confdir/$config/disklist") ||
286 &log_and_die ("ERROR: Cannot create $confdir/$config/disklist file : $!\n");
287 print DLE "#This file is generated by amaddclient.\n";
288 print DLE "#Don't edit it manually, otherwise, 'amaddclient -m ...' might not work\n";
291 open (DLE, "+<$confdir/$config/disklist") # open for read/write
292 || &log_and_die ("ERROR: Cannot open $confdir/$config/disklist file : $!\n");
294 my ($lclient, $ldiskdev, $dontcare ) = split(/\s+/, $_);
295 if (( $lclient eq $client ) && ($ldiskdev eq $diskdev) ) {
301 # if found and -m, do modification and exit
302 if ( defined $mod ) {
306 &log_and_die ("ERROR: $client $diskdev not found, cannot modify\n");
310 unless ( defined $dumptype ) {
311 $dumptype=$def_dumptype;
314 &mprint("$confdir/$config/disklist has '$client $diskdev ...' entry, file not updated\n"); }
316 print DLE "$client $diskdev ";
317 print DLE "{\n$dumptype\n" if ($sp_diskfile);
318 if ( defined $includefile ) {
319 print DLE "include file \"$includefile\"\n";
321 elsif ( defined $includelist ) {
322 print DLE "include list \"$includelist\"\n";
324 if ( defined $excludefile ) {
325 print DLE "exclude file \"$excludefile\"\n";
327 elsif ( defined $excludelist ) {
328 print DLE "exclude list \"$excludelist\"\n";
330 print DLE "}\n" if ($sp_diskfile);
332 print DLE " $dumptype\n" if ($sp_diskfile==0);
333 &mprint ("$confdir/$config/disklist updated\n");
338 # update .amandahosts on server and client
340 my $scp_opt1="-p"; # p: preserve mode
341 my $scp_opt2="-o ConnectTimeout=15"; #timeout after 15 seconds
343 my $ssh_opt="-x"; # -x as a placeholder, otherwise ssh complains
344 my $mkdir="mkdir -p";
345 my $client_conf_dir="$confdir/$config";
346 my $amanda_client_conf="$client_conf_dir/amanda-client.conf";
347 my $file="$amandahomedir/.amandahosts";
348 my $client_file="$amandahomedir/amanda-client.conf-$client";
350 if ( defined $batch ) {
352 $ssh_opt="-o BatchMode=yes";
355 &mprint ("updating $file on $host\n");
356 unless ( defined $root_user ) {
357 $root_user=$def_root_user;
360 open (HFILE, "+<$file")
361 || &log_and_die ("ERROR: Cannot open $file : $!\n");
364 if (/^\s*$client\s*$root_user\s*amindexd\s*amidxtaped\s/) {
370 &mprint ("$file contains $client $root_user, file not updated\n") ; }
372 print HFILE "$client $root_user amindexd amidxtaped\n";
376 # update client .amandahosts
377 unless ( $no_client_update ) {
379 &mprint ("Attempting to update $file on $client\n");
381 chdir ("$amandahomedir");
382 system "$scp", "$scp_opt1", "$scp_opt2", "$amanda_user\@$client:$file", "$file.tmp";
383 $exit_value = $? >> 8;
384 if ( $exit_value !=0 ) {
385 &mprint ("WARNING: $scp from $client not successful.\n");
386 &mprint ("Check $client:$file file.\n");
387 &mprint ("If entry '$host $amanda_user' is not present,\n");
388 &mprint ("append the entry to the file manually.\n");
392 unless ( -e "$file.tmp" ) {
393 &mprint ("WARNING: $file.tmp not found\n"); }
395 open (CFILE, "+<$file.tmp")
396 || &log_and_die ("ERROR: Cannot open $file.tmp file : $!\n");
398 if (/^\s*$host\s*$amanda_user\s*amdump\s/) {
404 &mprint ("$file contains $host $amanda_user, file not updated\n") ; }
406 print CFILE "$host $amanda_user amdump\n";
409 #make sure permission mode is correct
410 chmod (0600, "$file.tmp");
411 system "$scp", "$scp_opt1", "$scp_opt2", "$file.tmp", "$client:$file";
412 $exit_value = $? >> 8;
413 if ( $exit_value !=0 ) {
414 &mprint ("WARNING: $scp to $client not successful.\n");
415 &mprint ("Check $client:$file file.\n");
416 &mprint ("If entry '$host $amanda_user amdump' is not present,\n");
417 &mprint ("append the entry to the file manually.\n");
422 unlink ("$file.tmp") || &mprint("unlink $file.tmp failed: $!\n");
423 &mprint ("$client:$file updated successfully\n");
427 # done updating client .amandahosts
429 #create amanda-client.conf and scp over to client
431 unless ( $no_client_update ) {
432 &mprint ("Creating amanda-client.conf for $client\n");
434 $auth="bsdtcp" unless ( defined $auth );
436 open (ACFILE, ">$client_file") || &log_and_die ("ERROR: Cannot open $client_file file : $!\n");
437 print ACFILE "#amanda-client.conf - Amanda client configuration file.\n";
438 print ACFILE "conf \"$config\"\n";
439 print ACFILE "index_server \"$host\"\n";
440 print ACFILE "tape_server \"$host\"\n";
441 print ACFILE "# auth - authentication scheme to use between server and client.\n";
442 print ACFILE "# Valid values are 'bsdtcp' or 'ssh'\n";
443 print ACFILE "auth \"$auth\"\n";
444 print ACFILE "# ssh keys file if ssh auth is used\n";
445 print ACFILE "ssh_keys \"$amandahomedir/.ssh/id_rsa_amrecover\"\n";
446 print ACFILE "gnutar_list_dir \"$tarlist\"\n" if ( defined $tarlist );
447 print ACFILE "amandates \"$amandates\"\n" if ( defined $amandates );
450 &mprint ("Creating $client_conf_dir on $client\n");
451 system "$ssh", "$ssh_opt", "$amanda_user\@$client", "$mkdir", "$client_conf_dir";
452 $exit_value = $? >> 8;
453 if ( $exit_value !=0 ) {
454 &mprint ("WARNING: Cannot create $client_conf_dir on $client\n");
455 &mprint ("Please copy $client_file to $client manually\n");
457 chmod (0600, "$client_file");
458 system "$scp", "$scp_opt1", "$scp_opt2", "$client_file", "$amanda_user\@$client:$amanda_client_conf";
459 $exit_value = $? >> 8;
460 if ( $exit_value !=0 ) {
461 &mprint ("WARNING: Cannot copy $client_file to $client\n");
462 &mprint ("Please copy $client_file to $client:$client_conf_dir manually\n");
464 &mprint ("Copy $client_file to $client successfully\n");
465 unlink($client_file);
470 #create gnutar_list_dir
471 if ( defined $tarlist && !defined $no_client_update ) {
472 system "$ssh", "$ssh_opt", "$amanda_user\@$client", "$mkdir", "$gnutar_list_dir";
473 $exit_value = $? >> 8;
474 if ( $exit_value !=0 ) {
475 &mprint ("WARNING: Cannot create $gnutar_list_dir on $client\n");
476 &mprint ("Please create $gnutar_list_dir on $client manually\n");
478 &mprint ("$client_file created on $client successfully\n");
482 &mprint ("File /var/lib/amanda/example/xinetd.amandaclient contains the latest Amanda client daemon configuration.\n");
483 &mprint ("Please merge it to /etc/xinetd.d/amandaclient.\n");
485 $ENV{'PATH'} = $oldPATH;