From 1fe60565c6ca8f57a5d81c14318380a6d6e6bdbf Mon Sep 17 00:00:00 2001
From: alvaro <alvaro@alia.(none)>
Date: Tue, 14 Feb 2012 00:03:03 -0800
Subject: [PATCH] Added restore capabilities

---
 utils/lodspk.sh          |  7 +++++-
 utils/modules/backup.sh  |  8 +++++--
 utils/modules/getvar.php |  5 ++++
 utils/modules/restore.sh | 49 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 66 insertions(+), 3 deletions(-)
 create mode 100644 utils/modules/getvar.php
 create mode 100755 utils/modules/restore.sh

diff --git a/utils/lodspk.sh b/utils/lodspk.sh
index 9571fbd3..b97a9876 100755
--- a/utils/lodspk.sh
+++ b/utils/lodspk.sh
@@ -9,7 +9,7 @@ if [[ $# -eq 0 || "$1" == "--help" ]]; then
 fi
 
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-operations=( create delete debug backup )
+operations=( create delete debug backup restore )
 currentOperation=
 
 if [[ ${operations[@]} =~ $1 ]]; then
@@ -25,6 +25,11 @@ if [[ $currentOperation == "backup" ]]; then
   $DIR/modules/backup.sh
 fi  
 
+## Restore
+if [[ $currentOperation == "restore" ]]; then
+  $DIR/modules/restore.sh
+fi  
+
 ## Create/delete
 if [[ $currentOperation == "create" ||  $currentOperation == "delete" ]]; then
   modules=( type service uri )
diff --git a/utils/modules/backup.sh b/utils/modules/backup.sh
index 396de766..7430c9af 100755
--- a/utils/modules/backup.sh
+++ b/utils/modules/backup.sh
@@ -1,8 +1,12 @@
 #!/bin/bash
 
+
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-BACKUPDIR=$HOME/lodspeakr_backup
+cd $DIR
+BASE=`php getvar.php basedir`
+NAME=`echo $BASE |sed -e 's/^http:\/\///g' -e 's/\/$//g' -e 's/\//_/g'`
 cd $DIR/../..
+BACKUPDIR=$HOME/lodspeakr_backup
 if [[ ! -d $BACKUPDIR ]]; then
   echo "WARNING: No $BACKUPDIR dir. Creating it." >&2
   mkdir $BACKUPDIR
@@ -13,7 +17,7 @@ if [[ ! -d $BACKUPDIR ]]; then
   exit 1
 fi
 
-tmpFile=lodspeakr_backup_`date +%Y%m%d%H%M%S`.tar.gz
+tmpFile=$NAME"-backup-"`date +%Y%m%d%H%M%S`.tar.gz
 
 tar -czf $tmpFile settings.inc.php models views
 
diff --git a/utils/modules/getvar.php b/utils/modules/getvar.php
new file mode 100644
index 00000000..ff5aeb6e
--- /dev/null
+++ b/utils/modules/getvar.php
@@ -0,0 +1,5 @@
+<?
+//error_reporting(E_ERROR);
+require_once('../../settings.inc.php');
+echo $conf[$argv[1]];
+?>
diff --git a/utils/modules/restore.sh b/utils/modules/restore.sh
new file mode 100755
index 00000000..cc330da8
--- /dev/null
+++ b/utils/modules/restore.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+cd $DIR
+BASE=`php getvar.php basedir`
+NAME=`echo $BASE |sed -e 's/^http:\/\///g' -e 's/\/$//g' -e 's/\//_/g'`
+cd $DIR/../..
+BACKUPDIR=$HOME/lodspeakr_backup
+if [[ ! -d $BACKUPDIR ]]; then
+  echo "ERROR: No $BACKUPDIR dir." >&2
+  exit 1
+fi
+
+LIST=( `ls $BACKUPDIR/$NAME-backup*` )
+CHOSEN=-1
+while [[ "$CHOSEN" -lt 0 || "$CHOSEN" -ge "${#LIST[@]}" ]] ;do
+j=0
+echo
+echo "Choose from the following available backups"
+  for i in ${LIST[@]}; do
+    echo "["$j"]" `basename $i`
+    let j=$j+1
+  done
+  echo -n "Select which backup to restore: "
+  read -u 1 CHOSEN
+done
+
+echo
+echo "*** ATTENTION ***"
+echo "Are you sure you want to restore this backup? This may lead to overrite settings.inc.php, models/ and views/"
+echo -n "(write 'yes' if you are sure): "
+confirm=no
+read confirm
+if [[ "$confirm" !=  "yes" ]];then
+  echo "Nothing done"
+  exit 0
+fi
+RESTORE=`basename ${LIST[$CHOSEN]}`
+echo "Restoring "$RESTORE"..."
+
+cp ${LIST[$CHOSEN]} .
+tar zxf $RESTORE
+if [[ $? -eq 0 ]];then
+  echo Restore successful
+else
+  echo A problem occurred in the restoring process
+fi
+
-- 
GitLab