From a1145184a26977f8368c138639bb6d2d6c6a515b Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 23 Aug 2011 07:29:08 +0000
Subject: [PATCH] LMS-2471 improved create-snapshot.sh, counterpart
 restore-from-snapshot.sh added

SVN: 22584
---
 openbis_all/source/bash/create-snapshot.sh    | 65 ++++++++------
 .../source/bash/restore-from-snapshot.sh      | 90 +++++++++++++++++++
 2 files changed, 129 insertions(+), 26 deletions(-)
 create mode 100755 openbis_all/source/bash/restore-from-snapshot.sh

diff --git a/openbis_all/source/bash/create-snapshot.sh b/openbis_all/source/bash/create-snapshot.sh
index 3bd35000b20..feed5a6095e 100755
--- a/openbis_all/source/bash/create-snapshot.sh
+++ b/openbis_all/source/bash/create-snapshot.sh
@@ -4,7 +4,7 @@
 # 
 # usage: create-snapshot.sh <configuration file>
 # 
-# The configuration files knows the following key-value pairs (stored as <key> = <value>):
+# The configuration file has to have the following key-value pairs (stored as <key> = <value>):
 # 
 # repository			Path to the directory which will store the snapshot.
 # servers					Path to to a directory which contains the folders 'datastore_server'
@@ -14,7 +14,6 @@
 # databases				Space separated list of database to be dumped.
 # index						Relative path to the lucene index. 
 # 								It is relative to <servers path>/openBIS-server/jetty/. 
-# 								If this optional attribute is missing no lucene index will added to the snapshot. 
 # 
 # Important Notes: 
 # - This script should be run after all servers have been stopped.
@@ -31,6 +30,10 @@ if [ $# -ne 1 ]; then
     exit 1
 fi
 
+##################################################
+#
+# Gathering parameters
+#
 CONFIGURATION_FILE="$1"
 
 SERVERS_PATH=`getValue $CONFIGURATION_FILE servers`
@@ -64,30 +67,42 @@ if [ -z "$DATABASES" ]; then
     echo "At least one database has to be specified in $CONFIGURATION_FILE."
     exit 1
 fi
-INDEX=`getValue $CONFIGURATION_FILE index`
-if [ -n "$INDEX" ]; then
-    if [ ! -d "$INDEX" ]; then
-        mkdir -p "$INDEX"
-    fi
+INDEX="$OPENBIS_AS_ROOT"`getValue $CONFIGURATION_FILE index`
+if [ -z "$INDEX" ]; then
+    echo "Index not specified in $CONFIGURATION_FILE."
 fi
+mkdir -p "$INDEX"
 TIMESTAMP=`date +%Y-%m-%d_%H:%M:%S`
-SNAPSHOT_FOLDER="openbis-snapshot-$TIMESTAMP"
-SNAPSHOT="$REPOSITORY/$SNAPSHOT_FOLDER"
+SNAPSHOT_FOLDER_NAME="openbis-snapshot-$TIMESTAMP"
+SNAPSHOT="$REPOSITORY/$SNAPSHOT_FOLDER_NAME"
 
 ##################################################
 #
-# Starting snapshot creation
+# Creating snapshot
 #
-echo "==== creating snapshot $SNAPSHOT.tgz"
+echo "==== Creating snapshot $SNAPSHOT.tgz"
 
 mkdir "$SNAPSHOT"
-cp -p "$CONFIGURATION_FILE" "$SNAPSHOT"
+cp -p "$CONFIGURATION_FILE" "$SNAPSHOT/snapshot.config"
 ############## dump the store ##############
-tar -cf "$SNAPSHOT/store.tar" -C "$STORE" .
-if [ $? -ne 0 ]; then
-    echo "Error creating store dump. Snapshot creation aborted."
-    exit 1
-fi
+for path in "$STORE"/*; do
+    index_of_last_slash=`expr $path : '.*/'`
+    file_name=${path:$index_of_last_slash}
+    if [ `expr $file_name : '[0-9]*'` -ne 0 ]; then 
+        if [ -h "$path" ]; then
+            echo "Share $file_name is not dumped because it is a symbolic link."
+        else
+            echo "Start dumping share $file_name."
+            parent_folder=${path:0:$index_of_last_slash}
+            tar -rf "$SNAPSHOT/store.tar" -C "$parent_folder" $file_name 
+            if [ $? -ne 0 ]; then
+                echo "Error while dumping share $file_name. Snapshot creation aborted."
+                exit 1
+            fi
+            echo "Share $file_name sucessfully dumped."
+        fi
+    fi
+done
 echo "Dump of store $STORE has been successfully created."
 ############## dump databases ##############
 for db in $DATABASES; do
@@ -98,17 +113,15 @@ for db in $DATABASES; do
     fi
     echo "Database '$db' has been successfully dumped."
 done
-############## dump index (if requested) ##############
-if [ -n "$INDEX" ]; then
-    tar -cf "$SNAPSHOT/index.tar" -C "$INDEX" .
-    if [ $? -ne 0 ]; then
-        echo "Error creating index dump. Snapshot creation aborted."
-        exit 1
-    fi
-    echo "Dump of index $INDEX has been successfully created."
+############## dump index ##############
+tar -cf "$SNAPSHOT/index.tar" -C "$INDEX" .
+if [ $? -ne 0 ]; then
+    echo "Error creating index dump. Snapshot creation aborted."
+    exit 1
 fi
+echo "Dump of index $INDEX has been successfully created."
 ############## packaging ##############
-tar -zcf "$SNAPSHOT.tgz" -C "$REPOSITORY" "$SNAPSHOT_FOLDER" 
+tar -zcf "$SNAPSHOT.tgz" -C "$REPOSITORY" "$SNAPSHOT_FOLDER_NAME" 
 if [ $? -ne 0 ]; then
     echo "Error packaging snapshot $SNAPSHOT."
     exit 1
diff --git a/openbis_all/source/bash/restore-from-snapshot.sh b/openbis_all/source/bash/restore-from-snapshot.sh
new file mode 100755
index 00000000000..8caed70e4de
--- /dev/null
+++ b/openbis_all/source/bash/restore-from-snapshot.sh
@@ -0,0 +1,90 @@
+#! /bin/bash
+# 
+# Restores store, databases and optionally lucene index of an openBIS instance from a snapshot 
+# created by create-snapshot.sh.
+# 
+# usage: restore-from-snapshot.sh <snapshot file>
+#
+function getValue {
+    file=$1
+    key=$2
+    awk -F ' *= *' -v key=$2 '{map[$1] = $2} END {print map[key]}' $file
+}
+
+function cleanUpAndExit {
+    rm -rf $TMPDIR
+    exit 1
+}
+
+if [ $# -ne 1 ]; then
+    echo "Usage: restore-from-snapshot.sh <snapshot file>"
+    exit 1
+fi
+
+##################################################
+#
+# Gathering parameters
+#
+SNAPSHOT_FILE=$1
+
+USER=$(whoami)
+TMPDIR=`mktemp -d /tmp/snapshot-XXXXX`
+echo "temp folder:$TMPDIR"
+tar -zxf  $SNAPSHOT_FILE -C $TMPDIR
+if [ $? -ne 0 ]; then
+    echo "Error: Couldn't unzip and untar $SNAPSHOT_FILE."
+    cleanUpAndExit
+fi
+
+for f in $TMPDIR/*; do SNAPSHOT_FILES="$f"; done
+SNAPSHOT_CONFIG_FILE=$SNAPSHOT_FILES/snapshot.config
+
+SERVERS_PATH=`getValue $SNAPSHOT_CONFIG_FILE servers`
+OPENBIS_AS_ROOT="$SERVERS_PATH/openBIS-server/jetty/"
+if [ ! -d "$OPENBIS_AS_ROOT" ]; then
+    echo "Error: $OPENBIS_AS_ROOT isn't a directory."
+    echo "Most probable reason: $SERVERS_PATH doesn't point to a valid openBIS instance."
+    cleanUpAndExit
+fi
+STORE=`getValue $SNAPSHOT_CONFIG_FILE store`
+DATABASES=`getValue $SNAPSHOT_CONFIG_FILE databases`
+INDEX="$OPENBIS_AS_ROOT"`getValue $SNAPSHOT_CONFIG_FILE index`
+
+##################################################
+#
+# Restoring from snapshot
+#
+echo "==== Restore from $SNAPSHOT_FILE"
+############## restore store ##############
+echo "Starting to restore the store $STORE."
+rm -rf "$STORE"
+mkdir -p "$STORE"
+tar -xf $SNAPSHOT_FILES/store.tar -C "$STORE"
+if [ $? -ne 0 ]; then
+    echo "Error: Couldn't restore store. Restoring aborted."
+    cleanUpAndExit
+fi
+echo "Store successfully restored."
+############## restore databases ##############
+for db in $DATABASES; do
+    psql -U postgres -q -c "drop database $db"
+    psql -U postgres -q -c "create database $db with owner $USER"
+    psql -U $USER -q -d $db -f $SNAPSHOT_FILES/$db.sql > /dev/null
+    if [ $? -ne 0 ]; then
+        echo "Error: Couldn't restore database '$db'."
+        cleanUpAndExit
+    fi
+    echo "Database '$db' has been successfully restored."
+done
+############## restore store ##############
+if [ -n "$INDEX" ]; then
+    tar -xf $SNAPSHOT_FILES/index.tar -C "$INDEX"
+    if [ $? -ne 0 ]; then
+        echo "Error: Couldn't restore index."
+        cleanUpAndExit
+    fi
+    echo "Index has been successfully restored."
+fi
+rm -rf $TMPDIR
+echo "==== Successfully restored from $SNAPSHOT_FILE"
+
-- 
GitLab