diff --git a/plugins/localdump/couchdb2.sh b/plugins/localdump/couchdb2.sh index c4981395a4a5c1b6f26df1b18c8fd61d40b77fa9..ea4bd3f472e650e1b93b09f72fe7084f6d1ee0d5 100755 --- a/plugins/localdump/couchdb2.sh +++ b/plugins/localdump/couchdb2.sh @@ -19,6 +19,7 @@ # 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-14 v1.6 backup security infos (no restore yet) +# 2022-04-21 v1.7 restore security infos # ================================================================================ if [ -z "$BACKUP_TARGETDIR" ]; then @@ -46,17 +47,22 @@ ARCHIVE_DIR=$(_j_getvar "${JOBFILE}" dir-dbarchive)/couchdb2 # make an couch api request # param string method ... one of GET|POST|DELETE # param string relative url, i.e. _all_dbs or _stats +# param string optional: data for POST|PUT requests function _couchapi(){ - method=$1 - apiurl=$2 - outfile=$3 + local method=$1 + local apiurl=$2 + # local outfile=$3 + local data=$3 sParams= # sParams="$sParams -u ${couchdbuser}:${couchdbpw}" sParams="$sParams -X ${method}" sParams="$sParams ${COUCH_URL}${apiurl}" - if [ ! -z "$outfile" ]; then - sParams="$sParams -o ${outfile}" + # if [ ! -z "$outfile" ]; then + # sParams="$sParams -o ${outfile}" + # fi + if [ -n "$data" ]; then + sParams="$sParams -d ${data}" fi curl $sParams 2>/dev/null } @@ -243,24 +249,52 @@ function _doBackupOfSingleInstance(){ } # ---------- 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 # 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(){ sMyfile=$1 - sMyDb=$2 - bFastMode=1 + dbname=$2 + + bFastMode=0 # 0 = delete db first and import | 1 = create and import (on empty instance only) echo 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}]" - if [ -z $sMyDb ]; then - sMyDb=`guessDB $sMyfile` - echo "detected db schema from file: [${sMyDb}]" + if [ -z "$COUCHDB_INSTANCE" ]; then + echo "ERROR: Name of the instance was not detected." + 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 - echo "db schema from param 2: [${sMyDb}]" + echo "using db schema from param 2: [${dbname}]" fi echo @@ -284,37 +318,38 @@ function restoreByFile(){ echo - # _getDblist | grep "^${sMyDb}$" + # _getDblist | grep "^${dbname}$" # if [ $? -eq 0 ]; then # echo DB exists ... need to drop it first # fi if [ $bFastMode -eq 0 ]; then - h2 deleting database [$sMyDb] ... + h2 deleting database [$dbname] ... color cmd - _couchapi DELETE $sMyDb + _couchapi DELETE $dbname fetchrc color reset fi - h2 creating database [$sMyDb] ... + h2 creating database [$dbname] ... color cmd - _couchapi PUT $sMyDb + _couchapi PUT $dbname fetchrc color reset h2 import file ... color cmd - # zcat ${sMyfile} | python ${dirPythonPackages}/couchdb/tools/load.py $COUCH_URL/$sMyDb - zcat ${sMyfile} | couchrestore --db $sMyDb + zcat ${sMyfile} | couchrestore --db $dbname fetchrc color reset 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 - cat "$SECURITYFILE" - echo "TODO: import this to $sMyDb." + echo "add security data: $SECDATA" + _couchapi PUT "${dbname}/_security" "$SECDATA" fetchrc color reset