diff --git a/xCAT-client/sbin/dumpxCATdb b/xCAT-client/sbin/dumpxCATdb index 77e84847b..cd48525b2 100644 --- a/xCAT-client/sbin/dumpxCATdb +++ b/xCAT-client/sbin/dumpxCATdb @@ -33,6 +33,25 @@ use strict; my $rc = 0; my $cmd; &parse_args; +if ($::BINARY) { # not using xCAT to dump, using the database utility + my $DBname = xCAT::Utils->get_DBName; + if ($DBname eq "DB2") { + $rc=&DB2_bindump; + if ($rc == 0) { + xCAT::MsgUtils->message("I", "Backup Complete."); + } else { + xCAT::MsgUtils->message("I", "Backup Failed."); + } + exit $rc; + } else { + xCAT::MsgUtils->message("E", + "Binary dump (-b) is only supported for DB2"); + exit 1; + } +} + + +# Dump using xCAT utilities my @output = xCAT::Utils->runcmd("tabdump", 0); if ($::RUNCMD_RC != 0) { # error @@ -111,13 +130,14 @@ sub parse_args { my $msg; my $usagemsg = - " dumpxCATdb -h \n dumpxCATdb -v \n dumpxCATdb [-a] [-V] <-p> [path to dump directory]"; + " dumpxCATdb -h \n dumpxCATdb -v \n dumpxCATdb [-a] [-V] <-p> [path to backup directory] \n dumpxCATdb -b [-V] <-p> [path to backup directory]"; Getopt::Long::Configure("posix_default"); Getopt::Long::Configure("no_gnu_compat"); Getopt::Long::Configure("bundling"); if ( !GetOptions( 'a|all' => \$::ALL, + 'b|bin' => \$::BINARY, 'p|path=s' => \$::PATH, 'h|help' => \$::HELP, 'V|verbose' => \$::DUMPVERBOSE, @@ -160,3 +180,101 @@ sub parse_args } +#----------------------------------------------------------------------------- + +=head3 DB2_bindump + + Uses the DB2 Database supplied backup utility to backup the database + +=cut + +#----------------------------------------------------------------------------- +sub DB2_bindump +{ + my $msg; + my $rc=0; + # check to see if they are setup to do an online dump + # Database has to have defined logretain RECOVERY and there + # must already been taken an offline backup. + my $cmd="db2 get database configuration for xcatdb > /tmp/db2output"; + $rc = &rundb2cmd($cmd); # must su to xcatdb + if ($rc != 0) + { + xCAT::MsgUtils->message("E", " $cmd error."); + return 1; + } + + # check to see if they setup log recover + $cmd = " egrep -i \"Log retain for recovery enabled\" /tmp/db2output"; + my @output=xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("E", "Log retain for recovery enabled (LOGRETAIN) = RECOVERY must be set to perform ONLINE Backups and one ONLINE backup must have been taken. See xCAT DB2 documentation. The section - Backup/Restore the database with DB2 Commands."); + return 1; + } else { # check to see if LOGRETAIN is ON + if (!grep(/ = RECOVERY/, @output)) { + xCAT::MsgUtils->message("E", "Log retain for recovery enabled (LOGRETAIN) = RECOVERY must be set to perform ONLINE Backups and one ONLINE backup must have been taken. See xCAT DB2 documentation. The section - Backup/Restore the database with DB2 Commands."); + return 1; + } + } + + # check to see if they have one backup + $cmd = " ls $::PATH"; + @output=xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("E", "One ONLINE backup must have been taken and exist in $::PATH. See xCAT DB2 documentation. The section - Backup/Restore the database with DB2 Commands."); + return 1; + } else { # check to see if LOGRETAIN is ON + if (!grep(/XCATDB/, @output)) { + xCAT::MsgUtils->message("E", "One ONLINE backup must have been taken and exist in $::PATH. See xCAT DB2 documentation. The section - Backup/Restore the database with DB2 Commands."); + return 1; + } + } + + # finally all checks ok, can take an ONLINE backup + # get password from cfgloc + + my $cmd="cat /etc/xcat/cfgloc"; + my $info=xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("E", "Cannot read /etc/xcat/cfgloc."); + return 1; + } + chomp($info); + my ($db,$inst,$pw) = split(/\|/, $info); + + my $cmd=" db2 backup db xcatdb user xcatdb using $pw ONLINE to $::PATH"; + $rc = &rundb2cmd($cmd); # must su to xcatdb + if ($rc != 0) + { + xCAT::MsgUtils->message("E", " $cmd error."); + return 1; + } + return 0; + +} +#----------------------------------------------------------------------------- + +=head3 rundb2cmd + + + Run a commmand as the xcatdb instance id + Input: command + +=cut +#----------------------------------------------------------------------------- +sub rundb2cmd +{ + my $orgcmd = shift; + my $rc = 0; + my $cmd = "\'"; + $cmd .= $orgcmd; + $cmd .= ' 2>&1'; + $cmd .= "\'"; + system("su - xcatdb -c $cmd"); + if ($? > 0) # error + { + $rc = $? >> 8; + } + return ($rc); +} + diff --git a/xCAT-client/sbin/restorexCATdb b/xCAT-client/sbin/restorexCATdb index 88000abe6..89b45cb55 100644 --- a/xCAT-client/sbin/restorexCATdb +++ b/xCAT-client/sbin/restorexCATdb @@ -24,6 +24,7 @@ use strict; restorexCATdb -p + restorexCATdb -b -p =cut @@ -33,7 +34,22 @@ use strict; my $rc = 0; &parse_args; - +if ($::BINARY) { # not using xCAT to dump, using the database utility + my $DBname = xCAT::Utils->get_DBName; + if ($DBname eq "DB2") { + $rc=&DB2_binrestore; + if ($rc == 0) { + xCAT::MsgUtils->message("I", "Restore Complete. You can now restart the daemons."); + } else { + xCAT::MsgUtils->message("E", "Restore Failed."); + } + exit $rc; + } else { + xCAT::MsgUtils->message("E", + "Binary restore (-b) is only supported for DB2"); + exit 1; + } +} # read all the *.csv files from the input directory and restore the database opendir DIRPATH, $::PATH; if ($? != 0) @@ -120,14 +136,16 @@ sub parse_args { my $msg; my $usagemsg = - " restorexCATdb -h \n restorexCATdb -v \n restorexCATdb [-a] [-V] <-p> [path to restore .csv files]\n"; + " restorexCATdb -h \n restorexCATdb -v \n restorexCATdb [-a] [-V] <-p> [path to restore .csv files]\n restorexCATdb -b [-V] <-t> [timestamp on restore backup file] <-p> [path to restore backup file]"; Getopt::Long::Configure("posix_default"); Getopt::Long::Configure("no_gnu_compat"); Getopt::Long::Configure("bundling"); if ( !GetOptions( 'a|all' => \$::ALL, + 'b|binary' => \$::BINARY, 'p|path=s' => \$::PATH, + 't|timestamp=s' => \$::TIMESTAMP, 'h|help' => \$::HELP, 'V|verbose' => \$::VERBOSEREST, 'v|version' => \$::VERSION @@ -164,4 +182,85 @@ sub parse_args } } +#----------------------------------------------------------------------------- + +=head3 DB2_binrestore + + Uses the DB2 Database supplied restore utility to restore the database + +=cut + +#----------------------------------------------------------------------------- +sub DB2_binrestore +{ + my $msg; + my $rc=0; + if (!($::TIMESTAMP)){ + xCAT::MsgUtils->message("E", "To restore from a binary backup, the timestamp of the backup must be provided with the -t option."); + return 1; + } + # check to see if xcatd running. Cannot restore if it is + my $cmd = "ps -ef | grep xcatd"; + my @output=xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC == 0) { # found something + if (grep(/xcatd: SSL listener/, @output)) { # xcatd running + xCAT::MsgUtils->message("E", "xcatd is still accessing the database. All applications accessing the DB2 database must be stopped before you can restore."); + return 1; + + } + } + # get password from cfgloc + + my $cmd="cat /etc/xcat/cfgloc"; + my $info=xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("E", "Cannot read /etc/xcat/cfgloc."); + return 1; + } + chomp($info); + my ($db,$inst,$pw) = split(/\|/, $info); + + # Hopefully nothing else is accessing the database, so try restore + $cmd="db2 restore db xcatdb user xcatdb using $pw from $::PATH taken at $::TIMESTAMP"; + $rc = &rundb2cmd($cmd); # must su to xcatdb + if ($rc != 0) + { + xCAT::MsgUtils->message("E", " $cmd error."); + return 1; + } + # Now roll forward any tranactions since the backup + $cmd="db2 rollforward database xcatdb to end of logs and complete"; + $rc = &rundb2cmd($cmd); # must su to xcatdb + if ($rc != 0) + { + xCAT::MsgUtils->message("E", " $cmd error."); + return 1; + } + return 0; +} +#----------------------------------------------------------------------------- + +=head3 rundb2cmd + + + Run a commmand as the xcatdb instance id + Input: command + +=cut +#----------------------------------------------------------------------------- +sub rundb2cmd +{ + my $orgcmd = shift; + my $rc = 0; + my $cmd = "\'"; + $cmd .= $orgcmd; + $cmd .= ' 2>&1'; + $cmd .= "\'"; + system("su - xcatdb -c $cmd"); + if ($? > 0) # error + { + $rc = $? >> 8; + } + return ($rc); +}