Skip to content
Snippets Groups Projects
Select Git revision
  • acc2ef98b0deea674ef9717f916335219f6e8948
  • master default protected
  • 7771-harden-postgres-backup
  • pgsql-dump-with-snapshots
  • update-colors
  • update-docs-css
  • usb-repair-stick
  • desktop-notification
  • 7000-corrections
  • db-detector
10 results

couchdb2_restore_deleted_db.sh

Blame
  • user avatar
    Hahn Axel (hahn) authored
    fab34bcb
    History
    couchdb2_restore_deleted_db.sh 4.80 KiB
    #!/bin/bash
    # ======================================================================
    #
    # HELPER :: COUCHDB 2
    #
    # Move files of a deleted database that it can be restored
    #
    # SYNTAX:
    # ./couchdb2_restore_deleted_db.sh [INSTANCE] [FILEFILTER]
    #
    # If no param is given you will be prompted
    #
    # EXAMPLE
    # ./couchdb2_restore_deleted_db.sh demo-couchdbcluster "part-of-dbname"
    #
    # ----------------------------------------------------------------------
    # 2022-04-22  v0.1  <axel.hahn@iml.unibe.ch>  first lines
    # ======================================================================
    
    
    cd "$( dirname $0 )/.." || exit 1
    
      . $(dirname $0)/jobhelper.sh
      . $(dirname $0)/inc_bash.sh
    
    BACKUP_BASEDIR=$( _j_getvar "${JOBFILE}" "dir-localdumps" )/couchdb2
    ARCHIVE_DIR=$(    _j_getvar "${JOBFILE}" "dir-dbarchive"  )/couchdb2
    
    DELETED_DIR=
    COUCHDB_INSTANCE=
    DBDUMP=
    
    # ------------------------------------------------------------
    
    # list deleted database dumps
    # an instance must be set before
    # param  regex  filter to output of ls -1
    function ch.listdbs(){
            ls -1 $DELETED_DIR | grep "$1"
    }
    
    # list couchdb instances
    function ch.listinstances(){
            ls -1 $BACKUP_BASEDIR
    }
    
    # select a database dump file
    # param  regex  filter on list of dump files
    function ch.setdb(){
            local _list
            typeset -i _count
    
            _list=$( ch.listdbs "$1" )
            _count=$( echo "$_list" | wc -l )
    
            if [ $_count -eq 1 ]; then
                    DBDUMP=$_list
            else
                    cecho head "----- databases: $_count"
                    echo "$_list"
                    color input
                    echo -n "database dump > "
                    color reset
                    read DBDUMP
            fi
    
            if [ ! -f "$DELETED_DIR/$DBDUMP" ]; then
                    cecho error "ERROR: wrong input for db dump"
                    cecho error "File [$DELETED_DIR/$DBDUMP] does not exist."
                    exit 1
            fi
    
            echo "INFO: dumpfile $DBDUMP was selected"
    }
    
    # set an instance
    # param  string  name of an instance; if none is given comes an interactive mode
    function ch.setinstance(){
            local _list
            typeset -i _count
    
            if [ -z "$1" ]; then
                    _list=$( ch.listinstances "$1" )
                    _count=$( echo "$_list" | wc -l )
                    cecho head "----- instances: $_count"
                    echo "$_list"
                    color input
                    echo -n "Instance: > "
                    color reset
                    read COUCHDB_INSTANCE
            else
                    COUCHDB_INSTANCE="$1"
            fi
            DELETED_DIR="$ARCHIVE_DIR/$COUCHDB_INSTANCE/deleted_databases"
            if [ ! -d "$DELETED_DIR" ]; then
                    cecho error "ERROR: invalid instance name."
                    cecho error "[$DELETED_DIR] does not exist"
                    exit
            fi
    
            echo "INFO: [$DELETED_DIR] exists"
            echo "INFO: instance [$COUCHDB_INSTANCE] was set."
    }
    
    # ------------------------------------------------------------
    
    # move data of a backup
    function ch.movedata(){
    
            local DBNAME="$( echo $DBDUMP | sed 's#__[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9].*##g' | sed 's#.couchdbdump.gz$##' )"
            local SEQFILE=${DELETED_DIR}/seq/__seq__${DBNAME}
            local SECURITYFILE=${DELETED_DIR}/security/__security__${DBNAME}.json
            local T_DBDUMP=$BACKUP_BASEDIR/$COUCHDB_INSTANCE/${DBNAME}.couchdbdump.gz
            local T_SEQFILE=$ARCHIVE_DIR/$COUCHDB_INSTANCE/seq/__seq__${DBNAME}
            local T_SECURITYFILE=$ARCHIVE_DIR/$COUCHDB_INSTANCE/security/__security__${DBNAME}.json
    
            echo "INFO: database name [$DBNAME]"
            echo "INFO:
    
            DUMP: $DELETED_DIR/$DBDUMP
            ----> $T_DBDUMP
    
            SEQ : $SEQFILE
            ----> $T_SEQFILE
    
            SEC : $SECURITYFILE
            ----> $T_SECURITYFILE
    
    "
            color input
            echo -n "RETURN to move backup data; Ctrl + C to abort > "
            color reset
            read dummy
    
            echo "Moving files ..."
            mv "$DELETED_DIR/$DBDUMP" "$T_DBDUMP" || exit 1
            ls -l "$T_DBDUMP"
    
            if [ -f "$SEQFILE" ]; then
                    mv "$SEQFILE" "$T_SEQFILE" || exit 2
                    ls -l "$T_SEQFILE"
            else
                    echo "SKIP: seq file does not exist $SEQFILE"
            fi
            if [ -f "$SECURITYFILE" ]; then
                    mv "$SECURITYFILE" "$T_SECURITYFILE" || exit 2
                    ls -l "$T_SECURITYFILE"
            else
                    echo "SKIP: security file does not exist $SECURITYFILE"
            fi
    
            echo "------ DONE!"
            # ls -l "$DBDUMP" "$SEQFILE" "$SECURITYFILE"
    
    }
    
    # ------------------------------------------------------------
    
    echo 
    echo ":: HELPER :: Couchdb2 - move files of a deleted database "
    echo
    
    ch.setinstance "$1"
    shift 1
    
    while [ $# -gt 0 ]; do
        ch.setdb "$1"
        ch.movedata
        sleep 3
        shift 1
    done
    
    echo "$0 finished."
    
    # ------------------------------------------------------------