mirror of https://github.com/pditzel/dbb.git

Patrick Ditzel
18.46.2017 697aafe6b441d26eba9a63ff43ec777d704ad435
Split postgres into postgres-local an d postgres remote. Get mariadb to run. Modified the finding of configurationfiles ind dbb.
2 files added
4 files modified
211 ■■■■ changed files
config/postgresql-localhost.cfg 15 ●●●●● patch | view | raw | blame | history
config/postgresql.cfg 17 ●●●● patch | view | raw | blame | history
dbb 36 ●●●● patch | view | raw | blame | history
dbb-modules/mariadb 10 ●●●● patch | view | raw | blame | history
dbb-modules/postgresql 61 ●●●●● patch | view | raw | blame | history
dbb-modules/postgresql-localhost 72 ●●●●● patch | view | raw | blame | history
config/postgresql-localhost.cfg
New file
@@ -0,0 +1,15 @@
################################################################################
# Backup Postgres @ localhost as user postres (Debian)
POSTGRES_BACKUP_LOCAL=TRUE
# Should every database backuped in a single file?
# Works together with POSTGRES_BACKUP_LOCAL_DUMP_ALL
POSTGRES_BACKUP_LOCAL_SINGLE=TRUE
# Should all databases backuped in one large file?
# Works together with POSTGRES_BACKUP_LOCAL_SINGLE
POSTGRES_BACKUP_LOCAL_DUMP_ALL=TRUE
config/postgresql.cfg
@@ -1,19 +1,6 @@
################################################################################
# Backup Postgres @ localhost as user postres (Debian)
POSTGRES_BACKUP_LOCAL=TRUE
# Should every database backuped in a single file?
# Works together with POSTGRES_BACKUP_LOCAL_DUMP_ALL
POSTGRES_BACKUP_LOCAL_SINGLE=TRUE
# Should all databases backuped in one large file?
# Works together with POSTGRES_BACKUP_LOCAL_SINGLE
POSTGRES_BACKUP_LOCAL_DUMP_ALL=TRUE
################################################################################
## Enable backup of remote postgres databases specified in ~/.pgpass
POSTGRES_BACKUP_REMOTE=TRUE
dbb
@@ -31,11 +31,11 @@
        # If true then read it
        source $BACKUPCFG
        if [ -d $BACKUPCFG.d ]; then
            if [ ! "$(find $BACKUPCFG.d/*.cfg | cut -d "." -f1)" ]; then
            if [ ! "$(find $BACKUPCFG.d/*.cfg)" ]; then
                echo "Configurationdirectory for modules exist but it is empty" | /usr/bin/logger -s -i -t databasebackup
            else
                echo "Read module configuration" | /usr/bin/logger -s -i -t databasebackup
                for MODULECFG in $(find $BACKUPCFG.d/*.cfg | cut -d "/" -f4 | cut -d "." -f1); do source $BACKUPCFG.d/$MODULECFG.cfg; done
                for MODULECFG in $(find $BACKUPCFG.d/*.cfg); do source "$BACKUPCFG".d/"${MODULECFG##*/}"; done
            fi
        else
            echo "Configurationdirectory does not exists, can't load any configurationfile" | /usr/bin/logger -s -i -t databasebackup
@@ -52,11 +52,11 @@
        while read -r configline; do
            echo "$configline" | grep -v '^$' | grep -v '^#' | /usr/bin/logger -s -i -t databasebackup
        done <<< "$(cat $BACKUPCFG)"
        for MODCFGLINE in $(find $BACKUPCFG.d/*.cfg | cut -d "." -f1); do
        for MODCFGLINE in $(find $BACKUPCFG.d/*.cfg); do
            echo "Debuginfo for $MODCFGLINE" | /usr/bin/logger -s -i -t databasebackup
            while read -r modconfigline; do
                echo "$modconfigline" | grep -v '^$' | grep -v '^#' | /usr/bin/logger -s -i -t databasebackup
            done <<< "$(cat $BACKUPCFG.d/$MODCFGLINE.cfg)"
            done <<< "$(cat "$MODCFGLINE")"
        done
        echo "################################################################################" | /usr/bin/logger -s -i -t databasebackup
    fi
@@ -99,11 +99,11 @@
        if [ ! "$(ls -A $BACKUPCFG.d)" ] && [ ! "$(ls -A $INSTALLATION_PATH_PREFIX/lib/dbb-modules)" ]; then
            debug "Configurationdirectory or directory for modules exist but it is empty"
        else
            for MODULE in $(find $BACKUPCFG.d/*.cfg | cut -d "/" -f4 | cut -d "." -f1); do
                source $INSTALLATION_PATH_PREFIX/lib/dbb-modules/$MODULE
                debug "Load module: $INSTALLATION_PATH_PREFIX/lib/dbb-modules/$MODULE"
            for MODULE in $(find $BACKUPCFG.d/*.cfg); do
                source $INSTALLATION_PATH_PREFIX/lib/dbb-modules/"$(echo "${MODULE##*/}" | cut -d "." -f1)"
                debug "Load module: $INSTALLATION_PATH_PREFIX/lib/dbb-modules/$(echo "${MODULE##*/}" | cut -d "." -f1)"
                # Check the dependenncies defined in the modulefunction -> should be defined in configfiles
                check-$MODULE-deps
                check-"$(echo "${MODULE##*/}" | cut -d "." -f1)"-deps
            done
        fi
    else
@@ -159,23 +159,23 @@
        debug "REMOVE_NUMBER: $REMOVE_NUMBER"
        debug "FILE_PREFIX_NAME_TO_REMOVE: $BFH_FILE_PREFIX_NAME_TO_REMOVE-$BFH_FILE"
        # Check if there is an backupfile from the current day
        if [ -f "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"-"$BFH_FILE" ]; then
        if [ -f "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"_"$BFH_FILE" ]; then
            # If yes append miniutes and seconds to the date-profix of the filename
            debug "File $BFH_BACKUPDIR_DIR/$BACKUP_DAY-$BFH_FILE already exists. Rename the new one."
            DATE_TIME_SUFFIX=$(date +%H%M%S)
            debug "File $BFH_BACKUPDIR_DIR/$BACKUP_DAY_$BFH_FILE already exists. Rename the new one."
            DATE_TIME_SUFFIX=$(date +%H:%M:%S)
            # ... and move it into the targetdir
            mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"-"$DATE_TIME_SUFFIX"-"$BFH_FILE"
            mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"_"$DATE_TIME_SUFFIX"_"$BFH_FILE"
        else
            # If there is no backupfile of the current day move it to the backupfolder
            mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"-"$BFH_FILE"
            mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"_"$BFH_FILE"
        fi
        # Check if there are files older then the days to keep set in the config
        if [ -f "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"-"$BFH_FILE" ]; then
            # if yes remove it
            rm "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"-"$BFH_FILE"
            rm "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"_"$BFH_FILE"
            # Also remove the files with the extended prefix in the name
            # If there is ab file with the extende prefix then there has to be a file with tne normal prefix
            rm "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"?????-"$BFH_FILE"
            rm "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"_????????_"$BFH_FILE"
        else
            # If no file exists do nothing but some debuginfo
            debug "File $BFH_BACKUPDIR_DIR/$BFH_FILE_PREFIX_NAME_TO_REMOVE-$BFH_FILE does not exists, so can not remove it."
@@ -234,9 +234,9 @@
        debug "Load dbb-modules"
        load_dbbmodules
        # Run modul-main-functions
        for MODULEMAIN in $(find $BACKUPCFG.d/*.cfg | cut -d "/" -f4 | cut -d "." -f1); do
            debug "run $MODULEMAIN-main - function of module $MODULEMAIN"
            $MODULEMAIN-main
        for MODULEMAIN in $(find $BACKUPCFG.d/*.cfg); do
            debug "run $MODULEMAIN-main - function of module '$(echo "${MODULEMAIN##*/}" | cut -d "." -f1)'"
            "$(echo "${MODULEMAIN##*/}" | cut -d "." -f1)"-main
        done
            # The final action: remove the dumps
        rm -rf "$TMP_DIR"
dbb-modules/mariadb
@@ -21,15 +21,15 @@
}
function mariadb_remote {
    MARIADBHOSTNAME=$(cat ~/.my.cnf-$DATABASE | grep host | cut -d "=" -f2)
    for MARIADB in $(ls .my.cnf-* | cut -d "-" -f2)
        do
        /usr/bin/mysqldump --defaults-file="~/.my.cnf-$MARIADB" --skip-dump-date $MARIADB > $TMP_DIR/$MARIADBHOSTNAME_$MARIADB.mysql
    for MARIADB in $(ls ~/.my.cnf-* | cut -d "-" -f2); do
        debug "Function mariadb_remote: Doing Database: $MARIADB"
        MARIADBHOSTNAME=$(cat ~/.my.cnf-"$MARIADB" | grep host | cut -d "=" -f2)
        /usr/bin/mysqldump --defaults-file=~/.my.cnf-"$MARIADB" --skip-dump-date "$MARIADB" > "$TMP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql
        debug "Diff MySQLDump $MYSQLDB"
        diff "$TMP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql "$BACKUP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        debug "Differences found -> moving to BACKUP_DIR"
            mv "$TMP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql "$BACKUP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql
        backup_file_handler "$TMP_DIR" "$BACKUP_DIR" "$MARIADBHOSTNAME"_"$MARIADB".mysql
    else
            debug "No differences found in Database $MARIADB"
    fi
dbb-modules/postgresql
@@ -6,10 +6,6 @@
    if [ ! -e /usr/bin/pg_dump ]; then
        debug "It seems that you dont have psql installed. You may have problems to backup remote databases"
    fi
    numberRegex='^[0-9]+$'
    if ! [[ $(id -u postgres 2>&1) =~ $numberRegex ]] ; then
        debug "It seems that you dont have the user postgres on your computer. You may have problems to backup local postgres databases"
    fi
    if [ -r ~/.pgpass ]; then
        debug "function check-postgresql-deps: ~/.pgpass exists, doing reomte backup."
    else
@@ -18,55 +14,6 @@
    fi
}
# PostgreSQL local
function pg_local_all {
    # Debugoutput id DEBUG is enabled
    debug "Dump all from Postgres local"
    # Dump all databases in one file as user postgres (Debian)
    sudo -H -i -u postgres pg_dumpall > "$TMP_DIR"/local_dumpall.pgsql
    debug "Diff alldumps from Postgres local"
    # Check if there are differences between the actual dump and the last dump
    backup_file_handler "$TMP_DIR $BACKUP_DIR" local_dumpall.pgsql
}
function pg_local_single {
    # Get a list with all databases on localhost. Do it as user postgres (Debian)
    # parallelize the following
    for DB in $(sudo -H -i -u postgres /usr/bin/psql -At -c "SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn ORDER BY datname;" postgres)
        do
            debug "Dumping local database $DB"
            # Dump each database in a single file
            sudo -H -i -u postgres /usr/bin/pg_dump -Fp "$DB" > "$TMP_DIR"/local_"$DB".pgsql
            backup_file_handler "$TMP_DIR" "$BACKUP_DIR" local_"$DB".pgsql
            echo ""
        done
}
# Do the local backupjobs
function run_local_pg_backups {
    # Test if the current backupuser has access via sudo to postgres
    # This part has to be improved!
    sudo -l -U "$BACKUPUSER" | grep postgres
    if [ $? -eq 0 ];then
        # If the user has access vi sudo to postgres and a full dump is configured, do it.
        if [ "$POSTGRES_BACKUP_LOCAL_DUMP_ALL" == "TRUE" ]; then
            pg_local_all
        fi
        # If the user has access vi sudo to postgres and dumps for each database is configured , do it.
        if [ "$POSTGRES_BACKUP_LOCAL_SINGLE" == "TRUE" ]; then
            pg_local_single
        fi
    else
        # If the user is not permitted to acces the postgresdatabases vi sudo throw an errormessage
        echo "" | $LOGGER
        echo "The backupuser does not the permission to act as user postgres" | $LOGGER
        echo "Please add the following line to your /etc/sudoers:" | $LOGGER
        echo "backupuser ALL=(postgres) NOPASSWD:/usr/bin/psql,/usr/bin/pg_dump,/usr/bin/pg_dumpall" | $LOGGER
        echo "" | $LOGGER
    fi
}
################################################################################
@@ -96,8 +43,8 @@
        # I don't know if the following statement really effect to pg_dump :-(
        export PGSSLMODE=require
        # Dump the databases which are defined in the params
        /usr/bin/pg_dump -U "$TRGTBDUSER" -h "$TRGTHOST" -p "$TRGTPORT" "$TRGTDB" > "$TMP_DIR"/"$TRGTHOST"-"$TRGTDB".pgql
        backup_file_handler "$TMP_DIR" "$BACKUP_DIR" "$TRGTHOST"-"$TRGTDB".pgql
        /usr/bin/pg_dump -U "$TRGTBDUSER" -h "$TRGTHOST" -p "$TRGTPORT" "$TRGTDB" > "$TMP_DIR"/"$TRGTHOST"_"$TRGTDB".pgql
        backup_file_handler "$TMP_DIR" "$BACKUP_DIR" "$TRGTHOST"_"$TRGTDB".pgql
    else
        # If no encrypted connection to the postgres-server can be established throw an errormessage
        echo "" | $LOGGER
@@ -118,6 +65,7 @@
                # For each entry do the backup
                debug "run dump with params $LINE"
                # CAUTION: No doublequotes in the following line. The var $LINE has to be splittet!
                # DO NOT DOUBLEQUOTE $LINE
            dump_remote_pgdb $LINE
            # To get the params for the function the .pgpass-file is striped from the comments,
            # the ":" are replaces against whitespaces and only the first four coloums are used
@@ -135,9 +83,6 @@
}
function postgresql-main {
    if [ "$POSTGRES_BACKUP_LOCAL" = "TRUE" ]; then
        run_local_pg_backups
    fi
    if [ "$POSTGRES_BACKUP_REMOTE" = "TRUE" ]; then
        run_remote_pg_backups
    fi
dbb-modules/postgresql-localhost
New file
@@ -0,0 +1,72 @@
#!/bin/bash
# dependencies
function check-postgresql-localhost-deps {
    if [ ! -e /usr/bin/pg_dump ]; then
        debug "It seems that you dont have psql installed. You may have problems to backup remote databases"
    fi
    numberRegex='^[0-9]+$'
    if ! [[ $(id -u postgres 2>&1) =~ $numberRegex ]] ; then
        debug "It seems that you dont have the user postgres on your computer. You may have problems to backup local postgres databases"
    fi
}
# PostgreSQL local
function pg_local_all {
    # Debugoutput id DEBUG is enabled
    debug "Dump all from Postgres local"
    # Dump all databases in one file as user postgres (Debian)
    sudo -H -u postgres pg_dumpall > "$TMP_DIR"/local_dumpall.pgsql
    debug "Diff alldumps from Postgres local"
    # Check if there are differences between the actual dump and the last dump
    backup_file_handler "$TMP_DIR $BACKUP_DIR" local_dumpall.pgsql
}
function pg_local_single {
    # Get a list with all databases on localhost. Do it as user postgres (Debian)
    # parallelize the following
    for DB in $(sudo -H -u postgres /usr/bin/psql -At -c "SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn ORDER BY datname;" postgres)
        do
            debug "Dumping local database $DB"
            # Dump each database in a single file
            sudo -H -u postgres /usr/bin/pg_dump -Fp "$DB" > "$TMP_DIR"/local_"$DB".pgsql
            backup_file_handler "$TMP_DIR" "$BACKUP_DIR" local_"$DB".pgsql
            echo ""
        done
}
# Do the local backupjobs
function run_local_pg_backups {
    # Test if the current backupuser has access via sudo to postgres
    # This part has to be improved!
    sudo -l -U "$BACKUPUSER" | grep postgres
    if [ $? -eq 0 ];then
        # If the user has access vi sudo to postgres and a full dump is configured, do it.
        if [ "$POSTGRES_BACKUP_LOCAL_DUMP_ALL" == "TRUE" ]; then
            pg_local_all
        fi
        # If the user has access vi sudo to postgres and dumps for each database is configured , do it.
        if [ "$POSTGRES_BACKUP_LOCAL_SINGLE" == "TRUE" ]; then
            pg_local_single
        fi
    else
        # If the user is not permitted to acces the postgresdatabases vi sudo throw an errormessage
        echo "" | $LOGGER
        echo "The backupuser does not the permission to act as user postgres" | $LOGGER
        echo "Please add the following line to your /etc/sudoers:" | $LOGGER
        echo "backupuser ALL=(postgres) NOPASSWD:/usr/bin/psql,/usr/bin/pg_dump,/usr/bin/pg_dumpall" | $LOGGER
        echo "" | $LOGGER
    fi
}
################################################################################
function postgresql-localhost-main {
    if [ "$POSTGRES_BACKUP_LOCAL" = "TRUE" ]; then
        run_local_pg_backups
    fi
}