From 8c5db0f1f20b90d5a70026a72d22a9ca98dcddba Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch> Date: Thu, 17 Mar 2022 13:29:07 +0100 Subject: [PATCH] set prefix __DB__ in backup --- plugins/localdump/couchdb.sh | 2 +- plugins/localdump/mysql.sh | 3 +- plugins/localdump/newservice.sh.example | 133 ++++++++++++++++++++++++ plugins/localdump/pgsql.sh | 3 +- plugins/localdump/readme.md | 12 +++ plugins/localdump/sqlite.sh | 2 +- 6 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 plugins/localdump/newservice.sh.example diff --git a/plugins/localdump/couchdb.sh b/plugins/localdump/couchdb.sh index ac858da..5015682 100755 --- a/plugins/localdump/couchdb.sh +++ b/plugins/localdump/couchdb.sh @@ -142,7 +142,7 @@ function _doBackupOfSingleInstance(){ for dbname in $(_getDblist) do - echo "----- $(date) ${COUCHDB_INSTANCE} -- ${dbname}" + echo -n "__DB__${SERVICENAME} ${COUCHDB_INSTANCE} $_dbname backup " OUTFILE=${BACKUP_TARGETDIR}/${COUCHDB_INSTANCE}/$(get_outfile "${dbname}").couchdbdump python ${dirPythonPackages}/couchdb/tools/dump.py "${COUCHDB_URL}/${dbname}" >"${OUTFILE}" diff --git a/plugins/localdump/mysql.sh b/plugins/localdump/mysql.sh index 5c876bd..104344c 100755 --- a/plugins/localdump/mysql.sh +++ b/plugins/localdump/mysql.sh @@ -158,8 +158,7 @@ function mysql.backup(){ for _dbname in $( mysql.db.list ) do - echo "--- database $_dbname" - echo -n "backup ... " + echo -n "__DB__${SERVICENAME} $_dbname backup " _outfile="${BACKUP_TARGETDIR}/$(get_outfile ${_dbname}).sql" mysql.db.dump "$_dbname" "$_outfile" diff --git a/plugins/localdump/newservice.sh.example b/plugins/localdump/newservice.sh.example new file mode 100644 index 0000000..db7043c --- /dev/null +++ b/plugins/localdump/newservice.sh.example @@ -0,0 +1,133 @@ +#!/bin/bash +# ================================================================================ +# +# LOCALDUMP :: NEW SERVICE +# +# ================================================================================ + +if [ -z "$BACKUP_TARGETDIR" ]; then + echo "ERROR: you cannot start $(basename $0) directly" + rc=$rc+1 + exit 1 +fi + +# -------------------------------------------------------------------------------- +# CONFIG +# -------------------------------------------------------------------------------- + +SERVICEFOUND=0 + +# -------------------------------------------------------------------------------- +# FUNCTIONS +# -------------------------------------------------------------------------------- + +function checkRequirements(){ + + j_requireBinary "client" 1 + j_requireBinary "dumper" 1 + j_requireProcess "service1|service2" 1 + + if [ ! -d $SOURCE_DIR ]; then + echo "INFO: directory $SOURCE_DIR doees not exist." + rc=$rc+1 + fi + + # set flag and reset return code + test $rc -eq 0 && SERVICEFOUND=1 + rc=0 +} + +# detect and backup all databases of the service +function doBackup(){ + + create_targetdir + + for DATABASE_DIR in __DATABASE_LIST__ + do + + DUMPCOMMAND [params] "$DATABASE" > "$OUTFILE" + fetchrc + + # $myrc is last returncode - set in fetchrc + if [ $myrc -eq 0 ]; then + echo -n "gzip ... " + compress_file "$OUTFILE" + else + echo "ERROR occured - no gzip" + fi + ls -l "$OUTFILE"* + echo + done + +} + +# restore database dump file into database +# param string database dump file (gzipped) +# param string optional: database to import; default: database is parsed from file +function restoreByFile(){ + sMyfile=$1 + sMyDb=$2 + + if [ -z "$sMyDb" ]; then + h2 "analyze dump $sMyfile" + sMyDb=$(guessDB $sMyfile) + echo "detected db schema from file: [${sMyDb}]" + else + echo "db schema from param 2: [${sMyDb}]" + fi + + echo + + echo import to "$sMyDb"... + + h2 ensure that database exists ... + color cmd + echo "CREATE DATABASE IF NOT EXISTS ${sMyDb};" | mysql + color reset + + h2 import ... + ls -l "$sMyfile" + echo "import to database [${sMyDb}]" + color cmd + zcat "$sMyfile" | mysql "${sMyDb}" + fetchrc + color reset + +} + +# -------------------------------------------------------------------------------- +# MAIN +# -------------------------------------------------------------------------------- + + +# ----- set action + +checkRequirements >/dev/null 2>&1 +if [ $SERVICEFOUND -eq 0 ]; then + echo "INFO: service [$SERVICENAME] is not avilable on this machine." +fi + +case $1 in + check) + # repeat check ... but show output + echo + echo Details: + checkRequirements + echo + ;; + backup) + test $SERVICEFOUND -eq 1 && doBackup + ;; + restore) + shift 1 + test $SERVICEFOUND -eq 1 && restoreByFile $* + ;; + *) + echo ERROR: wrong syntax: + echo $0 $* + exit 1 +esac + +echo "INFO: $0 $* [$SERVICENAME] final returncode rc=$rc" + +# -------------------------------------------------------------------------------- diff --git a/plugins/localdump/pgsql.sh b/plugins/localdump/pgsql.sh index 9dafcc7..9fbd42d 100755 --- a/plugins/localdump/pgsql.sh +++ b/plugins/localdump/pgsql.sh @@ -54,8 +54,7 @@ function doPgsqlBackup(){ sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;" for DATABASE in $(su ${PGUSER} -c "psql -At -c '$sSqlGetDblist' postgres" 2>/dev/null) do - echo "----- $DATABASE" - echo -n "backup ... " + echo -n "__DB__${SERVICENAME} $_dbname backup " OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql" su ${PGUSER} -c "pg_dump -Fp ${DATABASE} >$OUTFILE" fetchrc diff --git a/plugins/localdump/readme.md b/plugins/localdump/readme.md index 8e75008..e308d6f 100644 --- a/plugins/localdump/readme.md +++ b/plugins/localdump/readme.md @@ -24,3 +24,15 @@ dir-localdumps configures the target base directory for dumps. Below are subdire In those are the dumps containing name of database scheme and a timestamp. All dumps are gzip compressed. keep-days contains an integer for the days to keep database dumps locally. Older dumps will be removed. + +## Global vars in plugin scripts + +``` +BACKUP_BASEDIR {string} base directory for db dumps +BACKUP_DATE {string} string with current timestamp; will be part of filename for backups +BACKUP_KEEP_DAYS {int} count of days how long to keep db dumps below $BACKUP_BASEDIR +BACKUP_PLUGINDIR {string} scripts for supported databases; [APP]/plugins/localdump +BACKUP_SCRIPT {string} script name of db service +BACKUP_TARGETDIR {string} target directory db dumps of current service; eg. /var/backup/mysql/ +SERVICENAME {string} name of db service (one of mysql|pgsql|...) +``` \ No newline at end of file diff --git a/plugins/localdump/sqlite.sh b/plugins/localdump/sqlite.sh index 38e3f09..3009397 100755 --- a/plugins/localdump/sqlite.sh +++ b/plugins/localdump/sqlite.sh @@ -36,7 +36,7 @@ function doSqliteBackup(){ for DATABASE_FILE in $(_j_getvar ${FILEDEFS} "sqlite") do - echo "--- database $DATABASE_FILE" + echo -n "__DB__${SERVICENAME} $_dbname backup " if [ ! -f "$DATABASE_FILE" ]; then color error echo "ERROR: given database file does not exist: $DATABASE_FILE" -- GitLab