Skip to content
Snippets Groups Projects
Commit 7e74c167 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

couchdb2: restore security infos

parent 517739f5
No related branches found
No related tags found
1 merge request!63couchdb2: restore security infos
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
# 2022-03-17 v1.4 WIP: add lines with prefix __DB__ # 2022-03-17 v1.4 WIP: add lines with prefix __DB__
# 2022-04-07 v1.5 check archive file, not only seq file # 2022-04-07 v1.5 check archive file, not only seq file
# 2022-04-14 v1.6 backup security infos (no restore yet) # 2022-04-14 v1.6 backup security infos (no restore yet)
# 2022-04-21 v1.7 restore security infos
# ================================================================================ # ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then if [ -z "$BACKUP_TARGETDIR" ]; then
...@@ -46,17 +47,22 @@ ARCHIVE_DIR=$(_j_getvar "${JOBFILE}" dir-dbarchive)/couchdb2 ...@@ -46,17 +47,22 @@ ARCHIVE_DIR=$(_j_getvar "${JOBFILE}" dir-dbarchive)/couchdb2
# make an couch api request # make an couch api request
# param string method ... one of GET|POST|DELETE # param string method ... one of GET|POST|DELETE
# param string relative url, i.e. _all_dbs or _stats # param string relative url, i.e. _all_dbs or _stats
# param string optional: data for POST|PUT requests
function _couchapi(){ function _couchapi(){
method=$1 local method=$1
apiurl=$2 local apiurl=$2
outfile=$3 # local outfile=$3
local data=$3
sParams= sParams=
# sParams="$sParams -u ${couchdbuser}:${couchdbpw}" # sParams="$sParams -u ${couchdbuser}:${couchdbpw}"
sParams="$sParams -X ${method}" sParams="$sParams -X ${method}"
sParams="$sParams ${COUCH_URL}${apiurl}" sParams="$sParams ${COUCH_URL}${apiurl}"
if [ ! -z "$outfile" ]; then # if [ ! -z "$outfile" ]; then
sParams="$sParams -o ${outfile}" # sParams="$sParams -o ${outfile}"
# fi
if [ -n "$data" ]; then
sParams="$sParams -d ${data}"
fi fi
curl $sParams 2>/dev/null curl $sParams 2>/dev/null
} }
...@@ -243,24 +249,52 @@ function _doBackupOfSingleInstance(){ ...@@ -243,24 +249,52 @@ function _doBackupOfSingleInstance(){
} }
# ---------- RESTORE # ---------- RESTORE
#
# example:
#
# (1)
# cd /var/iml-archive/couchdb2
# or
# cd /var/iml-backup/couchdb2
#
# (2)
# /opt/imlbackup/client/localdump.sh restore couchdb2 measured-preview-couchdbcluster/mydb.couchdbdump.gz axel-01
# ^ ^ ^ ^
# | | | |
# action: restore ---------------+ | | |
# database service: couchdb2 ------------+ | |
# filename with instance as relative path --------+ |
# optional: target database --------------------------------------------------------------------------+
#
# restore a single backup file; the instance and db name will be detected from file # restore a single backup file; the instance and db name will be detected from file
# param string filename of db dump (full path or relative to BACKUP_TARGETDIR) # param string filename of db dump (full path or relative to BACKUP_TARGETDIR)
# param string optional: target database; default: detect name from import database
function restoreByFile(){ function restoreByFile(){
sMyfile=$1 sMyfile=$1
sMyDb=$2 dbname=$2
bFastMode=1
bFastMode=0 # 0 = delete db first and import | 1 = create and import (on empty instance only)
echo echo
h2 "analyze dump $sMyfile" h2 "analyze dump $sMyfile"
COUCHDB_INSTANCE=`echo $sMyfile | sed "s#${BACKUP_TARGETDIR}##g" | sed "s#\./##g" | sed "s#^/##g" | cut -f 1 -d "/"` COUCHDB_INSTANCE=$(echo $sMyfile | sed "s#${BACKUP_TARGETDIR}##g" | sed "s#\./##g" | sed "s#^/##g" | cut -f 1 -d "/")
echo "detected COUCHDB_INSTANCE : [${COUCHDB_INSTANCE}]" echo "detected COUCHDB_INSTANCE : [${COUCHDB_INSTANCE}]"
if [ -z $sMyDb ]; then if [ -z "$COUCHDB_INSTANCE" ]; then
sMyDb=`guessDB $sMyfile` echo "ERROR: Name of the instance was not detected."
echo "detected db schema from file: [${sMyDb}]" echo " For couchdb restore you should cd to the ${BACKUP_TARGETDIR} or ${ARCHIVE_DIR}"
exit 1
fi
local _sourceDB="$( guessDB $sMyfile | sed 's#.couchdbdump.gz$##' )"
echo "detected source database : [${_sourceDB}]"
if [ -z "$dbname" ]; then
dbname="$_sourceDB"
echo "using the same as target : [${dbname}]"
else else
echo "db schema from param 2: [${sMyDb}]" echo "using db schema from param 2: [${dbname}]"
fi fi
echo echo
...@@ -284,37 +318,38 @@ function restoreByFile(){ ...@@ -284,37 +318,38 @@ function restoreByFile(){
echo echo
# _getDblist | grep "^${sMyDb}$" # _getDblist | grep "^${dbname}$"
# if [ $? -eq 0 ]; then # if [ $? -eq 0 ]; then
# echo DB exists ... need to drop it first # echo DB exists ... need to drop it first
# fi # fi
if [ $bFastMode -eq 0 ]; then if [ $bFastMode -eq 0 ]; then
h2 deleting database [$sMyDb] ... h2 deleting database [$dbname] ...
color cmd color cmd
_couchapi DELETE $sMyDb _couchapi DELETE $dbname
fetchrc fetchrc
color reset color reset
fi fi
h2 creating database [$sMyDb] ... h2 creating database [$dbname] ...
color cmd color cmd
_couchapi PUT $sMyDb _couchapi PUT $dbname
fetchrc fetchrc
color reset color reset
h2 import file ... h2 import file ...
color cmd color cmd
# zcat ${sMyfile} | python ${dirPythonPackages}/couchdb/tools/load.py $COUCH_URL/$sMyDb zcat ${sMyfile} | couchrestore --db $dbname
zcat ${sMyfile} | couchrestore --db $sMyDb
fetchrc fetchrc
color reset color reset
h2 add security infos ... h2 add security infos ...
SECURITYFILE=${ARCHIVE_DIR}/${COUCHDB_INSTANCE}/security/__security__$sMyDb.json # todo: this will fail when restoring from "deleted_databases" folder
SECURITYFILE=${ARCHIVE_DIR}/${COUCHDB_INSTANCE}/security/__security__${_sourceDB}.json
SECDATA="$( cat $SECURITYFILE )"
color cmd color cmd
cat "$SECURITYFILE" echo "add security data: $SECDATA"
echo "TODO: import this to $sMyDb." _couchapi PUT "${dbname}/_security" "$SECDATA"
fetchrc fetchrc
color reset color reset
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment