From f66405ce90475f24d3ae355821abdef3da599d44 Mon Sep 17 00:00:00 2001
From: lissav <lissav@8638fb3e-16cb-4fca-ae20-7b5d299a9bcd>
Date: Fri, 1 Jun 2012 15:17:08 +0000
Subject: [PATCH]  binary restore for PostgreSQL

git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@12993 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
---
 xCAT-client/sbin/restorexCATdb | 73 ++++++++++++++++++++++++++++++++--
 1 file changed, 69 insertions(+), 4 deletions(-)

diff --git a/xCAT-client/sbin/restorexCATdb b/xCAT-client/sbin/restorexCATdb
index 89b45cb55..cb4b13cad 100644
--- a/xCAT-client/sbin/restorexCATdb
+++ b/xCAT-client/sbin/restorexCATdb
@@ -44,10 +44,20 @@ if ($::BINARY) {  # not using xCAT to dump, using the database utility
        xCAT::MsgUtils->message("E", "Restore Failed.");
      }
      exit $rc;
-   }  else {
-    xCAT::MsgUtils->message("E",
-                            "Binary restore (-b) is only supported for DB2");
-    exit 1;
+   } else {
+     if ($DBname eq "PG") {
+       $rc=&PG_binrestore;
+       if ($rc == 0) {
+         xCAT::MsgUtils->message("I", "Restore Complete.");
+       } else {
+         xCAT::MsgUtils->message("I", "Restore Failed.");
+       }
+       exit $rc;
+      }  else {
+        xCAT::MsgUtils->message("E",
+                            "Binary dump (-b) is not supported for $DBname");
+        exit 1;
+     }
    }
 }
 # read all the *.csv files from the input directory and restore the database
@@ -240,6 +250,61 @@ sub DB2_binrestore
 }
 #-----------------------------------------------------------------------------
 
+=head3 PG_binrestore 
+  
+    Uses the PostgreSQL Database supplied backup utility to restore the database
+
+=cut
+
+#-----------------------------------------------------------------------------
+sub PG_binrestore 
+{
+    my $msg;
+    my $rc=0;
+    my $pgcmddir               = "/usr/bin";      
+    # get path to Postgresql commands if running 9.X version
+    my $cmd = "rpm -qa | grep postgresql";
+    my @output=xCAT::Utils->runcmd($cmd, 0);
+    if ($::RUNCMD_RC != 0)
+    {
+      my $message =
+      "\nPostgreSQL is not installed.  If on AIX, it should be first obtained from the xcat dependency tarballs and installed before running this command.\n If on Linux, install from the OS CDs.";
+    xCAT::MsgUtils->message("E", " $cmd failed. $message");
+    exit(1);
+    }
+    # check if 9.X release,  setup different
+    if  (grep(/postgresql9/, @output)) {  # postgresql 9.x
+       # figure out which 9.x release  and build path
+       # for example 9.x release /usr/pgsql-9.x/bin
+       my @parseout= split(/\-/, $output[0]);
+       my @ptflevel= split ("postgresql9",$parseout[0]);
+       my $postgres9=@ptflevel[1];  # set it to the PTF level
+       $pgcmddir        = "/usr/pgsql-9.$postgres9/bin"; # pg cmds location
+    }
+    # Get database, admin from cfgloc file
+    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,$admin,$pw) = split(/\|/, $info); 
+    my ($info1,$info2) = split(/=/,$db);
+    my ($dbname,$host) = split(/;/,$info2);
+    
+    # restore from backup file
+    my $cmd="$pgcmddir/pg_restore  $::PATH -U postgres -d $dbname -c ";
+    my $info=xCAT::Utils->runcmd($cmd, -1);
+    if ($::RUNCMD_RC != 0) {
+      xCAT::MsgUtils->message("E", "$cmd failed"); 
+      return 1;
+    }
+    return 0;
+   
+}
+#-----------------------------------------------------------------------------
+
 =head3   rundb2cmd