diff --git a/plugins/localdump/couchdb.sh b/plugins/localdump/couchdb.sh index ac858da3d72c7c84d5b762f4a396299ad26a205c..5015682d4b6297d15c06d3189e4a4345c3ca64f6 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 5c876bd1dc9fb8ea953b1f798b3810d6e9a90963..104344cef9ddc1d1a16d85e4066181dc98a4e6b3 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 0000000000000000000000000000000000000000..db7043c498be9ed7b463c083954265df3fa818d0 --- /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 9dafcc7d1a278f574c4205e7ce4acafa7274404c..9fbd42d439de3598e6e64ef69b6b11409fe26ae7 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 8e7500856b595f5aef8a335fc3398dceaa9bde0c..e308d6f5ffb9e4176f17a78b0e64f953fbdf3552 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 38e3f0935018840c7be2fb461cafb995ec2b1d88..30093975d4912d0fa1bf2e11b23a1a796c06afd9 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"