From 5a01b6118e9b88089855dcdb9508214dc6c72f3d Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 1 Nov 2023 14:13:37 -0700 Subject: [PATCH 01/68] feat - make processes and files save as username dbbackup instead of root --- Dockerfile | 82 ++++++------ README.md | 113 ++++++++-------- install/assets/functions/10-db-backup | 177 ++++++++++++++------------ install/etc/cont-init.d/10-db-backup | 1 + 4 files changed, 199 insertions(+), 174 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9b7278d..79aaa10 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,50 +19,53 @@ ENV INFLUX_VERSION=1.8.0 \ ### Dependencies RUN source /assets/functions/00-container && \ set -ex && \ + addgroup -S -g 10000 dbbackup && \ + adduser -S -D -H -u 10000 -G dbbackup -g "Tired of I.T! DB Backup" dbbackup && \ + \ package update && \ package upgrade && \ package install .db-backup-build-deps \ - build-base \ - bzip2-dev \ - cargo \ - git \ - go \ - libarchive-dev \ - openssl-dev \ - libffi-dev \ - python3-dev \ - py3-pip \ - xz-dev \ - && \ + build-base \ + bzip2-dev \ + cargo \ + git \ + go \ + libarchive-dev \ + openssl-dev \ + libffi-dev \ + python3-dev \ + py3-pip \ + xz-dev \ + && \ \ package install .db-backup-run-deps \ - bzip2 \ - groff \ - libarchive \ - mariadb-client \ - mariadb-connector-c \ - mongodb-tools \ - openssl \ - pigz \ - postgresql16 \ - postgresql16-client \ - pv \ - py3-botocore \ - py3-colorama \ - py3-cryptography \ - py3-docutils \ - py3-jmespath \ - py3-rsa \ - py3-setuptools \ - py3-s3transfer \ - py3-yaml \ - python3 \ - redis \ - sqlite \ - xz \ - zip \ - zstd \ - && \ + bzip2 \ + groff \ + libarchive \ + mariadb-client \ + mariadb-connector-c \ + mongodb-tools \ + openssl \ + pigz \ + postgresql16 \ + postgresql16-client \ + pv \ + py3-botocore \ + py3-colorama \ + py3-cryptography \ + py3-docutils \ + py3-jmespath \ + py3-rsa \ + py3-setuptools \ + py3-s3transfer \ + py3-yaml \ + python3 \ + redis \ + sqlite \ + xz \ + zip \ + zstd \ + && \ \ apkArch="$(uname -m)"; \ case "$apkArch" in \ @@ -106,5 +109,4 @@ RUN source /assets/functions/00-container && \ /tmp/* \ /usr/src/* - COPY install / diff --git a/README.md b/README.md index 5e408f1..f736563 100644 --- a/README.md +++ b/README.md @@ -35,41 +35,40 @@ Currently backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, R ## Table of Contents -- [github.com/tiredofit/docker-db-backup](#githubcomtiredofitdocker-db-backup) - - [About](#about) - - [Maintainer](#maintainer) - - [Table of Contents](#table-of-contents) - - [Prerequisites and Assumptions](#prerequisites-and-assumptions) - - [Installation](#installation) - - [Build from Source](#build-from-source) - - [Prebuilt Images](#prebuilt-images) - - [Multi Architecture](#multi-architecture) - - [Configuration](#configuration) - - [Quick Start](#quick-start) - - [Persistent Storage](#persistent-storage) - - [Environment Variables](#environment-variables) - - [Base Images used](#base-images-used) - - [Container Options](#container-options) - - [Database Specific Options](#database-specific-options) - - [For Influx DB2](#for-influx-db2) - - [Scheduling Options](#scheduling-options) - - [Backup Options](#backup-options) - - [Backing Up to S3 Compatible Services](#backing-up-to-s3-compatible-services) - - [Upload to a Azure storage account by `blobxfer`](#upload-to-a-azure-storage-account-by-blobxfer) - - [Maintenance](#maintenance) - - [Shell Access](#shell-access) - - [Manual Backups](#manual-backups) - - [Restoring Databases](#restoring-databases) - - [Custom Scripts](#custom-scripts) - - [Path Options](#path-options) - - [Pre Backup](#pre-backup) - - [Post backup](#post-backup) - - [Support](#support) - - [Usage](#usage) - - [Bugfixes](#bugfixes) - - [Feature Requests](#feature-requests) - - [Updates](#updates) - - [License](#license) +- [About](#about) +- [Maintainer](#maintainer) +- [Table of Contents](#table-of-contents) +- [Prerequisites and Assumptions](#prerequisites-and-assumptions) +- [Installation](#installation) + - [Build from Source](#build-from-source) + - [Prebuilt Images](#prebuilt-images) + - [Multi Architecture](#multi-architecture) +- [Configuration](#configuration) + - [Quick Start](#quick-start) + - [Persistent Storage](#persistent-storage) + - [Environment Variables](#environment-variables) + - [Base Images used](#base-images-used) + - [Container Options](#container-options) + - [Database Specific Options](#database-specific-options) + - [For Influx DB2](#for-influx-db2) + - [Scheduling Options](#scheduling-options) + - [Backup Options](#backup-options) + - [Backing Up to S3 Compatible Services](#backing-up-to-s3-compatible-services) + - [Upload to a Azure storage account by `blobxfer`](#upload-to-a-azure-storage-account-by-blobxfer) +- [Maintenance](#maintenance) + - [Shell Access](#shell-access) + - [Manual Backups](#manual-backups) + - [Restoring Databases](#restoring-databases) + - [Custom Scripts](#custom-scripts) + - [Path Options](#path-options) + - [Pre Backup](#pre-backup) + - [Post backup](#post-backup) +- [Support](#support) + - [Usage](#usage) + - [Bugfixes](#bugfixes) + - [Feature Requests](#feature-requests) + - [Updates](#updates) +- [License](#license) > **NOTE**: If you are using this with a docker-compose file along with a seperate SQL container, take care not to set the variables to backup immediately, more so have it delay execution for a minute, otherwise you will get a failed first backup. @@ -150,6 +149,8 @@ Be sure to view the following repositories to understand all the customizable op | `CREATE_LATEST_SYMLINK` | Create a symbolic link pointing to last backup in this format: `latest-(DB_TYPE)-(DB_NAME)-(DB_HOST)` | `TRUE` | | `PRE_SCRIPT` | Fill this variable in with a command to execute pre backing up | | | `POST_SCRIPT` | Fill this variable in with a command to execute post backing up | | +| `USER_DBBACKUP` | The uid that the image should read and write files as (username is `dbbackup`) | `10000` | +| `GROUP_DBBACKUP` | The gid that the image should read and write files as (groupname is `dbbackup`) | `10000` | | `SPLIT_DB` | For each backup, create a new archive. `TRUE` or `FALSE` (MySQL and Postgresql Only) | `TRUE` | ### Database Specific Options @@ -189,27 +190,27 @@ Your Organization will be mapped to `DB_USER` and your root token will need to b ### Backup Options -| Parameter | Description | Default | `_FILE` | -| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | -| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, ZSTD `ZSTD` or none `NONE` | `ZSTD` | | -| `COMPRESSION_LEVEL` | Numerical value of what level of compression to use, most allow `1` to `9` except for `ZSTD` which allows for `1` to `19` - | `3` | | -| `ENABLE_PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` | `TRUE` | | -| `PARALLEL_COMPRESSION_THREADS` | Maximum amount of threads to use when compressing - Integer value e.g. `8` | `autodetected` | | -| `GZ_RSYNCABLE` | Use `--rsyncable` (gzip only) for faster rsync transfers and incremental backup deduplication. e.g. `TRUE` | `FALSE` | | -| `ENABLE_CHECKSUM` | Generate either a MD5 or SHA1 in Directory, `TRUE` or `FALSE` | `TRUE` | | -| `CHECKSUM` | Either `MD5` or `SHA1` | `MD5` | | -| `EXTRA_OPTS` | If you need to pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | -| `EXTRA_DUMP_OPTS` | If you need to pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | -| `EXTRA_ENUMERATION_OPTS` | If you need to pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | -| `MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet if backing up MySQL / MariaDB | `512M` | | -| `MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction with MySQL / MariaDB | `TRUE` | | -| `MYSQL_STORED_PROCEDURES` | Backup stored procedures with MySQL / MariaDB | `TRUE` | | -| `MYSQL_ENABLE_TLS` | Enable TLS functionality for MySQL client | `FALSE` | | -| `MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | -| `MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | -| `MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | -| `MYSQL_TLS_CERT_FILE` | Filename to load client certificate for connecting via TLS | | x | -| `MYSQL_TLS_KEY_FILE` | Filename to load client key for connecting via TLS | | x | +| Parameter | Description | Default | `_FILE` | +| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | +| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, ZSTD `ZSTD` or none `NONE` | `ZSTD` | | +| `COMPRESSION_LEVEL` | Numerical value of what level of compression to use, most allow `1` to `9` except for `ZSTD` which allows for `1` to `19` - | `3` | | +| `ENABLE_PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` | `TRUE` | | +| `PARALLEL_COMPRESSION_THREADS` | Maximum amount of threads to use when compressing - Integer value e.g. `8` | `autodetected` | | +| `GZ_RSYNCABLE` | Use `--rsyncable` (gzip only) for faster rsync transfers and incremental backup deduplication. e.g. `TRUE` | `FALSE` | | +| `ENABLE_CHECKSUM` | Generate either a MD5 or SHA1 in Directory, `TRUE` or `FALSE` | `TRUE` | | +| `CHECKSUM` | Either `MD5` or `SHA1` | `MD5` | | +| `EXTRA_OPTS` | If you need to pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `EXTRA_DUMP_OPTS` | If you need to pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `EXTRA_ENUMERATION_OPTS` | If you need to pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | +| `MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet if backing up MySQL / MariaDB | `512M` | | +| `MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction with MySQL / MariaDB | `TRUE` | | +| `MYSQL_STORED_PROCEDURES` | Backup stored procedures with MySQL / MariaDB | `TRUE` | | +| `MYSQL_ENABLE_TLS` | Enable TLS functionality for MySQL client | `FALSE` | | +| `MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | +| `MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | +| `MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | +| `MYSQL_TLS_CERT_FILE` | Filename to load client certificate for connecting via TLS | | x | +| `MYSQL_TLS_KEY_FILE` | Filename to load client key for connecting via TLS | | x | - When using compression with MongoDB, only `GZ` compression is possible. diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index afd0af8..5543ef2 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1,5 +1,21 @@ #!/command/with-contenv bash +bootstrap_filesystem() { + if [ ! -d "${DB_DUMP_TARGET}" ]; then + mkdir -p "${DB_DUMP_TARGET}" + fi + if [ "$(stat -c %U "${DB_DUMP_TARGET}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${DB_DUMP_TARGET}" ; fi + + if [ -d "${DB_DUMP_TARGET_ARCHIVE}" ]; then + if [ "$(stat -c %U "${DB_DUMP_TARGET_ARCHIVE}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${DB_DUMP_TARGET_ARCHIVE}" ; fi + fi + + if [ ! -d "${TEMP_LOCATION}" ]; then + mkdir -p "${TEMP_LOCATION}" + fi + if [ "$(stat -c %U "${TEMP_LOCATION}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${TEMP_LOCATION}" ; fi +} + bootstrap_variables() { sanity_var DB_TYPE "Set appropriate DB_TYPE" transform_file_var \ @@ -132,7 +148,7 @@ backup_couch() { compression pre_dbbackup ${DB_NAME} print_notice "Dumping CouchDB database: '${DB_NAME}' ${compression_string}" - curl -sSL -X GET ${DB_HOST}:${DB_PORT}/${DB_NAME}/_all_docs?include_docs=true ${compress_cmd} | $compress_cmd > "${TEMP_LOCATION}"/"${target}" + run_as_user curl -sSL -X GET ${DB_HOST}:${DB_PORT}/${DB_NAME}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -159,11 +175,11 @@ backup_influx() { compression pre_dbbackup $db print_notice "Dumping Influx database: '${db}'" - influxd backup ${influx_compression} ${bucket} -portable -host ${DB_HOST}:${DB_PORT} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} "${TEMP_LOCATION}"/"${target_dir}" + run_as_user influxd backup ${influx_compression} ${bucket} -portable -host ${DB_HOST}:${DB_PORT} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} "${TEMP_LOCATION}"/"${target_dir}" exit_code=$? check_exit_code backup $target_dir print_notice "Creating archive file of '${target_dir}' with tar ${compression_string}" - tar cf - "${TEMP_LOCATION}"/"${target_dir}" | $dir_compress_cmd > "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" + run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" > /dev/null target=influx_${db}_${DB_HOST#*//}_${now}.tar${extension} ltarget=influx_${db}_${DB_HOST#*//} generate_checksum @@ -181,7 +197,7 @@ backup_influx() { compression pre_dbbackup $db print_notice "Dumping Influx2 database: '${db}'" - influx backup --org ${DB_USER} ${bucket} --host ${DB_HOST}:${DB_PORT} --token ${DB_PASS} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --compression none "${TEMP_LOCATION}"/"${target_dir}" + run_as_user influx backup --org ${DB_USER} ${bucket} --host ${DB_HOST}:${DB_PORT} --token ${DB_PASS} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --compression none "${TEMP_LOCATION}"/"${target_dir}" exit_code=$? check_exit_code backup $target_dir create_archive @@ -214,7 +230,7 @@ backup_mongo() { fi pre_dbbackup "${DB_NAME}" print_notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" - silent mongodump --archive=${TEMP_LOCATION}/${target} ${mongo_compression} ${mongo_backup_parameter} + silent run_as_user mongodump --archive=${TEMP_LOCATION}/${target} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? check_exit_code backup $target generate_checksum @@ -230,7 +246,7 @@ backup_mssql() { compression pre_dbbackup "${DB_NAME}" print_notice "Dumping MSSQL database: '${DB_NAME}'" - silent /opt/mssql-tools18/bin/sqlcmd -C -S ${DB_HOST}\,${DB_PORT} -U ${DB_USER} -P ${DB_PASS} -Q "BACKUP DATABASE [${DB_NAME}] TO DISK = N'${TEMP_LOCATION}/${target}' WITH NOFORMAT, NOINIT, NAME = '${DB_NAME}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${DB_HOST}\,${DB_PORT} -U ${DB_USER} -P ${DB_PASS} -Q "BACKUP DATABASE [${DB_NAME}] TO DISK = N'${TEMP_LOCATION}/${target}' WITH NOFORMAT, NOINIT, NAME = '${DB_NAME}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target generate_checksum @@ -249,7 +265,7 @@ backup_mysql() { if [ "${DB_NAME,,}" = "all" ] ; then print_debug "Preparing to back up everything except for information_schema and _* prefixes" - db_names=$(mysql -h ${DB_HOST} -P $DB_PORT -u$DB_USER ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_ENUMERATION_OPTS} --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema ) + db_names=$(run_as_user mysql -h ${DB_HOST} -P $DB_PORT -u$DB_USER ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_ENUMERATION_OPTS} --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema ) if [ -n "${DB_NAME_EXCLUDE}" ] ; then db_names_exclusions=$(echo "${DB_NAME_EXCLUDE}" | tr ',' '\n') for db_exclude in ${db_names_exclusions} ; do @@ -271,7 +287,7 @@ backup_mysql() { compression pre_dbbackup $db print_notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" - mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} $db | $compress_cmd > "${TEMP_LOCATION}"/"${target}" + run_as_user mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} $db | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -287,7 +303,7 @@ backup_mysql() { compression pre_dbbackup all print_notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --databases $(echo ${db_names} | xargs) | $compress_cmd > "${TEMP_LOCATION}"/"${target}" + run_as_user mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -306,7 +322,7 @@ backup_pgsql() { fi if [ "${DB_NAME,,}" = "all" ] ; then print_debug "Preparing to back up all databases" - db_names=$(psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) + db_names=$(run_as_user psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) if [ -n "${DB_NAME_EXCLUDE}" ] ; then db_names_exclusions=$(echo "${DB_NAME_EXCLUDE}" | tr ',' '\n') for db_exclude in ${db_names_exclusions} ; do @@ -328,7 +344,7 @@ backup_pgsql() { compression pre_dbbackup $db print_notice "Dumping PostgresSQL database: '${db}' ${compression_string}" - pg_dump -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} $db ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} | $compress_cmd > ${TEMP_LOCATION}/${target} + run_as_user pg_dump -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} $db ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -344,7 +360,7 @@ backup_pgsql() { compression pre_dbbackup all print_notice "Dumping all PostgreSQL databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - tmp_db_names=$(psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) + tmp_db_names=$(run_as_user psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) for r_db_name in $(echo $db_names | xargs); do tmp_db_names=$(echo "$tmp_db_names" | xargs | sed "s|${r_db_name}||g" ) done @@ -352,7 +368,7 @@ backup_pgsql() { for x_db_name in ${tmp_db_names} ; do pgexclude_arg=$(echo ${pgexclude_arg} --exclude-database=${x_db_name}) done - pg_dumpall -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} ${pgexclude_arg} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} | $compress_cmd > ${TEMP_LOCATION}/${target} + run_as_user pg_dumpall -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} ${pgexclude_arg} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -367,7 +383,7 @@ backup_redis() { print_notice "Dumping Redis - Flushing Redis Cache First" target=redis_all_${DB_HOST,,}_${now}.rdb ltarget=redis_${DB_HOST,,} - echo bgsave | silent redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} --rdb ${TEMP_LOCATION}/${target} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} + echo bgsave | silent run_as_user redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} --rdb ${TEMP_LOCATION}/${target} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} sleep 10 try=5 while [ $try -gt 0 ] ; do @@ -385,7 +401,7 @@ backup_redis() { target_original=${target} compression pre_dbbackup all - $compress_cmd "${TEMP_LOCATION}/${target_original}" + run_as_user ${compress_cmd} "${TEMP_LOCATION}/${target_original}" check_exit_code backup $target generate_checksum move_dbbackup @@ -402,10 +418,10 @@ backup_sqlite3() { compression pre_dbbackup $db print_notice "Dumping sqlite3 database: '${DB_HOST}' ${compression_string}" - silent sqlite3 "${DB_HOST}" ".backup '${TEMP_LOCATION}/backup.sqlite3'" + silent run_as_user sqlite3 "${DB_HOST}" ".backup '${TEMP_LOCATION}/backup.sqlite3'" exit_code=$? check_exit_code backup $target - cat "${TEMP_LOCATION}"/backup.sqlite3 | ${dir_compress_cmd} > "${TEMP_LOCATION}/${target}" + run_as_user cat "${TEMP_LOCATION}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_LOCATION}/${target}" > /dev/null generate_checksum move_dbbackup check_exit_code move $target @@ -420,7 +436,7 @@ check_availability() { counter=0 code_received=0 while [ "${code_received}" != "200" ]; do - code_received=$(curl -XGET -sSL -o /dev/null -L -w ''%{http_code}'' ${DB_HOST}:${DB_PORT}) + code_received=$(run_as_user curl -XGET -sSL -o /dev/null -L -w ''%{http_code}'' ${DB_HOST}:${DB_PORT}) if [ "${code_received}" = "200" ] ; then break ; fi sleep 5 (( counter+=5 )) @@ -431,7 +447,7 @@ check_availability() { counter=0 case "${INFLUX_VERSION,,}" in 1 ) - while ! (nc -z ${DB_HOST#*//} ${DB_PORT}) ; do + while ! (run_as_user nc -z ${DB_HOST#*//} ${DB_PORT}) ; do sleep 5 (( counter+=5 )) print_warn "InfluxDB Host '${DB_HOST#*//}' is not accessible, retrying.. ($counter seconds so far)" @@ -440,7 +456,7 @@ check_availability() { 2 ) code_received=0 while [ "${code_received}" != "200" ]; do - code_received=$(curl -XGET -sSL -o /dev/null -w ''%{http_code}'' ${DB_HOST}:${DB_PORT}/health) + code_received=$(run_as_user curl -XGET -sSL -o /dev/null -w ''%{http_code}'' ${DB_HOST}:${DB_PORT}/health) if [ "${code_received}" = "200" ] ; then break ; fi sleep 5 (( counter+=5 )) @@ -454,7 +470,7 @@ check_availability() { print_debug "Skipping Connectivity Check" else counter=0 - while ! (nc -z ${DB_HOST} ${DB_PORT}) ; do + while ! (run_as_user nc -z ${DB_HOST} ${DB_PORT}) ; do sleep 5 (( counter+=5 )) print_warn "Mongo Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" @@ -465,7 +481,7 @@ check_availability() { counter=0 transform_file_var DB_PASS export MYSQL_PWD=${DB_PASS} - while ! (mysqladmin -u"${DB_USER}" -P"${DB_PORT}" -h"${DB_HOST}" ${mysql_tls_args} status > /dev/null 2>&1) ; do + while ! (run_as_user mysqladmin -u"${DB_USER}" -P"${DB_PORT}" -h"${DB_HOST}" ${mysql_tls_args} status > /dev/null 2>&1) ; do sleep 5 (( counter+=5 )) print_warn "MySQL/MariaDB Server '${DB_HOST}' is not accessible, retrying.. (${counter} seconds so far)" @@ -473,7 +489,7 @@ check_availability() { ;; "mssql" ) counter=0 - while ! (nc -z ${DB_HOST} ${DB_PORT}) ; do + while ! (run_as_user nc -z ${DB_HOST} ${DB_PORT}) ; do sleep 5 (( counter+=5 )) print_warn "MSSQL Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" @@ -481,7 +497,7 @@ check_availability() { ;; "pgsql" ) counter=0 - until pg_isready --host=${DB_HOST} --port=${DB_PORT} -q + until run_as_user pg_isready --host=${DB_HOST} --port=${DB_PORT} -q do sleep 5 (( counter+=5 )) @@ -490,7 +506,7 @@ check_availability() { ;; "redis" ) counter=0 - while ! (nc -z "${DB_HOST}" "${DB_PORT}") ; do + while ! (run_as_user nc -z "${DB_HOST}" "${DB_PORT}") ; do sleep 5 (( counter+=5 )) print_warn "Redis Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" @@ -549,19 +565,19 @@ cleanup_old_data() { case "${BACKUP_LOCATION,,}" in "blobxfer" ) print_info "Cleaning up old backups on filesystem" - mkdir -p "${DB_DUMP_TARGET}" - find "${DB_DUMP_TARGET}"/ -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm {} \; + run_as_user mkdir -p "${DB_DUMP_TARGET}" + find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm -f {} \; print_info "Syncing changes via blobxfer" - silent blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} --delete --delete-only + silent run_as_user blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} --delete --delete-only ;; "file" | "filesystem" ) print_info "Cleaning up old backups on filesystem" - mkdir -p "${DB_DUMP_TARGET}" - find "${DB_DUMP_TARGET}"/ -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm {} \; + run_as_user mkdir -p "${DB_DUMP_TARGET}" + run_as_user find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm -f {} \; ;; "s3" | "minio" ) print_info "Cleaning up old backups on S3 storage" - aws ${PARAM_AWS_ENDPOINT_URL} s3 ls s3://${S3_BUCKET}/${S3_PATH}/ ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} | grep " DIR " -v | grep " PRE " -v | while read -r s3_file; do + run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 ls s3://${S3_BUCKET}/${S3_PATH}/ ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} | grep " DIR " -v | grep " PRE " -v | while read -r s3_file; do s3_createdate=$(echo $s3_file | awk {'print $1" "$2'}) s3_createdate=$(date -d "$s3_createdate" "+%s") s3_olderthan=$(echo $(( $(date +%s)-${DB_CLEANUP_TIME}*60 ))) @@ -569,7 +585,7 @@ cleanup_old_data() { s3_filename=$(echo $s3_file | awk {'print $4'}) if [ "$s3_filename" != "" ] ; then print_debug "Deleting $s3_filename" - aws ${PARAM_AWS_ENDPOINT_URL} s3 rm s3://${S3_BUCKET}/${S3_PATH}/${s3_filename} ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} + run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 rm s3://${S3_BUCKET}/${S3_PATH}/${s3_filename} ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} fi fi @@ -582,7 +598,6 @@ cleanup_old_data() { fi } - compression() { if var_false "${ENABLE_PARALLEL_COMPRESSION}" ; then PARALLEL_COMPRESSION_THREADS=1 @@ -652,7 +667,7 @@ compression() { create_archive() { if [ "${exit_code}" = "0" ] ; then print_notice "Creating archive file of '${target_dir}' with tar ${compression_string}" - tar cf - "${TEMP_LOCATION}"/"${target_dir}" | $dir_compress_cmd > "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" + run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} > "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" else print_error "Skipping creating archive file because backup did not complete successfully" fi @@ -674,8 +689,9 @@ generate_checksum() { print_notice "Generating ${checksum_extension^^} for '${target}'" cd "${TEMP_LOCATION}" - ${checksum_command} "${target}" > "${target}"."${checksum_extension}" - checksum_value=$(${checksum_command} "${target}" | awk ' { print $1}') + run_as_user ${checksum_command} "${target}" | run_as_user tee "${target}"."${checksum_extension}" > /dev/null + ## TODO - We're doing this twice, why not just pull from the previously generated file + checksum_value=$(run_as_user ${checksum_command} "${target}" | awk ' { print $1}') print_debug "${checksum_extension^^}: ${checksum_value} - ${target}" else print_error "Skipping Checksum creation because backup did not complete successfully" @@ -685,8 +701,8 @@ generate_checksum() { move_dbbackup() { if [ "${exit_code}" = "0" ] ; then - dbbackup_size="$(stat -c%s "${TEMP_LOCATION}"/"${target}")" - dbbackup_date="$(date -r "${TEMP_LOCATION}"/"${target}" +'%s')" + dbbackup_size="$(run_as_user stat -c%s "${TEMP_LOCATION}"/"${target}")" + dbbackup_date="$(run_as_user date -r "${TEMP_LOCATION}"/"${target}" +'%s')" case "${SIZE_VALUE,,}" in "b" | "bytes" ) @@ -700,26 +716,26 @@ move_dbbackup() { ;; esac if [ "$SIZE_VALUE" = "1" ] ; then - filesize=$(stat -c%s "${TEMP_LOCATION}"/"${target}") + filesize=$(run_as_user stat -c%s "${TEMP_LOCATION}"/"${target}") print_notice "Backup of ${target} created with the size of ${filesize} bytes" else - filesize=$(du -h "${TEMP_LOCATION}"/"${target}" | awk '{ print $1}') + filesize=$(run_as_user du -h "${TEMP_LOCATION}"/"${target}" | awk '{ print $1}') print_notice "Backup of ${target} created with the size of ${filesize}" fi case "${BACKUP_LOCATION,,}" in "file" | "filesystem" ) print_debug "Moving backup to filesystem" - mkdir -p "${DB_DUMP_TARGET}" - if var_true "${ENABLE_CHECKSUM}" ; then mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/ ; fi - mv "${TEMP_LOCATION}"/"${target}" "${DB_DUMP_TARGET}"/"${target}" + run_as_user mkdir -p "${DB_DUMP_TARGET}" + if var_true "${ENABLE_CHECKSUM}" ; then run_as_user mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/ ; fi + run_as_user mv "${TEMP_LOCATION}"/"${target}" "${DB_DUMP_TARGET}"/"${target}" move_exit_code=$? if var_true "${CREATE_LATEST_SYMLINK}" ; then - ln -sf "${DB_DUMP_TARGET}"/"${target}" "${DB_DUMP_TARGET}"/latest-"${ltarget}" + run_as_user ln -sf "${DB_DUMP_TARGET}"/"${target}" "${DB_DUMP_TARGET}"/latest-"${ltarget}" fi if [ -n "${DB_ARCHIVE_TIME}" ] ; then - mkdir -p "${DB_DUMP_TARGET_ARCHIVE}" - find "${DB_DUMP_TARGET}"/ -maxdepth 1 -mmin +"${DB_ARCHIVE_TIME}" -iname "*" -exec mv {} "${DB_DUMP_TARGET_ARCHIVE}" \; + run_as_user mkdir -p "${DB_DUMP_TARGET_ARCHIVE}" + run_as_user find "${DB_DUMP_TARGET}"/ -type f -maxdepth 1 -mmin +"${DB_ARCHIVE_TIME}" -iname "*" -exec mv {} "${DB_DUMP_TARGET_ARCHIVE}" \; fi ;; "s3" | "minio" ) @@ -745,39 +761,39 @@ move_dbbackup() { silent aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_LOCATION}/${target} s3://${S3_BUCKET}/${S3_PATH}/${target} ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} move_exit_code=$? if var_true "${ENABLE_CHECKSUM}" ; then - silent aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_LOCATION}/*.${checksum_extension} s3://${S3_BUCKET}/${S3_PATH}/ ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} + silent run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_LOCATION}/*.${checksum_extension} s3://${S3_BUCKET}/${S3_PATH}/ ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} fi - if var_true "${ENABLE_CHECKSUM}" ; then rm -rf "${TEMP_LOCATION}"/*."${checksum_extension}"; fi - rm -rf "${TEMP_LOCATION}"/"${target}" + if var_true "${ENABLE_CHECKSUM}" ; then run_as_user rm -rf "${TEMP_LOCATION}"/*."${checksum_extension}"; fi + run_as_user rm -rf "${TEMP_LOCATION}"/"${target}" ;; "blobxfer" ) print_info "Moving backup to external storage with blobxfer" mkdir -p "${DB_DUMP_TARGET}" - if var_true "${ENABLE_CHECKSUM}" ; then mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/; fi + if var_true "${ENABLE_CHECKSUM}" ; then run_as_user mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/; fi - mv "${TEMP_LOCATION}"/"${target}" "${DB_DUMP_TARGET}"/"${target}" + run_as_user mv "${TEMP_LOCATION}"/"${target}" "${DB_DUMP_TARGET}"/"${target}" - silent blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} + silent run_as_user blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} move_exit_code=$? - if var_true "${ENABLE_CHECKSUM}" ; then rm -rf "${TEMP_LOCATION}"/*."${checksum_extension}" ; fi - rm -rf "${TEMP_LOCATION}"/"${target}" + if var_true "${ENABLE_CHECKSUM}" ; then run_as_user rm -rf "${TEMP_LOCATION}"/*."${checksum_extension}" ; fi + run_as_user rm -rf "${TEMP_LOCATION}"/"${target}" ;; esac else print_error "Skipping moving DB Backup to final location because backup did not complete successfully" fi - rm -rf "${TEMP_LOCATION}"/* + run_as_user rm -rf "${TEMP_LOCATION}"/* } prepare_dbbackup() { - dbbackup_start_time=$(date +"%s") - now=$(date +"%Y%m%d-%H%M%S") - now_time=$(date +"%H:%M:%S") - now_date=$(date +"%Y-%m-%d") + dbbackup_start_time=$(run_as_user date +"%s") + now=$(run_as_user date +"%Y%m%d-%H%M%S") + now_time=$(run_as_user date +"%H:%M:%S") + now_date=$(run_as_user date +"%Y-%m-%d") ltarget=${dbtype}_${DB_NAME,,}_${DB_HOST,,} target=${dbtype}_${DB_NAME,,}_${DB_HOST,,}_${now}.sql } @@ -786,11 +802,11 @@ pre_dbbackup() { ### Pre Script Support if [ -n "${PRE_SCRIPT}" ] ; then if var_true "${PRE_SCRIPT_SKIP_X_VERIFY}" ; then - eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + run_as_user eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else if [ -x "${PRE_SCRIPT}" ] ; then print_notice "Found PRE_SCRIPT environment variable. Executing '${PRE_SCRIPT}" - eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + run_as_user eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else print_error "Can't execute PRE_SCRIPT environment variable '${PRE_SCRIPT}' as its filesystem bit is not executible!" fi @@ -800,19 +816,19 @@ pre_dbbackup() { ### Pre Backup Custom Script Support if [ -d "/assets/custom-scripts/pre" ] && dir_notempty "/assets/custom-scripts/pre" ; then print_warning "Found Custom Post Scripts in /assets/custom-scripts/pre - Automatically moving them to '${SCRIPT_LOCATION_PRE}'" - mkdir -p "${SCRIPT_LOCATION_PRE}" - silent cp /assets/custom-scripts/pre/* "${SCRIPT_LOCATION_PRE}" + run_as_user mkdir -p "${SCRIPT_LOCATION_PRE}" + silent run_as_user cp /assets/custom-scripts/pre/* "${SCRIPT_LOCATION_PRE}" fi if [ -d "${SCRIPT_LOCATION_PRE}" ] && dir_notempty "${SCRIPT_LOCATION_PRE}" ; then for f in $(find ${SCRIPT_LOCATION_PRE} -name \*.sh -type f); do if var_true "${PRE_SCRIPT_SKIP_X_VERIFY}" ; then - ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + run_as_user ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else if [ -x "${f}" ] ; then print_notice "Executing pre backup custom script : '${f}'" ## script DB_TYPE DB_HOST DB_NAME STARTEPOCH BACKUP_FILENAME - ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + run_as_user ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else print_error "Can't run pre backup custom script: '${f}' as its filesystem bit is not executible!" fi @@ -822,26 +838,27 @@ pre_dbbackup() { } post_dbbackup() { - dbbackup_finish_time=$(date +"%s") - dbbackup_total_time=$(echo $((dbbackup_finish_time-dbbackup_start_time))) + dbbackup_finish_time=$(run_as_user date +"%s") + dbbackup_total_time=$(run_as_user echo $((dbbackup_finish_time-dbbackup_start_time))) if var_true "${CONTAINER_ENABLE_MONITORING}" ; then print_notice "Sending Backup Statistics to Zabbix" - silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.size -o "${dbbackup_size}" - silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.datetime -o "${dbbackup_date}" - silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.status -o "${exit_code}" - silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.backup_duration -o "$(echo $((dbbackup_finish_time-dbbackup_start_time)))" + ## TODO - Optimize this into one command + silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.size -o "${dbbackup_size}" + silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.datetime -o "${dbbackup_date}" + silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.status -o "${exit_code}" + silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.backup_duration -o "$(echo $((dbbackup_finish_time-dbbackup_start_time)))" if [ "$?" != "0" ] ; then print_error "Error sending statistics, consider disabling with 'CONTAINER_ENABLE_MONITORING=FALSE'" ; fi fi ### Post Script Support if [ -n "${POST_SCRIPT}" ] ; then if var_true "${POST_SCRIPT_SKIP_X_VERIFY}" ; then - eval "${POST_SCRIPT}" "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" + run_as_user eval "${POST_SCRIPT}" "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" else if [ -x "${POST_SCRIPT}" ] ; then print_notice "Found POST_SCRIPT environment variable. Executing '${POST_SCRIPT}" - eval "${POST_SCRIPT}" "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" + run_as_user eval "${POST_SCRIPT}" "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" else print_error "Can't execute POST_SCRIPT environment variable '${POST_SCRIPT}' as its filesystem bit is not executible!" fi @@ -851,19 +868,19 @@ post_dbbackup() { ### Post Backup Custom Script Support if [ -d "/assets/custom-scripts/" ] && dir_notempty "/assets/custom-scripts" ; then print_warning "Found Custom Post Scripts in /assets/custom-scripts/ - Automatically moving them to '${SCRIPT_LOCATION_POST}'" - mkdir -p "${SCRIPT_LOCATION_POST}" - silent cp /assets/custom-scripts/* "${SCRIPT_LOCATION_POST}" + run_as_user mkdir -p "${SCRIPT_LOCATION_POST}" + silent run_as_user cp /assets/custom-scripts/* "${SCRIPT_LOCATION_POST}" fi if [ -d "${SCRIPT_LOCATION_POST}" ] && dir_notempty "${SCRIPT_LOCATION_POST}" ; then - for f in $(find ${SCRIPT_LOCATION_POST} -name \*.sh -type f); do + for f in $(run_as_user find ${SCRIPT_LOCATION_POST} -name \*.sh -type f); do if var_true "${POST_SCRIPT_SKIP_X_VERIFY}" ; then - ${f} "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" + run_as_user ${f} "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" else if [ -x "${f}" ] ; then print_notice "Executing post backup custom script : '${f}'" ## script EXIT_CODE DB_TYPE DB_HOST DB_NAME STARTEPOCH FINISHEPOCH DURATIONEPOCH BACKUP_FILENAME FILESIZE CHECKSUMVALUE - ${f} "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" + run_as_user ${f} "${exit_code}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" else print_error "Can't run post backup custom script: '${f}' as its filesystem bit is not executible!" fi @@ -876,6 +893,10 @@ post_dbbackup() { unset s3_ca_cert } +run_as_user() { + s6-setuidgid dbbackup $@ +} + sanity_test() { sanity_var DB_TYPE "Database Type" sanity_var DB_HOST "Database Host" diff --git a/install/etc/cont-init.d/10-db-backup b/install/etc/cont-init.d/10-db-backup index 46a75d4..4e854dd 100755 --- a/install/etc/cont-init.d/10-db-backup +++ b/install/etc/cont-init.d/10-db-backup @@ -6,6 +6,7 @@ prepare_service 03-monitoring PROCESS_NAME="db-backup" output_off +bootstrap_filesystem bootstrap_variables sanity_test setup_mode From bcf7bc5ecdfea92eed7e0936e9dc6a7cc670666d Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 1 Nov 2023 14:18:27 -0700 Subject: [PATCH 02/68] rearrange compression function --- install/assets/functions/10-db-backup | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 5543ef2..8587977 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -608,14 +608,6 @@ compression() { fi case "${COMPRESSION,,}" in - gz* ) - compress_cmd="pigz -q -${COMPRESSION_LEVEL} -p ${PARALLEL_COMPRESSION_THREADS} ${gz_rsyncable}" - compression_type="gzip" - extension=".gz" - dir_compress_cmd=${compress_cmd} - target_dir=${target} - target=${target}.gz - ;; bz* ) compress_cmd="pbzip2 -q -${COMPRESSION_LEVEL} -p${PARALLEL_COMPRESSION_THREADS} " compression_type="bzip2" @@ -624,6 +616,14 @@ compression() { target_dir=${target} target=${target}.bz2 ;; + gz* ) + compress_cmd="pigz -q -${COMPRESSION_LEVEL} -p ${PARALLEL_COMPRESSION_THREADS} ${gz_rsyncable}" + compression_type="gzip" + extension=".gz" + dir_compress_cmd=${compress_cmd} + target_dir=${target} + target=${target}.gz + ;; xz* ) compress_cmd="pixz -${COMPRESSION_LEVEL} -p ${PARALLEL_COMPRESSION_THREADS} " compression_type="xzip" From 3ff3cdb19c336e2df1181bc09cfbd42f414debf0 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 1 Nov 2023 14:32:43 -0700 Subject: [PATCH 03/68] feat - Add TARGET_DB_DUMP_PERMISSION to set file and directory permissions --- README.md | 21 +++++++++++---------- install/assets/defaults/10-db-backup | 1 + install/assets/functions/10-db-backup | 4 ++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f736563..c705cd4 100644 --- a/README.md +++ b/README.md @@ -175,16 +175,17 @@ Your Organization will be mapped to `DB_USER` and your root token will need to b ### Scheduling Options -| Parameter | Description | Default | -| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | -| `DB_DUMP_FREQ` | How often to do a dump, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | -| `DB_DUMP_BEGIN` | What time to do the first dump. Defaults to immediate. Must be in one of two formats | | -| | Absolute HHMM, e.g. `2330` or `0415` | | -| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | -| `DB_DUMP_TARGET` | Directory where the database dumps are kept. | `${DB_DUMP_TARGET}/archive/` | -| `DB_DUMP_TARGET_ARCHIVE` | Optional Directory where the database dumps archives are kept. | | -| `DB_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when dump frequency fires). 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | `FALSE` | -| `DB_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from `DB_DUMP_TARGET` to `DB_DUMP_TARGET_ARCHIVE` - which is useful when pairing against an external backup system. | | +| Parameter | Description | Default | +| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | +| `DB_DUMP_FREQ` | How often to do a dump, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | +| `DB_DUMP_BEGIN` | What time to do the first dump. Defaults to immediate. Must be in one of two formats | | +| | Absolute HHMM, e.g. `2330` or `0415` | | +| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | +| `DB_DUMP_TARGET` | Directory where the database dumps are kept. | `${DB_DUMP_TARGET}/archive/` | +| `DB_DUMP_TARGET_PERMISSION` | Directory and File permissions to apply to files. | `700` | +| `DB_DUMP_TARGET_ARCHIVE` | Optional Directory where the database dumps archives are kept. | | +| `DB_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when dump frequency fires). 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | `FALSE` | +| `DB_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from `DB_DUMP_TARGET` to `DB_DUMP_TARGET_ARCHIVE` - which is useful when pairing against an external backup system. | | - You may need to wrap your `DB_DUMP_BEGIN` value in quotes for it to properly parse. There have been reports of backups that start with a `0` get converted into a different format which will not allow the timer to start at the correct time. diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index 60280d8..bc65ce4 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -30,3 +30,4 @@ SIZE_VALUE=${SIZE_VALUE:-"bytes"} SKIP_AVAILABILITY_CHECK=${SKIP_AVAILABILITY_CHECK:-"FALSE"} SPLIT_DB=${SPLIT_DB:-"TRUE"} TEMP_LOCATION=${TEMP_LOCATION:-"/tmp/backups"} +DB_DUMP_TARGET_PERMISSION=${DB_DUMP_TARGET_PERMISSION:-"700"} \ No newline at end of file diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 8587977..b6ed680 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -5,9 +5,11 @@ bootstrap_filesystem() { mkdir -p "${DB_DUMP_TARGET}" fi if [ "$(stat -c %U "${DB_DUMP_TARGET}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${DB_DUMP_TARGET}" ; fi + if [ "$(stat -c %a "${DB_DUMP_TARGET}")" != "${DB_DUMP_TARGET_PERMISSION}" ] ; then chmod -R ${DB_DUMP_TARGET_PERMISSION} "${DB_DUMP_TARGET}" ; fi if [ -d "${DB_DUMP_TARGET_ARCHIVE}" ]; then if [ "$(stat -c %U "${DB_DUMP_TARGET_ARCHIVE}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${DB_DUMP_TARGET_ARCHIVE}" ; fi + if [ "$(stat -c %a "${DB_DUMP_TARGET_ARCHIVE}")" != "${DB_DUMP_TARGET_PERMISSION}" ] ; then chmod -R ${DB_DUMP_TARGET_PERMISSION} "${DB_DUMP_TARGET_ARCHIVE}" ; fi fi if [ ! -d "${TEMP_LOCATION}" ]; then @@ -690,6 +692,7 @@ generate_checksum() { print_notice "Generating ${checksum_extension^^} for '${target}'" cd "${TEMP_LOCATION}" run_as_user ${checksum_command} "${target}" | run_as_user tee "${target}"."${checksum_extension}" > /dev/null + chmod ${DB_DUMP_TARGET_PERMISSION} "${target}"."${checksum_extension}" ## TODO - We're doing this twice, why not just pull from the previously generated file checksum_value=$(run_as_user ${checksum_command} "${target}" | awk ' { print $1}') print_debug "${checksum_extension^^}: ${checksum_value} - ${target}" @@ -723,6 +726,7 @@ move_dbbackup() { print_notice "Backup of ${target} created with the size of ${filesize}" fi + chmod ${DB_DUMP_TARGET_PERMISSION} "${TEMP_LOCATION}"/"${target}" case "${BACKUP_LOCATION,,}" in "file" | "filesystem" ) print_debug "Moving backup to filesystem" From e03fefeb02650ac3ad1969bb39f7fbcc3ac78eaa Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 1 Nov 2023 14:34:34 -0700 Subject: [PATCH 04/68] fix - Optimize generating checksum routines --- install/assets/functions/10-db-backup | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index b6ed680..f0e7208 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -693,8 +693,7 @@ generate_checksum() { cd "${TEMP_LOCATION}" run_as_user ${checksum_command} "${target}" | run_as_user tee "${target}"."${checksum_extension}" > /dev/null chmod ${DB_DUMP_TARGET_PERMISSION} "${target}"."${checksum_extension}" - ## TODO - We're doing this twice, why not just pull from the previously generated file - checksum_value=$(run_as_user ${checksum_command} "${target}" | awk ' { print $1}') + checksum_value=$(run_as_user cat "${target}"."${checksum_extension}" | awk '{print $1}') print_debug "${checksum_extension^^}: ${checksum_value} - ${target}" else print_error "Skipping Checksum creation because backup did not complete successfully" From 74e7a7e74c62ee59243426c050b0367d61ea3f61 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 1 Nov 2023 14:43:43 -0700 Subject: [PATCH 05/68] feat - optimize zabbix sending routines to be one process --- install/assets/functions/10-db-backup | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index f0e7208..4956f0e 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -844,13 +844,15 @@ post_dbbackup() { dbbackup_finish_time=$(run_as_user date +"%s") dbbackup_total_time=$(run_as_user echo $((dbbackup_finish_time-dbbackup_start_time))) - if var_true "${CONTAINER_ENABLE_MONITORING}" ; then + if var_true "${CONTAINER_ENABLE_MONITORING}" && [ "${CONTAINER_MONITORING_BACKEND,,}" = "zabbix" ]; then + source /assets/defaults/03-monitoring print_notice "Sending Backup Statistics to Zabbix" - ## TODO - Optimize this into one command - silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.size -o "${dbbackup_size}" - silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.datetime -o "${dbbackup_date}" - silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.status -o "${exit_code}" - silent run_as_user zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.backup_duration -o "$(echo $((dbbackup_finish_time-dbbackup_start_time)))" + cat < Date: Wed, 1 Nov 2023 15:44:03 -0700 Subject: [PATCH 06/68] feat - add file logging support --- README.md | 5 +- install/assets/defaults/10-db-backup | 5 +- install/assets/functions/10-db-backup | 217 +++++++++++------- .../etc/services.available/10-db-backup/run | 17 +- 4 files changed, 153 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index c705cd4..2918618 100644 --- a/README.md +++ b/README.md @@ -144,14 +144,17 @@ Be sure to view the following repositories to understand all the customizable op | `BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | | `MODE` | `AUTO` mode to use internal scheduling routines or `MANUAL` to simply use this as manual backups only executed by your own means | `AUTO` | | `MANUAL_RUN_FOREVER` | `TRUE` or `FALSE` if you wish to try to make the container exit after the backup | `TRUE` | -| `TEMP_LOCATION` | Perform Backups and Compression in this temporary directory | `/tmp/backups/` | + | `DEBUG_MODE` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed. | `FALSE` | +| `LOG_PATH` | Path to log files | `/logs` | +| `LOG_FILE` | Logfile name | `dbbackup.log` | | `CREATE_LATEST_SYMLINK` | Create a symbolic link pointing to last backup in this format: `latest-(DB_TYPE)-(DB_NAME)-(DB_HOST)` | `TRUE` | | `PRE_SCRIPT` | Fill this variable in with a command to execute pre backing up | | | `POST_SCRIPT` | Fill this variable in with a command to execute post backing up | | | `USER_DBBACKUP` | The uid that the image should read and write files as (username is `dbbackup`) | `10000` | | `GROUP_DBBACKUP` | The gid that the image should read and write files as (groupname is `dbbackup`) | `10000` | | `SPLIT_DB` | For each backup, create a new archive. `TRUE` or `FALSE` (MySQL and Postgresql Only) | `TRUE` | +| `TEMP_LOCATION` | Perform Backups and Compression in this temporary directory | `/tmp/backups/` | ### Database Specific Options diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index bc65ce4..d87ba1b 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -9,9 +9,13 @@ CREATE_LATEST_SYMLINK=${CREATE_LATEST_SYMLINK:-"TRUE"} DB_DUMP_BEGIN=${DB_DUMP_BEGIN:-+0} DB_DUMP_FREQ=${DB_DUMP_FREQ:-1440} DB_DUMP_TARGET=${DB_DUMP_TARGET:-"/backup"} +DB_DUMP_TARGET_PERMISSION=${DB_DUMP_TARGET_PERMISSION:-"700"} DB_DUMP_TARGET_ARCHIVE=${DB_DUMP_TARGET_ARCHIVE:-"${DB_DUMP_TARGET}/archive/"} ENABLE_CHECKSUM=${ENABLE_CHECKSUM:-"TRUE"} ENABLE_PARALLEL_COMPRESSION=${ENABLE_PARALLEL_COMPRESSION:-"TRUE"} +LOG_FILE=${LOG_FILE:-"dbbackup.log"} +LOG_LEVEL=${LOG_LEVEL:-"debug"} +LOG_PATH=${LOG_PATH:-"/logs"} MANUAL_RUN_FOREVER=${MANUAL_RUN_FOREVER:-"TRUE"} MODE=${MODE:-"AUTO"} MYSQL_ENABLE_TLS=${MYSQL_ENABLE_TLS:-"FALSE"} @@ -30,4 +34,3 @@ SIZE_VALUE=${SIZE_VALUE:-"bytes"} SKIP_AVAILABILITY_CHECK=${SKIP_AVAILABILITY_CHECK:-"FALSE"} SPLIT_DB=${SPLIT_DB:-"TRUE"} TEMP_LOCATION=${TEMP_LOCATION:-"/tmp/backups"} -DB_DUMP_TARGET_PERMISSION=${DB_DUMP_TARGET_PERMISSION:-"700"} \ No newline at end of file diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 4956f0e..306c006 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -12,6 +12,15 @@ bootstrap_filesystem() { if [ "$(stat -c %a "${DB_DUMP_TARGET_ARCHIVE}")" != "${DB_DUMP_TARGET_PERMISSION}" ] ; then chmod -R ${DB_DUMP_TARGET_PERMISSION} "${DB_DUMP_TARGET_ARCHIVE}" ; fi fi + if [ ! -d "${LOG_PATH}" ]; then + mkdir -p "${LOG_PATH}" + fi + + if [ "$(stat -c %U "${LOG_PATH}")" != "dbbackup" ] ; then chown dbbackup:dbbackup "${LOG_PATH}" ; fi + if [ "$(stat -c %a "${LOG_PATH}")" != "755" ] ; then chmod -R 755 "${LOG_PATH}" ; fi + + create_logrotate dbbackup "${LOG_PATH}"/"${LOG_FILE}" none dbbackup dbbackup + if [ ! -d "${TEMP_LOCATION}" ]; then mkdir -p "${TEMP_LOCATION}" fi @@ -98,7 +107,7 @@ bootstrap_variables() { apkArch="$(apk --print-arch)"; \ case "$apkArch" in x86_64) mssql=true ;; - *) print_error "MSSQL cannot operate on $apkArch processor!" ; exit 1 ;; + *) write_log error "MSSQL cannot operate on $apkArch processor!" ; exit 1 ;; esac dbtype=mssql DB_PORT=${DB_PORT:-1433} @@ -118,7 +127,7 @@ bootstrap_variables() { dbtype=sqlite3 ;; * ) - print_error "I don't recognize 'DB_TYPE=${DB_TYPE}' - Exitting.." + write_log error "I don't recognize 'DB_TYPE=${DB_TYPE}' - Exitting.." exit 99 ;; esac @@ -149,7 +158,7 @@ backup_couch() { ltarget=couch_${DB_NAME}_${DB_HOST#*//} compression pre_dbbackup ${DB_NAME} - print_notice "Dumping CouchDB database: '${DB_NAME}' ${compression_string}" + write_log notice "Dumping CouchDB database: '${DB_NAME}' ${compression_string}" run_as_user curl -sSL -X GET ${DB_HOST}:${DB_PORT}/${DB_NAME}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target @@ -161,7 +170,7 @@ backup_couch() { backup_influx() { if [ "${DB_NAME,,}" = "all" ] ; then - print_debug "Preparing to back up everything" + write_log debug "Preparing to back up everything" db_names=justbackupeverything else db_names=$(echo "${DB_NAME}" | tr ',' '\n') @@ -176,11 +185,11 @@ backup_influx() { ltarget=influx_${db}_${DB_HOST#*//} compression pre_dbbackup $db - print_notice "Dumping Influx database: '${db}'" + write_log notice "Dumping Influx database: '${db}'" run_as_user influxd backup ${influx_compression} ${bucket} -portable -host ${DB_HOST}:${DB_PORT} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} "${TEMP_LOCATION}"/"${target_dir}" exit_code=$? check_exit_code backup $target_dir - print_notice "Creating archive file of '${target_dir}' with tar ${compression_string}" + write_log notice "Creating archive file of '${target_dir}' with tar ${compression_string}" run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" > /dev/null target=influx_${db}_${DB_HOST#*//}_${now}.tar${extension} ltarget=influx_${db}_${DB_HOST#*//} @@ -198,7 +207,7 @@ backup_influx() { ltarget=influx2_${db}_${DB_HOST#*//} compression pre_dbbackup $db - print_notice "Dumping Influx2 database: '${db}'" + write_log notice "Dumping Influx2 database: '${db}'" run_as_user influx backup --org ${DB_USER} ${bucket} --host ${DB_HOST}:${DB_PORT} --token ${DB_PASS} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --compression none "${TEMP_LOCATION}"/"${target_dir}" exit_code=$? check_exit_code backup $target_dir @@ -231,7 +240,7 @@ backup_mongo() { mongo_backup_parameter="--host ${DB_HOST} --port ${DB_PORT} ${MONGO_USER_STR}${MONGO_PASS_STR}${MONGO_AUTH_STR}${MONGO_DB_STR} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS}" fi pre_dbbackup "${DB_NAME}" - print_notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" + write_log notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" silent run_as_user mongodump --archive=${TEMP_LOCATION}/${target} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? check_exit_code backup $target @@ -247,7 +256,7 @@ backup_mssql() { ltarget=mssql_${DB_NAME,,}_${DB_HOST,,} compression pre_dbbackup "${DB_NAME}" - print_notice "Dumping MSSQL database: '${DB_NAME}'" + write_log notice "Dumping MSSQL database: '${DB_NAME}'" silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${DB_HOST}\,${DB_PORT} -U ${DB_USER} -P ${DB_PASS} -Q "BACKUP DATABASE [${DB_NAME}] TO DISK = N'${TEMP_LOCATION}/${target}' WITH NOFORMAT, NOINIT, NAME = '${DB_NAME}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target @@ -266,12 +275,12 @@ backup_mysql() { fi if [ "${DB_NAME,,}" = "all" ] ; then - print_debug "Preparing to back up everything except for information_schema and _* prefixes" + write_log debug "Preparing to back up everything except for information_schema and _* prefixes" db_names=$(run_as_user mysql -h ${DB_HOST} -P $DB_PORT -u$DB_USER ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_ENUMERATION_OPTS} --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema ) if [ -n "${DB_NAME_EXCLUDE}" ] ; then db_names_exclusions=$(echo "${DB_NAME_EXCLUDE}" | tr ',' '\n') for db_exclude in ${db_names_exclusions} ; do - print_debug "Excluding '${db_exclude}' from ALL DB_NAME backups" + write_log debug "Excluding '${db_exclude}' from ALL DB_NAME backups" db_names=$(echo "$db_names" | sed "/${db_exclude}/d" ) done fi @@ -279,7 +288,7 @@ backup_mysql() { db_names=$(echo "${DB_NAME}" | tr ',' '\n') fi - print_debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" + write_log debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" if var_true "${SPLIT_DB}" ; then for db in ${db_names} ; do @@ -288,7 +297,7 @@ backup_mysql() { ltarget=mysql_${db}_${DB_HOST,,} compression pre_dbbackup $db - print_notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" + write_log notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" run_as_user mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} $db | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target @@ -298,13 +307,13 @@ backup_mysql() { post_dbbackup $db done else - print_debug "Not splitting database dumps into their own files" + write_log debug "Not splitting database dumps into their own files" prepare_dbbackup target=mysql_all_${DB_HOST,,}_${now}.sql ltarget=mysql_all_${DB_HOST,,} compression pre_dbbackup all - print_notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" + write_log notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" run_as_user mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target @@ -323,12 +332,12 @@ backup_pgsql() { authdb=${DB_USER} fi if [ "${DB_NAME,,}" = "all" ] ; then - print_debug "Preparing to back up all databases" + write_log debug "Preparing to back up all databases" db_names=$(run_as_user psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) if [ -n "${DB_NAME_EXCLUDE}" ] ; then db_names_exclusions=$(echo "${DB_NAME_EXCLUDE}" | tr ',' '\n') for db_exclude in ${db_names_exclusions} ; do - print_debug "Excluding '${db_exclude}' from ALL DB_NAME backups" + write_log debug "Excluding '${db_exclude}' from ALL DB_NAME backups" db_names=$(echo "$db_names" | sed "/${db_exclude}/d" ) done fi @@ -336,7 +345,7 @@ backup_pgsql() { db_names=$(echo "${DB_NAME}" | tr ',' '\n') fi - print_debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" + write_log debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" if var_true "${SPLIT_DB}" ; then for db in ${db_names} ; do @@ -345,7 +354,7 @@ backup_pgsql() { ltarget=pgsql_${db}_${DB_HOST,,} compression pre_dbbackup $db - print_notice "Dumping PostgresSQL database: '${db}' ${compression_string}" + write_log notice "Dumping PostgresSQL database: '${db}' ${compression_string}" run_as_user pg_dump -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} $db ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target @@ -355,13 +364,13 @@ backup_pgsql() { post_dbbackup $db done else - print_debug "Not splitting database dumps into their own files" + write_log debug "Not splitting database dumps into their own files" prepare_dbbackup target=pgsql_all_${DB_HOST,,}_${now}.sql ltarget=pgsql_${db}_${DB_HOST,,} compression pre_dbbackup all - print_notice "Dumping all PostgreSQL databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" + write_log notice "Dumping all PostgreSQL databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" tmp_db_names=$(run_as_user psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) for r_db_name in $(echo $db_names | xargs); do tmp_db_names=$(echo "$tmp_db_names" | xargs | sed "s|${r_db_name}||g" ) @@ -382,7 +391,7 @@ backup_pgsql() { backup_redis() { prepare_dbbackup - print_notice "Dumping Redis - Flushing Redis Cache First" + write_log notice "Dumping Redis - Flushing Redis Cache First" target=redis_all_${DB_HOST,,}_${now}.rdb ltarget=redis_${DB_HOST,,} echo bgsave | silent run_as_user redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} --rdb ${TEMP_LOCATION}/${target} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} @@ -392,12 +401,12 @@ backup_redis() { saved=$(echo 'info Persistence' | redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} | awk '/rdb_bgsave_in_progress:0/{print "saved"}') ok=$(echo 'info Persistence' | redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} | awk '/rdb_last_bgsave_status:ok/{print "ok"}') if [[ "$saved" = "saved" ]] && [[ "$ok" = "ok" ]]; then - print_notice "Redis Backup Complete" + write_log notice "Redis Backup Complete" exit_code=0 break fi try=$((try - 1)) - print_warn "Redis Busy - Waiting and retrying in 5 seconds" + write_log warn "Redis Busy - Waiting and retrying in 5 seconds" sleep 5 done target_original=${target} @@ -419,7 +428,7 @@ backup_sqlite3() { ltarget=sqlite3_${db}.sqlite3 compression pre_dbbackup $db - print_notice "Dumping sqlite3 database: '${DB_HOST}' ${compression_string}" + write_log notice "Dumping sqlite3 database: '${DB_HOST}' ${compression_string}" silent run_as_user sqlite3 "${DB_HOST}" ".backup '${TEMP_LOCATION}/backup.sqlite3'" exit_code=$? check_exit_code backup $target @@ -442,7 +451,7 @@ check_availability() { if [ "${code_received}" = "200" ] ; then break ; fi sleep 5 (( counter+=5 )) - print_warn "CouchDB Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "CouchDB Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" done ;; "influx" ) @@ -452,7 +461,7 @@ check_availability() { while ! (run_as_user nc -z ${DB_HOST#*//} ${DB_PORT}) ; do sleep 5 (( counter+=5 )) - print_warn "InfluxDB Host '${DB_HOST#*//}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "InfluxDB Host '${DB_HOST#*//}' is not accessible, retrying.. ($counter seconds so far)" done ;; 2 ) @@ -462,20 +471,20 @@ check_availability() { if [ "${code_received}" = "200" ] ; then break ; fi sleep 5 (( counter+=5 )) - print_warn "InfluxDB Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "InfluxDB Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" done ;; esac ;; "mongo" ) if [ -n "${MONGO_CUSTOM_URI}" ] ; then - print_debug "Skipping Connectivity Check" + write_log debug "Skipping Connectivity Check" else counter=0 while ! (run_as_user nc -z ${DB_HOST} ${DB_PORT}) ; do sleep 5 (( counter+=5 )) - print_warn "Mongo Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "Mongo Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" done fi ;; @@ -486,7 +495,7 @@ check_availability() { while ! (run_as_user mysqladmin -u"${DB_USER}" -P"${DB_PORT}" -h"${DB_HOST}" ${mysql_tls_args} status > /dev/null 2>&1) ; do sleep 5 (( counter+=5 )) - print_warn "MySQL/MariaDB Server '${DB_HOST}' is not accessible, retrying.. (${counter} seconds so far)" + write_log warn "MySQL/MariaDB Server '${DB_HOST}' is not accessible, retrying.. (${counter} seconds so far)" done ;; "mssql" ) @@ -494,7 +503,7 @@ check_availability() { while ! (run_as_user nc -z ${DB_HOST} ${DB_PORT}) ; do sleep 5 (( counter+=5 )) - print_warn "MSSQL Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "MSSQL Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" done ;; "pgsql" ) @@ -503,7 +512,7 @@ check_availability() { do sleep 5 (( counter+=5 )) - print_warn "Postgres Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "Postgres Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" done ;; "redis" ) @@ -511,20 +520,20 @@ check_availability() { while ! (run_as_user nc -z "${DB_HOST}" "${DB_PORT}") ; do sleep 5 (( counter+=5 )) - print_warn "Redis Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "Redis Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" done ;; "sqlite3" ) if [[ ! -e "${DB_HOST}" ]]; then - print_error "File '${DB_HOST}' does not exist." + write_log error "File '${DB_HOST}' does not exist." exit_code=2 exit $exit_code elif [[ ! -f "${DB_HOST}" ]]; then - print_error "File '${DB_HOST}' is not a file." + write_log error "File '${DB_HOST}' is not a file." exit_code=2 exit $exit_code elif [[ ! -r "${DB_HOST}" ]]; then - print_error "File '${DB_HOST}' is not readable." + write_log error "File '${DB_HOST}' is not readable." exit_code=2 exit $exit_code fi @@ -534,15 +543,15 @@ check_availability() { } check_exit_code() { - print_debug "DB Backup Exit Code is ${exit_code}" + write_log debug "DB Backup Exit Code is ${exit_code}" case "${1}" in backup ) case "${exit_code}" in 0 ) - print_info "DB Backup of '${2}' completed successfully" + write_log info "DB Backup of '${2}' completed successfully" ;; * ) - print_error "DB Backup of '${2}' reported errors" + write_log error "DB Backup of '${2}' reported errors" master_exit_code=1 ;; esac @@ -550,10 +559,10 @@ check_exit_code() { move ) case "${move_exit_code}" in 0 ) - print_debug "Moving of backup '${2}' completed successfully" + write_log debug "Moving of backup '${2}' completed successfully" ;; * ) - print_error "Moving of backup '${2}' reported errors" + write_log error "Moving of backup '${2}' reported errors" master_exit_code=1 ;; esac @@ -566,19 +575,19 @@ cleanup_old_data() { if [ "${master_exit_code}" != 1 ]; then case "${BACKUP_LOCATION,,}" in "blobxfer" ) - print_info "Cleaning up old backups on filesystem" + write_log info "Cleaning up old backups on filesystem" run_as_user mkdir -p "${DB_DUMP_TARGET}" find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm -f {} \; - print_info "Syncing changes via blobxfer" + write_log info "Syncing changes via blobxfer" silent run_as_user blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} --delete --delete-only ;; "file" | "filesystem" ) - print_info "Cleaning up old backups on filesystem" + write_log info "Cleaning up old backups on filesystem" run_as_user mkdir -p "${DB_DUMP_TARGET}" run_as_user find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm -f {} \; ;; "s3" | "minio" ) - print_info "Cleaning up old backups on S3 storage" + write_log info "Cleaning up old backups on S3 storage" run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 ls s3://${S3_BUCKET}/${S3_PATH}/ ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} | grep " DIR " -v | grep " PRE " -v | while read -r s3_file; do s3_createdate=$(echo $s3_file | awk {'print $1" "$2'}) s3_createdate=$(date -d "$s3_createdate" "+%s") @@ -586,7 +595,7 @@ cleanup_old_data() { if [[ $s3_createdate -le $s3_olderthan ]] ; then s3_filename=$(echo $s3_file | awk {'print $4'}) if [ "$s3_filename" != "" ] ; then - print_debug "Deleting $s3_filename" + write_log debug "Deleting $s3_filename" run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 rm s3://${S3_BUCKET}/${S3_PATH}/${s3_filename} ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} fi fi @@ -595,7 +604,7 @@ cleanup_old_data() { ;; esac else - print_error "Skipping Cleaning up old backups because there were errors in backing up" + write_log error "Skipping Cleaning up old backups because there were errors in backing up" fi fi } @@ -668,10 +677,10 @@ compression() { create_archive() { if [ "${exit_code}" = "0" ] ; then - print_notice "Creating archive file of '${target_dir}' with tar ${compression_string}" + write_log notice "Creating archive file of '${target_dir}' with tar ${compression_string}" run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} > "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" else - print_error "Skipping creating archive file because backup did not complete successfully" + write_log error "Skipping creating archive file because backup did not complete successfully" fi } @@ -689,14 +698,14 @@ generate_checksum() { ;; esac - print_notice "Generating ${checksum_extension^^} for '${target}'" + write_log notice "Generating ${checksum_extension^^} for '${target}'" cd "${TEMP_LOCATION}" run_as_user ${checksum_command} "${target}" | run_as_user tee "${target}"."${checksum_extension}" > /dev/null chmod ${DB_DUMP_TARGET_PERMISSION} "${target}"."${checksum_extension}" checksum_value=$(run_as_user cat "${target}"."${checksum_extension}" | awk '{print $1}') - print_debug "${checksum_extension^^}: ${checksum_value} - ${target}" + write_log debug "${checksum_extension^^}: ${checksum_value} - ${target}" else - print_error "Skipping Checksum creation because backup did not complete successfully" + write_log error "Skipping Checksum creation because backup did not complete successfully" fi fi } @@ -719,16 +728,16 @@ move_dbbackup() { esac if [ "$SIZE_VALUE" = "1" ] ; then filesize=$(run_as_user stat -c%s "${TEMP_LOCATION}"/"${target}") - print_notice "Backup of ${target} created with the size of ${filesize} bytes" + write_log notice "Backup of ${target} created with the size of ${filesize} bytes" else filesize=$(run_as_user du -h "${TEMP_LOCATION}"/"${target}" | awk '{ print $1}') - print_notice "Backup of ${target} created with the size of ${filesize}" + write_log notice "Backup of ${target} created with the size of ${filesize}" fi chmod ${DB_DUMP_TARGET_PERMISSION} "${TEMP_LOCATION}"/"${target}" case "${BACKUP_LOCATION,,}" in "file" | "filesystem" ) - print_debug "Moving backup to filesystem" + write_log debug "Moving backup to filesystem" run_as_user mkdir -p "${DB_DUMP_TARGET}" if var_true "${ENABLE_CHECKSUM}" ; then run_as_user mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/ ; fi run_as_user mv "${TEMP_LOCATION}"/"${target}" "${DB_DUMP_TARGET}"/"${target}" @@ -742,20 +751,20 @@ move_dbbackup() { fi ;; "s3" | "minio" ) - print_debug "Moving backup to S3 Bucket" + write_log debug "Moving backup to S3 Bucket" if [ -n "${S3_KEY_ID}" ] && [ -n "${S3_KEY_SECRET}" ]; then export AWS_ACCESS_KEY_ID=${S3_KEY_ID} export AWS_SECRET_ACCESS_KEY=${S3_KEY_SECRET} else - print_debug "Variable S3_KEY_ID or S3_KEY_SECRET is not set. Please ensure sufficiant IAM role is assigned." + write_log debug "Variable S3_KEY_ID or S3_KEY_SECRET is not set. Please ensure sufficiant IAM role is assigned." fi export AWS_DEFAULT_REGION=${S3_REGION} if [ -f "${S3_CERT_CA_FILE}" ] ; then - print_debug "Using Custom CA for S3 Backups" + write_log debug "Using Custom CA for S3 Backups" s3_ca_cert="--ca-bundle ${S3_CERT_CA_FILE}" fi if var_true "${S3_CERT_SKIP_VERIFY}" ; then - print_debug "Skipping SSL verification for HTTPS S3 Hosts" + write_log debug "Skipping SSL verification for HTTPS S3 Hosts" s3_ssl="--no-verify-ssl" fi @@ -771,7 +780,7 @@ move_dbbackup() { run_as_user rm -rf "${TEMP_LOCATION}"/"${target}" ;; "blobxfer" ) - print_info "Moving backup to external storage with blobxfer" + write_log info "Moving backup to external storage with blobxfer" mkdir -p "${DB_DUMP_TARGET}" if var_true "${ENABLE_CHECKSUM}" ; then run_as_user mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/; fi @@ -786,7 +795,7 @@ move_dbbackup() { ;; esac else - print_error "Skipping moving DB Backup to final location because backup did not complete successfully" + write_log error "Skipping moving DB Backup to final location because backup did not complete successfully" fi run_as_user rm -rf "${TEMP_LOCATION}"/* @@ -808,17 +817,17 @@ pre_dbbackup() { run_as_user eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else if [ -x "${PRE_SCRIPT}" ] ; then - print_notice "Found PRE_SCRIPT environment variable. Executing '${PRE_SCRIPT}" + write_log notice "Found PRE_SCRIPT environment variable. Executing '${PRE_SCRIPT}" run_as_user eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else - print_error "Can't execute PRE_SCRIPT environment variable '${PRE_SCRIPT}' as its filesystem bit is not executible!" + write_log error "Can't execute PRE_SCRIPT environment variable '${PRE_SCRIPT}' as its filesystem bit is not executible!" fi fi fi ### Pre Backup Custom Script Support if [ -d "/assets/custom-scripts/pre" ] && dir_notempty "/assets/custom-scripts/pre" ; then - print_warning "Found Custom Post Scripts in /assets/custom-scripts/pre - Automatically moving them to '${SCRIPT_LOCATION_PRE}'" + write_log warning "Found Custom Post Scripts in /assets/custom-scripts/pre - Automatically moving them to '${SCRIPT_LOCATION_PRE}'" run_as_user mkdir -p "${SCRIPT_LOCATION_PRE}" silent run_as_user cp /assets/custom-scripts/pre/* "${SCRIPT_LOCATION_PRE}" fi @@ -829,11 +838,11 @@ pre_dbbackup() { run_as_user ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else if [ -x "${f}" ] ; then - print_notice "Executing pre backup custom script : '${f}'" + write_log notice "Executing pre backup custom script : '${f}'" ## script DB_TYPE DB_HOST DB_NAME STARTEPOCH BACKUP_FILENAME run_as_user ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" else - print_error "Can't run pre backup custom script: '${f}' as its filesystem bit is not executible!" + write_log error "Can't run pre backup custom script: '${f}' as its filesystem bit is not executible!" fi fi done @@ -846,14 +855,14 @@ post_dbbackup() { if var_true "${CONTAINER_ENABLE_MONITORING}" && [ "${CONTAINER_MONITORING_BACKEND,,}" = "zabbix" ]; then source /assets/defaults/03-monitoring - print_notice "Sending Backup Statistics to Zabbix" - cat < /dev/null + ;; + esac + ;; + error ) + shift 1 + case "${LOG_LEVEL,,}" in + "debug" | "notice" | "warn" | "error") + print_error "$@" + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + ;; + esac + ;; + info ) + shift 1 + print_info "$@" + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + ;; + notice ) + shift 1 + case "${LOG_LEVEL,,}" in + "debug" | "notice" ) + print_notice "$@" + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + ;; + esac + ;; + warn ) + shift 1 + case "${LOG_LEVEL,,}" in + "debug" | "notice" | "warn" ) + print_warn "$@" + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + ;; + esac + ;; + esac +} \ No newline at end of file diff --git a/install/etc/services.available/10-db-backup/run b/install/etc/services.available/10-db-backup/run index 546383e..1fd046d 100755 --- a/install/etc/services.available/10-db-backup/run +++ b/install/etc/services.available/10-db-backup/run @@ -7,10 +7,11 @@ PROCESS_NAME="db-backup" bootstrap_variables +write_log info "Starting instance - hostname $(hostname) container_name ${CONTAINER_NAME}" if [ "${MODE,,}" = "manual" ] || [ "${1,,}" = "manual" ] || [ "${1,,}" = "now" ]; then DB_DUMP_BEGIN=+0 manual=TRUE - print_debug "Detected Manual Mode" + write_log debug "Detected Manual Mode" else sleep 5 current_time=$(date +"%s") @@ -26,15 +27,15 @@ else fi waittime=$(($target_time - $current_time)) fi - print_debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}" - print_info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")" + write_log debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}" + write_log info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")" sleep $waittime fi while true; do mkdir -p "${TEMP_LOCATION}" backup_start_time=$(date +"%s") - print_debug "Backup routines started time: $(date +'%Y-%m-%d %T %Z')" + write_log debug "Backup routines started time: $(date +'%Y-%m-%d %T %Z')" case "${dbtype,,}" in "couch" ) check_availability @@ -73,16 +74,16 @@ while true; do backup_finish_time=$(date +"%s") backup_total_time=$(echo $((backup_finish_time-backup_start_time))) if [ -z "$master_exit_code" ] ; then master_exit_code="0" ; fi - print_info "Backup routines finish time: $(date -d @${backup_finish_time} +"%Y-%m-%d %T %Z") with overall exit code ${master_exit_code}" - print_notice "Backup routines time taken: $(echo ${backup_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" + write_log info "Backup routines finish time: $(date -d @${backup_finish_time} +"%Y-%m-%d %T %Z") with overall exit code ${master_exit_code}" + write_log notice "Backup routines time taken: $(echo ${backup_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" cleanup_old_data if var_true "${manual}" ; then - print_debug "Exiting due to manual mode" + write_log debug "Exiting due to manual mode" exit ${master_exit_code}; else - print_notice "Sleeping for another $(($DB_DUMP_FREQ*60-backup_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($DB_DUMP_FREQ*60-backup_total_time))))" +"%Y-%m-%d %T %Z") " + write_log notice "Sleeping for another $(($DB_DUMP_FREQ*60-backup_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($DB_DUMP_FREQ*60-backup_total_time))))" +"%Y-%m-%d %T %Z") " sleep $(($DB_DUMP_FREQ*60-backup_total_time)) fi done From 2b10a0b6791f94d2b212114dcfbcd3731b52b0a8 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 1 Nov 2023 15:55:32 -0700 Subject: [PATCH 07/68] feat - only cleanup / mv backups of the same file_name pattern --- install/assets/functions/10-db-backup | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 306c006..6b1fc23 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -577,14 +577,14 @@ cleanup_old_data() { "blobxfer" ) write_log info "Cleaning up old backups on filesystem" run_as_user mkdir -p "${DB_DUMP_TARGET}" - find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm -f {} \; + find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "${ltarget}*" -exec rm -f {} \; write_log info "Syncing changes via blobxfer" silent run_as_user blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} --delete --delete-only ;; "file" | "filesystem" ) write_log info "Cleaning up old backups on filesystem" run_as_user mkdir -p "${DB_DUMP_TARGET}" - run_as_user find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "*" -exec rm -f {} \; + run_as_user find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "${ltarget}*" -exec rm -f {} \; ;; "s3" | "minio" ) write_log info "Cleaning up old backups on S3 storage" @@ -599,7 +599,6 @@ cleanup_old_data() { run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 rm s3://${S3_BUCKET}/${S3_PATH}/${s3_filename} ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} fi fi - done ;; esac @@ -678,7 +677,7 @@ compression() { create_archive() { if [ "${exit_code}" = "0" ] ; then write_log notice "Creating archive file of '${target_dir}' with tar ${compression_string}" - run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} > "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" + run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" > /dev/null else write_log error "Skipping creating archive file because backup did not complete successfully" fi @@ -747,7 +746,7 @@ move_dbbackup() { fi if [ -n "${DB_ARCHIVE_TIME}" ] ; then run_as_user mkdir -p "${DB_DUMP_TARGET_ARCHIVE}" - run_as_user find "${DB_DUMP_TARGET}"/ -type f -maxdepth 1 -mmin +"${DB_ARCHIVE_TIME}" -iname "*" -exec mv {} "${DB_DUMP_TARGET_ARCHIVE}" \; + run_as_user find "${DB_DUMP_TARGET}"/ -type f -maxdepth 1 -mmin +"${DB_ARCHIVE_TIME}" -iname "${ltarget}*" -exec mv {} "${DB_DUMP_TARGET_ARCHIVE}" \; fi ;; "s3" | "minio" ) From 96f9825e19251d319aae2a9207f956e22bb24b78 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 14:59:44 -0700 Subject: [PATCH 08/68] Update README.md with new options --- README.md | 797 ++++++++++++++++++++------ install/assets/defaults/10-db-backup | 1 - install/assets/functions/10-db-backup | 12 +- 3 files changed, 615 insertions(+), 195 deletions(-) diff --git a/README.md b/README.md index 2918618..526cace 100644 --- a/README.md +++ b/README.md @@ -13,21 +13,22 @@ This will build a container for backing up multiple types of DB Servers -Currently backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis servers. +Backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis servers. - dump to local filesystem or backup to S3 Compatible services, and Azure. -- select database user and password -- backup all databases, single, or multiple databases -- backup all to separate files or one singular file -- choose to have an MD5 or SHA1 sum after backup for verification -- delete old backups after specific amount of time -- choose compression type (none, gz, bz, xz, zstd) -- connect to any container running on the same system -- Script to perform restores -- Zabbix Monitoring capabilities -- select how often to run a dump -- select when to start the first dump, whether time of day or relative to container start time -- Execute script after backup for monitoring/alerting purposes +- multiple backup job support + - selectable when to start the first dump, whether time of day or relative to container start time + - selectable interval + - selectable omit scheduling during periods of time + - selectable database user and password + - selectable cleanup and archive capabilities + - selectable database name support - all databases, single, or multiple databases + - backup all to separate files or one singular file +- checksum support choose to have an MD5 or SHA1 hash generated after backup for verification +- compression support (none, gz, bz, xz, zstd) +- Zabbix Metrics support +- Hooks to execute pre and post backup job for customization purposes +- Companion script to aid in restores ## Maintainer @@ -49,20 +50,56 @@ Currently backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, R - [Environment Variables](#environment-variables) - [Base Images used](#base-images-used) - [Container Options](#container-options) - - [Database Specific Options](#database-specific-options) - - [For Influx DB2](#for-influx-db2) - - [Scheduling Options](#scheduling-options) - - [Backup Options](#backup-options) - - [Backing Up to S3 Compatible Services](#backing-up-to-s3-compatible-services) - - [Upload to a Azure storage account by `blobxfer`](#upload-to-a-azure-storage-account-by-blobxfer) + - [Job Defaults](#job-defaults) + - [Compression Options](#compression-options) + - [Scheduling Options](#scheduling-options) + - [Default Database Options](#default-database-options) + - [CouchDB](#couchdb) + - [InfluxDB](#influxdb) + - [MariaDB/MySQL](#mariadbmysql) + - [Microsoft SQL](#microsoft-sql) + - [MongoDB](#mongodb) + - [Postgresql](#postgresql) + - [Redis](#redis) + - [Default Storage Options](#default-storage-options) + - [Filesystem](#filesystem) + - [S3](#s3) + - [Azure](#azure) + - [Hooks](#hooks) + - [Path Options](#path-options) + - [Pre Backup](#pre-backup) + - [Post backup](#post-backup) + - [Job Backup Options](#job-backup-options) + - [Compression Options](#compression-options-1) + - [Scheduling Options](#scheduling-options-1) + - [Specific Database Options](#specific-database-options) + - [CouchDB](#couchdb-1) + - [InfluxDB](#influxdb-1) + - [MariaDB/MySQL](#mariadbmysql-1) + - [Microsoft SQL](#microsoft-sql-1) + - [MongoDB](#mongodb-1) + - [Postgresql](#postgresql-1) + - [Redis](#redis-1) + - [SQLite](#sqlite) + - [Specific Storage Options](#specific-storage-options) + - [Filesystem](#filesystem-1) + - [S3](#s3-1) + - [Azure](#azure-1) + - [Hooks](#hooks-1) + - [Path Options](#path-options-1) + - [Pre Backup](#pre-backup-1) + - [Post backup](#post-backup-1) + - [Notifications](#notifications) + - [Custom Notifications](#custom-notifications) + - [Email Notifications](#email-notifications) + - [Matrix Notifications](#matrix-notifications) + - [Mattermost Notifications](#mattermost-notifications) + - [Rocketchat Notifications](#rocketchat-notifications) - [Maintenance](#maintenance) - [Shell Access](#shell-access) - [Manual Backups](#manual-backups) + - [Manual Backups](#manual-backups-1) - [Restoring Databases](#restoring-databases) - - [Custom Scripts](#custom-scripts) - - [Path Options](#path-options) - - [Pre Backup](#pre-backup) - - [Post backup](#post-backup) - [Support](#support) - [Usage](#usage) - [Bugfixes](#bugfixes) @@ -70,8 +107,6 @@ Currently backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, R - [Updates](#updates) - [License](#license) -> **NOTE**: If you are using this with a docker-compose file along with a seperate SQL container, take care not to set the variables to backup immediately, more so have it delay execution for a minute, otherwise you will get a failed first backup. - ## Prerequisites and Assumptions - You must have a working connection to one of the supported DB Servers and appropriate credentials @@ -110,11 +145,10 @@ Images are built primarily for `amd64` architecture, and may also include builds ### Quick Start -- The quickest way to get started is using [docker-compose](https://docs.docker.com/compose/). See the examples folder for a working [docker-compose.yml](examples/docker-compose.yml) that can be modified for development or production use. +- The quickest way to get started is using [docker-compose](https://docs.docker.com/compose/). See the examples folder for a series of example compose.yml that can be modified for development or production use. - Set various [environment variables](#environment-variables) to understand the capabilities of this image. - Map [persistent storage](#data-volumes) for access to configuration and data files for backup. -- Make [networking ports](#networking) available for public access if necessary ### Persistent Storage @@ -139,119 +173,571 @@ Be sure to view the following repositories to understand all the customizable op #### Container Options -| Parameter | Description | Default | -| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------- | --------------- | -| `BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | -| `MODE` | `AUTO` mode to use internal scheduling routines or `MANUAL` to simply use this as manual backups only executed by your own means | `AUTO` | -| `MANUAL_RUN_FOREVER` | `TRUE` or `FALSE` if you wish to try to make the container exit after the backup | `TRUE` | +| Parameter | Description | Default | +| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------- | --------------- | +| `MODE` | `AUTO` mode to use internal scheduling routines or `MANUAL` to simply use this as manual backups only executed by your own means | `AUTO` | +| `USER_DBBACKUP` | The uid that the image should read and write files as (username is `dbbackup`) | `10000` | +| `GROUP_DBBACKUP` | The gid that the image should read and write files as (groupname is `dbbackup`) | `10000` | +| `LOG_PATH` | Path to log files | `/logs` | +| `TEMP_PATH` | Perform Backups and Compression in this temporary directory | `/tmp/backups/` | +| `MANUAL_RUN_FOREVER` | `TRUE` or `FALSE` if you wish to try to make the container exit after the backup | `TRUE` | +| `DEBUG_MODE` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed. | `FALSE` | +| `BACKUP_JOB_CONCURRENCY` | How many backup jobs to run concurrently | `1` | -| `DEBUG_MODE` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed. | `FALSE` | -| `LOG_PATH` | Path to log files | `/logs` | -| `LOG_FILE` | Logfile name | `dbbackup.log` | -| `CREATE_LATEST_SYMLINK` | Create a symbolic link pointing to last backup in this format: `latest-(DB_TYPE)-(DB_NAME)-(DB_HOST)` | `TRUE` | -| `PRE_SCRIPT` | Fill this variable in with a command to execute pre backing up | | -| `POST_SCRIPT` | Fill this variable in with a command to execute post backing up | | -| `USER_DBBACKUP` | The uid that the image should read and write files as (username is `dbbackup`) | `10000` | -| `GROUP_DBBACKUP` | The gid that the image should read and write files as (groupname is `dbbackup`) | `10000` | -| `SPLIT_DB` | For each backup, create a new archive. `TRUE` or `FALSE` (MySQL and Postgresql Only) | `TRUE` | -| `TEMP_LOCATION` | Perform Backups and Compression in this temporary directory | `/tmp/backups/` | +#### Job Defaults +If these are set and no other defaults or variables are set explicitly, they will be added to any of the backup jobs. -### Database Specific Options +| Variable | Description | Default | +| --------------------------------- | ------------------------------------------------------------------------------------- | ------------ | +| `DEFAULT_BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | +| `DEFAULT_CHECKSUM` | Either `MD5` or `SHA1` or `NONE` | `MD5` | +| `DEFAULT_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `notice` | +| `DEFAULT_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | -| Parameter | Description | Default | `_FILE` | -| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | ------- | -| `DB_AUTH` | (Mongo/PGSQL Only - Optional) Authentication Database | | | -| `DB_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `mssql` `pgsql` `mongo` `redis` `sqlite3` | | | -| `DB_HOST` | Server Hostname e.g. `mariadb`. For `sqlite3`, full path to DB file e.g. `/backup/db.sqlite3` | | x | -| `DB_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. Backup multiple by separating with commas eg `db1,db2` | | x | -| `DB_NAME_EXCLUDE` | If using `ALL` - use this as to exclude databases separated via commas from being backed up | | x | -| `DB_USER` | username for the database(s) - Can use `root` for MySQL | | x | -| `DB_PASS` | (optional if DB doesn't require it) password for the database | | x | -| `DB_PORT` | (optional) Set port to connect to DB_HOST. Defaults are provided | varies | x | -| `INFLUX_VERSION` | What Version of Influx are you backing up from `1`.x or `2` series - AMD64 and ARM64 only for `2` | | | -| `MONGO_CUSTOM_URI` | If you wish to override the MongoDB Connection string enter it here e.g. `mongodb+srv://username:password@cluster.id.mongodb.net` | | x | -| | This environment variable will be parsed and populate the `DB_NAME` and `DB_HOST` variables to properly build your backup filenames. You can override them by making your own entries | | | +##### Compression Options -#### For Influx DB2 +| Variable | Description | Default | +| -------------------------------------- | ---------------------------------------------------------------------------------------------- | -------------- | +| `DEFAULT_COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, ZSTD `ZSTD` or none `NONE` | `ZSTD` | +| `DEFAULT_COMPRESSION_LEVEL` | Numerical value of what level of compression to use, most allow `1` to `9` | `3` | +| | except for `ZSTD` which allows for `1` to `19` | | +| `DEFAULT_GZ_RSYNCABLE` | Use `--rsyncable` (gzip only) for faster rsync transfers and incremental backup deduplication. | `FALSE` | +| `DEFAULT_ENABLE_PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` | `TRUE` | +| `DEFAULT_PARALLEL_COMPRESSION_THREADS` | Maximum amount of threads to use when compressing - Integer value e.g. `8` | `autodetected` | -Your Organization will be mapped to `DB_USER` and your root token will need to be mapped to `DB_PASS`. You may use `DB_NAME=ALL` to backup the entire set of databases. For `DB_HOST` use syntax of `http(s)://db-name` +##### Scheduling Options -### Scheduling Options +| Variable | Description | Default | +| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | +| `DEFAULT_BACKUP_INTERVAL` | How often to do a backup, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | +| `DEFAULT_BACKUP_BEGIN` | What time to do the initial backup. Defaults to immediate. (`+1`) | `+0` | +| | Must be in one of two formats: | | +| | Absolute HHMM, e.g. `2330` or `0415` | | +| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | +| `DEFAULT_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when backup interval executes) | `FALSE` | +| | 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | | +| `DEFAULT_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from | `DEFAULT_BACKUP_BLACKOUT_BEGIN` | Use `HHMM` notation to start a blackout period where no backups occur eg `0420` | | +| `DEFAULT_BACKUP_BLACKOUT_END` | Use `HHMM` notation to set the end period where no backups occur eg `0430` | | -| Parameter | Description | Default | -| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | -| `DB_DUMP_FREQ` | How often to do a dump, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | -| `DB_DUMP_BEGIN` | What time to do the first dump. Defaults to immediate. Must be in one of two formats | | -| | Absolute HHMM, e.g. `2330` or `0415` | | -| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | -| `DB_DUMP_TARGET` | Directory where the database dumps are kept. | `${DB_DUMP_TARGET}/archive/` | -| `DB_DUMP_TARGET_PERMISSION` | Directory and File permissions to apply to files. | `700` | -| `DB_DUMP_TARGET_ARCHIVE` | Optional Directory where the database dumps archives are kept. | | -| `DB_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when dump frequency fires). 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | `FALSE` | -| `DB_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from `DB_DUMP_TARGET` to `DB_DUMP_TARGET_ARCHIVE` - which is useful when pairing against an external backup system. | | +> You may need to wrap your `DEFAULT_BACKUP_BEGIN` value in quotes for it to properly parse. There have been reports of backups that start with a `0` get converted into a different format which will not allow the timer to start at the correct time. -- You may need to wrap your `DB_DUMP_BEGIN` value in quotes for it to properly parse. There have been reports of backups that start with a `0` get converted into a different format which will not allow the timer to start at the correct time. -### Backup Options +##### Default Database Options -| Parameter | Description | Default | `_FILE` | -| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | -| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, ZSTD `ZSTD` or none `NONE` | `ZSTD` | | -| `COMPRESSION_LEVEL` | Numerical value of what level of compression to use, most allow `1` to `9` except for `ZSTD` which allows for `1` to `19` - | `3` | | -| `ENABLE_PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` | `TRUE` | | -| `PARALLEL_COMPRESSION_THREADS` | Maximum amount of threads to use when compressing - Integer value e.g. `8` | `autodetected` | | -| `GZ_RSYNCABLE` | Use `--rsyncable` (gzip only) for faster rsync transfers and incremental backup deduplication. e.g. `TRUE` | `FALSE` | | -| `ENABLE_CHECKSUM` | Generate either a MD5 or SHA1 in Directory, `TRUE` or `FALSE` | `TRUE` | | -| `CHECKSUM` | Either `MD5` or `SHA1` | `MD5` | | -| `EXTRA_OPTS` | If you need to pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | -| `EXTRA_DUMP_OPTS` | If you need to pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | -| `EXTRA_ENUMERATION_OPTS` | If you need to pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | -| `MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet if backing up MySQL / MariaDB | `512M` | | -| `MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction with MySQL / MariaDB | `TRUE` | | -| `MYSQL_STORED_PROCEDURES` | Backup stored procedures with MySQL / MariaDB | `TRUE` | | -| `MYSQL_ENABLE_TLS` | Enable TLS functionality for MySQL client | `FALSE` | | -| `MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | -| `MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | -| `MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | -| `MYSQL_TLS_CERT_FILE` | Filename to load client certificate for connecting via TLS | | x | -| `MYSQL_TLS_KEY_FILE` | Filename to load client key for connecting via TLS | | x | +###### CouchDB -- When using compression with MongoDB, only `GZ` compression is possible. +| Variable | Description | Default | `_FILE` | +| -------------- | ------------ | ------- | ------- | +| `DEFAULT_PORT` | CouchDB Port | `5984` | x | -#### Backing Up to S3 Compatible Services +###### InfluxDB -If `BACKUP_LOCATION` = `S3` then the following options are used. +| Variable | Description | Default | `_FILE` | +| ------------------------ | ------------------------------------------------------------------------------------------------------- | ------- | ------- | +| `DEFAULT_PORT` | InfluxDB Port | | x | +| | Version 1.x | `8088` | | +| | Version 2.x | `8086` | | +| `DEFAULT_INFLUX_VERSION` | What Version of Influx are you backing up from `1`.x or `2` series - amd64 and aarch/armv8 only for `2` | `2` | | -| Parameter | Description | Default | `_FILE` | -| --------------------- | ----------------------------------------------------------------------------------------- | ------- | ------- | -| `S3_BUCKET` | S3 Bucket name e.g. `mybucket` | | x | -| `S3_KEY_ID` | S3 Key ID (Optional) | | x | -| `S3_KEY_SECRET` | S3 Key Secret (Optional) | | x | -| `S3_PATH` | S3 Pathname to save to (must NOT end in a trailing slash e.g. '`backup`') | | x | -| `S3_REGION` | Define region in which bucket is defined. Example: `ap-northeast-2` | | x | -| `S3_HOST` | Hostname (and port) of S3-compatible service, e.g. `minio:8080`. Defaults to AWS. | | x | -| `S3_PROTOCOL` | Protocol to connect to `S3_HOST`. Either `http` or `https`. Defaults to `https`. | `https` | x | -| `S3_EXTRA_OPTS` | Add any extra options to the end of the `aws-cli` process execution | | x | -| `S3_CERT_CA_FILE` | Map a volume and point to your custom CA Bundle for verification e.g. `/certs/bundle.pem` | | x | -| _*OR*_ | | | | -| `S3_CERT_SKIP_VERIFY` | Skip verifying self signed certificates when connecting | `TRUE` | | +###### MariaDB/MySQL -- When `S3_KEY_ID` and/or `S3_KEY_SECRET` is not set, will try to use IAM role assigned (if any) for uploading the backup files to S3 bucket. +| Variable | Description | Default | `_FILE` | +| ---------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | +| `DEFAULT_PORT` | MariaDB Port | `3306` | x | +| `DEFAULT_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DEFAULT_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet if backing up MySQL / MariaDB | `512M` | | +| `DEFAULT_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction with MySQL / MariaDB | `TRUE` | | +| `DEFAULT_MYSQL_STORED_PROCEDURES` | Backup stored procedures with MySQL / MariaDB | `TRUE` | | +| `DEFAULT_MYSQL_ENABLE_TLS` | Enable TLS functionality for MySQL/MariaDB client | `FALSE` | | +| `DEFAULT_MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | +| `DEFAULT_MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | +| `DEFAULT_MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | +| `DEFAULT_MYSQL_TLS_CERT_FILE` | Filename to load client certificate for connecting via TLS | | x | +| `DEFAULT_MYSQL_TLS_KEY_FILE` | Filename to load client key for connecting via TLS | | x | -#### Upload to a Azure storage account by `blobxfer` -Support to upload backup files with [blobxfer](https://github.com/Azure/blobxfer) to the Azure file share storage. +###### Microsoft SQL -If `BACKUP_LOCATION` = `blobxfer` then the following options are used. +| Variable | Description | Default | `_FILE` | +| -------------- | ------------------ | ------- | ------- | +| `DEFAULT_PORT` | Microsoft SQL Port | `1433` | x | -| Parameter | Description | Default | `_FILE` | -| ------------------------------ | ------------------------------------------- | ------------------- | ------- | -| `BLOBXFER_STORAGE_ACCOUNT` | Microsoft Azure Cloud storage account name. | | x | -| `BLOBXFER_STORAGE_ACCOUNT_KEY` | Microsoft Azure Cloud storage account key. | | x | -| `BLOBXFER_REMOTE_PATH` | Remote Azure path | `/docker-db-backup` | x | +###### MongoDB -> This service uploads files from backup targed directory `DB_DUMP_TARGET`. -> If the a cleanup configuration in `DB_CLEANUP_TIME` is defined, the remote directory on Azure storage will also be cleaned automatically. +| Variable | Description | Default | `_FILE` | +| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ | ------- | ------- | +| `DEFAULT_AUTH` | (Optional) Authentication Database | | x | +| `DEFAULT_PORT` | MongoDB Port | `27017` | x | +| `MONGO_CUSTOM_URI` | If you wish to override the MongoDB Connection string enter it here e.g. `mongodb+srv://username:password@cluster.id.mongodb.net` | | x | +| | This environment variable will be parsed and populate the `DB_NAME` and `DB_HOST` variables to properly build your backup filenames. | | | +| | You can override them by making your own entries | | | + +###### Postgresql + +| Variable | Description | Default | `_FILE` | +| -------------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | +| `DEFAULT_AUTH` | (Optional) Authentication Database | | x | +| `DEFAULT_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DEFAULT_PORT` | PostgreSQL Port | `5432` | x | + +###### Redis + +| Variable | Description | Default | `_FILE` | +| -------------------------------- | --------------------------------------------------------------------------------------------------- | ------- | ------- | +| `DEFAULT_PORT` | Default Redis Port | `6379` | x | +| `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | + + +##### Default Storage Options + +Options that are related to the value of `DEFAULT_BACKUP_LOCATION` + +###### Filesystem + +If `DEFAULT_BACKUP_LOCTION` = `FILESYSTEM` then the following options are used: + +| Variable | Description | Default | +| --------------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------- | +| `DEFAULT_CREATE_LATEST_SYMLINK` | Create a symbolic link pointing to last backup in this format: `latest-(DB_TYPE)-(DB_NAME)-(DB_HOST)` | `TRUE` | +| `DEFAULT_FILESYSTEM_PATH` | Directory where the database dumps are kept. | `/backup` | +| `DEFAULT_FILESYSTEM_ARCHIVE_PATH` | Optional Directory where the database dumps archives are kept | `${DEFAULT_FILESYSTEM_PATH}/archive/` | +| `DEFAULT_FILESYSTEM_PERMISSION` | Directory and File permissions to apply to files. | `700` | + +###### S3 + +If `DEFAULT_BACKUP_LOCATION` = `S3` then the following options are used: + +| Parameter | Description | Default | `_FILE` | +| ----------------------------- | ----------------------------------------------------------------------------------------- | ------- | ------- | +| `DEFAULT_S3_BUCKET` | S3 Bucket name e.g. `mybucket` | | x | +| `DEFAULT_S3_KEY_ID` | S3 Key ID (Optional) | | x | +| `DEFAULT_S3_KEY_SECRET` | S3 Key Secret (Optional) | | x | +| `DEFAULT_S3_PATH` | S3 Pathname to save to (must NOT end in a trailing slash e.g. '`backup`') | | x | +| `DEFAULT_S3_REGION` | Define region in which bucket is defined. Example: `ap-northeast-2` | | x | +| `DEFAULT_S3_HOST` | Hostname (and port) of S3-compatible service, e.g. `minio:8080`. Defaults to AWS. | | x | +| `DEFAULT_S3_PROTOCOL` | Protocol to connect to `DEFAULT_S3_HOST`. Either `http` or `https`. Defaults to `https`. | `https` | x | +| `DEFAULT_S3_EXTRA_OPTS` | Add any extra options to the end of the `aws-cli` process execution | | x | +| `DEFAULT_S3_CERT_CA_FILE` | Map a volume and point to your custom CA Bundle for verification e.g. `/certs/bundle.pem` | | x | +| _*OR*_ | | | | +| `DEFAULT_S3_CERT_SKIP_VERIFY` | Skip verifying self signed certificates when connecting | `TRUE` | | + +- When `DEFAULT_S3_KEY_ID` and/or `DEFAULT_S3_KEY_SECRET` is not set, will try to use IAM role assigned (if any) for uploading the backup files to S3 bucket. + +###### Azure + +If `DEFAULT_BACKUP_LOCATION` = `blobxfer` then the following options are used:. + +| Parameter | Description | Default | `_FILE` | +| -------------------------------------- | ------------------------------------------- | ------------------- | ------- | +| `DEFAULT_BLOBXFER_STORAGE_ACCOUNT` | Microsoft Azure Cloud storage account name. | | x | +| `DEFAULT_BLOBXFER_STORAGE_ACCOUNT_KEY` | Microsoft Azure Cloud storage account key. | | x | +| `DEFAULT_BLOBXFER_REMOTE_PATH` | Remote Azure path | `/docker-db-backup` | x | + +> This service uploads files from backup targed directory `DEFAULT_FILESYSTEM_PATH`. +> If the a cleanup configuration in `DEFAULT_CLEANUP_TIME` is defined, the remote directory on Azure storage will also be cleaned automatically. + +##### Hooks + +###### Path Options + +| Parameter | Description | Default | +| ------------------------------ | --------------------------------------------------------------------------- | ----------------------- | +| `DEFAULT_SCRIPT_LOCATION_PRE` | Location on filesystem inside container to execute bash scripts pre backup | `/assets/scripts/pre/` | +| `DEFAULT_SCRIPT_LOCATION_POST` | Location on filesystem inside container to execute bash scripts post backup | `/assets/scripts/post/` | +| `DEFAULT_PRE_SCRIPT` | Fill this variable in with a command to execute pre backing up | | +| `DEFAULT_POST_SCRIPT` | Fill this variable in with a command to execute post backing up | | + +###### Pre Backup + +If you want to execute a custom script before a backup starts, you can drop bash scripts with the extension of `.sh` in the location defined in `DB01_SCRIPT_LOCATION_PRE`. See the following example to utilize: + +```bash +$ cat pre-script.sh +##!/bin/bash + +# #### Example Pre Script +# #### $1=DBXX_TYPE (Type of Backup) +# #### $2=DBXX_HOST (Backup Host) +# #### $3=DBXX_NAME (Name of Database backed up +# #### $4=BACKUP START TIME (Seconds since Epoch) +# #### $5=BACKUP FILENAME (Filename) + +echo "${1} Backup Starting on ${2} for ${3} at ${4}. Filename: ${5}" +``` + + ## script DBXX_TYPE DBXX_HOST DBXX_NAME STARTEPOCH BACKUP_FILENAME + ${f} "${backup_job_db_type}" "${backup_job_db_host}" "${backup_job_db_name}" "${backup_routines_start_time}" "${target}" + +Outputs the following on the console: + +`mysql Backup Starting on example-db for example at 1647370800. Filename: mysql_example_example-db_202200315-000000.sql.bz2 + +###### Post backup + +If you want to execute a custom script at the end of a backup, you can drop bash scripts with the extension of `.sh` in the location defined in `DB01_SCRIPT_LOCATION_POST`. Also to support legacy users `/assets/custom-scripts` is also scanned and executed.See the following example to utilize: + +```bash +$ cat post-script.sh +##!/bin/bash + +# #### Example Post Script +# #### $1=EXIT_CODE (After running backup routine) +# #### $2=DBXX_TYPE (Type of Backup) +# #### $3=DBXX_HOST (Backup Host) +# #### #4=DBXX_NAME (Name of Database backed up +# #### $5=BACKUP START TIME (Seconds since Epoch) +# #### $6=BACKUP FINISH TIME (Seconds since Epoch) +# #### $7=BACKUP TOTAL TIME (Seconds between Start and Finish) +# #### $8=BACKUP FILENAME (Filename) +# #### $9=BACKUP FILESIZE +# #### $10=HASH (If CHECKSUM enabled) +# #### $11=MOVE_EXIT_CODE + +echo "${1} ${2} Backup Completed on ${3} for ${4} on ${5} ending ${6} for a duration of ${7} seconds. Filename: ${8} Size: ${9} bytes MD5: ${10}" +``` + + ## script EXIT_CODE DB_TYPE DB_HOST DB_NAME STARTEPOCH FINISHEPOCH DURATIONEPOCH BACKUP_FILENAME FILESIZE CHECKSUMVALUE + ${f} "${exit_code}" "${dbtype}" "${backup_job_db_host}" "${backup_job_db_name}" "${backup_routines_start_time}" "${backup_routines_finish_time}" "${backup_routines_total_time}" "${target}" "${FILESIZE}" "${checksum_value}" "${move_exit_code} + +Outputs the following on the console: + +`0 mysql Backup Completed on example-db for example on 1647370800 ending 1647370920 for a duration of 120 seconds. Filename: mysql_example_example-db_202200315-000000.sql.bz2 Size: 7795 bytes Hash: 952fbaafa30437494fdf3989a662cd40 0` + +If you wish to change the size value from bytes to megabytes set environment variable `DB01_SIZE_VALUE=megabytes` + +You must make your scripts executable otherwise there is an internal check that will skip trying to run it otherwise. +If for some reason your filesystem or host is not detecting it right, use the environment variable `DB01_POST_SCRIPT_SKIP_X_VERIFY=TRUE` to bypass. + + +#### Job Backup Options + +If `DEFAULT_` variables are set and you do not wish for the settings to carry over into your jobs, you can set the appropriate environment variable with the value of `unset`. +Otherwise, override them per backup job. Additional backup jobs can be scheduled by using `DB02_`,`DB03_`,`DB04_` ... prefixes. See [Specific Database Options](#specific-database-options) which may overrule this list. + +| Parameter | Description | Default | `_FILE` | +| ----------- | ---------------------------------------------------------------------------------------------- | ------- | ------- | +| `DB01_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `mssql` `pgsql` `mongo` `redis` `sqlite3` | | | +| `DB01_HOST` | Server Hostname e.g. `mariadb`. For `sqlite3`, full path to DB file e.g. `/backup/db.sqlite3` | | x | +| `DB01_NAME` | Schema Name e.g. `database` | | x | +| `DB01_USER` | username for the database(s) - Can use `root` for MySQL | | x | +| `DB01_PASS` | (optional if DB doesn't require it) password for the database | | x | +| | | + +| Variable | Description | Default | +| ------------------------------ | --------------------------------------------------------------------------------------------------------- | ------------ | +| `DB01_BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | +| `DB01_CHECKSUM` | Either `MD5` or `SHA1` or `NONE` | `MD5` | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | +| `DB01_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `debug` | +| `DB01_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | + +##### Compression Options + +| Variable | Description | Default | +| ----------------------------------- | ---------------------------------------------------------------------------------------------- | -------------- | +| `DB01_COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, ZSTD `ZSTD` or none `NONE` | `ZSTD` | +| `DB01_COMPRESSION_LEVEL` | Numerical value of what level of compression to use, most allow `1` to `9` | `3` | +| | except for `ZSTD` which allows for `1` to `19` | | +| `DB01_GZ_RSYNCABLE` | Use `--rsyncable` (gzip only) for faster rsync transfers and incremental backup deduplication. | `FALSE` | +| `DB01_ENABLE_PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` | `TRUE` | +| `DB01_PARALLEL_COMPRESSION_THREADS` | Maximum amount of threads to use when compressing - Integer value e.g. `8` | `autodetected` | + +##### Scheduling Options + +| Variable | Description | Default | +| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------- | +| `DB01_BACKUP_INTERVAL` | How often to do a backup, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | +| `DB01_BACKUP_BEGIN` | What time to do the initial backup. Defaults to immediate. (`+1`) | `+0` | +| | Must be in one of two formats: | | +| | Absolute HHMM, e.g. `2330` or `0415` | | +| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | +| `DB01_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when backup interval executes) | `FALSE` | +| | 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | | +| `DB01_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from `DB01_BACKUP_FILESYSTEM_PATH` | | +| | to `DB01_BACKUP_FILESYSTEM_ARCHIVE_PATH` - which is useful when pairing against an external backup system. | | +| `DB01_BACKUP_BLACKOUT_BEGIN` | Use `HHMM` notation to start a blackout period where no backups occur eg `0420` | | +| `DB01_BACKUP_BLACKOUT_END` | Use `HHMM` notation to set the end period where no backups occur eg `0430` | | + +##### Specific Database Options + +###### CouchDB + +| Variable | Description | Default | `_FILE` | +| ----------- | ------------ | ------- | ------- | +| `DB01_PORT` | CouchDB Port | `5984` | x | + +###### InfluxDB + +| Variable | Description | Default | `_FILE` | +| --------------------- | ------------------------------------------------------------------------------------------------------- | ------- | ------- | +| `DB01_PORT` | InfluxDB Port | | x | +| | Version 1.x | `8088` | | +| | Version 2.x | `8086` | | +| `DB01_INFLUX_VERSION` | What Version of Influx are you backing up from `1`.x or `2` series - amd64 and aarch/armv8 only for `2` | `2` | | + +> Your Organization will be mapped to `DB_USER` and your root token will need to be mapped to `DB_PASS`. +> You may use `DB_NAME=ALL` to backup the entire set of databases. +> For `DB_HOST` use syntax of `http(s)://db-name` + + +###### MariaDB/MySQL + +| Variable | Description | Default | `_FILE` | +| ------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | +| `DB01_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. | | | +| | Backup multiple by separating with commas eg `db1,db2` | | x | +| `DB01_NAME_EXCLUDE` | If using `ALL` - use this as to exclude databases separated via commas from being backed up | | x | +| `DB01_PORT` | MariaDB Port | `3306` | x | +| `DB01_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet if backing up MySQL / MariaDB | `512M` | | +| `DB01_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction with MySQL / MariaDB | `TRUE` | | +| `DB01_MYSQL_STORED_PROCEDURES` | Backup stored procedures with MySQL / MariaDB | `TRUE` | | +| `DB01_MYSQL_ENABLE_TLS` | Enable TLS functionality for MySQL/MariaDB client | `FALSE` | | +| `DB01_MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | +| `DB01_MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | +| `DB01_MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | +| `DB01_MYSQL_TLS_CERT_FILE` | Filename to load client certificate for connecting via TLS | | x | +| `DB01_MYSQL_TLS_KEY_FILE` | Filename to load client key for connecting via TLS | | x | + + +###### Microsoft SQL + +| Variable | Description | Default | `_FILE` | +| ----------- | ------------------ | ------- | ------- | +| `DB01_PORT` | Microsoft SQL Port | `1433` | x | + +###### MongoDB + +| Variable | Description | Default | `_FILE` | +| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------- | ------- | +| `DB01_AUTH` | (Optional) Authentication Database | | | +| `DB01_PORT` | MongoDB Port | `27017` | x | +| `DB01_MONGO_CUSTOM_URI` | If you wish to override the MongoDB Connection string enter it here e.g. `mongodb+srv://username:password@cluster.id.mongodb.net` | | x | +| | This environment variable will be parsed and populate the `DB_NAME` and `DB_HOST` variables to properly build your backup filenames. | | | +| | You can override them by making your own entries | | | + +###### Postgresql + +| Variable | Description | Default | `_FILE` | +| ----------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | +| `DB01_AUTH` | (Optional) Authentication Database | | | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | +| `DB01_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. | | | +| | Backup multiple by separating with commas eg `db1,db2` | | x | +| `DB01_PORT` | PostgreSQL Port | `5432` | x | + +###### Redis + +| Variable | Description | Default | `_FILE` | +| ------------------------ | --------------------------------------------------------------------------------------------------------- | ------- | ------- | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | +| `DB01_PORT` | Redis Port | `6379` | x | + +###### SQLite + +| Variable | Description | Default | `_FILE` | +| ----------- | -------------------------------------------------------- | ------- | ------- | +| `DB01_HOST` | Enter the full path to DB file e.g. `/backup/db.sqlite3` | | x | + +##### Specific Storage Options + +Options that are related to the value of `DB01_BACKUP_LOCATION` + +###### Filesystem + +If `DB01_BACKUP_LOCTION` = `FILESYSTEM` then the following options are used: + +| Variable | Description | Default | +| ------------------------------ | ----------------------------------------------------------------------------------------------------- | --------------------------------- | +| `DB01_CREATE_LATEST_SYMLINK` | Create a symbolic link pointing to last backup in this format: `latest-(DB_TYPE)-(DB_NAME)-(DB_HOST)` | `TRUE` | +| `DB01_FILESYSTEM_PATH` | Directory where the database dumps are kept. | `/backup` | +| `DB01_FILESYSTEM_ARCHIVE_PATH` | Optional Directory where the database dumps archives are kept | `${DB01_FILESYSTEM_PATH/archive/` | +| `DB01_FILESYSTEM_PERMISSION` | Directory and File permissions to apply to files. | `700` | + +###### S3 + +If `DB01_BACKUP_LOCATION` = `S3` then the following options are used: + +| Parameter | Description | Default | `_FILE` | +| -------------------------- | ----------------------------------------------------------------------------------------- | ------- | ------- | +| `DB01_S3_BUCKET` | S3 Bucket name e.g. `mybucket` | | x | +| `DB01_S3_KEY_ID` | S3 Key ID (Optional) | | x | +| `DB01_S3_KEY_SECRET` | S3 Key Secret (Optional) | | x | +| `DB01_S3_PATH` | S3 Pathname to save to (must NOT end in a trailing slash e.g. '`backup`') | | x | +| `DB01_S3_REGION` | Define region in which bucket is defined. Example: `ap-northeast-2` | | x | +| `DB01_S3_HOST` | Hostname (and port) of S3-compatible service, e.g. `minio:8080`. Defaults to AWS. | | x | +| `DB01_S3_PROTOCOL` | Protocol to connect to `DB01_S3_HOST`. Either `http` or `https`. Defaults to `https`. | `https` | x | +| `DB01_S3_EXTRA_OPTS` | Add any extra options to the end of the `aws-cli` process execution | | x | +| `DB01_S3_CERT_CA_FILE` | Map a volume and point to your custom CA Bundle for verification e.g. `/certs/bundle.pem` | | x | +| _*OR*_ | | | | +| `DB01_S3_CERT_SKIP_VERIFY` | Skip verifying self signed certificates when connecting | `TRUE` | | + +> When `DB01_S3_KEY_ID` and/or `DB01_S3_KEY_SECRET` is not set, will try to use IAM role assigned (if any) for uploading the backup files to S3 bucket. + +###### Azure + +If `DB01_BACKUP_LOCATION` = `blobxfer` then the following options are used:. + +| Parameter | Description | Default | `_FILE` | +| ----------------------------------- | ------------------------------------------- | ------------------- | ------- | +| `DB01_BLOBXFER_STORAGE_ACCOUNT` | Microsoft Azure Cloud storage account name. | | x | +| `DB01_BLOBXFER_STORAGE_ACCOUNT_KEY` | Microsoft Azure Cloud storage account key. | | x | +| `DB01_BLOBXFER_REMOTE_PATH` | Remote Azure path | `/docker-db-backup` | x | + +> This service uploads files from backup target directory `DB01_BACKUP_FILESYSTEM_PATH`. +> If the a cleanup configuration in `DB01_CLEANUP_TIME` is defined, the remote directory on Azure storage will also be cleaned automatically. + +##### Hooks + +###### Path Options + +| Parameter | Description | Default | +| --------------------------- | --------------------------------------------------------------------------- | ----------------------- | +| `DB01_SCRIPT_LOCATION_PRE` | Location on filesystem inside container to execute bash scripts pre backup | `/assets/scripts/pre/` | +| `DB01_SCRIPT_LOCATION_POST` | Location on filesystem inside container to execute bash scripts post backup | `/assets/scripts/post/` | +| `DB01_PRE_SCRIPT` | Fill this variable in with a command to execute pre backing up | | +| `DB01_POST_SCRIPT` | Fill this variable in with a command to execute post backing up | | + +###### Pre Backup + +If you want to execute a custom script before a backup starts, you can drop bash scripts with the extension of `.sh` in the location defined in `DB01_SCRIPT_LOCATION_PRE`. See the following example to utilize: + +```bash +$ cat pre-script.sh +##!/bin/bash + +# #### Example Pre Script +# #### $1=DB01_TYPE (Type of Backup) +# #### $2=DB01_HOST (Backup Host) +# #### $3=DB01_NAME (Name of Database backed up +# #### $4=BACKUP START TIME (Seconds since Epoch) +# #### $5=BACKUP FILENAME (Filename) + +echo "${1} Backup Starting on ${2} for ${3} at ${4}. Filename: ${5}" +``` + + ## script DB01_TYPE DB01_HOST DB01_NAME STARTEPOCH BACKUP_FILENAME + ${f} "${backup_job_db_type}" "${backup_job_db_host}" "${backup_job_db_name}" "${backup_routines_start_time}" "${target}" + +Outputs the following on the console: + +`mysql Backup Starting on example-db for example at 1647370800. Filename: mysql_example_example-db_202200315-000000.sql.bz2 + +###### Post backup + +If you want to execute a custom script at the end of a backup, you can drop bash scripts with the extension of `.sh` in the location defined in `DB01_SCRIPT_LOCATION_POST`. Also to support legacy users `/assets/custom-scripts` is also scanned and executed.See the following example to utilize: + +```bash +$ cat post-script.sh +##!/bin/bash + +# #### Example Post Script +# #### $1=EXIT_CODE (After running backup routine) +# #### $2=DB_TYPE (Type of Backup) +# #### $3=DB_HOST (Backup Host) +# #### #4=DB_NAME (Name of Database backed up +# #### $5=BACKUP START TIME (Seconds since Epoch) +# #### $6=BACKUP FINISH TIME (Seconds since Epoch) +# #### $7=BACKUP TOTAL TIME (Seconds between Start and Finish) +# #### $8=BACKUP FILENAME (Filename) +# #### $9=BACKUP FILESIZE +# #### $10=HASH (If CHECKSUM enabled) +# #### $11=MOVE_EXIT_CODE + +echo "${1} ${2} Backup Completed on ${3} for ${4} on ${5} ending ${6} for a duration of ${7} seconds. Filename: ${8} Size: ${9} bytes MD5: ${10}" +``` + + ## script EXIT_CODE DB_TYPE DB_HOST DB_NAME STARTEPOCH FINISHEPOCH DURATIONEPOCH BACKUP_FILENAME FILESIZE CHECKSUMVALUE + ${f} "${exit_code}" "${dbtype}" "${dbhost}" "${dbname}" "${backup_routines_start_time}" "${backup_routines_finish_time}" "${backup_routines_total_time}" "${target}" "${FILESIZE}" "${checksum_value}" "${move_exit_code} + +Outputs the following on the console: + +`0 mysql Backup Completed on example-db for example on 1647370800 ending 1647370920 for a duration of 120 seconds. Filename: mysql_example_example-db_202200315-000000.sql.bz2 Size: 7795 bytes Hash: 952fbaafa30437494fdf3989a662cd40 0` + +If you wish to change the size value from bytes to megabytes set environment variable `DB01_SIZE_VALUE=megabytes` + +You must make your scripts executable otherwise there is an internal check that will skip trying to run it otherwise. +If for some reason your filesystem or host is not detecting it right, use the environment variable `DB01_POST_SCRIPT_SKIP_X_VERIFY=TRUE` to bypass. + + +#### Notifications + +This image has capabilities on sending notifications via a handful of services when a backup job fails. This is a global option that cannot be individually set per backup job. + +| Parameter | Description | Default | +| ---------------------- | --------------------------------------------------------------------------------- | ------- | +| `ENABLE_NOTIFICATIONS` | Enable Notifications | `FALSE` | +| `NOTIFICATION_TYPE` | `CUSTOM` `EMAIL` `MATRIX` `MATTERMOST` `ROCKETCHAT` - Seperate Multiple by commas | | + +##### Custom Notifications + +The following is sent to the custom script. Use how you wish: + +```` +$1 unix timestamp +$2 logfile +$3 errorcode +$4 subject +$5 body/error message +```` + +| Parameter | Description | Default | +| ---------------------------- | ------------------------------------------------------- | ------- | +| `NOTIFICATION_CUSTOM_SCRIPT` | Path and name of custom script to execute notification. | | + + +##### Email Notifications +| Parameter | Description | Default | `_FILE` | +| ----------- | ----------------------------------------------------------------------------------------- | ------- | ------- | +| `MAIL_FROM` | What email address to send mail from for errors | | | +| `MAIL_TO` | What email address to send mail to for errors. Send to multiple by seperating with comma. | | | +| `SMTP_HOST` | What SMTP server to use for sending mail | | x | +| `SMTP_PORT` | What SMTP port to use for sending mail | | x | + +##### Matrix Notifications + +Fetch a `MATRIX_ACCESS_TOKEN`: + +```` +curl -XPOST -d '{"type":"m.login.password", "user":"myuserid", "password":"mypass"}' "https://matrix.org/_matrix/client/r0/login" +```` + +Copy the JSON response `access_token` that will look something like this: + +```` +{"access_token":"MDAxO...blahblah","refresh_token":"MDAxO...blahblah","home_server":"matrix.org","user_id":"@myuserid:matrix.org"} +```` + +| Parameter | Description | Default | `_FILE` | +| --------------------- | ---------------------------------------------------------------------------------------- | ------- | ------- | +| `MATRIX_HOST` | URL (https://matrix.example.com) of Matrix Homeserver | | x | +| `MATRIX_ROOM` | Room ID eg `\!abcdef:example.com` to send to. Send to multiple by seperating with comma. | | x | +| `MATRIX_ACCESS_TOKEN` | Access token of user authorized to send to room | | x | + +##### Mattermost Notifications +| Parameter | Description | Default | `_FILE` | +| ------------------------ | -------------------------------------------------------------------------------------------- | ------- | ------- | +| `MATTERMOST_WEBHOOK_URL` | Full URL to send webhook notifications to | | x | +| `MATTERMOST_RECIPIENT` | Channel or User to send Webhook notifications to. Send to multiple by seperating with comma. | | x | +| `MATTERMOST_USERNAME` | Username to send as eg `tiredofit` | | x | + +##### Rocketchat Notifications +| Parameter | Description | Default | `_FILE` | +| ------------------------ | -------------------------------------------------------------------------------------------- | ------- | ------- | +| `ROCKETCHAT_WEBHOOK_URL` | Full URL to send webhook notifications to | | x | +| `ROCKETCHAT_RECIPIENT` | Channel or User to send Webhook notifications to. Send to multiple by seperating with comma. | | x | +| `ROCKETCHAT_USERNAME` | Username to send as eg `tiredofit` | | x | ## Maintenance @@ -269,6 +755,9 @@ Manual Backups can be performed by entering the container and typing `backup-now - Recently there was a request to have the container work with Kubernetes cron scheduling. This can theoretically be accomplished by setting the container `MODE=MANUAL` and then setting `MANUAL_RUN_FOREVER=FALSE` - You would also want to disable a few features from the upstream base images specifically `CONTAINER_ENABLE_SCHEDULING` and `CONTAINER_ENABLE_MONITORING`. This should allow the container to start, execute a backup by executing and then exit cleanly. An alternative way to running the script is to execute `/etc/services.available/10-db-backup/run`. +### Manual Backups +Manual Backups can be performed by entering the container and typing `backup-now`. This will execute all the backup tasks that are scheduled by means of the `BACKUPXX_` variables. Alternatively if you wanted to execute a job on its own you could simply type `backup01-now` (or whatever your number would be). There is no concurrency, and jobs will be executed sequentially. + ### Restoring Databases Entering in the container and executing `restore` will execute a menu based script to restore your backups - MariaDB, Postgres, and Mongo supported. @@ -292,75 +781,7 @@ The script can also be executed skipping the interactive mode by using the follo If you only enter some of the arguments you will be prompted to fill them in. -### Custom Scripts -#### Path Options - -| Parameter | Description | Default | -| ---------------------- | --------------------------------------------------------------------------- | ----------------------- | -| `SCRIPT_LOCATION_PRE` | Location on filesystem inside container to execute bash scripts pre backup | `/assets/scripts/pre/` | -| `SCRIPT_LOCATION_POST` | Location on filesystem inside container to execute bash scripts post backup | `/assets/scripts/post/` | - -#### Pre Backup - -If you want to execute a custom script before a backup starts, you can drop bash scripts with the extension of `.sh` in the location defined in `SCRIPT_LOCATION_PRE`. See the following example to utilize: - -```bash -$ cat pre-script.sh -##!/bin/bash - -# #### Example Pre Script -# #### $1=DB_TYPE (Type of Backup) -# #### $2=DB_HOST (Backup Host) -# #### $3=DB_NAME (Name of Database backed up -# #### $4=BACKUP START TIME (Seconds since Epoch)ff -# #### $5=BACKUP FILENAME (Filename) - -echo "${1} Backup Starting on ${2} for ${3} at ${4}. Filename: ${5}" -``` - - ## script DB_TYPE DB_HOST DB_NAME STARTEPOCH BACKUP_FILENAME - ${f} "${dbtype}" "${dbhost}" "${dbname}" "${backup_start_time}" "${target}" - -Outputs the following on the console: - -`mysql Backup Starting on example-db for example at 1647370800. Filename: mysql_example_example-db_202200315-000000.sql.bz2 - -#### Post backup - -If you want to execute a custom script at the end of a backup, you can drop bash scripts with the extension of `.sh` in the location defined in `SCRIPT_LOCATION_POST`. Also to support legacy users `/assets/custom-scripts` is also scanned and executed.See the following example to utilize: - -```bash -$ cat post-script.sh -##!/bin/bash - -# #### Example Post Script -# #### $1=EXIT_CODE (After running backup routine) -# #### $2=DB_TYPE (Type of Backup) -# #### $3=DB_HOST (Backup Host) -# #### #4=DB_NAME (Name of Database backed up -# #### $5=BACKUP START TIME (Seconds since Epoch) -# #### $6=BACKUP FINISH TIME (Seconds since Epoch) -# #### $7=BACKUP TOTAL TIME (Seconds between Start and Finish) -# #### $8=BACKUP FILENAME (Filename) -# #### $9=BACKUP FILESIZE -# #### $10=HASH (If CHECKSUM enabled) -# #### $11=MOVE_EXIT_CODE - -echo "${1} ${2} Backup Completed on ${3} for ${4} on ${5} ending ${6} for a duration of ${7} seconds. Filename: ${8} Size: ${9} bytes MD5: ${10}" -``` - - ## script EXIT_CODE DB_TYPE DB_HOST DB_NAME STARTEPOCH FINISHEPOCH DURATIONEPOCH BACKUP_FILENAME FILESIZE CHECKSUMVALUE - ${f} "${exit_code}" "${dbtype}" "${dbhost}" "${dbname}" "${backup_start_timme}" "${backup_finish_time}" "${backup_total_time}" "${target}" "${FILESIZE}" "${checksum_value}" "${move_exit_code} - -Outputs the following on the console: - -`0 mysql Backup Completed on example-db for example on 1647370800 ending 1647370920 for a duration of 120 seconds. Filename: mysql_example_example-db_202200315-000000.sql.bz2 Size: 7795 bytes Hash: 952fbaafa30437494fdf3989a662cd40 0` - -If you wish to change the size value from bytes to megabytes set environment variable `SIZE_VALUE=megabytes` - -You must make your scripts executable otherwise there is an internal check that will skip trying to run it otherwise. -If for some reason your filesystem or host is not detecting it right, use the environment variable `POST_SCRIPT_SKIP_X_VERIFY=TRUE` to bypass. ## Support diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index d87ba1b..f1b2554 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -13,7 +13,6 @@ DB_DUMP_TARGET_PERMISSION=${DB_DUMP_TARGET_PERMISSION:-"700"} DB_DUMP_TARGET_ARCHIVE=${DB_DUMP_TARGET_ARCHIVE:-"${DB_DUMP_TARGET}/archive/"} ENABLE_CHECKSUM=${ENABLE_CHECKSUM:-"TRUE"} ENABLE_PARALLEL_COMPRESSION=${ENABLE_PARALLEL_COMPRESSION:-"TRUE"} -LOG_FILE=${LOG_FILE:-"dbbackup.log"} LOG_LEVEL=${LOG_LEVEL:-"debug"} LOG_PATH=${LOG_PATH:-"/logs"} MANUAL_RUN_FOREVER=${MANUAL_RUN_FOREVER:-"TRUE"} diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 6b1fc23..00c7ac2 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -19,7 +19,7 @@ bootstrap_filesystem() { if [ "$(stat -c %U "${LOG_PATH}")" != "dbbackup" ] ; then chown dbbackup:dbbackup "${LOG_PATH}" ; fi if [ "$(stat -c %a "${LOG_PATH}")" != "755" ] ; then chmod -R 755 "${LOG_PATH}" ; fi - create_logrotate dbbackup "${LOG_PATH}"/"${LOG_FILE}" none dbbackup dbbackup + echo "0 0 * * * /usr/sbin/logrotate_dbbackup >/dev/null 2>&1" > /assets/cron/dbbackup_logrotate if [ ! -d "${TEMP_LOCATION}" ]; then mkdir -p "${TEMP_LOCATION}" @@ -963,7 +963,7 @@ write_log() { case "${LOG_LEVEL,,}" in "debug" ) print_debug "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null ;; esac ;; @@ -972,21 +972,21 @@ write_log() { case "${LOG_LEVEL,,}" in "debug" | "notice" | "warn" | "error") print_error "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null ;; esac ;; info ) shift 1 print_info "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null ;; notice ) shift 1 case "${LOG_LEVEL,,}" in "debug" | "notice" ) print_notice "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null ;; esac ;; @@ -995,7 +995,7 @@ write_log() { case "${LOG_LEVEL,,}" in "debug" | "notice" | "warn" ) print_warn "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}"/"${LOG_FILE}" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null ;; esac ;; From 0845ec30b394b86a2c5c7e040f2747a0ffdd9390 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:00:13 -0700 Subject: [PATCH 09/68] Update install/etc folder contents --- install/etc/cont-init.d/10-db-backup | 5 +- .../etc/services.available/10-db-backup/run | 89 ------------------- 2 files changed, 2 insertions(+), 92 deletions(-) delete mode 100755 install/etc/services.available/10-db-backup/run diff --git a/install/etc/cont-init.d/10-db-backup b/install/etc/cont-init.d/10-db-backup index 4e854dd..546eae5 100755 --- a/install/etc/cont-init.d/10-db-backup +++ b/install/etc/cont-init.d/10-db-backup @@ -6,10 +6,9 @@ prepare_service 03-monitoring PROCESS_NAME="db-backup" output_off -bootstrap_filesystem -bootstrap_variables -sanity_test setup_mode +db_backup_container_init +create_schedulers backup create_zabbix dbbackup liftoff diff --git a/install/etc/services.available/10-db-backup/run b/install/etc/services.available/10-db-backup/run deleted file mode 100755 index 1fd046d..0000000 --- a/install/etc/services.available/10-db-backup/run +++ /dev/null @@ -1,89 +0,0 @@ -#!/command/with-contenv bash - -source /assets/functions/00-container -source /assets/functions/10-db-backup -source /assets/defaults/10-db-backup -PROCESS_NAME="db-backup" - -bootstrap_variables - -write_log info "Starting instance - hostname $(hostname) container_name ${CONTAINER_NAME}" -if [ "${MODE,,}" = "manual" ] || [ "${1,,}" = "manual" ] || [ "${1,,}" = "now" ]; then - DB_DUMP_BEGIN=+0 - manual=TRUE - write_log debug "Detected Manual Mode" -else - sleep 5 - current_time=$(date +"%s") - today=$(date +"%Y%m%d") - - if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then - waittime=$(( ${BASH_REMATCH[1]} * 60 )) - target_time=$(($current_time + $waittime)) - else - target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s") - if [[ "$target_time" < "$current_time" ]]; then - target_time=$(($target_time + 24*60*60)) - fi - waittime=$(($target_time - $current_time)) - fi - write_log debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}" - write_log info "Next Backup at $(date -d @${target_time} +"%Y-%m-%d %T %Z")" - sleep $waittime -fi - -while true; do - mkdir -p "${TEMP_LOCATION}" - backup_start_time=$(date +"%s") - write_log debug "Backup routines started time: $(date +'%Y-%m-%d %T %Z')" - case "${dbtype,,}" in - "couch" ) - check_availability - backup_couch - ;; - "influx" ) - check_availability - backup_influx - ;; - "mssql" ) - check_availability - backup_mssql - ;; - "mysql" ) - check_availability - backup_mysql - ;; - "mongo" ) - check_availability - backup_mongo - ;; - "pgsql" ) - check_availability - backup_pgsql - ;; - "redis" ) - check_availability - backup_redis - ;; - "sqlite3" ) - check_availability - backup_sqlite3 - ;; - esac - - backup_finish_time=$(date +"%s") - backup_total_time=$(echo $((backup_finish_time-backup_start_time))) - if [ -z "$master_exit_code" ] ; then master_exit_code="0" ; fi - write_log info "Backup routines finish time: $(date -d @${backup_finish_time} +"%Y-%m-%d %T %Z") with overall exit code ${master_exit_code}" - write_log notice "Backup routines time taken: $(echo ${backup_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" - - cleanup_old_data - - if var_true "${manual}" ; then - write_log debug "Exiting due to manual mode" - exit ${master_exit_code}; - else - write_log notice "Sleeping for another $(($DB_DUMP_FREQ*60-backup_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($DB_DUMP_FREQ*60-backup_total_time))))" +"%Y-%m-%d %T %Z") " - sleep $(($DB_DUMP_FREQ*60-backup_total_time)) - fi -done From 57c853d02a5f3559f44e2d2eb5d9210b568a4d70 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:00:33 -0700 Subject: [PATCH 10/68] MIT --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 5dd7a07..35e8726 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2022 Dave Conroy +Copyright (c) 2023 Dave Conroy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From b0413e6708a190dd31999d6784148f87fedfef69 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:00:56 -0700 Subject: [PATCH 11/68] Version 4.0.x examples --- examples/compose.yml | 67 +++++++++++++++++++ .../{docker-compose.yml => compose.yml} | 59 ++++++++-------- .../mssql/{docker-compose.yml => compose.yml} | 45 ++++++------- examples/mysql/docker-compose.yml | 53 --------------- examples/{mysql => }/post-script.sh | 0 5 files changed, 118 insertions(+), 106 deletions(-) create mode 100644 examples/compose.yml rename examples/mssql-blobxfer/{docker-compose.yml => compose.yml} (60%) rename examples/mssql/{docker-compose.yml => compose.yml} (69%) delete mode 100644 examples/mysql/docker-compose.yml rename examples/{mysql => }/post-script.sh (100%) diff --git a/examples/compose.yml b/examples/compose.yml new file mode 100644 index 0000000..8c3f108 --- /dev/null +++ b/examples/compose.yml @@ -0,0 +1,67 @@ +services: + example-db: + hostname: example-db-host + container_name: example-db + image: tiredofit/mariadb:10.11 + ports: + - 3306:3306 + volumes: + - ./db:/var/lib/mysql + environment: + - ROOT_PASS=examplerootpassword + - DB_NAME=example + - DB_USER=example + - DB_PASS=examplepassword + restart: always + networks: + - example-db-network + + example-db-backup: + container_name: example-db-backup + image: tiredofit/db-backup + volumes: + - ./backups:/backup + #- ./post-script.sh:/assets/custom-scripts/post-script.sh + environment: + - TIMEZONE=America/Vancouver + - CONTAINER_NAME=example-db-backup + - CONTAINER_ENABLE_MONITORING=FALSE + # - DEBUG_MODE=TRUE + + - BACKUP_JOB_CONCURRENCY=1 # Only run one job at a time + - DEFAULT_CHECKSUM=NONE # Don't create checksums + - DEFAULT_COMPRESSION=ZSTD # Compress all with ZSTD + - DEFAULT_DUMP_INTERVAL=1440 # Backup every 1440 minutes + - DEFAULT_DUMP_BEGIN=0000 # Start backing up at midnight + - DEFAULT_CLEANUP_TIME=8640 # Cleanup backups after a week + + - DB01_TYPE=mariadb + - DB01_HOST=example-db-host + - DB01_NAME=example + - DB01_USER=example + - DB01_PASS=examplepassword + - DB01_DUMP_INTERVAL=30 # (override) Backup every 30 minutes + - DB01_DUMP_BEGIN=+1 # (override) Backup starts immediately + - DB01_CLEANUP_TIME=180 # (override) Cleanup backups they are older than 180 minutes + - DB01_CHECKSUM=SHA1 # (override) Create a SHA1 checksum + - DB01_COMPRESSION=GZ # (override) Compress with GZIP + + #- DB02_TYPE=postgres + #- DB02_HOST=example-postgres-host + #- DB02_NAME=example + #- DB02_USER=example + #- DB02_PASS=examplepassword + #- DB02_DUMP_INTERVAL=60 # (override) Backup every 60 minutes + #- DB02_DUMP_BEGIN=+10 # (override) Backup starts in ten minutes + #- DB02_CLEANUP_TIME=240 # (override) Cleanup backups they are older than 240 minutes + #- DB02_CHECKSUM=MD5 # (override) Create a SHA1 checksum + #- DB02_COMPRESSION=BZ # (override) Compress with BZIP + + restart: always + networks: + - example-db-network + +networks: + example-db-network: + name: example-db-network + diff --git a/examples/mssql-blobxfer/docker-compose.yml b/examples/mssql-blobxfer/compose.yml similarity index 60% rename from examples/mssql-blobxfer/docker-compose.yml rename to examples/mssql-blobxfer/compose.yml index efe648f..462834a 100644 --- a/examples/mssql-blobxfer/docker-compose.yml +++ b/examples/mssql-blobxfer/compose.yml @@ -1,13 +1,7 @@ -# +# # Example for Microsoft SQL Server # upload with blobxfer to azure storage -# - -version: '2' - -networks: - example-mssql-blobxfer-net: - name: example-mssql-blobxfer-net +# services: example-mssql-s3-db: @@ -32,7 +26,7 @@ services: # execute in terminal --> docker build -t tiredofit/db-backup-mssql-blobxfer . # replace --> image: tiredofit/db-backup-mssql # image: tiredofit/db-backup - image: tiredofit/db-backup-mssql-blobxfer + image: tiredofit/db-backup links: - example-mssql-s3-db volumes: @@ -40,30 +34,35 @@ services: - ./tmp/backups:/tmp/backups # shared tmp backup directory #- ./post-script.sh:/assets/custom-scripts/post-script.sh environment: - # - DEBUG_MODE=TRUE - - DB_TYPE=mssql - - DB_HOST=example-db-host - # - DB_PORT=1488 - # - DB_NAME=ALL # [ALL] not working on sql server. - # create database with name `test1` manually first - - DB_NAME=test1 # Create this database - - DB_USER=sa - - DB_PASS=5hQa0utRFBpIY3yhoIyE - - DB_DUMP_FREQ=1 # backup every 5 minute - # - DB_DUMP_BEGIN=0000 # backup starts immediately - - DB_CLEANUP_TIME=3 # clean backups they are older than 60 minutes - - ENABLE_CHECKSUM=TRUE - - CHECKSUM=SHA1 - - COMPRESSION=GZ - - SPLIT_DB=FALSE + - TIMEZONE=America/Vancouver - CONTAINER_ENABLE_MONITORING=FALSE + - CONTAINER_NAME=example-mssql-blobxfer-db-backup + # - DEBUG_MODE=TRUE + - DB01_TYPE=mssql + - DB01_HOST=example-db-host + # - DB01_PORT=1488 + # create database with name `test1` manually first + - DB01_NAME=test1 # Create this database + - DB01_USER=sa + - DB01_PASS=5hQa0utRFBpIY3yhoIyE + - DB01_DUMP_INTERVAL=5 # backup every 5 minute + # - DB01_DUMP_BEGIN=0000 # backup starts at midnight vs not set immediately + - DB01_CLEANUP_TIME=60 # clean backups they are older than 60 minutes + - DB01_CHECKSUM=SHA1 # Set Checksum to be SHA1 + - DB01_COMPRESSION=GZ # Set compression to use GZIP + + # === S3 Blobxfer === - - BACKUP_LOCATION=blobxfer + - DB01_BACKUP_LOCATION=blobxfer # Add here azure storage account - - BLOBXFER_STORAGE_ACCOUNT={TODO Add Storage Name} + - DB01_BLOBXFER_STORAGE_ACCOUNT={TODO Add Storage Name} # Add here azure storage account key - - BLOBXFER_STORAGE_ACCOUNT_KEY={TODO Add Key} - - BLOBXFER_REMOTE_PATH=docker-db-backup + - SB01_BLOBXFER_STORAGE_ACCOUNT_KEY={TODO Add Key} + - DB01_BLOBXFER_REMOTE_PATH=docker-db-backup restart: always networks: - example-mssql-blobxfer-net: \ No newline at end of file + example-mssql-blobxfer-net: + +networks: + example-mssql-blobxfer-net: + name: example-mssql-blobxfer-net diff --git a/examples/mssql/docker-compose.yml b/examples/mssql/compose.yml similarity index 69% rename from examples/mssql/docker-compose.yml rename to examples/mssql/compose.yml index 4e9554c..b2ae491 100644 --- a/examples/mssql/docker-compose.yml +++ b/examples/mssql/compose.yml @@ -1,12 +1,6 @@ -# +# # Example for Microsoft SQL Server -# - -version: '2' - -networks: - example-mssql-net: - name: example-mssql-net +# services: example-mssql-db: @@ -31,7 +25,7 @@ services: # execute in terminal --> docker build -t tiredofit/db-backup-mssql . # replace --> image: tiredofit/db-backup-mssql # image: tiredofit/db-backup - image: tiredofit/db-backup-mssql + image: tiredofit/db-backup links: - example-mssql-db volumes: @@ -39,23 +33,28 @@ services: - ./tmp/backups:/tmp/backups # shared tmp backup directory #- ./post-script.sh:/assets/custom-scripts/post-script.sh environment: + - TIMEZONE=America/Vancouver + - CONTAINER_ENABLE_MONITORING=FALSE + - CONTAINER_NAME=example-mssql-blobxfer-db-backup + # - DEBUG_MODE=TRUE - - DB_TYPE=mssql - - DB_HOST=example-db-host + - DB01_TYPE=mssql + - DB01_HOST=example-db-host # - DB_PORT=1488 # - DB_NAME=ALL # [ALL] not working on sql server. # create database with name `test1` manually first - - DB_NAME=test1 - - DB_USER=sa - - DB_PASS=5hQa0utRFBpIY3yhoIyE - - DB_DUMP_FREQ=1 # backup every minute - # - DB_DUMP_BEGIN=0000 # backup starts immediately - - DB_CLEANUP_TIME=5 # clean backups they are older than 5 minute - - ENABLE_CHECKSUM=FALSE - - CHECKSUM=SHA1 - - COMPRESSION=GZ - - SPLIT_DB=FALSE - - CONTAINER_ENABLE_MONITORING=FALSE + - DB01_NAME=test1 + - DB01_USER=sa + - DB01_PASS=5hQa0utRFBpIY3yhoIyE + - DB01_DUMP_INTERVAL=1 # backup every minute + # - DB01_DUMP_BEGIN=0000 # backup starts at midnight vs unset immediately + - DB01_CLEANUP_TIME=5 # clean backups they are older than 5 minute + - DB01_CHECKSUM=NONE + - DB01_COMPRESSION=GZ restart: always networks: - example-mssql-net: \ No newline at end of file + example-mssql-net: + +networks: + example-mssql-net: + name: example-mssql-net diff --git a/examples/mysql/docker-compose.yml b/examples/mysql/docker-compose.yml deleted file mode 100644 index f306aae..0000000 --- a/examples/mysql/docker-compose.yml +++ /dev/null @@ -1,53 +0,0 @@ -version: '2' - -networks: - example-db-network: - name: example-db-network - -services: - example-db: - hostname: example-db-host - container_name: example-db - image: mariadb:latest - ports: - - 13306:3306 - volumes: - - ./db:/var/lib/mysql - environment: - - MYSQL_ROOT_PASSWORD=examplerootpassword - - MYSQL_DATABASE=example - - MYSQL_USER=example - - MYSQL_PASSWORD=examplepassword - restart: always - networks: - - example-db-network - - example-db-backup: - container_name: example-db-backup - image: tiredofit/db-backup - links: - - example-db - volumes: - - ./backups:/backup - #- ./post-script.sh:/assets/custom-scripts/post-script.sh - environment: - - TIMEZONE=America/Vancouver - - CONTAINER_ENABLE_MONITORING=FALSE - # - DEBUG_MODE=TRUE - - DB_TYPE=mariadb - - DB_HOST=example-db-host - - DB_NAME=example - - DB_USER=example - - DB_PASS=examplepassword - - DB_DUMP_FREQ=1 # backup every minute - # - DB_DUMP_BEGIN=0000 # backup starts immediately - - DB_CLEANUP_TIME=5 # clean backups they are older than 5 minute - - CHECKSUM=SHA1 - - COMPRESSION=GZ - - SPLIT_DB=FALSE - - restart: always - networks: - - example-db-network - - diff --git a/examples/mysql/post-script.sh b/examples/post-script.sh similarity index 100% rename from examples/mysql/post-script.sh rename to examples/post-script.sh From cfbac00268cf86e9607a17135161f7f07db82dd5 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:01:17 -0700 Subject: [PATCH 12/68] Zabbix Template > 4.0.x --- zabbix_templates/db_backup.json | 319 ++++++++++++++------------------ 1 file changed, 135 insertions(+), 184 deletions(-) diff --git a/zabbix_templates/db_backup.json b/zabbix_templates/db_backup.json index c550dec..7dadc3a 100644 --- a/zabbix_templates/db_backup.json +++ b/zabbix_templates/db_backup.json @@ -1,10 +1,9 @@ { "zabbix_export": { - "version": "6.0", - "date": "2022-03-18T13:32:12Z", - "groups": [ + "version": "6.4", + "template_groups": [ { - "uuid": "fa56524b5dbb4ec09d9777a6f7ccfbe4", + "uuid": "10b88d2b3a3a4c72b43bdce9310e1162", "name": "DB/Backup" }, { @@ -14,10 +13,10 @@ ], "templates": [ { - "uuid": "5fc64d517afb4cc5bc09a3ef58b43ef7", + "uuid": "5a16c1bd694145389eed5ee803d954cc", "template": "DB Backup", "name": "DB Backup", - "description": "Template for Docker DB Backup Image\n\nMeant for use specifically with https://github.com/tiredofit/docker-db-backup\nLast tested with version 3.0.2", + "description": "Template for Docker DB Backup Image\n\nMeant for use specifically with https://github.com/tiredofit/docker-db-backup Version > 4.0.0\n\nSupports auto discovery of backup jobs and creates graphs and triggers", "groups": [ { "name": "DB/Backup" @@ -26,134 +25,144 @@ "name": "Templates/Databases" } ], - "items": [ + "discovery_rules": [ { - "uuid": "72fd00fa2dd24e479f5affe03e8711d8", - "name": "DB Backup: Backup Duration", + "uuid": "94bb6f862e1841f8b2834b04c41c1d86", + "name": "Backup", "type": "TRAP", - "key": "dbbackup.backup_duration", + "key": "dbbackup.backup.[{#NAME}]", "delay": "0", - "history": "7d", - "units": "uptime", - "description": "How long the backup took", - "tags": [ + "item_prototypes": [ { - "tag": "Application", - "value": "DB Backup" - } - ] - }, - { - "uuid": "3549a2c9d56849babc6dc3c855484c1e", - "name": "DB Backup: Backup Time", - "type": "TRAP", - "key": "dbbackup.datetime", - "delay": "0", - "history": "7d", - "units": "unixtime", - "request_method": "POST", - "tags": [ + "uuid": "bb6472e30bff4d9c908b1d34b893e622", + "name": "[{#NAME}] Last Backup", + "type": "TRAP", + "key": "dbbackup.backup.datetime.[{#NAME}]", + "delay": "0", + "history": "7d", + "units": "unixtime", + "description": "Datestamp of last database backup", + "trigger_prototypes": [ + { + "uuid": "3681b56bb882466fb304a48b4beb15f0", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "name": "[{#NAME}] No backups detected in 2 days", + "priority": "HIGH", + "manual_close": "YES" + }, + { + "uuid": "6c70136c84994197b6396a143b4e956f", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "name": "[{#NAME}] No backups detected in 3 days", + "priority": "DISASTER", + "manual_close": "YES" + }, + { + "uuid": "d2038025cab643019cb9610c301f0cb9", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "name": "[{#NAME}] No backups detected in 4 days", + "priority": "DISASTER", + "manual_close": "YES" + }, + { + "uuid": "ea85f02d032c4a1dbc1b6e91a3b2b37b", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)=0", + "name": "[{#NAME}] No backups detected in 5 days", + "priority": "DISASTER", + "manual_close": "YES" + } + ] + }, { - "tag": "Application", - "value": "DB Backup" + "uuid": "8ec2b2f44ddf4f36b3dbb2aa15e3a32f", + "name": "[{#NAME}] Duration", + "type": "TRAP", + "key": "dbbackup.backup.duration.[{#NAME}]", + "delay": "0", + "history": "7d", + "units": "uptime", + "description": "How long the DB Backup job took" + }, + { + "uuid": "43b700c03897465eb7e49bbfe8fc9fc5", + "name": "[{#NAME}] Size", + "type": "TRAP", + "key": "dbbackup.backup.size.[{#NAME}]", + "delay": "0", + "history": "7d", + "description": "Backup Size", + "trigger_prototypes": [ + { + "uuid": "849f8660bee04427aff55af47b6f509c", + "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>1.2", + "name": "[{#NAME}] Backup 20% Greater in size", + "priority": "WARNING", + "manual_close": "YES" + }, + { + "uuid": "74d16a7680544c65af22cc568ce3d59d", + "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>0.2", + "name": "[{#NAME}] Backup 20% Smaller in Size", + "priority": "WARNING" + }, + { + "uuid": "5595d769c73f4eaeadda95c84c2c0f17", + "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])<1K", + "name": "[{#NAME}] Backup Empty", + "priority": "HIGH" + } + ] + }, + { + "uuid": "a6fc542a565c4baba8429ed9ab31b5ae", + "name": "[{#NAME}] Status", + "type": "TRAP", + "key": "dbbackup.backup.status.[{#NAME}]", + "delay": "0", + "history": "7d", + "description": "Maps exit code by DB Backup procedure", + "valuemap": { + "name": "Backup Status" + }, + "trigger_prototypes": [ + { + "uuid": "74b91e28453b4c2a84743f5e371495c1", + "expression": "last(/DB Backup/dbbackup.backup.status.[{#NAME}])=1", + "name": "[{#NAME}] Backup - Failed with errors", + "priority": "WARNING", + "manual_close": "YES" + } + ] } ], - "triggers": [ + "graph_prototypes": [ { - "uuid": "3ac1e074ffea46eb8002c9c08a85e7b4", - "expression": "nodata(/DB Backup/dbbackup.datetime,2d)=1", - "name": "DB-Backup: No backups detected in 2 days", - "priority": "DISASTER", - "manual_close": "YES" + "uuid": "b5e8e9fe0c474fedba2b06366234afdf", + "name": "[{#NAME}] Backup Duration", + "graph_items": [ + { + "color": "199C0D", + "calc_fnc": "ALL", + "item": { + "host": "DB Backup", + "key": "dbbackup.backup.duration.[{#NAME}]" + } + } + ] }, { - "uuid": "b8b5933dfa1a488c9c37dd7f4784c1ff", - "expression": "fuzzytime(/DB Backup/dbbackup.datetime,172800s)=0 and fuzzytime(/DB Backup/dbbackup.datetime,259200s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,345600s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,432800s)<>0", - "name": "DB Backup: No Backups occurred in 2 days", - "priority": "AVERAGE" - }, - { - "uuid": "35c5f420d0e142cc9601bae38decdc40", - "expression": "fuzzytime(/DB Backup/dbbackup.datetime,172800s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,259200s)=0 and fuzzytime(/DB Backup/dbbackup.datetime,345600s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,432800s)<>0", - "name": "DB Backup: No Backups occurred in 3 days", - "priority": "AVERAGE" - }, - { - "uuid": "03c3719d82c241e886a0383c7d908a77", - "expression": "fuzzytime(/DB Backup/dbbackup.datetime,172800s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,259200s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,345600s)=0 and fuzzytime(/DB Backup/dbbackup.datetime,432800s)<>0", - "name": "DB Backup: No Backups occurred in 4 days", - "priority": "AVERAGE" - }, - { - "uuid": "1634a03e44964e42b7e0101f5f68499c", - "expression": "fuzzytime(/DB Backup/dbbackup.datetime,172800s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,259200s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,345600s)<>0 and fuzzytime(/DB Backup/dbbackup.datetime,432800s)=0", - "name": "DB Backup: No Backups occurred in 5 days or more", - "priority": "HIGH" - } - ] - }, - { - "uuid": "467dfec952b34f5aa4cc890b4351b62d", - "name": "DB Backup: Backup Size", - "type": "TRAP", - "key": "dbbackup.size", - "delay": "0", - "history": "7d", - "units": "B", - "request_method": "POST", - "tags": [ - { - "tag": "Application", - "value": "DB Backup" - } - ], - "triggers": [ - { - "uuid": "a41eb49b8a3541afb6de247dca750e38", - "expression": "last(/DB Backup/dbbackup.size)/last(/DB Backup/dbbackup.size,#2)>1.2", - "name": "DB Backup: 20% Greater in Size", - "priority": "WARNING", - "manual_close": "YES" - }, - { - "uuid": "422f66be5049403293f3d96fc53f20cd", - "expression": "last(/DB Backup/dbbackup.size)/last(/DB Backup/dbbackup.size,#2)<0.2", - "name": "DB Backup: 20% Smaller in Size", - "priority": "WARNING", - "manual_close": "YES" - }, - { - "uuid": "d6d9d875b92f4d799d4bc89aabd4e90e", - "expression": "last(/DB Backup/dbbackup.size)<1K", - "name": "DB Backup: empty", - "priority": "HIGH" - } - ] - }, - { - "uuid": "a6b13e8b46a64abab64a4d44d620d272", - "name": "DB Backup: Last Backup Status", - "type": "TRAP", - "key": "dbbackup.status", - "delay": "0", - "history": "7d", - "description": "Maps Exit Codes received by backup applications", - "valuemap": { - "name": "DB Backup Status" - }, - "tags": [ - { - "tag": "Application", - "value": "DB Backup" - } - ], - "triggers": [ - { - "uuid": "23d71e356f96493180f02d4b84a79fd6", - "expression": "last(/DB Backup/dbbackup.status)=1", - "name": "DB Backup: Failed Backup Detected", - "priority": "HIGH", - "manual_close": "YES" + "uuid": "99b5deb4e28f40059c50846c7be2ef26", + "name": "[{#NAME}] Backup Size", + "graph_items": [ + { + "color": "199C0D", + "calc_fnc": "ALL", + "item": { + "host": "DB Backup", + "key": "dbbackup.backup.size.[{#NAME}]" + } + } + ] } ] } @@ -168,38 +177,10 @@ "value": "Database" } ], - "dashboards": [ - { - "uuid": "90c81bb47184401ca9663626784a6f30", - "name": "DB Backup", - "pages": [ - { - "widgets": [ - { - "type": "GRAPH_CLASSIC", - "name": "Backup Size", - "width": "23", - "height": "5", - "fields": [ - { - "type": "GRAPH", - "name": "graphid", - "value": { - "name": "DB Backup: Backup Size", - "host": "DB Backup" - } - } - ] - } - ] - } - ] - } - ], "valuemaps": [ { - "uuid": "82f3a3d01b3c42b8942b59d2363724e0", - "name": "DB Backup Status", + "uuid": "92a87279388b4fd1ac51c1e417e1776e", + "name": "Backup Status", "mappings": [ { "value": "0", @@ -214,36 +195,6 @@ } ] } - ], - "graphs": [ - { - "uuid": "6e02c200b76046bab76062cd1ab086b2", - "name": "DB Backup: Backup Duration", - "graph_items": [ - { - "color": "199C0D", - "item": { - "host": "DB Backup", - "key": "dbbackup.backup_duration" - } - } - ] - }, - { - "uuid": "b881ee18f05c4f4c835982c9dfbb55d6", - "name": "DB Backup: Backup Size", - "type": "STACKED", - "graph_items": [ - { - "sortorder": "1", - "color": "1A7C11", - "item": { - "host": "DB Backup", - "key": "dbbackup.size" - } - } - ] - } ] } } \ No newline at end of file From ee294c08a28867df2b9633514f94797796a1cc71 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:01:41 -0700 Subject: [PATCH 13/68] Update scheduler template --- install/assets/dbbackup/template-dbbackup/run | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100755 install/assets/dbbackup/template-dbbackup/run diff --git a/install/assets/dbbackup/template-dbbackup/run b/install/assets/dbbackup/template-dbbackup/run new file mode 100755 index 0000000..f28f813 --- /dev/null +++ b/install/assets/dbbackup/template-dbbackup/run @@ -0,0 +1,114 @@ +#!/command/with-contenv bash + +source /assets/functions/00-container +PROCESS_NAME="db-backup{{BACKUP_NUMBER}}-scheduler" +check_container_initialized +check_service_initialized init 10-db-backup +source /assets/functions/10-db-backup +source /assets/defaults/10-db-backup +bootstrap_variables backup_init {{BACKUP_NUMBER}} +bootstrap_variables parse_variables {{BACKUP_NUMBER}} +PROCESS_NAME="{{BACKUP_NUMBER}}-${backup_job_db_host}__${backup_job_db_name}" + + +trap ctrl_c INT + +if [[ "${MODE,,}" =~ "standalone" ]] || [ "${1,,}" = "manual" ] || [ "${1,,}" = "now" ]; then + print_debug "Detected Manual Mode" + persist=false + backup_job_backup_begin=+0 +else + silent sleep {{BACKUP_NUMBER}} + current_time=$(date +'%s') + today=$(date +"%Y%m%d") + + if [[ ${backup_job_backup_begin} =~ ^\+(.*)$ ]]; then + waittime=$(( ${BASH_REMATCH[1]} * 60 )) + target_time=$(($current_time + $waittime)) + else + target_time=$(date --date="${today} ${backup_job_backup_begin}" +'%s') + if [[ "$target_time" < "$current_time" ]]; then + target_time=$(($target_time + 24*60*60)) + fi + waittime=$(($target_time - $current_time)) + fi + print_debug "Wait Time: ${waittime} Target time: ${target_time} Current Time: ${current_time}" + print_info "Next Backup at $(date -d @${target_time} +'%Y-%m-%d %T %Z')" + silent sleep $waittime + set +x +fi + +while true; do + if [ -n "${backup_job_blackout_start}" ] && [ -n "${backup_job_blackout_finish}" ] ; then + hour_minute=$(date +%H%M) + if [[ "${hour_minute}" > "${backup_job_blackout_start}" ]] && [[ "${hour_minute}" < "${backup_job_blackout_finish}" ]] ; then + blackout=true + else + blackout=false + fi + fi + if var_true "${blackout}" ; then + print_notice "Detected Blackout Period - Not performing backup operations" + else + process_limiter + backup_routines_start_time=$(date +'%s') + echo "{{BACKUP_NUMBER}}" >> /tmp/.container/db-backup-backups + print_debug "Backup {{BACKUP_NUMBER}} routines started time: $(date +'%Y-%m-%d %T %Z')" + bootstrap_filesystem + case "${dbtype,,}" in + "couch" ) + check_availability + backup_couch + ;; + "influx" ) + check_availability + backup_influx + ;; + "mssql" ) + check_availability + backup_mssql + ;; + "mysql" ) + check_availability + backup_mysql + ;; + "mongo" ) + check_availability + backup_mongo + ;; + "pgsql" ) + check_availability + backup_pgsql + ;; + "redis" ) + check_availability + backup_redis + ;; + "sqlite3" ) + check_availability + backup_sqlite3 + ;; + esac + backup_routines_finish_time=$(date +'%s') + backup_routines_total_time=$(echo $((backup_routines_finish_time-backup_routines_start_time))) + if [ -z "${exitcode_backup}" ] ; then exitcode_backup="0" ; fi + print_info "Backup {{BACKUP_NUMBER}} routines finish time: $(date -d @${backup_routines_finish_time} +'%Y-%m-%d %T %Z') with exit code ${exitcode_backup}" + print_notice "Backup {{BACKUP_NUMBER}} routines time taken: $(echo ${backup_routines_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" + sed -i "/^{{BACKUP_NUMBER}}/d" /tmp/.container/db-backup-backups + fi + + symlink_log + + if var_false "${persist}" ; then + print_debug "Exiting due to manual mode" + exit ${exitcode_backup}; + else + if var_true "${stop_scheduler_backup}" ; then + print_error "Stopping backup_scheduler {{BACKUP_NUMBER}} due to detected errors. Fix and restart container." + s6-svc -d /var/run/s6/legacy-services/dbbackup-{{BACKUP_NUMBER}} + else + print_notice "Sleeping for another $(($backup_job_backup_interval*60-backup_routines_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($backup_job_backup_interval*60-backup_routines_total_time))))" +'%Y-%m-%d %T %Z') " + silent sleep $(($backup_job_backup_interval*60-backup_routines_total_time)) + fi + fi +done From 82d8175eeb52c864831d15c3a07aaa120635ece3 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:01:59 -0700 Subject: [PATCH 14/68] Support 4.0.x defaults --- install/assets/defaults/10-db-backup | 64 +++++++++++++++------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index f1b2554..c7bf54c 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -1,35 +1,39 @@ #!/command/with-contenv bash -BACKUP_LOCATION=${BACKUP_LOCATION:-"FILESYSTEM"} -BLOBXFER_REMOTE_PATH=${BLOBXFER_REMOTE_PATH:-"/docker-db-backup"} -CHECKSUM=${CHECKSUM:-"MD5"} -COMPRESSION=${COMPRESSION:-"ZSTD"} -COMPRESSION_LEVEL=${COMPRESSION_LEVEL:-"3"} -CREATE_LATEST_SYMLINK=${CREATE_LATEST_SYMLINK:-"TRUE"} -DB_DUMP_BEGIN=${DB_DUMP_BEGIN:-+0} -DB_DUMP_FREQ=${DB_DUMP_FREQ:-1440} -DB_DUMP_TARGET=${DB_DUMP_TARGET:-"/backup"} -DB_DUMP_TARGET_PERMISSION=${DB_DUMP_TARGET_PERMISSION:-"700"} -DB_DUMP_TARGET_ARCHIVE=${DB_DUMP_TARGET_ARCHIVE:-"${DB_DUMP_TARGET}/archive/"} -ENABLE_CHECKSUM=${ENABLE_CHECKSUM:-"TRUE"} -ENABLE_PARALLEL_COMPRESSION=${ENABLE_PARALLEL_COMPRESSION:-"TRUE"} -LOG_LEVEL=${LOG_LEVEL:-"debug"} +BACKUP_JOB_CONCURRENCY=${BACKUP_JOB_CONCURRENCY:-"1"} +DEFAULT_BACKUP_BEGIN=${DEFAULT_BACKUP_BEGIN:-+0} +DEFAULT_BACKUP_INTERVAL=${DEFAULT_BACKUP_INTERVAL:-1440} +DEFAULT_BACKUP_INTERVAL=${DEFAULT_BACKUP_INTERVAL:-1440} +DEFAULT_BACKUP_LOCATION=${DEFAULT_BACKUP_LOCATION:-"FILESYSTEM"} +DEFAULT_BLOBXFER_REMOTE_PATH=${DEFAULT_BLOBXFER_REMOTE_PATH:-"/docker-db-backup"} +DEFAULT_CHECKSUM=${DEFAULT_CHECKSUM:-"MD5"} +DEFAULT_COMPRESSION=${DEFAULT_COMPRESSION:-"ZSTD"} +DEFAULT_COMPRESSION_LEVEL=${DEFAULT_COMPRESSION_LEVEL:-"3"} +DEFAULT_CREATE_LATEST_SYMLINK=${DEFAULT_CREATE_LATEST_SYMLINK:-"TRUE"} +DEFAULT_ENABLE_PARALLEL_COMPRESSION=${DEFAULT_ENABLE_PARALLEL_COMPRESSION:-"TRUE"} +DEFAULT_FILESYSTEM_PATH=${DEFAULT_FILESYSTEM_PATH:-"/backup"} +DEFAULT_FILESYSTEM_PERMISSION=${DEFAULT_FILESYSTEM_PERMISSION:-"700"} +DEFAULT_FILESYSTEM_ARCHIVE_PATH=${DEFAULT_FILESYSTEM_ARCHIVE_PATH:-"${DEFAULT_FILESYSTEM_PATH}/archive/"} +DEFAULT_LOG_LEVEL=${DEFAULT_LOG_LEVEL:-"notice"} +DEFAULT_MYSQL_ENABLE_TLS=${DEFAULT_MYSQL_ENABLE_TLS:-"FALSE"} +DEFAULT_MYSQL_MAX_ALLOWED_PACKET=${DEFAULT_MYSQL_MAX_ALLOWED_PACKET:-"512M"} +DEFAULT_MYSQL_SINGLE_TRANSACTION=${DEFAULT_MYSQL_SINGLE_TRANSACTION:-"TRUE"} +DEFAULT_MYSQL_STORED_PROCEDURES=${DEFAULT_MYSQL_STORED_PROCEDURES:-"TRUE"} +DEFAULT_MYSQL_TLS_CA_FILE=${DEFAULT_MYSQL_TLS_CA_FILE:-"/etc/ssl/cert.pem"} +DEFAULT_MYSQL_TLS_VERIFY=${DEFAULT_MYSQL_TLS_VERIFY:-"FALSE"} +DEFAULT_MYSQL_TLS_VERSION=${DEFAULT_MYSQL_TLS_VERSION:-"TLSv1.1,TLSv1.2,TLSv1.3"} +DEFAULT_PARALLEL_COMPRESSION_THREADS=${DEFAULT_PARALLEL_COMPRESSION_THREADS:-"$(nproc)"} +DEFAULT_S3_CERT_SKIP_VERIFY=${DEFAULT_S3_CERT_SKIP_VERIFY:-"TRUE"} +DEFAULT_S3_PROTOCOL=${DEFAULT_S3_PROTOCOL:-"https"} +DEFAULT_SCRIPT_LOCATION_PRE=${DEFAULT_SCRIPT_LOCATION_PRE:-"/assets/scripts/pre/"} +DEFAULT_SCRIPT_LOCATION_POST=${DEFAULT_SCRIPT_LOCATION_POST:-"/assets/scripts/post/"} +DEFAULT_SIZE_VALUE=${DEFAULT_SIZE_VALUE:-"bytes"} +DEFAULT_SKIP_AVAILABILITY_CHECK=${DEFAULT_SKIP_AVAILABILITY_CHECK:-"FALSE"} +DEFAULT_SPLIT_DB=${DEFAULT_SPLIT_DB:-"TRUE"} LOG_PATH=${LOG_PATH:-"/logs"} MANUAL_RUN_FOREVER=${MANUAL_RUN_FOREVER:-"TRUE"} MODE=${MODE:-"AUTO"} -MYSQL_ENABLE_TLS=${MYSQL_ENABLE_TLS:-"FALSE"} -MYSQL_MAX_ALLOWED_PACKET=${MYSQL_MAX_ALLOWED_PACKET:-"512M"} -MYSQL_SINGLE_TRANSACTION=${MYSQL_SINGLE_TRANSACTION:-"TRUE"} -MYSQL_STORED_PROCEDURES=${MYSQL_STORED_PROCEDURES:-"TRUE"} -MYSQL_TLS_CA_FILE=${MYSQL_TLS_CA_FILE:-"/etc/ssl/cert.pem"} -MYSQL_TLS_VERIFY=${MYSQL_TLS_VERIFY:-"FALSE"} -MYSQL_TLS_VERSION=${MYSQL_TLS_VERSION:-"TLSv1.1,TLSv1.2,TLSv1.3"} -PARALLEL_COMPRESSION_THREADS=${PARALLEL_COMPRESSION_THREADS:-"$(nproc)"} -S3_CERT_SKIP_VERIFY=${S3_CERT_SKIP_VERIFY:-"TRUE"} -S3_PROTOCOL=${S3_PROTOCOL:-"https"} -SCRIPT_LOCATION_PRE=${SCRIPT_LOCATION_PRE:-"/assets/scripts/pre/"} -SCRIPT_LOCATION_POST=${SCRIPT_LOCATION_POST:-"/assets/scripts/post/"} -SIZE_VALUE=${SIZE_VALUE:-"bytes"} -SKIP_AVAILABILITY_CHECK=${SKIP_AVAILABILITY_CHECK:-"FALSE"} -SPLIT_DB=${SPLIT_DB:-"TRUE"} -TEMP_LOCATION=${TEMP_LOCATION:-"/tmp/backups"} + +TEMP_PATH=${TEMP_PATH:-"/tmp/backups"} +if [ -n "${TEMP_LOCATION}" ] ; then TEMP_PATH=${TEMP_LOCATION:-"/tmp/backups"} ; fi # To be removed 4.3.0 + From c0c7202b6d41e241a239c4b83c0a127862364b46 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:02:13 -0700 Subject: [PATCH 15/68] Add multi host support --- install/assets/functions/10-db-backup | 1199 +++++++++++++++++-------- 1 file changed, 825 insertions(+), 374 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 00c7ac2..4bf8e37 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1,15 +1,15 @@ #!/command/with-contenv bash bootstrap_filesystem() { - if [ ! -d "${DB_DUMP_TARGET}" ]; then - mkdir -p "${DB_DUMP_TARGET}" + if [ ! -d "${backup_job_filesystem_path}" ]; then + mkdir -p "${backup_job_filesystem_path}" fi - if [ "$(stat -c %U "${DB_DUMP_TARGET}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${DB_DUMP_TARGET}" ; fi - if [ "$(stat -c %a "${DB_DUMP_TARGET}")" != "${DB_DUMP_TARGET_PERMISSION}" ] ; then chmod -R ${DB_DUMP_TARGET_PERMISSION} "${DB_DUMP_TARGET}" ; fi + if [ "$(stat -c %U "${backup_job_filesystem_path}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${backup_job_filesystem_path}" ; fi + if [ "$(stat -c %a "${backup_job_filesystem_path}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R ${backup_job_filesystem_permission} "${backup_job_filesystem_path}" ; fi - if [ -d "${DB_DUMP_TARGET_ARCHIVE}" ]; then - if [ "$(stat -c %U "${DB_DUMP_TARGET_ARCHIVE}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${DB_DUMP_TARGET_ARCHIVE}" ; fi - if [ "$(stat -c %a "${DB_DUMP_TARGET_ARCHIVE}")" != "${DB_DUMP_TARGET_PERMISSION}" ] ; then chmod -R ${DB_DUMP_TARGET_PERMISSION} "${DB_DUMP_TARGET_ARCHIVE}" ; fi + if [ -d "${backup_job_filesystem_archive}" ]; then + if [ "$(stat -c %U "${backup_job_filesystem_archive}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${backup_job_filesystem_archive}" ; fi + if [ "$(stat -c %a "${backup_job_filesystem_archive}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R ${backup_job_filesystem_permission} "${backup_job_filesystem_archive}" ; fi fi if [ ! -d "${LOG_PATH}" ]; then @@ -17,182 +17,375 @@ bootstrap_filesystem() { fi if [ "$(stat -c %U "${LOG_PATH}")" != "dbbackup" ] ; then chown dbbackup:dbbackup "${LOG_PATH}" ; fi + if [ ! -d "${LOG_PATH}"/$(date +'%Y%m%d') ]; then run_as_user mkdir -p "${LOG_PATH}"/$(date +'%Y%m%d'); fi if [ "$(stat -c %a "${LOG_PATH}")" != "755" ] ; then chmod -R 755 "${LOG_PATH}" ; fi - echo "0 0 * * * /usr/sbin/logrotate_dbbackup >/dev/null 2>&1" > /assets/cron/dbbackup_logrotate - - if [ ! -d "${TEMP_LOCATION}" ]; then - mkdir -p "${TEMP_LOCATION}" + if [ ! -d "${TEMP_PATH}" ]; then + mkdir -p "${TEMP_PATH}" fi - if [ "$(stat -c %U "${TEMP_LOCATION}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${TEMP_LOCATION}" ; fi + if [ "$(stat -c %U "${TEMP_PATH}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${TEMP_PATH}" ; fi } bootstrap_variables() { - sanity_var DB_TYPE "Set appropriate DB_TYPE" - transform_file_var \ - DB_HOST \ - DB_NAME \ - DB_PORT \ - DB_USER \ - DB_PASS + backup_init() { + backup_instance_number=${1} + backup_instance_vars=$(mktemp) + set -o posix ; set | grep -oE "^backup_job_.*=" | tr " " "\n" | grep -oE ".*=" | sed "/--/d" > "${backup_instance_vars}" + while read -r backup_instance_var ; do + unset "$(echo "${backup_instance_var}" | cut -d = -f 1)" + done < "${backup_instance_vars}" - case "${DB_TYPE,,}" in - couch* ) - dbtype=couch - DB_PORT=${DB_PORT:-5984} - sanity_var DB_USER - sanity_var DB_PASS - ;; - influx* ) - dbtype=influx - case "${INFLUX_VERSION}" in - 1) DB_PORT=${DB_PORT:-8088} ;; - 2) DB_PORT=${DB_PORT:-8086} ;; - esac - sanity_var DB_USER - sanity_var DB_PASS - sanity_var INFLUX_VERSION "What InfluxDB version you are backing up from '1' or '2'" - ;; - mongo* ) - dbtype=mongo - transform_file_var MONGO_CUSTOM_URI - if [ -n "${MONGO_CUSTOM_URI}" ] ; then - mongo_uri_proto=$(echo "${MONGO_CUSTOM_URI}" | grep :// | sed -e's,^\(.*://\).*,\1,g') - mongo_uri_scratch="${MONGO_CUSTOM_URI/${mongo_uri_proto}/}" - mongo_uri_username_password="$(echo "${mongo_uri_scratch}" | grep @ | rev | cut -d@ -f2- | rev)" - if [ -n "${mongo_uri_username_password}" ]; then mongo_uri_scratch="$(echo "${mongo_uri_scratch}" | rev | cut -d@ -f1 | rev)" ; fi - mongo_uri_port="$(echo "${mongo_uri_scratch}" | grep : | rev | cut -d: -f2- | rev)" - if [ -n "${mongo_uri_port}" ]; then mongo_uri_port="$(echo "${mongo_uri_scratch}" | rev | cut -d: -f1 | cut -d/ -f2 | rev)" ; fi - mongo_uri_hostname="$(echo "${mongo_uri_scratch}" | cut -d/ -f1 | cut -d: -f1 )" - mongo_uri_database="$(echo "${mongo_uri_scratch}" | cut -d/ -f2 | cut -d? -f1 )" - mongo_uri_options="$(echo "${mongo_uri_scratch}" | cut -d/ -f2 | cut -d? -f2 )" - DB_NAME=${DB_NAME:-"${mongo_uri_database,,}"} - DB_HOST=${DB_HOST:-"${mongo_uri_hostname,,}"} + transform_file_var \ + DEFAULT_AUTH \ + DEFAULT_TYPE \ + DEFAULT_HOST \ + DEFAULT_PORT \ + DEFAULT_NAME \ + DEFAULT_NAME_EXCLUDE \ + DEFAULT_USER \ + DEFAULT_PASS \ + DEFAULT_MONGO_CUSTOM_URI \ + DEFAULT_MYSQL_TLS_CA_FILE \ + DEFAULT_MYSQL_TLS_CERT_FILE \ + DEFAULT_MYSQL_TLS_KEY_FILE \ + DEFAULT_S3_BUCKET \ + DEFAULT_S3_KEY_ID \ + DEFAULT_S3_KEY_SECRET \ + DEFAULT_S3_PATH \ + DEFAULT_S3_REGION \ + DEFAULT_S3_HOST \ + DEFAULT_S3_PROTOCOL \ + DEFAULT_S3_EXTRA_OPTS \ + DEFAULT_S3_CERT_CA_FILE \ + DEFAULT_BLOBXFER_STORAGE_ACCOUNT \ + DEFAULT_BLOBXFER_STORAGE_ACCOUNT_KEY \ + DEFAULT_BLOBXFER_REMOTE_PATH \ + DB"${backup_instance_number}"_AUTH \ + DB"${backup_instance_number}"_TYPE \ + DB"${backup_instance_number}"_HOST \ + DB"${backup_instance_number}"_PORT \ + DB"${backup_instance_number}"_MYSQL_TLS_CERT_FILE \ + DB"${backup_instance_number}"_MYSQL_TLS_KEY_FILE \ + DB"${backup_instance_number}"_S3_BUCKET \ + DB"${backup_instance_number}"_S3_KEY_ID \ + DB"${backup_instance_number}"_S3_KEY_SECRET \ + DB"${backup_instance_number}"_S3_PATH \ + DB"${backup_instance_number}"_S3_REGION \ + DB"${backup_instance_number}"_S3_HOST \ + DB"${backup_instance_number}"_S3_PROTOCOL \ + DB"${backup_instance_number}"_S3_EXTRA_OPTS \ + DB"${backup_instance_number}"_S3_CERT_CA_FILE \ + DB"${backup_instance_number}"_BLOBXFER_STORAGE_ACCOUNT \ + DB"${backup_instance_number}"_BLOBXFER_STORAGE_ACCOUNT_KEY \ + DB"${backup_instance_number}"_BLOBXFER_REMOTE_PATH \ + BLOBXFER_STORAGE_ACCOUNT \ + BLOBXFER_STORAGE_KEY \ + DB_HOST \ + DB_NAME \ + DB_PORT \ + DB_USER \ + DB_PASS \ + MONGO_CUSTOM_URI \ + DB_AUTH \ + S3_BUCKET \ + S3_KEY_ID \ + S3_KEY_SECRET \ + S3_PATH \ + S3_REGION \ + S3_HOST \ + S3_PROTOCOL \ + S3_EXTRA_OPTS + ## Legacy after DEFAULT + set -o posix ; set | grep -E "^DB${backup_instance_number}_|^DEFAULT_|^DB_|^ARCHIVE|^BACKUP_|^BLOBXFER_|^CHECKSUM|^COMPRESSION|^CREATE_|^ENABLE_|^EXTRA_|^GZ_|^INFLUX_|^MYSQL_|^MONGO_|^PARALLEL|^PRE_|^POST_|^S3|^SKIP|^SPLIT"| tr " " "\n" > "${backup_instance_vars}" + + ## Legacy checks from removed variables + if [ -n "${ENABLE_CHECKSUM}" ]; then + print_warn "Deprecated Variable 'ENABLE_CHECKSUM' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + if var_false "${ENABLE_CHECKSUM}" ; then + DEFAULT_CHECKSUM=NONE + fi + fi + + #if [ -n "${DB_DUMP_FREQ}" ]; then + # print_warn "Deprecated Variable 'DB_DUMP_FREQ' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + # DEFAULT_BACKUP_INTERVAL=${DB_DUMP_FREQ} + #fi + + #if [ -n "${DB_DUMP_BEGIN}" ]; then + # print_warn "Deprecated Variable 'DB_DUMP_BEGIN' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + # DEFAULT_BACKUP_BEGIN=${DB_DUMP_BEGIN} + #fi + + if [ -n "${DB_DUMP_TARGET}" ]; then + print_warn "Deprecated Variable 'DB_DUMP_TARGET' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + DEFAULT_FILESYSTEM_PATH="${DB_DUMP_TARGET}" + fi + + if [ -n "${DB_DUMP_TARGET_ARCHIVE}" ]; then + print_warn "Deprecated Variable 'DB_DUMP_TARGET_ACRHIVE' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + DEFAULT_FILESYSTEM_ARCHIVE_PATH="${DB_DUMP_TARGET_ARCHIVE}" + fi + + if [ -n "${EXTRA_DUMP_OPTS}" ]; then + print_warn "Deprecated Variable 'EXTRA_DUMP_OPTS' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + DEFAULT_EXTRA_BACKUP_OPTS="${EXTRA_DUMP_OPTS}" + fi + ## + + transform_backup_instance_variable() { + if grep -q "^DB${1}_${2}=" "${backup_instance_vars}" && [ "$(grep "^DB${1}_${2}=" "${backup_instance_vars}" | cut -d = -f2)" != "unset" ]; then + export "$3"="$(grep "^DB${1}_${2}=" "${backup_instance_vars}" | cut -d = -f2)" + elif grep -q "^DB_${2}=" "${backup_instance_vars}" && [ "$(grep "^DB_${2}=" "${backup_instance_vars}" | cut -d = -f2)" != "unset" ]; then + # Allow old legacy work, perhaps remove old DB_ functionality in future? This should allow for seamless upgrades + #print_warn "Legacy Variable 'DB_${2}'' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + export "$3"="$(grep "^DB_${2}=" "${backup_instance_vars}" | cut -d = -f2)" + elif grep -q "^${2}=" "${backup_instance_vars}" && [ "$(grep "^${2}=" "${backup_instance_vars}" | cut -d = -f2)" != "unset" ]; then + print_warn "Legacy Variable '${2}' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + export "$3"="$(grep "^${2}=" "${backup_instance_vars}" | cut -d = -f2)" + elif grep -q "^DEFAULT_${2}=" "${backup_instance_vars}" && [ "$(grep "^DEFAULT_${2}=" "${backup_instance_vars}" | cut -d = -f2)" != "unset" ]; then + export "$3"="$(grep "^DEFAULT_${2}=" "${backup_instance_vars}" | cut -d = -f2)" + fi + } + + transform_backup_instance_variable "${backup_instance_number}" ARCHIVE_TIME backup_job_archive_time + transform_backup_instance_variable "${backup_instance_number}" AUTH backup_job_db_auth + transform_backup_instance_variable "${backup_instance_number}" BACKUP_BEGIN backup_job_backup_begin + transform_backup_instance_variable "${backup_instance_number}" BACKUP_INTERVAL backup_job_backup_interval + transform_backup_instance_variable "${backup_instance_number}" BACKUP_LOCATION backup_job_backup_location + transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_BEGIN backup_job_snapshot_blackout_start + transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_END backup_job_snapshot_blackout_finish + transform_backup_instance_variable "${backup_instance_number}" BLOBXFER_REMOTE_PATH backup_job_blobxfer_remote_path + transform_backup_instance_variable "${backup_instance_number}" BLOBXFER_STORAGE_ACCOUNT backup_job_blobxfer_storage_account + transform_backup_instance_variable "${backup_instance_number}" BLOBXFER_STORAGE_KEY backup_job_blobxfer_storage_key + transform_backup_instance_variable "${backup_instance_number}" CHECKSUM backup_job_checksum + transform_backup_instance_variable "${backup_instance_number}" CLEANUP_TIME backup_job_cleanup_time + transform_backup_instance_variable "${backup_instance_number}" COMPRESSION backup_job_compression + transform_backup_instance_variable "${backup_instance_number}" COMPRESSION_LEVEL backup_job_compression_level + transform_backup_instance_variable "${backup_instance_number}" CREATE_LATEST_SYMLINK backup_job_create_latest_symlink + transform_backup_instance_variable "${backup_instance_number}" ENABLE_PARALLEL_COMPRESSION backup_job_parallel_compression + transform_backup_instance_variable "${backup_instance_number}" EXTRA_DUMP_OPTS backup_job_extra_dump_opts + transform_backup_instance_variable "${backup_instance_number}" EXTRA_ENUMERATION_OPTS backup_job_extra_enumeration_opts + transform_backup_instance_variable "${backup_instance_number}" EXTRA_OPTS backup_job_extra_opts + transform_backup_instance_variable "${backup_instance_number}" FILESYSTEM_ARCHIVE_PATH backup_job_filesystem_archive_path + transform_backup_instance_variable "${backup_instance_number}" FILESYSTEM_PATH backup_job_filesystem_path + transform_backup_instance_variable "${backup_instance_number}" FILESYSTEM_PERMISSION backup_job_filesystem_permission + transform_backup_instance_variable "${backup_instance_number}" GZ_RSYNCABLE backup_job_gz_rsyncable + transform_backup_instance_variable "${backup_instance_number}" HOST backup_job_db_host + transform_backup_instance_variable "${backup_instance_number}" INFLUX_VERSION backup_job_influx_version + transform_backup_instance_variable "${backup_instance_number}" LOG_LEVEL backup_job_log_level + transform_backup_instance_variable "${backup_instance_number}" MONGO_CUSTOM_URI backup_job_mongo_custom_uri + transform_backup_instance_variable "${backup_instance_number}" MYSQL_ENABLE_TLS backup_job_mysql_enable_tls + transform_backup_instance_variable "${backup_instance_number}" MYSQL_MAX_ALLOWED_PACKET backup_job_mysql_max_allowed_packet + transform_backup_instance_variable "${backup_instance_number}" MYSQL_SINGLE_TRANSACTION backup_job_mysql_single_transaction + transform_backup_instance_variable "${backup_instance_number}" MYSQL_STORED_PROCEDURES backup_job_mysql_stored_procedures + transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_CA_FILE backup_job_mysql_tls_ca_file + transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_CERT_FILE backup_job_mysql_tls_cert_file + transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_KEY_FILE backup_job_mysql_tls_key_file + transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_VERIFY backup_job_mysql_tls_verify + transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_VERSION backup_job_mysql_tls_version + transform_backup_instance_variable "${backup_instance_number}" NAME backup_job_db_name + transform_backup_instance_variable "${backup_instance_number}" NAME_EXCLUDE backup_job_db_name_exclude + transform_backup_instance_variable "${backup_instance_number}" PARALLEL_COMPRESSION_THREADS backup_job_parallel_compression_threads + transform_backup_instance_variable "${backup_instance_number}" PASS backup_job_db_pass + transform_backup_instance_variable "${backup_instance_number}" PORT backup_job_db_port + transform_backup_instance_variable "${backup_instance_number}" POST_SCRIPT_X_VERIFY backup_job_post_script_x_verify + transform_backup_instance_variable "${backup_instance_number}" POST_SCRIPT backup_job_post_script + transform_backup_instance_variable "${backup_instance_number}" PRE_SCRIPT backup_job_pre_script + transform_backup_instance_variable "${backup_instance_number}" PRE_SCRIPT_X_VERIFY backup_job_pre_script_x_verify + transform_backup_instance_variable "${backup_instance_number}" S3_BUCKET backup_job_s3_bucket + transform_backup_instance_variable "${backup_instance_number}" S3_CERT_CA_FILE backup_job_s3_cert_ca_file + transform_backup_instance_variable "${backup_instance_number}" S3_CERT_SKIP_VERIFY backup_job_s3_cert_skip_verify + transform_backup_instance_variable "${backup_instance_number}" S3_EXTRA_OPTS backup_job_s3_extra_opts + transform_backup_instance_variable "${backup_instance_number}" S3_HOST backup_job_s3_host + transform_backup_instance_variable "${backup_instance_number}" S3_KEY_ID backup_job_s3_key_id + transform_backup_instance_variable "${backup_instance_number}" S3_KEY_SECRET backup_job_s3_key_secret + transform_backup_instance_variable "${backup_instance_number}" S3_PATH backup_job_s3_path + transform_backup_instance_variable "${backup_instance_number}" S3_PROTOCOL backup_job_s3_protocol + transform_backup_instance_variable "${backup_instance_number}" S3_REGION backup_job_s3_region + transform_backup_instance_variable "${backup_instance_number}" SCRIPT_LOCATION_POST backup_job_script_location_post + transform_backup_instance_variable "${backup_instance_number}" SCRIPT_LOCATION_PRE backup_job_script_location_pre + transform_backup_instance_variable "${backup_instance_number}" SIZE_VALUE backup_job_size_value + transform_backup_instance_variable "${backup_instance_number}" SKIP_AVAILABILITY_CHECK backup_job_skip_availability_check + transform_backup_instance_variable "${backup_instance_number}" SPLIT_DB backup_job_split_db + transform_backup_instance_variable "${backup_instance_number}" TYPE backup_job_db_type + transform_backup_instance_variable "${backup_instance_number}" USER backup_job_db_user + rm -rf "${backup_instance_vars}" + } + + upgrade_lonely_variables() { + upgrade_lonely_variables_tmp=$(mktemp) + set -o posix; set | grep "^$1" | grep -v "^$1[0-9]._" > "${upgrade_lonely_variables_tmp}" + while read -r exist_var ; do + if [ ! -v "${1}"01_"$(echo "${exist_var}" | cut -d = -f1 | cut -d _ -f2)" ] ; then + export "${1}"01_"$(echo "${exist_var}" | cut -d = -f1 | cut -d _ -f2-9)"="$(echo "${exist_var}" | cut -d = -f2)" else - DB_PORT=${DB_PORT:-27017} - [[ ( -n "${DB_USER}" ) ]] && MONGO_USER_STR=" --username ${DB_USER}" - [[ ( -n "${DB_PASS}" ) ]] && MONGO_PASS_STR=" --password ${DB_PASS}" - [[ ( -n "${DB_NAME}" ) ]] && MONGO_DB_STR=" --db ${DB_NAME}" - transform_file_var DB_AUTH - [[ ( -n "${DB_AUTH}" ) ]] && MONGO_AUTH_STR=" --authenticationDatabase ${DB_AUTH}" + print_error "Variable ${1}01_$(echo "${exist_var}" | cut -d = -f1 | cut -d _ -f2) already exists, fix your variables and start again.." + exit 1 fi - ;; - "mysql" | "mariadb" ) - dbtype=mysql - DB_PORT=${DB_PORT:-3306} - sanity_var DB_NAME "Database Name to backup. Multiple seperated by commas" - transform_file_var DB_PASS - if [ -n "${DB_PASS}" ] ; then export MYSQL_PWD=${DB_PASS} ; fi - if var_true "${MYSQL_ENABLE_TLS}" ; then - if [ -n "${MYSQL_TLS_CA_FILE}" ] ; then - mysql_tls_args="--ssl_ca=${MYSQL_TLS_CA_FILE}" - fi - if [ -n "${MYSQL_TLS_CERT_FILE}" ] ; then - mysql_tls_args="${mysql_tls_args} --ssl_cert=${MYSQL_TLS_CERT_FILE}" - fi - if [ -n "${MYSQL_TLS_KEY_FILE}" ] ; then - mysql_tls_args="${mysql_tls_args} --ssl_key=${MYSQL_TLS_KEY_FILE}" - fi + done < "${upgrade_lonely_variables_tmp}" + rm -rf "${upgrade_lonely_variables_tmp}" + } - if var_true "${TLS_VERIFY}" ; then - mysql_tls_args="${mysql_tls_args} --sslverify-server-cert" - fi - - if [ -n "${MYSQL_TLS_VERSION}" ] ; then - mysql_tls_args="${mysql_tls_args} --tls_version=${MYSQL_TLS_VERSION}" - fi + parse_variables() { + local v_instance=${1} + check_var() { + ## Check is Variable is Defined + ## Usage: check_var transformed_varname real_varname "Description" + output_off + print_debug "Looking for existence of $2 environment variable" + if [ ! -v "$1" ]; then + print_error "No '$3' Entered! - Set '\$$2' environment variable - Halting Backup Number ${v_instance}" + s6-svc -d /var/run/s6/legacy-services/dbbackup-${v_instance} + exit 1 fi - ;; - "mssql" | "microsoftsql" ) - apkArch="$(apk --print-arch)"; \ - case "$apkArch" in - x86_64) mssql=true ;; - *) write_log error "MSSQL cannot operate on $apkArch processor!" ; exit 1 ;; - esac - dbtype=mssql - DB_PORT=${DB_PORT:-1433} - ;; - postgres* | "pgsql" ) - dbtype=pgsql - DB_PORT=${DB_PORT:-5432} - [[ ( -n "${DB_PASS}" ) ]] && POSTGRES_PASS_STR="PGPASSWORD=${DB_PASS}" - sanity_var DB_NAME "Database Name to backup. Multiple seperated by commas" - ;; - "redis" ) - dbtype=redis - DB_PORT=${DB_PORT:-6379} - [[ ( -n "${DB_PASS}" ) ]] && REDIS_PASS_STR=" -a ${DB_PASS}" - ;; - sqlite* ) - dbtype=sqlite3 - ;; - * ) - write_log error "I don't recognize 'DB_TYPE=${DB_TYPE}' - Exitting.." - exit 99 - ;; + output_on + } + + check_var backup_job_db_type DB${v_instance}_TYPE "appropriate database type" + + case "${backup_job_db_type,,}" in + couch* ) + dbtype=couch + backup_job_backup_job_db_port=${backup_job_db_port:-5984} + check_var backup_job_db_user DB${v_instance}_USER "database username" + check_var backup_job_db_pass DB${v_instance}_PASS "database password" + ;; + influx* ) + dbtype=influx + case "${backup_job_influx_version}" in + 1) backup_job_db_port=${backup_job_db_port:-8088} ;; + 2) backup_job_db_port=${backup_job_db_port:-8086} ;; + esac + check_var backup_job_db_user DB${v_instance}_USER "database username" + check_var backup_job_db_pass DB${v_instance}_PASS "database password" + check_var backup_job_influx_version DB${v_instance}_INFLUX_VERSION "InfluxDB version you are backing up from" + ;; + mongo* ) + dbtype=mongo + if [ -n "${backup_job_mongo_custom_uri}" ] ; then + mongo_uri_proto=$(echo "${backup_job_mongo_custom_uri}" | grep :// | sed -e's,^\(.*://\).*,\1,g') + mongo_uri_scratch="${backup_job_mongo_custom_uri/${mongo_uri_proto}/}" + mongo_uri_username_password="$(echo "${mongo_uri_scratch}" | grep @ | rev | cut -d@ -f2- | rev)" + if [ -n "${mongo_uri_username_password}" ]; then mongo_uri_scratch="$(echo "${mongo_uri_scratch}" | rev | cut -d@ -f1 | rev)" ; fi + mongo_uri_port="$(echo "${mongo_uri_scratch}" | grep : | rev | cut -d: -f2- | rev)" + if [ -n "${mongo_uri_port}" ]; then mongo_uri_port="$(echo "${mongo_uri_scratch}" | rev | cut -d: -f1 | cut -d/ -f2 | rev)" ; fi + mongo_uri_hostname="$(echo "${mongo_uri_scratch}" | cut -d/ -f1 | cut -d: -f1 )" + mongo_uri_database="$(echo "${mongo_uri_scratch}" | cut -d/ -f2 | cut -d? -f1 )" + mongo_uri_options="$(echo "${mongo_uri_scratch}" | cut -d/ -f2 | cut -d? -f2 )" + backup_job_db_name=${backup_job_db_name:-"${mongo_uri_database,,}"} + backup_job_db_host=${backup_job_db_host:-"${mongo_uri_hostname,,}"} + else + backup_job_db_port=${backup_job_db_port:-27017} + [[ ( -n "${backup_job_db_user}" ) ]] && MONGO_USER_STR=" --username ${backup_job_db_user}" + [[ ( -n "${backup_job_db_pass}" ) ]] && MONGO_PASS_STR=" --password ${backup_job_db_pass}" + [[ ( -n "${backup_job_db_name}" ) ]] && MONGO_DB_STR=" --db ${backup_job_db_name}" + [[ ( -n "${backup_job_db_auth}" ) ]] && MONGO_AUTH_STR=" --authenticationDatabase ${backup_job_db_auth}" + fi + ;; + "mysql" | "mariadb" ) + dbtype=mysql + backup_job_db_port=${backup_job_db_port:-3306} + check_var backup_job_db_name DB${v_instance}_NAME "database name. Seperate multiple with commas" + + if [ -n "${backup_job_db_pass}" ] ; then export MYSQL_PWD=${backup_job_db_pass} ; fi + if var_true "${backup_job_mysql_enable_tls}" ; then + if [ -n "${backup_job_mysql_tls_ca_file}" ] ; then + mysql_tls_args="--ssl_ca=${backup_job_mysql_tls_ca_file}" + fi + if [ -n "${backup_job_mysql_tls_cert_file}" ] ; then + mysql_tls_args="${mysql_tls_args} --ssl_cert=${backup_job_mysql_tls_cert_file}" + fi + if [ -n "${backup_job_mysql_tls_key_file}" ] ; then + mysql_tls_args="${mysql_tls_args} --ssl_key=${backup_job_mysql_tls_key_file}" + fi + + if var_true "${backup_job_mysql_tls_verify}" ; then + mysql_tls_args="${mysql_tls_args} --sslverify-server-cert" + fi + + if [ -n "${backup_job_mysql_tls_version}" ] ; then + mysql_tls_args="${mysql_tls_args} --tls_version=${backup_job_mysql_tls_version}" + fi + fi + ;; + "mssql" | "microsoftsql" ) + apkArch="$(apk --print-arch)"; \ + case "$apkArch" in + x86_64) mssql=true ;; + *) write_log error "MSSQL cannot operate on $apkArch processor!" ; exit 1 ;; + esac + dbtype=mssql + backup_job_db_port=${backup_job_db_port:-1433} + ;; + postgres* | "pgsql" ) + dbtype=pgsql + backup_job_db_port=${backup_job_db_port:-5432} + [[ ( -n "${backup_job_db_pass}" ) ]] && POSTGRES_PASS_STR="PGPASSWORD=${backup_job_db_pass}" + check_var backup_job_db_name DB${v_instance}_NAME "database name. Seperate multiple with commas" + ;; + "redis" ) + dbtype=redis + backup_job_db_port=${backup_job_db_port:-6379} + [[ ( -n "${backup_job_db_pass}" ) ]] && REDIS_PASS_STR=" -a ${backup_job_db_pass}" + ;; + sqlite* ) + dbtype=sqlite3 + ;; + * ) + write_log error "I don't recognize 'DB${v_instance}_TYPE=${backup_job_db_type}' - Exitting.." + exit 99 + ;; + esac + } + + case "${1}" in + backup_init ) backup_init "$2" ;; + parse_variables) parse_variables "$2" ;; + upgrade ) upgrade_lonely_variables "$2" ;; esac - - if [ "${BACKUP_LOCATION,,}" = "s3" ] || [ "${BACKUP_LOCATION,,}" = "minio" ] ; then - transform_file_var \ - S3_BUCKET \ - S3_KEY_ID \ - S3_KEY_SECRET \ - S3_PATH \ - S3_REGION \ - S3_HOST \ - S3_PROTOCOL \ - S3_EXTRA_OPTS \ - S3_CERT_CA_FILE - fi - - if [ "${BACKUP_LOCATION,,}" = "blobxfer" ] ; then - transform_file_var \ - BLOBXFER_STORAGE_ACCOUNT \ - BLOBXFER_STORAGE_KEY - fi } backup_couch() { prepare_dbbackup - target=couch_${DB_NAME}_${DB_HOST#*//}_${now}.txt - ltarget=couch_${DB_NAME}_${DB_HOST#*//} + target=couch_${backup_job_db_name}_${backup_job_db_host#*//}_${now}.txt + ltarget=couch_${backup_job_db_name}_${backup_job_db_host#*//} compression - pre_dbbackup ${DB_NAME} - write_log notice "Dumping CouchDB database: '${DB_NAME}' ${compression_string}" - run_as_user curl -sSL -X GET ${DB_HOST}:${DB_PORT}/${DB_NAME}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null + pre_dbbackup ${backup_job_db_name} + write_log notice "Dumping CouchDB database: '${backup_job_db_name}' ${compression_string}" + run_as_user curl -sSL -X GET ${backup_job_db_host}:${backup_job_db_port}/${backup_job_db_name}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum move_dbbackup check_exit_code move $target - post_dbbackup ${DB_NAME} + post_dbbackup ${backup_job_db_name} } backup_influx() { - if [ "${DB_NAME,,}" = "all" ] ; then + if [ "${backup_job_db_name,,}" = "all" ] ; then write_log debug "Preparing to back up everything" db_names=justbackupeverything else - db_names=$(echo "${DB_NAME}" | tr ',' '\n') + db_names=$(echo "${backup_job_db_name}" | tr ',' '\n') fi - case "${INFLUX_VERSION,,}" in + case "${backup_job_db_influx_version,,}" in 1 ) for db in ${db_names}; do prepare_dbbackup if [ "${db}" != "justbackupeverything" ] ; then bucket="-db ${db}" ; else db=all ; fi - target=influx_${db}_${DB_HOST#*//}_${now} - ltarget=influx_${db}_${DB_HOST#*//} + target=influx_${db}_${backup_job_db_host#*//}_${now} + ltarget=influx_${db}_${backup_job_db_host#*//} compression pre_dbbackup $db write_log notice "Dumping Influx database: '${db}'" - run_as_user influxd backup ${influx_compression} ${bucket} -portable -host ${DB_HOST}:${DB_PORT} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} "${TEMP_LOCATION}"/"${target_dir}" + run_as_user influxd backup ${influx_compression} ${bucket} -portable -host ${backup_job_db_host}:${backup_job_db_port} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} "${TEMP_PATH}"/"${target_dir}" exit_code=$? check_exit_code backup $target_dir write_log notice "Creating archive file of '${target_dir}' with tar ${compression_string}" - run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" > /dev/null - target=influx_${db}_${DB_HOST#*//}_${now}.tar${extension} - ltarget=influx_${db}_${DB_HOST#*//} + run_as_user tar cf - "${TEMP_PATH}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target_dir}".tar"${extension}" > /dev/null + target=influx_${db}_${backup_job_db_host#*//}_${now}.tar${extension} + ltarget=influx_${db}_${backup_job_db_host#*//} generate_checksum move_dbbackup check_exit_code move $target_dir @@ -203,17 +396,17 @@ backup_influx() { for db in ${db_names}; do prepare_dbbackup if [ "${db}" != "justbackupeverything" ] ; then bucket="--bucket $db" ; else db=all ; fi - target=influx2_${db}_${DB_HOST#*//}_${now} - ltarget=influx2_${db}_${DB_HOST#*//} + target=influx2_${db}_${backup_job_db_host#*//}_${now} + ltarget=influx2_${db}_${backup_job_db_host#*//} compression pre_dbbackup $db write_log notice "Dumping Influx2 database: '${db}'" - run_as_user influx backup --org ${DB_USER} ${bucket} --host ${DB_HOST}:${DB_PORT} --token ${DB_PASS} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --compression none "${TEMP_LOCATION}"/"${target_dir}" + run_as_user influx backup --org ${backup_job_db_user} ${bucket} --host ${backup_job_db_host}:${backup_job_db_port} --token ${backup_job_db_pass} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --compression none "${TEMP_PATH}"/"${target_dir}" exit_code=$? check_exit_code backup $target_dir create_archive - target=influx2_${db}_${DB_HOST#*//}_${now}.tar${extension} - ltarget=influx2_${db}_${DB_HOST#*//} + target=influx2_${db}_${backup_job_db_host#*//}_${now}.tar${extension} + ltarget=influx2_${db}_${backup_job_db_host#*//} generate_checksum move_dbbackup check_exit_code move $target_dir @@ -225,80 +418,81 @@ backup_influx() { backup_mongo() { prepare_dbbackup + ## TODO REMOVE ENABLE_COMPRESSION if [ "${ENABLE_COMPRESSION,,}" = "none" ] || [ "${ENABLE_COMPRESSION,,}" = "false" ] ; then - target=${dbtype}_${DB_NAME,,}_${DB_HOST,,}_${now}.archive - ltarget=${dbtype}_${DB_NAME,,}_${DB_HOST,,} + target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive + ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} else - target=${dbtype}_${DB_NAME,,}_${DB_HOST,,}_${now}.archive.gz - ltarget=${dbtype}_${DB_NAME,,}_${DB_HOST,,} + target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive.gz + ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} mongo_compression="--gzip" compression_string="and compressing with gzip" fi - if [ -n "${MONGO_CUSTOM_URI}" ] ; then - mongo_backup_parameter="--uri=${MONGO_CUSTOM_URI} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS}" + if [ -n "${backup_job_mongo_custom_uri}" ] ; then + mongo_backup_parameter="--uri=${backup_job_mongo_custom_uri} ${backup_job_extra_opts} ${backup_job_extra_dump_opts}" else - mongo_backup_parameter="--host ${DB_HOST} --port ${DB_PORT} ${MONGO_USER_STR}${MONGO_PASS_STR}${MONGO_AUTH_STR}${MONGO_DB_STR} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS}" + mongo_backup_parameter="--host ${backup_job_db_host} --port ${backup_job_db_port} ${MONGO_USER_STR}${MONGO_PASS_STR}${MONGO_AUTH_STR}${MONGO_DB_STR} ${backup_job_extra_opts} ${backup_job_extra_dump_opts}" fi - pre_dbbackup "${DB_NAME}" + pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" - silent run_as_user mongodump --archive=${TEMP_LOCATION}/${target} ${mongo_compression} ${mongo_backup_parameter} + silent run_as_user mongodump --archive=${TEMP_PATH}/${target} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? check_exit_code backup $target generate_checksum move_dbbackup check_exit_code move $target - post_dbbackup "${DB_NAME}" + post_dbbackup "${backup_job_db_name}" } backup_mssql() { prepare_dbbackup - target=mssql_${DB_NAME,,}_${DB_HOST,,}_${now}.bak - ltarget=mssql_${DB_NAME,,}_${DB_HOST,,} + target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.bak + ltarget=mssql_${backup_job_db_name,,}_${backup_job_db_host,,} compression - pre_dbbackup "${DB_NAME}" + pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${DB_HOST}\,${DB_PORT} -U ${DB_USER} -P ${DB_PASS} -Q "BACKUP DATABASE [${DB_NAME}] TO DISK = N'${TEMP_LOCATION}/${target}' WITH NOFORMAT, NOINIT, NAME = '${DB_NAME}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target generate_checksum move_dbbackup check_exit_code move $target - post_dbbackup $DB_NAME + ppost_dbbackup "${backup_job_db_name}" } backup_mysql() { - if var_true "${MYSQL_SINGLE_TRANSACTION}" ; then + if var_true "${backup_job_mysql_single_transaction}" ; then single_transaction="--single-transaction" fi - if var_true "${MYSQL_STORED_PROCEDURES}" ; then + if var_true "${backup_job_mysql_stored_procedures}" ; then stored_procedures="--routines" fi - if [ "${DB_NAME,,}" = "all" ] ; then + if [ "${backup_job_db_name,,}" = "all" ] ; then write_log debug "Preparing to back up everything except for information_schema and _* prefixes" - db_names=$(run_as_user mysql -h ${DB_HOST} -P $DB_PORT -u$DB_USER ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_ENUMERATION_OPTS} --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema ) - if [ -n "${DB_NAME_EXCLUDE}" ] ; then - db_names_exclusions=$(echo "${DB_NAME_EXCLUDE}" | tr ',' '\n') + db_names=$(run_as_user mysql -h ${backup_job_db_host} -P $backup_job_db_port -u$backup_job_db_user ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_enumeration_opts} --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema ) + if [ -n "${backup_job_db_name_exclude}" ] ; then + db_names_exclusions=$(echo "${backup_job_db_name_exclude}" | tr ',' '\n') for db_exclude in ${db_names_exclusions} ; do write_log debug "Excluding '${db_exclude}' from ALL DB_NAME backups" db_names=$(echo "$db_names" | sed "/${db_exclude}/d" ) done fi else - db_names=$(echo "${DB_NAME}" | tr ',' '\n') + db_names=$(echo "${backup_job_db_name}" | tr ',' '\n') fi write_log debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" - if var_true "${SPLIT_DB}" ; then + if var_true "${backup_job_split_db}" ; then for db in ${db_names} ; do prepare_dbbackup - target=mysql_${db}_${DB_HOST,,}_${now}.sql - ltarget=mysql_${db}_${DB_HOST,,} + target=mysql_${db}_${backup_job_db_host,,}_${now}.sql + ltarget=mysql_${db}_${backup_job_db_host,,} compression pre_dbbackup $db write_log notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" - run_as_user mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} $db | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null + run_as_user mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -309,12 +503,12 @@ backup_mysql() { else write_log debug "Not splitting database dumps into their own files" prepare_dbbackup - target=mysql_all_${DB_HOST,,}_${now}.sql - ltarget=mysql_all_${DB_HOST,,} + target=mysql_all_${backup_job_db_host,,}_${now}.sql + ltarget=mysql_all_${backup_job_db_host,,} compression pre_dbbackup all write_log notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - run_as_user mysqldump --max-allowed-packet=${MYSQL_MAX_ALLOWED_PACKET} -h ${DB_HOST} -P ${DB_PORT} -u${DB_USER} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null + run_as_user mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -325,37 +519,37 @@ backup_mysql() { } backup_pgsql() { - export PGPASSWORD=${DB_PASS} - if [ -n "${DB_AUTH}" ] ; then - authdb=${DB_AUTH} + export PGPASSWORD=${backup_job_db_pass} + if [ -n "${backup_job_db_auth}" ] ; then + authdb=${backup_job_db_auth} else - authdb=${DB_USER} + authdb=${backup_job_db_user} fi - if [ "${DB_NAME,,}" = "all" ] ; then + if [ "${backup_job_db_name,,}" = "all" ] ; then write_log debug "Preparing to back up all databases" - db_names=$(run_as_user psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) - if [ -n "${DB_NAME_EXCLUDE}" ] ; then - db_names_exclusions=$(echo "${DB_NAME_EXCLUDE}" | tr ',' '\n') + db_names=$(run_as_user psql -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) + if [ -n "${backup_job_db_name_exclude}" ] ; then + db_names_exclusions=$(echo "${backup_job_db_name_exclude}" | tr ',' '\n') for db_exclude in ${db_names_exclusions} ; do write_log debug "Excluding '${db_exclude}' from ALL DB_NAME backups" db_names=$(echo "$db_names" | sed "/${db_exclude}/d" ) done fi else - db_names=$(echo "${DB_NAME}" | tr ',' '\n') + db_names=$(echo "${backup_job_db_name}" | tr ',' '\n') fi write_log debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" - if var_true "${SPLIT_DB}" ; then + if var_true "${backup_job_split_db}" ; then for db in ${db_names} ; do prepare_dbbackup - target=pgsql_${db}_${DB_HOST,,}_${now}.sql - ltarget=pgsql_${db}_${DB_HOST,,} + target=pgsql_${db}_${backup_job_db_host,,}_${now}.sql + ltarget=pgsql_${db}_${backup_job_db_host,,} compression pre_dbbackup $db write_log notice "Dumping PostgresSQL database: '${db}' ${compression_string}" - run_as_user pg_dump -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} $db ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null + run_as_user pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -366,12 +560,12 @@ backup_pgsql() { else write_log debug "Not splitting database dumps into their own files" prepare_dbbackup - target=pgsql_all_${DB_HOST,,}_${now}.sql - ltarget=pgsql_${db}_${DB_HOST,,} + target=pgsql_all_${backup_job_db_host,,}_${now}.sql + ltarget=pgsql_${db}_${backup_job_db_host,,} compression pre_dbbackup all write_log notice "Dumping all PostgreSQL databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - tmp_db_names=$(run_as_user psql -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) + tmp_db_names=$(run_as_user psql -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} -d ${authdb} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' ) for r_db_name in $(echo $db_names | xargs); do tmp_db_names=$(echo "$tmp_db_names" | xargs | sed "s|${r_db_name}||g" ) done @@ -379,7 +573,7 @@ backup_pgsql() { for x_db_name in ${tmp_db_names} ; do pgexclude_arg=$(echo ${pgexclude_arg} --exclude-database=${x_db_name}) done - run_as_user pg_dumpall -h ${DB_HOST} -U ${DB_USER} -p ${DB_PORT} ${pgexclude_arg} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} | ${compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target}" > /dev/null + run_as_user pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -392,14 +586,14 @@ backup_pgsql() { backup_redis() { prepare_dbbackup write_log notice "Dumping Redis - Flushing Redis Cache First" - target=redis_all_${DB_HOST,,}_${now}.rdb - ltarget=redis_${DB_HOST,,} - echo bgsave | silent run_as_user redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} --rdb ${TEMP_LOCATION}/${target} ${EXTRA_OPTS} ${EXTRA_DUMP_OPTS} + target=redis_all_${backup_job_db_host,,}_${now}.rdb + ltarget=redis_${backup_job_db_host,,} + echo bgsave | silent run_as_user redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${target} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} sleep 10 try=5 while [ $try -gt 0 ] ; do - saved=$(echo 'info Persistence' | redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} | awk '/rdb_bgsave_in_progress:0/{print "saved"}') - ok=$(echo 'info Persistence' | redis-cli -h ${DB_HOST} -p ${DB_PORT} ${REDIS_PASS_STR} | awk '/rdb_last_bgsave_status:ok/{print "ok"}') + saved=$(echo 'info Persistence' | redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} | awk '/rdb_bgsave_in_progress:0/{print "saved"}') + ok=$(echo 'info Persistence' | redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} | awk '/rdb_last_bgsave_status:ok/{print "ok"}') if [[ "$saved" = "saved" ]] && [[ "$ok" = "ok" ]]; then write_log notice "Redis Backup Complete" exit_code=0 @@ -412,7 +606,7 @@ backup_redis() { target_original=${target} compression pre_dbbackup all - run_as_user ${compress_cmd} "${TEMP_LOCATION}/${target_original}" + run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" check_exit_code backup $target generate_checksum move_dbbackup @@ -422,17 +616,17 @@ backup_redis() { backup_sqlite3() { prepare_dbbackup - db=$(basename "${DB_HOST}") + db=$(basename "${backup_job_db_host}") db="${db%.*}" target=sqlite3_${db}_${now}.sqlite3 ltarget=sqlite3_${db}.sqlite3 compression pre_dbbackup $db - write_log notice "Dumping sqlite3 database: '${DB_HOST}' ${compression_string}" - silent run_as_user sqlite3 "${DB_HOST}" ".backup '${TEMP_LOCATION}/backup.sqlite3'" + write_log notice "Dumping sqlite3 database: '${backup_job_db_host}' ${compression_string}" + silent run_as_user sqlite3 "${backup_job_db_host}" ".backup '${TEMP_PATH}/backup.sqlite3'" exit_code=$? check_exit_code backup $target - run_as_user cat "${TEMP_LOCATION}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_LOCATION}/${target}" > /dev/null + run_as_user cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${target}" > /dev/null generate_checksum move_dbbackup check_exit_code move $target @@ -441,99 +635,98 @@ backup_sqlite3() { check_availability() { ### Set the Database Type - if var_false "${SKIP_AVAILABILITY_CHECK}" ; then + if var_false "${backup_job_skip_availability_check}" ; then case "$dbtype" in "couch" ) counter=0 code_received=0 while [ "${code_received}" != "200" ]; do - code_received=$(run_as_user curl -XGET -sSL -o /dev/null -L -w ''%{http_code}'' ${DB_HOST}:${DB_PORT}) + code_received=$(run_as_user curl -XGET -sSL -o /dev/null -L -w ''%{http_code}'' ${backup_job_db_host}:${backup_job_db_port}) if [ "${code_received}" = "200" ] ; then break ; fi sleep 5 (( counter+=5 )) - write_log warn "CouchDB Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "CouchDB Host '${backup_job_db_host}' is not accessible, retrying.. ($counter seconds so far)" done ;; "influx" ) counter=0 - case "${INFLUX_VERSION,,}" in + case "${backup_job_influx_version,,}" in 1 ) - while ! (run_as_user nc -z ${DB_HOST#*//} ${DB_PORT}) ; do + while ! (run_as_user nc -z ${backup_job_db_host#*//} ${backup_job_db_port}) ; do sleep 5 (( counter+=5 )) - write_log warn "InfluxDB Host '${DB_HOST#*//}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "InfluxDB Host '${backup_job_db_host#*//}' is not accessible, retrying.. ($counter seconds so far)" done ;; 2 ) code_received=0 while [ "${code_received}" != "200" ]; do - code_received=$(run_as_user curl -XGET -sSL -o /dev/null -w ''%{http_code}'' ${DB_HOST}:${DB_PORT}/health) + code_received=$(run_as_user curl -XGET -sSL -o /dev/null -w ''%{http_code}'' ${backup_job_db_host}:${backup_job_db_port}/health) if [ "${code_received}" = "200" ] ; then break ; fi sleep 5 (( counter+=5 )) - write_log warn "InfluxDB Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "InfluxDB Host '${backup_job_db_host}' is not accessible, retrying.. ($counter seconds so far)" done ;; esac ;; "mongo" ) - if [ -n "${MONGO_CUSTOM_URI}" ] ; then + if [ -n "${backup_job_mongo_custom_uri}" ] ; then write_log debug "Skipping Connectivity Check" else counter=0 - while ! (run_as_user nc -z ${DB_HOST} ${DB_PORT}) ; do + while ! (run_as_user nc -z ${backup_job_db_host} ${backup_job_db_port}) ; do sleep 5 (( counter+=5 )) - write_log warn "Mongo Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "Mongo Host '${backup_job_db_host}' is not accessible, retrying.. ($counter seconds so far)" done fi ;; "mysql" ) counter=0 - transform_file_var DB_PASS - export MYSQL_PWD=${DB_PASS} - while ! (run_as_user mysqladmin -u"${DB_USER}" -P"${DB_PORT}" -h"${DB_HOST}" ${mysql_tls_args} status > /dev/null 2>&1) ; do + export MYSQL_PWD=${backup_job_db_pass} + while ! (run_as_user mysqladmin -u"${backup_job_db_user}" -P"${backup_job_db_port}" -h"${backup_job_db_host}" ${mysql_tls_args} status > /dev/null 2>&1) ; do sleep 5 (( counter+=5 )) - write_log warn "MySQL/MariaDB Server '${DB_HOST}' is not accessible, retrying.. (${counter} seconds so far)" + write_log warn "MySQL/MariaDB Server '${backup_job_db_host}' is not accessible, retrying.. (${counter} seconds so far)" done ;; "mssql" ) counter=0 - while ! (run_as_user nc -z ${DB_HOST} ${DB_PORT}) ; do + while ! (run_as_user nc -z ${backup_job_db_host} ${backup_job_db_port}) ; do sleep 5 (( counter+=5 )) - write_log warn "MSSQL Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "MSSQL Host '${backup_job_db_host}' is not accessible, retrying.. ($counter seconds so far)" done ;; "pgsql" ) counter=0 - until run_as_user pg_isready --host=${DB_HOST} --port=${DB_PORT} -q + until run_as_user pg_isready --host=${backup_job_db_host} --port=${backup_job_db_port} -q do sleep 5 (( counter+=5 )) - write_log warn "Postgres Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "Postgres Host '${backup_job_db_host}' is not accessible, retrying.. ($counter seconds so far)" done ;; "redis" ) counter=0 - while ! (run_as_user nc -z "${DB_HOST}" "${DB_PORT}") ; do + while ! (run_as_user nc -z "${backup_job_db_host}" "${backup_job_db_port}") ; do sleep 5 (( counter+=5 )) - write_log warn "Redis Host '${DB_HOST}' is not accessible, retrying.. ($counter seconds so far)" + write_log warn "Redis Host '${backup_job_db_host}' is not accessible, retrying.. ($counter seconds so far)" done ;; "sqlite3" ) - if [[ ! -e "${DB_HOST}" ]]; then - write_log error "File '${DB_HOST}' does not exist." + if [[ ! -e "${backup_job_db_host}" ]]; then + write_log error "File '${backup_job_db_host}' does not exist." exit_code=2 exit $exit_code - elif [[ ! -f "${DB_HOST}" ]]; then - write_log error "File '${DB_HOST}' is not a file." + elif [[ ! -f "${backup_job_db_host}" ]]; then + write_log error "File '${backup_job_db_host}' is not a file." exit_code=2 exit $exit_code - elif [[ ! -r "${DB_HOST}" ]]; then - write_log error "File '${DB_HOST}' is not readable." + elif [[ ! -r "${backup_job_db_host}" ]]; then + write_log error "File '${backup_job_db_host}' is not readable." exit_code=2 exit $exit_code fi @@ -552,6 +745,12 @@ check_exit_code() { ;; * ) write_log error "DB Backup of '${2}' reported errors" + notify \ + "$(date -d @"${backup_routines_start_time}" +'%Y%m%d_%H%M%S')" \ + "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ + "{exit_code}" \ + "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed completely" \ + "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." master_exit_code=1 ;; esac @@ -563,6 +762,12 @@ check_exit_code() { ;; * ) write_log error "Moving of backup '${2}' reported errors" + notify \ + "$(date -d @"${backup_routines_start_time}" +'%Y%m%d_%H%M%S')" \ + "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ + "{exit_code}" \ + "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed to move to destination" \ + "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." master_exit_code=1 ;; esac @@ -571,32 +776,32 @@ check_exit_code() { } cleanup_old_data() { - if [ -n "${DB_CLEANUP_TIME}" ]; then + if [ -n "${backup_job_cleanup_time}" ]; then if [ "${master_exit_code}" != 1 ]; then - case "${BACKUP_LOCATION,,}" in + case "${backup_job_backup_location,,}" in "blobxfer" ) write_log info "Cleaning up old backups on filesystem" - run_as_user mkdir -p "${DB_DUMP_TARGET}" - find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "${ltarget}*" -exec rm -f {} \; + run_as_user mkdir -p "${backup_job_filesystem_path}" + find "${backup_job_filesystem_path}"/ -type f -mmin +"${backup_job_cleanup_time}" -iname "${ltarget}*" -exec rm -f {} \; write_log info "Syncing changes via blobxfer" - silent run_as_user blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} --delete --delete-only + silent run_as_user blobxfer upload --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} --delete --delete-only ;; "file" | "filesystem" ) write_log info "Cleaning up old backups on filesystem" - run_as_user mkdir -p "${DB_DUMP_TARGET}" - run_as_user find "${DB_DUMP_TARGET}"/ -type f -mmin +"${DB_CLEANUP_TIME}" -iname "${ltarget}*" -exec rm -f {} \; + run_as_user mkdir -p "${backup_job_filesystem_path}" + run_as_user find "${backup_job_filesystem_path}"/ -type f -mmin +"${backup_job_cleanup_time}" -iname "${ltarget}*" -exec rm -f {} \; ;; "s3" | "minio" ) write_log info "Cleaning up old backups on S3 storage" - run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 ls s3://${S3_BUCKET}/${S3_PATH}/ ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} | grep " DIR " -v | grep " PRE " -v | while read -r s3_file; do + run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 ls s3://${backup_job_s3_bucket}/${backup_job_s3_path}/ ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} | grep " DIR " -v | grep " PRE " -v | while read -r s3_file; do s3_createdate=$(echo $s3_file | awk {'print $1" "$2'}) s3_createdate=$(date -d "$s3_createdate" "+%s") - s3_olderthan=$(echo $(( $(date +%s)-${DB_CLEANUP_TIME}*60 ))) + s3_olderthan=$(echo $(( $(date +%s)-${backup_job_cleanup_time}*60 ))) if [[ $s3_createdate -le $s3_olderthan ]] ; then s3_filename=$(echo $s3_file | awk {'print $4'}) if [ "$s3_filename" != "" ] ; then write_log debug "Deleting $s3_filename" - run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 rm s3://${S3_BUCKET}/${S3_PATH}/${s3_filename} ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} + run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 rm s3://${backup_job_s3_bucket}/${backup_job_s3_path}/${s3_filename} ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} fi fi done @@ -609,17 +814,17 @@ cleanup_old_data() { } compression() { - if var_false "${ENABLE_PARALLEL_COMPRESSION}" ; then - PARALLEL_COMPRESSION_THREADS=1 + if var_false "${backup_job_parallel_compression}" ; then + backup_job_parallel_compression_threads=1 fi - if var_true "${GZ_RSYNCABLE}" ; then + if var_true "${backup_job_gz_rsyncable}" ; then gz_rsyncable=--rsyncable fi - case "${COMPRESSION,,}" in + case "${backup_job_compression,,}" in bz* ) - compress_cmd="pbzip2 -q -${COMPRESSION_LEVEL} -p${PARALLEL_COMPRESSION_THREADS} " + compress_cmd="pbzip2 -q -${backup_job_compression_level} -p${backup_job_parallel_compression_threads} " compression_type="bzip2" dir_compress_cmd=${compress_cmd} extension=".bz2" @@ -627,7 +832,7 @@ compression() { target=${target}.bz2 ;; gz* ) - compress_cmd="pigz -q -${COMPRESSION_LEVEL} -p ${PARALLEL_COMPRESSION_THREADS} ${gz_rsyncable}" + compress_cmd="pigz -q -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="gzip" extension=".gz" dir_compress_cmd=${compress_cmd} @@ -635,7 +840,7 @@ compression() { target=${target}.gz ;; xz* ) - compress_cmd="pixz -${COMPRESSION_LEVEL} -p ${PARALLEL_COMPRESSION_THREADS} " + compress_cmd="pixz -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} " compression_type="xzip" dir_compress_cmd=${compress_cmd} extension=".xz" @@ -643,7 +848,7 @@ compression() { target=${target}.xz ;; zst* ) - compress_cmd="zstd -q -q --rm -${COMPRESSION_LEVEL} -T${PARALLEL_COMPRESSION_THREADS} ${gz_rsyncable}" + compress_cmd="zstd -q -q --rm -${backup_job_compression_level} -T${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="zstd" dir_compress_cmd=${compress_cmd} extension=".zst" @@ -661,9 +866,9 @@ compression() { case "${CONTAINER_LOG_LEVEL,,}" in "debug" ) if [ "${compression_type}" = "none" ] ; then - compression_string="with '${PARALLEL_COMPRESSION_THREADS}' threads" + compression_string="with '${backup_job_parallel_compression_threads}' threads" else - compression_string="and compressing with '${compression_type}:${COMPRESSION_LEVEL}' with '${PARALLEL_COMPRESSION_THREADS}' threads" + compression_string="and compressing with '${compression_type}:${backup_job_compression_level}' with '${backup_job_parallel_compression_threads}' threads" fi ;; * ) @@ -677,127 +882,332 @@ compression() { create_archive() { if [ "${exit_code}" = "0" ] ; then write_log notice "Creating archive file of '${target_dir}' with tar ${compression_string}" - run_as_user tar cf - "${TEMP_LOCATION}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_LOCATION}"/"${target_dir}".tar"${extension}" > /dev/null + run_as_user tar cf - "${TEMP_PATH}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target_dir}".tar"${extension}" > /dev/null else write_log error "Skipping creating archive file because backup did not complete successfully" fi } +create_schedulers() { + backup() { + bootstrap_variables upgrade BACKUP + local backup_instances=$(printenv | sort | grep -c "^DB[0-9]._HOST") + if [ -n "${DB_HOST}" ] && [ "${backup_instances}" ]; then + backup_instances=1; + print_debug "Detected using old DB_ variables" + fi + + for (( instance = 01; instance <= backup_instances; )) ; do + instance=$(printf "%02d" $instance) + cp -R /assets/dbbackup/template-dbbackup /etc/services.available/dbbackup-"${instance}" + sed -i "s|{{BACKUP_NUMBER}}|${instance}|g" /etc/services.available/dbbackup-"${instance}"/run + + cat <> /usr/bin/backup"${instance}"-now +#!/bin/bash +source /assets/functions/00-container +PROCESS_NAME=db-backup${instance} +print_info "Starting Manual Backup for db-backup${instance}" +/var/run/s6/legacy-services/dbbackup-${instance}/run now + +EOF + chmod +x /usr/bin/backup"${instance}"-now + if [ "${instance}" = "01" ] ; then + touch /usr/bin/backup-now + chmod +x /usr/bin/backup-now + cat < /usr/bin/backup-now +#!/bin/bash + +/usr/bin/backup${instance}-now + +EOF + else + echo "/usr/bin/backup${instance}-now" >> /usr/bin/backup-now + fi + + instance=$(echo "${instance} +1" | bc) + done + } + + case "${1}" in + backup ) backup ;; + esac +} + +ctrl_c() { + sed -i "/^{{BACKUP_NUMBER}}/d" /tmp/.container/db-backup-backups + symlink_log + print_warn "User aborted" + exit +} + +db_backup_container_init() { + rm -rf /tmp/.container/db-backup-backups + echo "0 0 * * * /usr/sbin/logrotate_dbbackup >/dev/null 2>&1" > /assets/cron/dbbackup_logrotate + touch /tmp/.container/db-backup-backups +} + generate_checksum() { - if var_true "${ENABLE_CHECKSUM}" ; then - if [ "${exit_code}" = "0" ] ; then - case "${CHECKSUM,,}" in - "md5" ) - checksum_command="md5sum" - checksum_extension="md5" + if [ "${exit_code}" = "0" ] ; then + case "${backup_job_checksum,,}" in + "md5" ) + checksum_command="md5sum" + checksum_extension="md5" + ;; + "sha1" ) + checksum_command="sha1sum" + checksum_extension="sha1" + ;; + "none" ) + return + ;; + esac + + write_log notice "Generating ${checksum_extension^^} for '${target}'" + cd "${TEMP_PATH}" + run_as_user ${checksum_command} "${target}" | run_as_user tee "${target}"."${checksum_extension}" > /dev/null + chmod ${backup_job_filesystem_permission} "${target}"."${checksum_extension}" + checksum_value=$(run_as_user cat "${target}"."${checksum_extension}" | awk '{print $1}') + write_log debug "${checksum_extension^^}: ${checksum_value} - ${target}" + else + write_log error "Skipping Checksum creation because backup did not complete successfully" + fi +} + +notify() { + notification_custom() { + if [ -n "${NOTIFICATION_SCRIPT}" ] ; then + if var_true "${NOTIFICATION_SCRIPT_SKIP_X_VERIFY}" ; then + eval "${NOTIFICATION_SCRIPT}" "${1}" "${2}" "${3}" "${4}" "${5}" + else + if [ -x "${NOTIFICATION_SCRIPT}" ] ; then + write_log notice "Found NOTIFICATION_SCRIPT environment variable. Executing '${NOTIFICATION_SCRIPT}" + # script timestamp logfile errorcode subject body + eval "${NOTIFICATION_SCRIPT}" "${1}" "${2}" "${3}" "${4}" "${5}" + else + write_log error "Can't execute NOTIFICATION_SCRIPT environment variable '${NOTIFICATION_SCRIPT}' as its filesystem bit is not executible!" + fi + fi + else + print_error "[notifications] No NOTIFICATION_SCRIPT variable set - Skipping sending Custom notifications" + fi + } + + notification_email() { + transform_file_var \ + SMTP_HOST \ + SMTP_PORT + if [ -z "${MAIL_FROM}" ] ; then write_log error "[notifications] No MAIL_FROM variable set - Skipping sending Email notifications" ; skip_mail=true ; fi + if [ -z "${MAIL_TO}" ] ; then write_log error "[notifications] No MAIL_TO variable set - Skipping sending Email notifications" ; skip_mail=true ; fi + if [ -z "${SMTP_HOST}" ] ; then write_log error "[notifications] No SMTP_HOST variable set - Skipping sending Email notifications" ; skip_mail=true ; fi + if [ -z "${SMTP_PORT}" ] ; then write_log error "[notifications] No SMTP_PORT variable set - Skipping sending Email notifications" ; skip_mail=true ; fi + if var_nottrue "${skip_mail}" ; then + mail_recipients=$(echo "${MAIL_TO}" | tr "," "\n") + for mail_recipient in $mail_recipients ; do + cat < /dev/null - chmod ${DB_DUMP_TARGET_PERMISSION} "${target}"."${checksum_extension}" - checksum_value=$(run_as_user cat "${target}"."${checksum_extension}" | awk '{print $1}') - write_log debug "${checksum_extension^^}: ${checksum_value} - ${target}" - else - write_log error "Skipping Checksum creation because backup did not complete successfully" - fi + done fi } move_dbbackup() { if [ "${exit_code}" = "0" ] ; then - dbbackup_size="$(run_as_user stat -c%s "${TEMP_LOCATION}"/"${target}")" - dbbackup_date="$(run_as_user date -r "${TEMP_LOCATION}"/"${target}" +'%s')" + dbbackup_size="$(run_as_user stat -c%s "${TEMP_PATH}"/"${target}")" + dbbackup_date="$(run_as_user date -r "${TEMP_PATH}"/"${target}" +'%s')" - case "${SIZE_VALUE,,}" in + case "${backup_job_size_value,,}" in "b" | "bytes" ) - SIZE_VALUE=1 + backup_job_size_value=1 ;; "[kK]" | "[kK][bB]" | "kilobytes" | "[mM]" | "[mM][bB]" | "megabytes" ) - SIZE_VALUE="-h" + backup_job_size_value="-h" ;; *) - SIZE_VALUE=1 + backup_job_size_value=1 ;; esac - if [ "$SIZE_VALUE" = "1" ] ; then - filesize=$(run_as_user stat -c%s "${TEMP_LOCATION}"/"${target}") - write_log notice "Backup of ${target} created with the size of ${filesize} bytes" + if [ "${backup_job_size_value}" = "1" ] ; then + filesize=$(run_as_user stat -c%s "${TEMP_PATH}"/"${target}") + write_log notice "Backup of '${target}' created with the size of ${filesize} bytes" else - filesize=$(run_as_user du -h "${TEMP_LOCATION}"/"${target}" | awk '{ print $1}') - write_log notice "Backup of ${target} created with the size of ${filesize}" + filesize=$(run_as_user du -h "${TEMP_PATH}"/"${target}" | awk '{ print $1}') + write_log notice "Backup of '${target}' created with the size of ${filesize}" fi - chmod ${DB_DUMP_TARGET_PERMISSION} "${TEMP_LOCATION}"/"${target}" - case "${BACKUP_LOCATION,,}" in + chmod ${backup_job_filesystem_permission} "${TEMP_PATH}"/"${target}" + case "${backup_job_backup_location,,}" in "file" | "filesystem" ) write_log debug "Moving backup to filesystem" - run_as_user mkdir -p "${DB_DUMP_TARGET}" - if var_true "${ENABLE_CHECKSUM}" ; then run_as_user mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/ ; fi - run_as_user mv "${TEMP_LOCATION}"/"${target}" "${DB_DUMP_TARGET}"/"${target}" + run_as_user mkdir -p "${backup_job_filesystem_path}" + if [ "${backup_job_checksum,,}" != "none" ] ; then run_as_user mv "${TEMP_PATH}"/*."${checksum_extension}" "${backup_job_filesystem_path}"/ ; fi + run_as_user mv "${TEMP_PATH}"/"${target}" "${backup_job_filesystem_path}"/"${target}" move_exit_code=$? - if var_true "${CREATE_LATEST_SYMLINK}" ; then - run_as_user ln -sf "${DB_DUMP_TARGET}"/"${target}" "${DB_DUMP_TARGET}"/latest-"${ltarget}" + if var_true "${backup_job_create_latest_symlink}" ; then + run_as_user ln -sf "${backup_job_filesystem_path}"/"${target}" "${backup_job_filesystem_path}"/latest-"${ltarget}" fi - if [ -n "${DB_ARCHIVE_TIME}" ] ; then - run_as_user mkdir -p "${DB_DUMP_TARGET_ARCHIVE}" - run_as_user find "${DB_DUMP_TARGET}"/ -type f -maxdepth 1 -mmin +"${DB_ARCHIVE_TIME}" -iname "${ltarget}*" -exec mv {} "${DB_DUMP_TARGET_ARCHIVE}" \; + if [ -n "${backup_job_archive_time}" ] ; then + run_as_user mkdir -p "${backup_job_filesystem_archive}" + run_as_user find "${backup_job_filesystem_path}"/ -type f -maxdepth 1 -mmin +"${backup_job_archive_time}" -iname "${ltarget}*" -exec mv {} "${backup_job_filesystem_archive}" \; fi ;; "s3" | "minio" ) write_log debug "Moving backup to S3 Bucket" - if [ -n "${S3_KEY_ID}" ] && [ -n "${S3_KEY_SECRET}" ]; then - export AWS_ACCESS_KEY_ID=${S3_KEY_ID} - export AWS_SECRET_ACCESS_KEY=${S3_KEY_SECRET} + if [ -n "${backup_job_s3_key_id}" ] && [ -n "${backup_job_s3_key_secret}" ]; then + export AWS_ACCESS_KEY_ID=${backup_job_s3_key_id} + export AWS_SECRET_ACCESS_KEY=${backup_job_s3_key_secret} else write_log debug "Variable S3_KEY_ID or S3_KEY_SECRET is not set. Please ensure sufficiant IAM role is assigned." fi - export AWS_DEFAULT_REGION=${S3_REGION} - if [ -f "${S3_CERT_CA_FILE}" ] ; then + export AWS_DEFAULT_REGION=${backup_job_s3_region} + if [ -f "${backup_job_s3_cert_ca_file}" ] ; then write_log debug "Using Custom CA for S3 Backups" - s3_ca_cert="--ca-bundle ${S3_CERT_CA_FILE}" + s3_ca_cert="--ca-bundle ${backup_job_s3_cert_ca_file}" fi - if var_true "${S3_CERT_SKIP_VERIFY}" ; then + if var_true "${backup_job_s3_cert_skip_verify}" ; then write_log debug "Skipping SSL verification for HTTPS S3 Hosts" s3_ssl="--no-verify-ssl" fi - [[ ( -n "${S3_HOST}" ) ]] && PARAM_AWS_ENDPOINT_URL=" --endpoint-url ${S3_PROTOCOL}://${S3_HOST}" + [[ ( -n "${S3_HOST}" ) ]] && PARAM_AWS_ENDPOINT_URL=" --endpoint-url ${backup_job_s3_protocol}://${backup_job_s3_host}" - silent aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_LOCATION}/${target} s3://${S3_BUCKET}/${S3_PATH}/${target} ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} + silent aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/${target} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/${target} ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} move_exit_code=$? - if var_true "${ENABLE_CHECKSUM}" ; then - silent run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_LOCATION}/*.${checksum_extension} s3://${S3_BUCKET}/${S3_PATH}/ ${s3_ssl} ${s3_ca_cert} ${S3_EXTRA_OPTS} + if [ "${backup_job_checksum}" != "none" ] ; then + silent run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/*.${checksum_extension} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/ ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} + run_as_user rm -rf "${TEMP_PATH}"/"${target}"."${checksum_extension}" fi - if var_true "${ENABLE_CHECKSUM}" ; then run_as_user rm -rf "${TEMP_LOCATION}"/*."${checksum_extension}"; fi - run_as_user rm -rf "${TEMP_LOCATION}"/"${target}" + run_as_user rm -rf "${TEMP_PATH}"/"${target}" ;; "blobxfer" ) write_log info "Moving backup to external storage with blobxfer" - mkdir -p "${DB_DUMP_TARGET}" - if var_true "${ENABLE_CHECKSUM}" ; then run_as_user mv "${TEMP_LOCATION}"/*."${checksum_extension}" "${DB_DUMP_TARGET}"/; fi + mkdir -p "${backup_job_filesystem_path}" + if [ "${backup_job_checksum}" != "none" ] ; then run_as_user mv "${TEMP_PATH}"/*."${checksum_extension}" "${backup_job_filesystem_path}"/; fi - run_as_user mv "${TEMP_LOCATION}"/"${target}" "${DB_DUMP_TARGET}"/"${target}" + run_as_user mv "${TEMP_PATH}"/"${target}" "${backup_job_filesystem_path}"/"${target}" - silent run_as_user blobxfer upload --mode file --remote-path ${BLOBXFER_REMOTE_PATH} --local-path ${DB_DUMP_TARGET} + silent run_as_user blobxfer upload --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} move_exit_code=$? - if var_true "${ENABLE_CHECKSUM}" ; then run_as_user rm -rf "${TEMP_LOCATION}"/*."${checksum_extension}" ; fi - run_as_user rm -rf "${TEMP_LOCATION}"/"${target}" + if [ "${backup_job_checksum}" != "none" ] ; then run_as_user rm -rf "${TEMP_PATH}"/"${target}"."${checksum_extension}" ; fi + run_as_user rm -rf "${TEMP_PATH}"/"${target}" ;; esac else write_log error "Skipping moving DB Backup to final location because backup did not complete successfully" fi - run_as_user rm -rf "${TEMP_LOCATION}"/* + run_as_user rm -rf "${TEMP_PATH}"/"${target}" } prepare_dbbackup() { @@ -805,41 +1215,41 @@ prepare_dbbackup() { now=$(run_as_user date +"%Y%m%d-%H%M%S") now_time=$(run_as_user date +"%H:%M:%S") now_date=$(run_as_user date +"%Y-%m-%d") - ltarget=${dbtype}_${DB_NAME,,}_${DB_HOST,,} - target=${dbtype}_${DB_NAME,,}_${DB_HOST,,}_${now}.sql + ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} + target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.sql } pre_dbbackup() { ### Pre Script Support - if [ -n "${PRE_SCRIPT}" ] ; then - if var_true "${PRE_SCRIPT_SKIP_X_VERIFY}" ; then - run_as_user eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + if [ -n "${backup_job_pre_script}" ] ; then + if var_true "${backup_job_pre_script_x_verify}" ; then + run_as_user eval "${backup_job_pre_script}" "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" else - if [ -x "${PRE_SCRIPT}" ] ; then - write_log notice "Found PRE_SCRIPT environment variable. Executing '${PRE_SCRIPT}" - run_as_user eval "${PRE_SCRIPT}" "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + if [ -x "${backup_job_pre_script}" ] ; then + write_log notice "Found PRE_SCRIPT environment variable. Executing '${backup_job_pre_script}" + run_as_user eval "${backup_job_pre_script}" "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" else - write_log error "Can't execute PRE_SCRIPT environment variable '${PRE_SCRIPT}' as its filesystem bit is not executible!" + write_log error "Can't execute PRE_SCRIPT environment variable '${backup_job_pre_script}' as its filesystem bit is not executible!" fi fi fi ### Pre Backup Custom Script Support if [ -d "/assets/custom-scripts/pre" ] && dir_notempty "/assets/custom-scripts/pre" ; then - write_log warning "Found Custom Post Scripts in /assets/custom-scripts/pre - Automatically moving them to '${SCRIPT_LOCATION_PRE}'" - run_as_user mkdir -p "${SCRIPT_LOCATION_PRE}" - silent run_as_user cp /assets/custom-scripts/pre/* "${SCRIPT_LOCATION_PRE}" + write_log warning "Found Custom Post Scripts in /assets/custom-scripts/pre - Automatically moving them to '${backup_job_script_location_pre}'" + run_as_user mkdir -p "${backup_job_script_location_pre}" + silent run_as_user cp /assets/custom-scripts/pre/* "${backup_job_script_location_pre}" fi - if [ -d "${SCRIPT_LOCATION_PRE}" ] && dir_notempty "${SCRIPT_LOCATION_PRE}" ; then - for f in $(find ${SCRIPT_LOCATION_PRE} -name \*.sh -type f); do - if var_true "${PRE_SCRIPT_SKIP_X_VERIFY}" ; then - run_as_user ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + if [ -d "${backup_job_script_location_pre}" ] && dir_notempty "${backup_job_script_location_pre}" ; then + for f in $(find ${backup_job_script_location_pre} -name \*.sh -type f); do + if var_true "${backup_job_pre_script_x_verify}" ; then + run_as_user ${f} "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" else if [ -x "${f}" ] ; then write_log notice "Executing pre backup custom script : '${f}'" ## script DB_TYPE DB_HOST DB_NAME STARTEPOCH BACKUP_FILENAME - run_as_user ${f} "${dbtype}" "${DB_HOST}" "${1}" "${dbbackup_start_time}" "${target}" + run_as_user ${f} "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" else write_log error "Can't run pre backup custom script: '${f}' as its filesystem bit is not executible!" fi @@ -855,45 +1265,46 @@ post_dbbackup() { if var_true "${CONTAINER_ENABLE_MONITORING}" && [ "${CONTAINER_MONITORING_BACKEND,,}" = "zabbix" ]; then source /assets/defaults/03-monitoring write_log notice "Sending Backup Statistics to Zabbix" + silent zabbix_sender -c -c "${ZABBIX_CONFIG_PATH}"/"${ZABBIX_CONFIG_FILE}" -k dbbackup.backup -o '[{"{#NAME}":"'${backup_job_db_host}.${backup_job_db_name}'"}]' cat < /dev/null + output_off + if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + fi + output_on ;; esac ;; error ) + CONTAINER_LOG_LEVEL=ERROR shift 1 - case "${LOG_LEVEL,,}" in + case "${backup_job_log_level,,}" in "debug" | "notice" | "warn" | "error") + output_off print_error "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null + if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + fi + output_on ;; esac ;; info ) + CONTAINER_LOG_LEVEL=INFO shift 1 print_info "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null + output_off + if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + fi + output_on ;; notice ) + CONTAINER_LOG_LEVEL=NOTICE shift 1 - case "${LOG_LEVEL,,}" in + case "${backup_job_log_level,,}" in "debug" | "notice" ) print_notice "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null + output_off + if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + fi + output_on ;; esac ;; warn ) + CONTAINER_LOG_LEVEL=WARN shift 1 - case "${LOG_LEVEL,,}" in + case "${backup_job_log_level,,}" in "debug" | "notice" | "warn" ) print_warn "$@" - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}"/"${DB_TYPE}"_"${DB_NAME}"_"${DB_HOST}"_"${now}".log > /dev/null + output_off + if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + fi + output_on ;; esac ;; From 3e666ef655ee95870fcfe6e378b6c7323418e10e Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:02:36 -0700 Subject: [PATCH 16/68] Add coreutils to run-deps --- Dockerfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 79aaa10..19b6b42 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,8 @@ FROM docker.io/tiredofit/${DISTRO}:${DISTRO_VARIANT} LABEL maintainer="Dave Conroy (github.com/tiredofit)" ### Set Environment Variables -ENV INFLUX_VERSION=1.8.0 \ - INFLUX2_VERSION=2.4.0 \ +ENV INFLUX1_CLIENT_VERSION=1.8.0 \ + INFLUX2_CLIENT_VERSION=2.4.0 \ MSODBC_VERSION=18.3.2.1-1 \ MSSQL_VERSION=18.3.1.1-1 \ AWS_CLI_VERSION=1.25.97 \ @@ -40,6 +40,7 @@ RUN source /assets/functions/00-container && \ \ package install .db-backup-run-deps \ bzip2 \ + coreutils \ groff \ libarchive \ mariadb-client \ @@ -75,10 +76,10 @@ RUN source /assets/functions/00-container && \ esac; \ \ if [ $mssql = "true" ] ; then curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/msodbcsql18_${MSODBC_VERSION}_${mssql_arch}.apk ; curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/mssql-tools18_${MSSQL_VERSION}_${mssql_arch}.apk ; ls -l ; echo y | apk add --allow-untrusted msodbcsql18_${MSODBC_VERSION}_${mssql_arch}.apk mssql-tools18_${MSSQL_VERSION}_${mssql_arch}.apk ; else echo >&2 "Detected non x86_64 or ARM64 build variant, skipping MSSQL installation" ; fi; \ - if [ $influx2 = "true" ] ; then curl -sSL https://dl.influxdata.com/influxdb/releases/influxdb2-client-${INFLUX2_VERSION}-linux-${influx_arch}.tar.gz | tar xvfz - --strip=1 -C /usr/src/ ; chmod +x /usr/src/influx ; mv /usr/src/influx /usr/sbin/ ; else echo >&2 "Unable to build Influx 2 on this system" ; fi ; \ + if [ $influx2 = "true" ] ; then curl -sSL https://dl.influxdata.com/influxdb/releases/influxdb2-client-${INFLUX2_CLIENT_VERSION}-linux-${influx_arch}.tar.gz | tar xvfz - --strip=1 -C /usr/src/ ; chmod +x /usr/src/influx ; mv /usr/src/influx /usr/sbin/ ; else echo >&2 "Unable to build Influx 2 on this system" ; fi ; \ clone_git_repo https://github.com/aws/aws-cli "${AWS_CLI_VERSION}" && \ python3 setup.py install --prefix=/usr && \ - clone_git_repo https://github.com/influxdata/influxdb "${INFLUX_VERSION}" && \ + clone_git_repo https://github.com/influxdata/influxdb "${INFLUX1_CLIENT_VERSION}" && \ go build -o /usr/sbin/influxd ./cmd/influxd && \ strip /usr/sbin/influxd && \ mkdir -p /usr/src/pbzip2 && \ From 599c3d794067b2bf4fa630857e3ac8f75b166fd3 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:02:52 -0700 Subject: [PATCH 17/68] Rework bin directory --- install/usr/local/bin/backup-now | 4 ---- install/usr/local/bin/logrotate_dbbackup | 24 ++++++++++++++++++++++++ install/usr/local/bin/restore | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) delete mode 100755 install/usr/local/bin/backup-now create mode 100755 install/usr/local/bin/logrotate_dbbackup diff --git a/install/usr/local/bin/backup-now b/install/usr/local/bin/backup-now deleted file mode 100755 index cd6e333..0000000 --- a/install/usr/local/bin/backup-now +++ /dev/null @@ -1,4 +0,0 @@ -#!/command/with-contenv bash - -echo '** Performing Manual Backup' -/etc/services.available/10-db-backup/run manual diff --git a/install/usr/local/bin/logrotate_dbbackup b/install/usr/local/bin/logrotate_dbbackup new file mode 100755 index 0000000..e852afa --- /dev/null +++ b/install/usr/local/bin/logrotate_dbbackup @@ -0,0 +1,24 @@ +#!/command/with-contenv bash + +source /assets/functions/00-container +source /assets/defaults/05-logging +source /assets/defaults/10-db-backup + +## Compress each log 2 days old +timestamp_2dayold_unixtime="$(stat -c %Y "${LOG_PATH}"/"$(date --date='2 days ago' +'%Y%m%d')")" +for logfile in "${LOG_PATH}"/"$(date --date='2 days ago' +'%Y%m%d')"/"$(date --date='2 days ago' +'%Y%m%d')"_*.log ; do + sudo -u restic zstd --rm --rsyncable "${logfile}" +done +touch -t $(date -d"@${timestamp_2dayold_unixtime}" +'%Y%m%d%H%m.%S') "${LOG_PATH}"/"$(date --date='2 days ago' +'%Y%m%d')" + +# Look fook files older than certain day and delete +if [ -n "${LOG_PATH}" ] && [ -d "${LOG_PATH}" ] ; then + find "${LOG_PATH}" -mtime +"${LOGROTATE_RETAIN_DAYS}" -type d -exec rm -rf {} + +fi + +# Look for stale symbolic links and delete accordingly +for symbolic_link in "${LOG_PATH}"/latest*.log ; do + if [ ! -e "${symbolic_link}" ] ; then + rm -rf "${symbolic_link}" + fi +done \ No newline at end of file diff --git a/install/usr/local/bin/restore b/install/usr/local/bin/restore index c17edf3..d75faee 100755 --- a/install/usr/local/bin/restore +++ b/install/usr/local/bin/restore @@ -38,7 +38,7 @@ else "-h" ) cat < Date: Fri, 3 Nov 2023 15:14:02 -0700 Subject: [PATCH 18/68] Add Postgres GLobals Backup if SPLIT_DB=TRUE --- install/assets/functions/10-db-backup | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 4bf8e37..52b95b6 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -557,6 +557,20 @@ backup_pgsql() { check_exit_code move $target post_dbbackup $db done + + prepare_dbbackup + target=pgsql_globals_${backup_job_db_host,,}_${now}.sql + ltarget=pgsql_globals_${backup_job_db_host,,} + compression + pre_dbbackup "globals" + print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" + pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g {backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + exit_code=$? + check_exit_code $target + generate_checksum + move_dbbackup + check_exit_code move $target + post_dbbackup "globals" else write_log debug "Not splitting database dumps into their own files" prepare_dbbackup From a10eb1dfbaabe1d26ce5c2fd9747de7825d064c9 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:22:59 -0700 Subject: [PATCH 19/68] Synchronize local storage from blobxfer before upload --- install/assets/functions/10-db-backup | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 52b95b6..ec4b7cc 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -564,7 +564,7 @@ backup_pgsql() { compression pre_dbbackup "globals" print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" - pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g {backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code $target generate_checksum @@ -1203,8 +1203,10 @@ move_dbbackup() { run_as_user rm -rf "${TEMP_PATH}"/"${target}" ;; "blobxfer" ) - write_log info "Moving backup to external storage with blobxfer" + write_log info "Synchronize local storage from S3 Bucket with blobxfer" + blobxfer download --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} --delete + write_log info "Moving backup to external storage with blobxfer" mkdir -p "${backup_job_filesystem_path}" if [ "${backup_job_checksum}" != "none" ] ; then run_as_user mv "${TEMP_PATH}"/*."${checksum_extension}" "${backup_job_filesystem_path}"/; fi From 7d77266d2ad72719f6c498aba4e047699739b693 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:32:33 -0700 Subject: [PATCH 20/68] Use relative path insteasd of absolute when creating_latest_symlink - Closes #256 --- install/assets/functions/10-db-backup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index ec4b7cc..ba0f723 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1166,7 +1166,7 @@ move_dbbackup() { run_as_user mv "${TEMP_PATH}"/"${target}" "${backup_job_filesystem_path}"/"${target}" move_exit_code=$? if var_true "${backup_job_create_latest_symlink}" ; then - run_as_user ln -sf "${backup_job_filesystem_path}"/"${target}" "${backup_job_filesystem_path}"/latest-"${ltarget}" + run_as_user ln -sfr "${backup_job_filesystem_path}"/"${target}" "${backup_job_filesystem_path}"/latest-"${ltarget}" fi if [ -n "${backup_job_archive_time}" ] ; then run_as_user mkdir -p "${backup_job_filesystem_archive}" From 0a867f971964bd5b0bfd8fe9380d51c1cffdbd79 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:48:39 -0700 Subject: [PATCH 21/68] feat - Add Support for MSSQL Transaction logs - Closes #200 --- README.md | 2 ++ install/assets/defaults/10-db-backup | 1 + install/assets/functions/10-db-backup | 46 +++++++++++++++++++-------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 526cace..6f175e0 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,7 @@ If these are set and no other defaults or variables are set explicitly, they wil | Variable | Description | Default | `_FILE` | | -------------- | ------------------ | ------- | ------- | | `DEFAULT_PORT` | Microsoft SQL Port | `1433` | x | +| `DEFAULT_MSSQL_MODE` | Backup `DATABASE` or `TRANSACTION` logs | `DATABASE` | ###### MongoDB @@ -513,6 +514,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ----------- | ------------------ | ------- | ------- | | `DB01_PORT` | Microsoft SQL Port | `1433` | x | +| `DB01_MSSQL_MODE` | Backup `DATABASE` or `TRANSACTION` logs | `DATABASE` | ###### MongoDB diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index c7bf54c..c62ac30 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -22,6 +22,7 @@ DEFAULT_MYSQL_STORED_PROCEDURES=${DEFAULT_MYSQL_STORED_PROCEDURES:-"TRUE"} DEFAULT_MYSQL_TLS_CA_FILE=${DEFAULT_MYSQL_TLS_CA_FILE:-"/etc/ssl/cert.pem"} DEFAULT_MYSQL_TLS_VERIFY=${DEFAULT_MYSQL_TLS_VERIFY:-"FALSE"} DEFAULT_MYSQL_TLS_VERSION=${DEFAULT_MYSQL_TLS_VERSION:-"TLSv1.1,TLSv1.2,TLSv1.3"} +DEFAULT_MSSQL_MODE=${DEFAULT_MSSQL_MODE:-"database"} DEFAULT_PARALLEL_COMPRESSION_THREADS=${DEFAULT_PARALLEL_COMPRESSION_THREADS:-"$(nproc)"} DEFAULT_S3_CERT_SKIP_VERIFY=${DEFAULT_S3_CERT_SKIP_VERIFY:-"TRUE"} DEFAULT_S3_PROTOCOL=${DEFAULT_S3_PROTOCOL:-"https"} diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index ba0f723..7a28a63 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -183,6 +183,7 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_KEY_FILE backup_job_mysql_tls_key_file transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_VERIFY backup_job_mysql_tls_verify transform_backup_instance_variable "${backup_instance_number}" MYSQL_TLS_VERSION backup_job_mysql_tls_version + transform_backup_instance_variable "${backup_instance_number}" MSSQL_MODE backup_job_mssql_mode transform_backup_instance_variable "${backup_instance_number}" NAME backup_job_db_name transform_backup_instance_variable "${backup_instance_number}" NAME_EXCLUDE backup_job_db_name_exclude transform_backup_instance_variable "${backup_instance_number}" PARALLEL_COMPRESSION_THREADS backup_job_parallel_compression_threads @@ -445,19 +446,38 @@ backup_mongo() { } backup_mssql() { - prepare_dbbackup - target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.bak - ltarget=mssql_${backup_job_db_name,,}_${backup_job_db_host,,} - compression - pre_dbbackup "${backup_job_db_name}" - write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" - exit_code=$? - check_exit_code backup $target - generate_checksum - move_dbbackup - check_exit_code move $target - ppost_dbbackup "${backup_job_db_name}" + case "${backup_job_mssql_mode,,}" in + db|database ) + prepare_dbbackup + target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.bak + ltarget=mssql_${backup_job_db_name,,}_${backup_job_db_host,,} + compression + pre_dbbackup "${backup_job_db_name}" + write_log notice "Dumping MSSQL database: '${DB_NAME}'" + silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + exit_code=$? + check_exit_code backup $target + generate_checksum + move_dbbackup + check_exit_code move $target + post_dbbackup "${backup_job_db_name}" + ;; + trn|transaction ) + prepare_dbbackup + target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.trn + ltarget=mssql_${backup_job_db_name,,}_trn_${backup_job_db_host,,} + compression + pre_dbbackup "${backup_job_db_name}" + write_log notice "Dumping MSSQL database: '${DB_NAME}'" + silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + exit_code=$? + check_exit_code backup $target + generate_checksum + move_dbbackup + check_exit_code move $target + post_dbbackup "${backup_job_db_name}" + ;; + }; } backup_mysql() { From 2cc97694f43c33f96ac86c76ae10fed79c7de90f Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 15:53:54 -0700 Subject: [PATCH 22/68] Fix formatting: --- README.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6f175e0..dc0fdcd 100644 --- a/README.md +++ b/README.md @@ -207,17 +207,18 @@ If these are set and no other defaults or variables are set explicitly, they wil ##### Scheduling Options -| Variable | Description | Default | -| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | -| `DEFAULT_BACKUP_INTERVAL` | How often to do a backup, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | -| `DEFAULT_BACKUP_BEGIN` | What time to do the initial backup. Defaults to immediate. (`+1`) | `+0` | -| | Must be in one of two formats: | | -| | Absolute HHMM, e.g. `2330` or `0415` | | -| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | -| `DEFAULT_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when backup interval executes) | `FALSE` | -| | 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | | -| `DEFAULT_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from | `DEFAULT_BACKUP_BLACKOUT_BEGIN` | Use `HHMM` notation to start a blackout period where no backups occur eg `0420` | | -| `DEFAULT_BACKUP_BLACKOUT_END` | Use `HHMM` notation to set the end period where no backups occur eg `0430` | | +| Variable | Description | Default | +| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------- | +| `DEFAULT_BACKUP_INTERVAL` | How often to do a backup, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | +| `DEFAULT_BACKUP_BEGIN` | What time to do the initial backup. Defaults to immediate. (`+1`) | `+0` | +| | Must be in one of two formats: | | +| | Absolute HHMM, e.g. `2330` or `0415` | | +| | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | +| `DEFAULT_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when backup interval executes) | `FALSE` | +| | 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | | +| `DEFAULT_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from | | +| `DEFAULT_BACKUP_BLACKOUT_BEGIN` | Use `HHMM` notation to start a blackout period where no backups occur eg `0420` | | +| `DEFAULT_BACKUP_BLACKOUT_END` | Use `HHMM` notation to set the end period where no backups occur eg `0430` | | > You may need to wrap your `DEFAULT_BACKUP_BEGIN` value in quotes for it to properly parse. There have been reports of backups that start with a `0` get converted into a different format which will not allow the timer to start at the correct time. @@ -260,9 +261,9 @@ If these are set and no other defaults or variables are set explicitly, they wil ###### Microsoft SQL -| Variable | Description | Default | `_FILE` | -| -------------- | ------------------ | ------- | ------- | -| `DEFAULT_PORT` | Microsoft SQL Port | `1433` | x | +| Variable | Description | Default | `_FILE` | +| -------------------- | --------------------------------------- | ---------- | ------- | +| `DEFAULT_PORT` | Microsoft SQL Port | `1433` | x | | `DEFAULT_MSSQL_MODE` | Backup `DATABASE` or `TRANSACTION` logs | `DATABASE` | ###### MongoDB @@ -511,9 +512,9 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled ###### Microsoft SQL -| Variable | Description | Default | `_FILE` | -| ----------- | ------------------ | ------- | ------- | -| `DB01_PORT` | Microsoft SQL Port | `1433` | x | +| Variable | Description | Default | `_FILE` | +| ----------------- | --------------------------------------- | ---------- | ------- | +| `DB01_PORT` | Microsoft SQL Port | `1433` | x | | `DB01_MSSQL_MODE` | Backup `DATABASE` or `TRANSACTION` logs | `DATABASE` | ###### MongoDB From 20bfcec1a9c3e7dbfe7461ec26589be2df4af375 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 18:03:52 -0700 Subject: [PATCH 23/68] feat - Add DEFAULT_RESOURCE_OPTIMIZED to adjust CPU scheduler --- README.md | 2 ++ install/assets/defaults/10-db-backup | 1 + install/assets/functions/10-db-backup | 44 ++++++++++++++------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index dc0fdcd..5f4575d 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,7 @@ If these are set and no other defaults or variables are set explicitly, they wil | `DEFAULT_BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | | `DEFAULT_CHECKSUM` | Either `MD5` or `SHA1` or `NONE` | `MD5` | | `DEFAULT_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `notice` | +| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | | `DEFAULT_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options @@ -437,6 +438,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | `DB01_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `debug` | +| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | | `DB01_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index c62ac30..2b28d67 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -24,6 +24,7 @@ DEFAULT_MYSQL_TLS_VERIFY=${DEFAULT_MYSQL_TLS_VERIFY:-"FALSE"} DEFAULT_MYSQL_TLS_VERSION=${DEFAULT_MYSQL_TLS_VERSION:-"TLSv1.1,TLSv1.2,TLSv1.3"} DEFAULT_MSSQL_MODE=${DEFAULT_MSSQL_MODE:-"database"} DEFAULT_PARALLEL_COMPRESSION_THREADS=${DEFAULT_PARALLEL_COMPRESSION_THREADS:-"$(nproc)"} +DEFAULT_RESOURCE_OPTIMIZED=${DEFAULT_RESOURCE_OPTIMIZED:-"FALSE"} DEFAULT_S3_CERT_SKIP_VERIFY=${DEFAULT_S3_CERT_SKIP_VERIFY:-"TRUE"} DEFAULT_S3_PROTOCOL=${DEFAULT_S3_PROTOCOL:-"https"} DEFAULT_SCRIPT_LOCATION_PRE=${DEFAULT_SCRIPT_LOCATION_PRE:-"/assets/scripts/pre/"} diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 7a28a63..23d02dc 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -193,6 +193,7 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" POST_SCRIPT backup_job_post_script transform_backup_instance_variable "${backup_instance_number}" PRE_SCRIPT backup_job_pre_script transform_backup_instance_variable "${backup_instance_number}" PRE_SCRIPT_X_VERIFY backup_job_pre_script_x_verify + transform_backup_instance_variable "${backup_instance_number}" RESOURCE_OPTIMIZED backup_job_resource_optimized transform_backup_instance_variable "${backup_instance_number}" S3_BUCKET backup_job_s3_bucket transform_backup_instance_variable "${backup_instance_number}" S3_CERT_CA_FILE backup_job_s3_cert_ca_file transform_backup_instance_variable "${backup_instance_number}" S3_CERT_SKIP_VERIFY backup_job_s3_cert_skip_verify @@ -337,6 +338,8 @@ bootstrap_variables() { exit 99 ;; esac + + if var_true "${backup_job_resource_optimized}" ; then nice="nice" ; fi } case "${1}" in @@ -436,7 +439,7 @@ backup_mongo() { fi pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" - silent run_as_user mongodump --archive=${TEMP_PATH}/${target} ${mongo_compression} ${mongo_backup_parameter} + silent run_as_user ${nice} mongodump --archive=${TEMP_PATH}/${target} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? check_exit_code backup $target generate_checksum @@ -454,7 +457,7 @@ backup_mssql() { compression pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user ${nice} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target generate_checksum @@ -469,7 +472,7 @@ backup_mssql() { compression pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user ${nice} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target generate_checksum @@ -512,7 +515,7 @@ backup_mysql() { compression pre_dbbackup $db write_log notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" - run_as_user mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${nice} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -528,7 +531,7 @@ backup_mysql() { compression pre_dbbackup all write_log notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - run_as_user mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${nice} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -569,7 +572,7 @@ backup_pgsql() { compression pre_dbbackup $db write_log notice "Dumping PostgresSQL database: '${db}' ${compression_string}" - run_as_user pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${nice} pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -577,14 +580,13 @@ backup_pgsql() { check_exit_code move $target post_dbbackup $db done - prepare_dbbackup target=pgsql_globals_${backup_job_db_host,,}_${now}.sql ltarget=pgsql_globals_${backup_job_db_host,,} compression pre_dbbackup "globals" print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" - pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${nice} pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code $target generate_checksum @@ -607,7 +609,7 @@ backup_pgsql() { for x_db_name in ${tmp_db_names} ; do pgexclude_arg=$(echo ${pgexclude_arg} --exclude-database=${x_db_name}) done - run_as_user pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${nice} pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target generate_checksum @@ -622,7 +624,7 @@ backup_redis() { write_log notice "Dumping Redis - Flushing Redis Cache First" target=redis_all_${backup_job_db_host,,}_${now}.rdb ltarget=redis_${backup_job_db_host,,} - echo bgsave | silent run_as_user redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${target} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} + echo bgsave | silent run_as_user ${nice} redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${target} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} sleep 10 try=5 while [ $try -gt 0 ] ; do @@ -657,10 +659,10 @@ backup_sqlite3() { compression pre_dbbackup $db write_log notice "Dumping sqlite3 database: '${backup_job_db_host}' ${compression_string}" - silent run_as_user sqlite3 "${backup_job_db_host}" ".backup '${TEMP_PATH}/backup.sqlite3'" + silent run_as_user ${nice} sqlite3 "${backup_job_db_host}" ".backup '${TEMP_PATH}/backup.sqlite3'" exit_code=$? check_exit_code backup $target - run_as_user cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${target}" > /dev/null + run_as_user ${nice} cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${target}" > /dev/null generate_checksum move_dbbackup check_exit_code move $target @@ -858,7 +860,7 @@ compression() { case "${backup_job_compression,,}" in bz* ) - compress_cmd="pbzip2 -q -${backup_job_compression_level} -p${backup_job_parallel_compression_threads} " + compress_cmd="${nice} pbzip2 -q -${backup_job_compression_level} -p${backup_job_parallel_compression_threads} " compression_type="bzip2" dir_compress_cmd=${compress_cmd} extension=".bz2" @@ -866,7 +868,7 @@ compression() { target=${target}.bz2 ;; gz* ) - compress_cmd="pigz -q -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} ${gz_rsyncable}" + compress_cmd="${nice} pigz -q -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="gzip" extension=".gz" dir_compress_cmd=${compress_cmd} @@ -874,7 +876,7 @@ compression() { target=${target}.gz ;; xz* ) - compress_cmd="pixz -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} " + compress_cmd="${nice} pixz -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} " compression_type="xzip" dir_compress_cmd=${compress_cmd} extension=".xz" @@ -882,7 +884,7 @@ compression() { target=${target}.xz ;; zst* ) - compress_cmd="zstd -q -q --rm -${backup_job_compression_level} -T${backup_job_parallel_compression_threads} ${gz_rsyncable}" + compress_cmd="${nice} zstd -q -q --rm -${backup_job_compression_level} -T${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="zstd" dir_compress_cmd=${compress_cmd} extension=".zst" @@ -984,11 +986,11 @@ generate_checksum() { if [ "${exit_code}" = "0" ] ; then case "${backup_job_checksum,,}" in "md5" ) - checksum_command="md5sum" + checksum_command="${nice} md5sum" checksum_extension="md5" ;; "sha1" ) - checksum_command="sha1sum" + checksum_command="${nice} sha1sum" checksum_extension="sha1" ;; "none" ) @@ -1216,7 +1218,7 @@ move_dbbackup() { silent aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/${target} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/${target} ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} move_exit_code=$? if [ "${backup_job_checksum}" != "none" ] ; then - silent run_as_user aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/*.${checksum_extension} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/ ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} + silent run_as_user ${nice} aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/*.${checksum_extension} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/ ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} run_as_user rm -rf "${TEMP_PATH}"/"${target}"."${checksum_extension}" fi @@ -1224,7 +1226,7 @@ move_dbbackup() { ;; "blobxfer" ) write_log info "Synchronize local storage from S3 Bucket with blobxfer" - blobxfer download --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} --delete + ${nice} blobxfer download --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} --delete write_log info "Moving backup to external storage with blobxfer" mkdir -p "${backup_job_filesystem_path}" @@ -1232,7 +1234,7 @@ move_dbbackup() { run_as_user mv "${TEMP_PATH}"/"${target}" "${backup_job_filesystem_path}"/"${target}" - silent run_as_user blobxfer upload --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} + silent run_as_user ${nice} blobxfer upload --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} move_exit_code=$? if [ "${backup_job_checksum}" != "none" ] ; then run_as_user rm -rf "${TEMP_PATH}"/"${target}"."${checksum_extension}" ; fi From 3837beae97ee842d3108f10bf9ef8d4a668ca69e Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 18:11:58 -0700 Subject: [PATCH 24/68] feat - Add checksum time, and hash output to Zabbix --- README.md | 2 +- install/assets/functions/10-db-backup | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f4575d..777d024 100644 --- a/README.md +++ b/README.md @@ -438,7 +438,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | `DB01_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `debug` | -| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | +| `DB01_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | | `DB01_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 23d02dc..7954f4e 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -998,12 +998,16 @@ generate_checksum() { ;; esac + checksum_routines_start_time=$(date +'%s') write_log notice "Generating ${checksum_extension^^} for '${target}'" cd "${TEMP_PATH}" run_as_user ${checksum_command} "${target}" | run_as_user tee "${target}"."${checksum_extension}" > /dev/null chmod ${backup_job_filesystem_permission} "${target}"."${checksum_extension}" checksum_value=$(run_as_user cat "${target}"."${checksum_extension}" | awk '{print $1}') + checksum_routines_finish_time=$(date +'%s') + checksum_routines_total_time=$(echo $((checksum_routines_finish_time-checksum_routines_start_time))) write_log debug "${checksum_extension^^}: ${checksum_value} - ${target}" + write_log debug "Checksum routines time taken: $(echo ${checksum_routines_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" else write_log error "Skipping Checksum creation because backup did not complete successfully" fi From dad5294295b046e49b45ba249cedbecb074db558 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 18:15:30 -0700 Subject: [PATCH 25/68] Update Zabbix Template --- install/assets/functions/10-db-backup | 6 ++++++ zabbix_templates/db_backup.json | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 7954f4e..9086a85 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1006,6 +1006,11 @@ generate_checksum() { checksum_value=$(run_as_user cat "${target}"."${checksum_extension}" | awk '{print $1}') checksum_routines_finish_time=$(date +'%s') checksum_routines_total_time=$(echo $((checksum_routines_finish_time-checksum_routines_start_time))) + zabbix_checksum_time=$(cat < Date: Fri, 3 Nov 2023 18:18:05 -0700 Subject: [PATCH 26/68] Add gpg to rundeps --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 19b6b42..a83ad3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,6 +41,7 @@ RUN source /assets/functions/00-container && \ package install .db-backup-run-deps \ bzip2 \ coreutils \ + gpg \ groff \ libarchive \ mariadb-client \ From 5dafa69439a8f50ab959dd2d376e3e7a1a329d95 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Fri, 3 Nov 2023 19:46:46 -0700 Subject: [PATCH 27/68] feat - add file encryption --- Dockerfile | 1 + README.md | 41 ++++++++++++---- install/assets/defaults/10-db-backup | 1 + install/assets/functions/10-db-backup | 71 ++++++++++++++++++++++++++- zabbix_templates/db_backup.json | 19 +++++++ 5 files changed, 121 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index a83ad3f..098bc10 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,6 +42,7 @@ RUN source /assets/functions/00-container && \ bzip2 \ coreutils \ gpg \ + gpg-agent \ groff \ libarchive \ mariadb-client \ diff --git a/README.md b/README.md index 777d024..4709758 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis serve - backup all to separate files or one singular file - checksum support choose to have an MD5 or SHA1 hash generated after backup for verification - compression support (none, gz, bz, xz, zstd) +- encryption support (passphrase and public key) - Zabbix Metrics support - Hooks to execute pre and post backup job for customization purposes - Companion script to aid in restores @@ -52,6 +53,7 @@ Backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis serve - [Container Options](#container-options) - [Job Defaults](#job-defaults) - [Compression Options](#compression-options) + - [Encryption Options](#encryption-options) - [Scheduling Options](#scheduling-options) - [Default Database Options](#default-database-options) - [CouchDB](#couchdb) @@ -71,6 +73,7 @@ Backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis serve - [Post backup](#post-backup) - [Job Backup Options](#job-backup-options) - [Compression Options](#compression-options-1) + - [Encryption Options](#encryption-options-1) - [Scheduling Options](#scheduling-options-1) - [Specific Database Options](#specific-database-options) - [CouchDB](#couchdb-1) @@ -192,7 +195,7 @@ If these are set and no other defaults or variables are set explicitly, they wil | `DEFAULT_BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | | `DEFAULT_CHECKSUM` | Either `MD5` or `SHA1` or `NONE` | `MD5` | | `DEFAULT_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `notice` | -| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | +| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | | `DEFAULT_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options @@ -206,6 +209,15 @@ If these are set and no other defaults or variables are set explicitly, they wil | `DEFAULT_ENABLE_PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` | `TRUE` | | `DEFAULT_PARALLEL_COMPRESSION_THREADS` | Maximum amount of threads to use when compressing - Integer value e.g. `8` | `autodetected` | +##### Encryption Options + +| Variable | Description | Default | +| ---------------------------- | ------------------------------------------- | ------- | +| `DEFAULT_ENCRYPT` | Encrypt file after backing up with GPG | `FALSE` | +| `DEFAULT_ENCRYPT_PASSPHRASE` | Passphrase to encrypt file with GPG | | +| *or* | | | +| `DEFAULT_ENCRYPT_PUBKEY` | Path of public key to encrypt file with GPG | | + ##### Scheduling Options | Variable | Description | Default | @@ -438,7 +450,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | `DB01_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `debug` | -| `DB01_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | +| `DB01_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | | `DB01_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options @@ -452,6 +464,15 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_ENABLE_PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` | `TRUE` | | `DB01_PARALLEL_COMPRESSION_THREADS` | Maximum amount of threads to use when compressing - Integer value e.g. `8` | `autodetected` | +##### Encryption Options + +| Variable | Description | Default | +| ------------------------- | ------------------------------------------- | ------- | +| `DB01_ENCRYPT` | Encrypt file after backing up with GPG | `FALSE` | +| `DB01_ENCRYPT_PASSPHRASE` | Passphrase to encrypt file with GPG | | +| *or* | | | +| `DB01_ENCRYPT_PUBKEY` | Path of public key to encrypt file with GPG | | + ##### Scheduling Options | Variable | Description | Default | @@ -494,9 +515,9 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | -| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | -| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | -| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | || +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | || +| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | || | `DB01_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. | | | | | Backup multiple by separating with commas eg `db1,db2` | | x | | `DB01_NAME_EXCLUDE` | If using `ALL` - use this as to exclude databases separated via commas from being backed up | | x | @@ -534,9 +555,9 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ----------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | | `DB01_AUTH` | (Optional) Authentication Database | | | -| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | -| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | -| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | | `DB01_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. | | | | | Backup multiple by separating with commas eg `db1,db2` | | x | | `DB01_PORT` | PostgreSQL Port | `5432` | x | @@ -545,8 +566,8 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ------------------------ | --------------------------------------------------------------------------------------------------------- | ------- | ------- | -| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | -| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | || +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | || | `DB01_PORT` | Redis Port | `6379` | x | ###### SQLite diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index 2b28d67..86bb12e 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -11,6 +11,7 @@ DEFAULT_COMPRESSION=${DEFAULT_COMPRESSION:-"ZSTD"} DEFAULT_COMPRESSION_LEVEL=${DEFAULT_COMPRESSION_LEVEL:-"3"} DEFAULT_CREATE_LATEST_SYMLINK=${DEFAULT_CREATE_LATEST_SYMLINK:-"TRUE"} DEFAULT_ENABLE_PARALLEL_COMPRESSION=${DEFAULT_ENABLE_PARALLEL_COMPRESSION:-"TRUE"} +DEFAULT_ENCRYPT=${DEFAULT_ENCRYPT:-"FALSE"} DEFAULT_FILESYSTEM_PATH=${DEFAULT_FILESYSTEM_PATH:-"/backup"} DEFAULT_FILESYSTEM_PERMISSION=${DEFAULT_FILESYSTEM_PERMISSION:-"700"} DEFAULT_FILESYSTEM_ARCHIVE_PATH=${DEFAULT_FILESYSTEM_ARCHIVE_PATH:-"${DEFAULT_FILESYSTEM_PATH}/archive/"} diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 9086a85..e1dfe2a 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -44,6 +44,8 @@ bootstrap_variables() { DEFAULT_NAME_EXCLUDE \ DEFAULT_USER \ DEFAULT_PASS \ + DEFAULT_ENCRYPT_PASSPHRASE \ + DEFAULT_ENCRYPT_PUBKEY \ DEFAULT_MONGO_CUSTOM_URI \ DEFAULT_MYSQL_TLS_CA_FILE \ DEFAULT_MYSQL_TLS_CERT_FILE \ @@ -64,6 +66,14 @@ bootstrap_variables() { DB"${backup_instance_number}"_TYPE \ DB"${backup_instance_number}"_HOST \ DB"${backup_instance_number}"_PORT \ + DB"${backup_instance_number}"_NAME \ + DB"${backup_instance_number}"_NAME_EXCLUDE \ + DB"${backup_instance_number}"_USER \ + DB"${backup_instance_number}"_PASS \ + DB"${backup_instance_number}"_ENCRYPT_PASSPHRASE \ + DB"${backup_instance_number}"_ENCRYPT_PUBKEY \ + DB"${backup_instance_number}"_MONGO_CUSTOM_URI \ + DB"${backup_instance_number}"_MYSQL_TLS_CA_FILE \ DB"${backup_instance_number}"_MYSQL_TLS_CERT_FILE \ DB"${backup_instance_number}"_MYSQL_TLS_KEY_FILE \ DB"${backup_instance_number}"_S3_BUCKET \ @@ -163,6 +173,9 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" COMPRESSION_LEVEL backup_job_compression_level transform_backup_instance_variable "${backup_instance_number}" CREATE_LATEST_SYMLINK backup_job_create_latest_symlink transform_backup_instance_variable "${backup_instance_number}" ENABLE_PARALLEL_COMPRESSION backup_job_parallel_compression + transform_backup_instance_variable "${backup_instance_number}" ENCRYPT backup_job_encrypt + transform_backup_instance_variable "${backup_instance_number}" ENCRYPT_PASSPHRASE backup_job_encrypt_passphrase + transform_backup_instance_variable "${backup_instance_number}" ENCRYPT_PUBKEY backup_job_encrypt_pubkey transform_backup_instance_variable "${backup_instance_number}" EXTRA_DUMP_OPTS backup_job_extra_dump_opts transform_backup_instance_variable "${backup_instance_number}" EXTRA_ENUMERATION_OPTS backup_job_extra_enumeration_opts transform_backup_instance_variable "${backup_instance_number}" EXTRA_OPTS backup_job_extra_opts @@ -359,6 +372,7 @@ backup_couch() { run_as_user curl -sSL -X GET ${backup_job_db_host}:${backup_job_db_port}/${backup_job_db_name}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -390,6 +404,7 @@ backup_influx() { run_as_user tar cf - "${TEMP_PATH}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target_dir}".tar"${extension}" > /dev/null target=influx_${db}_${backup_job_db_host#*//}_${now}.tar${extension} ltarget=influx_${db}_${backup_job_db_host#*//} + file_encryption generate_checksum move_dbbackup check_exit_code move $target_dir @@ -411,6 +426,7 @@ backup_influx() { create_archive target=influx2_${db}_${backup_job_db_host#*//}_${now}.tar${extension} ltarget=influx2_${db}_${backup_job_db_host#*//} + file_encryption generate_checksum move_dbbackup check_exit_code move $target_dir @@ -442,6 +458,7 @@ backup_mongo() { silent run_as_user ${nice} mongodump --archive=${TEMP_PATH}/${target} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -460,6 +477,7 @@ backup_mssql() { silent run_as_user ${nice} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -475,12 +493,13 @@ backup_mssql() { silent run_as_user ${nice} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target post_dbbackup "${backup_job_db_name}" ;; - }; + esac } backup_mysql() { @@ -518,6 +537,7 @@ backup_mysql() { run_as_user ${nice} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -534,6 +554,7 @@ backup_mysql() { run_as_user ${nice} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -575,6 +596,7 @@ backup_pgsql() { run_as_user ${nice} pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -582,13 +604,13 @@ backup_pgsql() { done prepare_dbbackup target=pgsql_globals_${backup_job_db_host,,}_${now}.sql - ltarget=pgsql_globals_${backup_job_db_host,,} compression pre_dbbackup "globals" print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" run_as_user ${nice} pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -612,6 +634,7 @@ backup_pgsql() { run_as_user ${nice} pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -644,6 +667,7 @@ backup_redis() { pre_dbbackup all run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" check_exit_code backup $target + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -663,6 +687,7 @@ backup_sqlite3() { exit_code=$? check_exit_code backup $target run_as_user ${nice} cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${target}" > /dev/null + file_encryption generate_checksum move_dbbackup check_exit_code move $target @@ -982,6 +1007,46 @@ db_backup_container_init() { touch /tmp/.container/db-backup-backups } +file_encryption() { + if var_true "${backup_job_encrypt}" ; then + if [ "${exit_code}" = "0" ] ; then + print_debug "Encrypting" + output_off + if [ -n "${backup_job_encrypt_passphrase}" ] && [ -n "${backup_job_encrypt_pubkey}" ]; then + print_error "Can't encrypt as both ENCRYPT_PASSPHRASE and ENCRYPT_PUBKEY exist!" + return + elif [ -n "${backup_job_encrypt_passphrase}" ] && [ -z "${backup_job_encrypt_pubkey}" ]; then + print_notice "Encrypting with GPG Passphrase" + encrypt_routines_start_time=$(date +'%s') + encrypt_tmp_dir=$(run_as_user mktemp -d) + echo "${backup_job_encrypt_passphrase}" | silent run_as_user ${nice} gpg --batch --home ${encrypt_tmp_dir} --yes --passphrase-fd 0 -c "${TEMP_PATH}"/"${target}" + rm -rf "${encrypt_tmp_dir}" + elif [ -z "${backup_job_encrypt_passphrase}" ] && [ -n "${backup_job_encrypt_pubkey}" ]; then + if [ -f "${backup_job_encrypt_pubkey}" ]; then + encrypt_routines_start_time=$(date +'%s') + print_notice "Encrypting with GPG Public Key" + encrypt_tmp_dir=$(run_as_user mktemp -d) + silent run_as_user ${nice} gpg --batch --yes --home "${encrypt_tmp_dir}" --recipient-file "${backup_job_encrypt_pubkey}" -c "${TEMP_PATH}"/"${target}" + rm -rf "${encrypt_tmp_dir}" + fi + fi + if [ -f "${TEMP_PATH}"/"${target}".gpg ]; then + rm -rf "${TEMP_PATH}"/"${target}" + target="${target}.gpg" + + encrypt_routines_finish_time=$(date +'%s') + encrypt_routines_total_time=$(echo $((encrypt_routines_finish_time-encrypt_routines_start_time))) + zabbix_encrypt_time=$(cat < Date: Sat, 4 Nov 2023 07:43:32 -0700 Subject: [PATCH 28/68] Also use ionice for DB_RESOURCE_OPTIMIZED --- install/assets/functions/10-db-backup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index e1dfe2a..2f2613d 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -352,7 +352,7 @@ bootstrap_variables() { ;; esac - if var_true "${backup_job_resource_optimized}" ; then nice="nice" ; fi + if var_true "${backup_job_resource_optimized}" ; then nice="nice -19 ionice -c2" ; fi } case "${1}" in From 3af9ef6d3d03eabb4515a8156664c19bc770893d Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sat, 4 Nov 2023 08:07:10 -0700 Subject: [PATCH 29/68] Rework timers --- README.md | 4 +- install/assets/dbbackup/template-dbbackup/run | 13 +- install/assets/functions/10-db-backup | 122 ++++++++++++------ 3 files changed, 87 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 4709758..75c5044 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ If these are set and no other defaults or variables are set explicitly, they wil | `DEFAULT_BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | | `DEFAULT_CHECKSUM` | Either `MD5` or `SHA1` or `NONE` | `MD5` | | `DEFAULT_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `notice` | -| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | +| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU and IO scheduler | `FALSE` | | `DEFAULT_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options @@ -450,7 +450,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | `DB01_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `debug` | -| `DB01_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU scheduler | `FALSE` | +| `DB01_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU and IO scheduler | `FALSE` | | `DB01_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options diff --git a/install/assets/dbbackup/template-dbbackup/run b/install/assets/dbbackup/template-dbbackup/run index f28f813..7208968 100755 --- a/install/assets/dbbackup/template-dbbackup/run +++ b/install/assets/dbbackup/template-dbbackup/run @@ -50,8 +50,8 @@ while true; do if var_true "${blackout}" ; then print_notice "Detected Blackout Period - Not performing backup operations" else + timer job start process_limiter - backup_routines_start_time=$(date +'%s') echo "{{BACKUP_NUMBER}}" >> /tmp/.container/db-backup-backups print_debug "Backup {{BACKUP_NUMBER}} routines started time: $(date +'%Y-%m-%d %T %Z')" bootstrap_filesystem @@ -89,11 +89,10 @@ while true; do backup_sqlite3 ;; esac - backup_routines_finish_time=$(date +'%s') - backup_routines_total_time=$(echo $((backup_routines_finish_time-backup_routines_start_time))) + timer job stop if [ -z "${exitcode_backup}" ] ; then exitcode_backup="0" ; fi - print_info "Backup {{BACKUP_NUMBER}} routines finish time: $(date -d @${backup_routines_finish_time} +'%Y-%m-%d %T %Z') with exit code ${exitcode_backup}" - print_notice "Backup {{BACKUP_NUMBER}} routines time taken: $(echo ${backup_routines_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" + print_info "Backup {{BACKUP_NUMBER}} routines finish time: $(date -d @${backup_job_finish_time} +'%Y-%m-%d %T %Z') with exit code ${exitcode_backup}" + print_notice "Backup {{BACKUP_NUMBER}} routines time taken: $(echo ${backup_job_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" sed -i "/^{{BACKUP_NUMBER}}/d" /tmp/.container/db-backup-backups fi @@ -107,8 +106,8 @@ while true; do print_error "Stopping backup_scheduler {{BACKUP_NUMBER}} due to detected errors. Fix and restart container." s6-svc -d /var/run/s6/legacy-services/dbbackup-{{BACKUP_NUMBER}} else - print_notice "Sleeping for another $(($backup_job_backup_interval*60-backup_routines_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($backup_job_backup_interval*60-backup_routines_total_time))))" +'%Y-%m-%d %T %Z') " - silent sleep $(($backup_job_backup_interval*60-backup_routines_total_time)) + print_notice "Sleeping for another $(($backup_job_backup_interval*60-backup_job_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($backup_job_backup_interval*60-backup_job_total_time))))" +'%Y-%m-%d %T %Z') " + silent sleep $(($backup_job_backup_interval*60-backup_job_total_time)) fi fi done diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 2f2613d..e124def 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -17,7 +17,7 @@ bootstrap_filesystem() { fi if [ "$(stat -c %U "${LOG_PATH}")" != "dbbackup" ] ; then chown dbbackup:dbbackup "${LOG_PATH}" ; fi - if [ ! -d "${LOG_PATH}"/$(date +'%Y%m%d') ]; then run_as_user mkdir -p "${LOG_PATH}"/$(date +'%Y%m%d'); fi + if [ ! -d "${LOG_PATH}"/"$(date +'%Y%m%d')" ]; then run_as_user mkdir -p "${LOG_PATH}"/$(date +'%Y%m%d'); fi if [ "$(stat -c %a "${LOG_PATH}")" != "755" ] ; then chmod -R 755 "${LOG_PATH}" ; fi if [ ! -d "${TEMP_PATH}" ]; then @@ -117,13 +117,7 @@ bootstrap_variables() { fi #if [ -n "${DB_DUMP_FREQ}" ]; then - # print_warn "Deprecated Variable 'DB_DUMP_FREQ' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" - # DEFAULT_BACKUP_INTERVAL=${DB_DUMP_FREQ} - #fi - - #if [ -n "${DB_DUMP_BEGIN}" ]; then - # print_warn "Deprecated Variable 'DB_DUMP_BEGIN' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" - # DEFAULT_BACKUP_BEGIN=${DB_DUMP_BEGIN} + # print_warn "Deprecated Variable 'DB_DUMP_FREQ' dnow_date=$(run_as_user date +"%Y-%m-%d") #fi if [ -n "${DB_DUMP_TARGET}" ]; then @@ -352,7 +346,7 @@ bootstrap_variables() { ;; esac - if var_true "${backup_job_resource_optimized}" ; then nice="nice -19 ionice -c2" ; fi + if var_true "${backup_job_resource_optimized}" ; then play_fair="nice -19 ionice -c2" ; fi } case "${1}" in @@ -372,6 +366,7 @@ backup_couch() { run_as_user curl -sSL -X GET ${backup_job_db_host}:${backup_job_db_port}/${backup_job_db_name}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -404,6 +399,7 @@ backup_influx() { run_as_user tar cf - "${TEMP_PATH}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target_dir}".tar"${extension}" > /dev/null target=influx_${db}_${backup_job_db_host#*//}_${now}.tar${extension} ltarget=influx_${db}_${backup_job_db_host#*//} + timer backup finish file_encryption generate_checksum move_dbbackup @@ -426,6 +422,7 @@ backup_influx() { create_archive target=influx2_${db}_${backup_job_db_host#*//}_${now}.tar${extension} ltarget=influx2_${db}_${backup_job_db_host#*//} + timer backup finish file_encryption generate_checksum move_dbbackup @@ -455,9 +452,10 @@ backup_mongo() { fi pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" - silent run_as_user ${nice} mongodump --archive=${TEMP_PATH}/${target} ${mongo_compression} ${mongo_backup_parameter} + silent run_as_user ${play_fair} mongodump --archive=${TEMP_PATH}/${target} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? check_exit_code backup $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -474,9 +472,10 @@ backup_mssql() { compression pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user ${nice} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -490,10 +489,11 @@ backup_mssql() { compression pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user ${nice} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? check_exit_code backup $target file_encryption + timer backup finish generate_checksum move_dbbackup check_exit_code move $target @@ -534,9 +534,10 @@ backup_mysql() { compression pre_dbbackup $db write_log notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" - run_as_user ${nice} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -551,9 +552,10 @@ backup_mysql() { compression pre_dbbackup all write_log notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - run_as_user ${nice} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -593,9 +595,10 @@ backup_pgsql() { compression pre_dbbackup $db write_log notice "Dumping PostgresSQL database: '${db}' ${compression_string}" - run_as_user ${nice} pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -607,9 +610,10 @@ backup_pgsql() { compression pre_dbbackup "globals" print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" - run_as_user ${nice} pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -631,9 +635,10 @@ backup_pgsql() { for x_db_name in ${tmp_db_names} ; do pgexclude_arg=$(echo ${pgexclude_arg} --exclude-database=${x_db_name}) done - run_as_user ${nice} pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target + timer backup finish file_encryption generate_checksum move_dbbackup @@ -647,7 +652,7 @@ backup_redis() { write_log notice "Dumping Redis - Flushing Redis Cache First" target=redis_all_${backup_job_db_host,,}_${now}.rdb ltarget=redis_${backup_job_db_host,,} - echo bgsave | silent run_as_user ${nice} redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${target} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} + echo bgsave | silent run_as_user ${play_fair} redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${target} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} sleep 10 try=5 while [ $try -gt 0 ] ; do @@ -666,6 +671,7 @@ backup_redis() { compression pre_dbbackup all run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" + timer backup finish check_exit_code backup $target file_encryption generate_checksum @@ -683,10 +689,11 @@ backup_sqlite3() { compression pre_dbbackup $db write_log notice "Dumping sqlite3 database: '${backup_job_db_host}' ${compression_string}" - silent run_as_user ${nice} sqlite3 "${backup_job_db_host}" ".backup '${TEMP_PATH}/backup.sqlite3'" + silent run_as_user ${play_fair} sqlite3 "${backup_job_db_host}" ".backup '${TEMP_PATH}/backup.sqlite3'" exit_code=$? check_exit_code backup $target - run_as_user ${nice} cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${target}" > /dev/null + run_as_user ${play_fair} cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${target}" > /dev/null + timer backup finish file_encryption generate_checksum move_dbbackup @@ -807,8 +814,8 @@ check_exit_code() { * ) write_log error "DB Backup of '${2}' reported errors" notify \ - "$(date -d @"${backup_routines_start_time}" +'%Y%m%d_%H%M%S')" \ - "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ + "$(date -d @"${backup_job_start_time}" +'%Y%m%d_%H%M%S')" \ + "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ "{exit_code}" \ "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed completely" \ "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." @@ -824,8 +831,8 @@ check_exit_code() { * ) write_log error "Moving of backup '${2}' reported errors" notify \ - "$(date -d @"${backup_routines_start_time}" +'%Y%m%d_%H%M%S')" \ - "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ + "$(date -d @"${backup_job_start_time}" +'%Y%m%d_%H%M%S')" \ + "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ "{exit_code}" \ "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed to move to destination" \ "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." @@ -885,7 +892,7 @@ compression() { case "${backup_job_compression,,}" in bz* ) - compress_cmd="${nice} pbzip2 -q -${backup_job_compression_level} -p${backup_job_parallel_compression_threads} " + compress_cmd="${play_fair} pbzip2 -q -${backup_job_compression_level} -p${backup_job_parallel_compression_threads} " compression_type="bzip2" dir_compress_cmd=${compress_cmd} extension=".bz2" @@ -893,7 +900,7 @@ compression() { target=${target}.bz2 ;; gz* ) - compress_cmd="${nice} pigz -q -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} ${gz_rsyncable}" + compress_cmd="${play_fair} pigz -q -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="gzip" extension=".gz" dir_compress_cmd=${compress_cmd} @@ -901,7 +908,7 @@ compression() { target=${target}.gz ;; xz* ) - compress_cmd="${nice} pixz -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} " + compress_cmd="${play_fair} pixz -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} " compression_type="xzip" dir_compress_cmd=${compress_cmd} extension=".xz" @@ -909,7 +916,7 @@ compression() { target=${target}.xz ;; zst* ) - compress_cmd="${nice} zstd -q -q --rm -${backup_job_compression_level} -T${backup_job_parallel_compression_threads} ${gz_rsyncable}" + compress_cmd="${play_fair} zstd -q -q --rm -${backup_job_compression_level} -T${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="zstd" dir_compress_cmd=${compress_cmd} extension=".zst" @@ -1019,14 +1026,14 @@ file_encryption() { print_notice "Encrypting with GPG Passphrase" encrypt_routines_start_time=$(date +'%s') encrypt_tmp_dir=$(run_as_user mktemp -d) - echo "${backup_job_encrypt_passphrase}" | silent run_as_user ${nice} gpg --batch --home ${encrypt_tmp_dir} --yes --passphrase-fd 0 -c "${TEMP_PATH}"/"${target}" + echo "${backup_job_encrypt_passphrase}" | silent run_as_user ${play_fair} gpg --batch --home ${encrypt_tmp_dir} --yes --passphrase-fd 0 -c "${TEMP_PATH}"/"${target}" rm -rf "${encrypt_tmp_dir}" elif [ -z "${backup_job_encrypt_passphrase}" ] && [ -n "${backup_job_encrypt_pubkey}" ]; then if [ -f "${backup_job_encrypt_pubkey}" ]; then encrypt_routines_start_time=$(date +'%s') print_notice "Encrypting with GPG Public Key" encrypt_tmp_dir=$(run_as_user mktemp -d) - silent run_as_user ${nice} gpg --batch --yes --home "${encrypt_tmp_dir}" --recipient-file "${backup_job_encrypt_pubkey}" -c "${TEMP_PATH}"/"${target}" + silent run_as_user ${play_fair} gpg --batch --yes --home "${encrypt_tmp_dir}" --recipient-file "${backup_job_encrypt_pubkey}" -c "${TEMP_PATH}"/"${target}" rm -rf "${encrypt_tmp_dir}" fi fi @@ -1051,11 +1058,11 @@ generate_checksum() { if [ "${exit_code}" = "0" ] ; then case "${backup_job_checksum,,}" in "md5" ) - checksum_command="${nice} md5sum" + checksum_command="${play_fair} md5sum" checksum_extension="md5" ;; "sha1" ) - checksum_command="${nice} sha1sum" + checksum_command="${play_fair} sha1sum" checksum_extension="sha1" ;; "none" ) @@ -1292,7 +1299,7 @@ move_dbbackup() { silent aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/${target} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/${target} ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} move_exit_code=$? if [ "${backup_job_checksum}" != "none" ] ; then - silent run_as_user ${nice} aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/*.${checksum_extension} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/ ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} + silent run_as_user ${play_fair} aws ${PARAM_AWS_ENDPOINT_URL} s3 cp ${TEMP_PATH}/*.${checksum_extension} s3://${backup_job_s3_bucket}/${backup_job_s3_path}/ ${s3_ssl} ${s3_ca_cert} ${backup_job_s3_extra_opts} run_as_user rm -rf "${TEMP_PATH}"/"${target}"."${checksum_extension}" fi @@ -1300,7 +1307,7 @@ move_dbbackup() { ;; "blobxfer" ) write_log info "Synchronize local storage from S3 Bucket with blobxfer" - ${nice} blobxfer download --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} --delete + ${play_fair} blobxfer download --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} --delete write_log info "Moving backup to external storage with blobxfer" mkdir -p "${backup_job_filesystem_path}" @@ -1308,7 +1315,7 @@ move_dbbackup() { run_as_user mv "${TEMP_PATH}"/"${target}" "${backup_job_filesystem_path}"/"${target}" - silent run_as_user ${nice} blobxfer upload --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} + silent run_as_user ${play_fair} blobxfer upload --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} move_exit_code=$? if [ "${backup_job_checksum}" != "none" ] ; then run_as_user rm -rf "${TEMP_PATH}"/"${target}"."${checksum_extension}" ; fi @@ -1322,11 +1329,40 @@ move_dbbackup() { run_as_user rm -rf "${TEMP_PATH}"/"${target}" } +timer() { + case "${1}" in + backup) + case "${2}" in + start) + dbbackup_start_time=$(run_as_user date +"%s") + ;; + stop) + dbbackup_finish_time=$(run_as_user date +"%s") + dbbackup_total_time=$(echo $((dbbackup_finish_time-dbbackup_start_time))) + ;; + esac + ;; + cron) + ;; + job) + case "${2}" in + start) + backup_job_start_time=$(date +'%s') + ;; + stop) + backup_job_finish_time=$(date +'%s') + backup_job_total_time=$(echo $((backup_job_finish_time-backup_job_start_time))) + ;; + esac + ;; + esac + +} prepare_dbbackup() { - dbbackup_start_time=$(run_as_user date +"%s") + timer backup start now=$(run_as_user date +"%Y%m%d-%H%M%S") - now_time=$(run_as_user date +"%H:%M:%S") now_date=$(run_as_user date +"%Y-%m-%d") + now_time=$(run_as_user date +"%H:%M:%S") ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.sql } @@ -1491,7 +1527,7 @@ symlink_log () { if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then local oldpwd=$(pwd) cd "${LOG_PATH}"/"$(date +'%Y%m%d')" - ln -sf $(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log ../latest-"${ltarget}".log + ln -sf $(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log ../latest-"${ltarget}".log cd "${oldpwd}" fi } @@ -1507,7 +1543,7 @@ write_log() { print_debug "$@" output_off if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi output_on ;; @@ -1521,7 +1557,7 @@ write_log() { output_off print_error "$@" if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi output_on ;; @@ -1533,7 +1569,7 @@ write_log() { print_info "$@" output_off if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi output_on ;; @@ -1545,7 +1581,7 @@ write_log() { print_notice "$@" output_off if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi output_on ;; @@ -1559,7 +1595,7 @@ write_log() { print_warn "$@" output_off if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_routines_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi output_on ;; From 5392bf5179dbbd03505c5ad2249f83d1de1ae00f Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 07:40:30 -0800 Subject: [PATCH 30/68] feat - add basic cron expression scheduling --- README.md | 28 ++- install/assets/dbbackup/template-dbbackup/run | 98 ++++---- install/assets/functions/10-db-backup | 237 +++++++++++++++++- 3 files changed, 287 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index 75c5044..d033223 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ If these are set and no other defaults or variables are set explicitly, they wil | `DEFAULT_BACKUP_LOCATION` | Backup to `FILESYSTEM`, `blobxfer` or `S3` compatible services like S3, Minio, Wasabi | `FILESYSTEM` | | `DEFAULT_CHECKSUM` | Either `MD5` or `SHA1` or `NONE` | `MD5` | | `DEFAULT_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `notice` | -| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU and IO scheduler | `FALSE` | +| `DEFAULT_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU and IO scheduler | `FALSE` | | `DEFAULT_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options @@ -224,9 +224,11 @@ If these are set and no other defaults or variables are set explicitly, they wil | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------- | | `DEFAULT_BACKUP_INTERVAL` | How often to do a backup, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | | `DEFAULT_BACKUP_BEGIN` | What time to do the initial backup. Defaults to immediate. (`+1`) | `+0` | -| | Must be in one of two formats: | | +| | Must be in one of four formats: | | | | Absolute HHMM, e.g. `2330` or `0415` | | | | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | +| | Full datestamp e.g. `2023-12-21 23:30:00` | | +| | Cron expression e.g. `30 23 * * *` - [Understand the format](https://en.wikipedia.org/wiki/ Cron) - *BACKUP_INTERVAL is ignored* | | | `DEFAULT_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when backup interval executes) | `FALSE` | | | 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | | | `DEFAULT_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from | | @@ -450,7 +452,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | `DB01_LOG_LEVEL` | Log output on screen and in files `INFO` `NOTICE` `ERROR` `WARN` `DEBUG` | `debug` | -| `DB01_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU and IO scheduler | `FALSE` | +| `DB01_RESOURCE_OPTIMIZED` | Perform operations at a lower priority to the CPU and IO scheduler | `FALSE` | | `DB01_SKIP_AVAILABILITY_CHECK` | Before backing up - skip connectivity check | `FALSE` | ##### Compression Options @@ -479,9 +481,11 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------- | | `DB01_BACKUP_INTERVAL` | How often to do a backup, in minutes after the first backup. Defaults to 1440 minutes, or once per day. | `1440` | | `DB01_BACKUP_BEGIN` | What time to do the initial backup. Defaults to immediate. (`+1`) | `+0` | -| | Must be in one of two formats: | | +| | Must be in one of four formats: | | | | Absolute HHMM, e.g. `2330` or `0415` | | | | Relative +MM, i.e. how many minutes after starting the container, e.g. `+0` (immediate), `+10` (in 10 minutes), or `+90` in an hour and a half | | +| | Full datestamp e.g. `2023-12-21 23:30:00` | | +| | Cron expression e.g. `30 23 * * *` - [Understand the format](https://en.wikipedia.org/wiki/ Cron) - *BACKUP_INTERVAL is ignored* | | | `DB01_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when backup interval executes) | `FALSE` | | | 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything. | | | `DB01_ARCHIVE_TIME` | Value in minutes to move all files files older than (x) from `DB01_BACKUP_FILESYSTEM_PATH` | | @@ -515,9 +519,9 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | -| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | || -| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | || -| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | || +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | | `DB01_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. | | | | | Backup multiple by separating with commas eg `db1,db2` | | x | | `DB01_NAME_EXCLUDE` | If using `ALL` - use this as to exclude databases separated via commas from being backed up | | x | @@ -555,9 +559,9 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ----------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | | `DB01_AUTH` | (Optional) Authentication Database | | | -| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | -| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | -| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | | `DB01_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. | | | | | Backup multiple by separating with commas eg `db1,db2` | | x | | `DB01_PORT` | PostgreSQL Port | `5432` | x | @@ -566,8 +570,8 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ------------------------ | --------------------------------------------------------------------------------------------------------- | ------- | ------- | -| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | || -| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | || +| `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | | `DB01_PORT` | Redis Port | `6379` | x | ###### SQLite diff --git a/install/assets/dbbackup/template-dbbackup/run b/install/assets/dbbackup/template-dbbackup/run index 7208968..0fe9839 100755 --- a/install/assets/dbbackup/template-dbbackup/run +++ b/install/assets/dbbackup/template-dbbackup/run @@ -19,34 +19,53 @@ if [[ "${MODE,,}" =~ "standalone" ]] || [ "${1,,}" = "manual" ] || [ "${1,,}" = backup_job_backup_begin=+0 else silent sleep {{BACKUP_NUMBER}} - current_time=$(date +'%s') - today=$(date +"%Y%m%d") + time_last_run=0 + time_current=$(date +'%s') - if [[ ${backup_job_backup_begin} =~ ^\+(.*)$ ]]; then - waittime=$(( ${BASH_REMATCH[1]} * 60 )) - target_time=$(($current_time + $waittime)) + if [[ "${backup_job_backup_begin}" =~ ^\+(.*)$ ]]; then + print_debug "BACKUP_BEGIN is a jump of minute starting with +" + timer plus_value + elif [[ "${backup_job_backup_begin}" =~ ^[0-9]{4}$ ]]; then + print_debug "BACKUP_BEGIN is a HHMM value" + timer time + elif [[ "${backup_job_backup_begin}" =~ ([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]([0-9]{2}):([0-9]{2}):([0-9]{2}) ]]; then + print_debug "BACKUP_BEGIN is a full date timestamp" + timer datetime + elif echo ${backup_job_backup_begin//\*/#} | grep -qP "^(((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|#) ?){5}$" ; then + print_debug "BACKUP_BEGIN is a cron expression" + time_last_run=$(date +"%s") + timer cron "${backup_job_backup_begin}" "${time_current}" "${time_last_run}" else - target_time=$(date --date="${today} ${backup_job_backup_begin}" +'%s') - if [[ "$target_time" < "$current_time" ]]; then - target_time=$(($target_time + 24*60*60)) - fi - waittime=$(($target_time - $current_time)) + print_error "_BACKUP_BEGIN is invalid - Unable to perform scheduling" + cat < "${backup_job_blackout_start}" ]] && [[ "${hour_minute}" < "${backup_job_blackout_finish}" ]] ; then + time_current_hour_minute=$(date +%H%M) + if [[ "${time_current_hour_minute}" > "${backup_job_blackout_start}" ]] && [[ "${time_current_hour_minute}" < "${backup_job_blackout_finish}" ]] ; then blackout=true else blackout=false fi fi + if var_true "${blackout}" ; then print_notice "Detected Blackout Period - Not performing backup operations" else @@ -55,40 +74,8 @@ while true; do echo "{{BACKUP_NUMBER}}" >> /tmp/.container/db-backup-backups print_debug "Backup {{BACKUP_NUMBER}} routines started time: $(date +'%Y-%m-%d %T %Z')" bootstrap_filesystem - case "${dbtype,,}" in - "couch" ) - check_availability - backup_couch - ;; - "influx" ) - check_availability - backup_influx - ;; - "mssql" ) - check_availability - backup_mssql - ;; - "mysql" ) - check_availability - backup_mysql - ;; - "mongo" ) - check_availability - backup_mongo - ;; - "pgsql" ) - check_availability - backup_pgsql - ;; - "redis" ) - check_availability - backup_redis - ;; - "sqlite3" ) - check_availability - backup_sqlite3 - ;; - esac + check_availability + backup_${dbtype,,} timer job stop if [ -z "${exitcode_backup}" ] ; then exitcode_backup="0" ; fi print_info "Backup {{BACKUP_NUMBER}} routines finish time: $(date -d @${backup_job_finish_time} +'%Y-%m-%d %T %Z') with exit code ${exitcode_backup}" @@ -106,8 +93,15 @@ while true; do print_error "Stopping backup_scheduler {{BACKUP_NUMBER}} due to detected errors. Fix and restart container." s6-svc -d /var/run/s6/legacy-services/dbbackup-{{BACKUP_NUMBER}} else - print_notice "Sleeping for another $(($backup_job_backup_interval*60-backup_job_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($backup_job_backup_interval*60-backup_job_total_time))))" +'%Y-%m-%d %T %Z') " - silent sleep $(($backup_job_backup_interval*60-backup_job_total_time)) + if [ ! "${time_cron}" = "true" ]; then + print_notice "Sleeping for another $(($backup_job_backup_interval*60-backup_job_total_time)) seconds. Waking up at $(date -d@"$(( $(date +%s)+$(($backup_job_backup_interval*60-backup_job_total_time))))" +'%Y-%m-%d %T %Z') " + silent sleep $(($backup_job_backup_interval*60-backup_job_total_time)) + else + time_last_run=$(date +"%s") + timer cron "${backup_job_backup_begin}" "${time_current}" "${time_last_run}" + print_notice "Sleeping for another ${time_wait} seconds. Waking up at $(date -d@${time_future} +'%Y-%m-%d %T %Z') " + silent sleep ${time_wait} + fi fi fi done diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index e124def..57f9ae0 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -30,7 +30,7 @@ bootstrap_variables() { backup_init() { backup_instance_number=${1} backup_instance_vars=$(mktemp) - set -o posix ; set | grep -oE "^backup_job_.*=" | tr " " "\n" | grep -oE ".*=" | sed "/--/d" > "${backup_instance_vars}" + set -o posix ; set | grep -oE "^backup_job_.*=" | grep -oE ".*=" | sed "/--/d" > "${backup_instance_vars}" while read -r backup_instance_var ; do unset "$(echo "${backup_instance_var}" | cut -d = -f 1)" done < "${backup_instance_vars}" @@ -106,32 +106,38 @@ bootstrap_variables() { S3_PROTOCOL \ S3_EXTRA_OPTS ## Legacy after DEFAULT - set -o posix ; set | grep -E "^DB${backup_instance_number}_|^DEFAULT_|^DB_|^ARCHIVE|^BACKUP_|^BLOBXFER_|^CHECKSUM|^COMPRESSION|^CREATE_|^ENABLE_|^EXTRA_|^GZ_|^INFLUX_|^MYSQL_|^MONGO_|^PARALLEL|^PRE_|^POST_|^S3|^SKIP|^SPLIT"| tr " " "\n" > "${backup_instance_vars}" + set -o posix ; set | grep -E "^DB${backup_instance_number}_|^DEFAULT_|^DB_|^ARCHIVE|^BACKUP_|^BLOBXFER_|^CHECKSUM|^COMPRESSION|^CREATE_|^ENABLE_|^EXTRA_|^GZ_|^INFLUX_|^MYSQL_|^MONGO_|^PARALLEL|^PRE_|^POST_|^S3|^SKIP|^SPLIT" > "${backup_instance_vars}" ## Legacy checks from removed variables if [ -n "${ENABLE_CHECKSUM}" ]; then - print_warn "Deprecated Variable 'ENABLE_CHECKSUM' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'ENABLE_CHECKSUM' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" if var_false "${ENABLE_CHECKSUM}" ; then DEFAULT_CHECKSUM=NONE fi fi - #if [ -n "${DB_DUMP_FREQ}" ]; then - # print_warn "Deprecated Variable 'DB_DUMP_FREQ' dnow_date=$(run_as_user date +"%Y-%m-%d") - #fi + if [ -n "${DB_DUMP_BEGIN}" ]; then + print_warn "Deprecated and unsupported variable 'DB_DUMP_BEGIN' dnow_date=$(run_as_user date +"%Y-%m-%d") + DEFAULT_BACKUP_BEGIN=${DB_BACKUP_BEGIN} + fi + + if [ -n "${DB_DUMP_FREQ}" ]; then + print_warn "Deprecated and unsupported variable 'DB_DUMP_FREQ' dnow_date=$(run_as_user date +"%Y-%m-%d") + DEFAULT_BACKUP_INTERVAL=${DB_BACKUP_INTERVAL} + fi if [ -n "${DB_DUMP_TARGET}" ]; then - print_warn "Deprecated Variable 'DB_DUMP_TARGET' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_FILESYSTEM_PATH="${DB_DUMP_TARGET}" fi if [ -n "${DB_DUMP_TARGET_ARCHIVE}" ]; then - print_warn "Deprecated Variable 'DB_DUMP_TARGET_ACRHIVE' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET_ACRHIVE' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_FILESYSTEM_ARCHIVE_PATH="${DB_DUMP_TARGET_ARCHIVE}" fi if [ -n "${EXTRA_DUMP_OPTS}" ]; then - print_warn "Deprecated Variable 'EXTRA_DUMP_OPTS' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'EXTRA_DUMP_OPTS' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_EXTRA_BACKUP_OPTS="${EXTRA_DUMP_OPTS}" fi ## @@ -218,7 +224,8 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" SPLIT_DB backup_job_split_db transform_backup_instance_variable "${backup_instance_number}" TYPE backup_job_db_type transform_backup_instance_variable "${backup_instance_number}" USER backup_job_db_user - rm -rf "${backup_instance_vars}" + + backup_job_backup_begin=$(echo "${backup_job_backup_begin}" | sed -e "s|'||g" -e 's|"||g') } upgrade_lonely_variables() { @@ -704,7 +711,7 @@ backup_sqlite3() { check_availability() { ### Set the Database Type if var_false "${backup_job_skip_availability_check}" ; then - case "$dbtype" in + case "${dbtype}" in "couch" ) counter=0 code_received=0 @@ -1343,6 +1350,201 @@ timer() { esac ;; cron) + parse_expression() { + local expressions=${1//,/ } + expressions=${expressions//\*/#} + + local validate_all="" + local validate_temp="" + + for expression in ${expressions}; do + if [ "${expression}" = "#" ] || [ "${expression}" = "${3}" ]; then + echo "${3}" + return 0 + fi + + expression_step=${expression##*\/} + expression_number=${expression%%\/*} + validate_temp="" + local expression_start= + local expression_end= + if [ "${expression_number}" = "#" ]; then + expression_start=0 + expression_end="${2}" + else + expression_start=${expression_number%%-*} + expression_end=${expression_number##*-} + fi + + validate_temp=$(seq ${expression_start} ${expression_end}) + + if [ "${expression_step}" != "${expression}" ]; then + for step in ${validate_temp}; do + if [ $(( (${step} - ${expression_start}) % ${expression_step} )) -eq 0 ]; then + validate_all="$validate_all ${step}" + fi + done + else + validate_all="${validate_all} ${validate_temp}" + fi + done + + validate_all=$(echo $validate_all | tr ' ' '\n' | sort -n -u | tr '\n' ' ') + for entry in $validate_all; do + if [ "${entry}" -ge "${3}" ]; then + echo "${entry}" + return 0 + fi + done + + echo ${validate_all%% *} + } + + local cron_compare="${3}" + local cron_compare_seconds=${cron_compare} + local cron_compare_difference=$((${cron_compare} - ${4})) + + if [ "${cron_compare_difference}" -lt 60 ]; then + cron_compare=$((${cron_compare} + $(( 60-${cron_compare_difference} )) )) + fi + + local cron_current_seconds=$(date --date="@${cron_compare_seconds}" +"%-S") + if [ $cron_current_seconds -ne 0 ]; then + cron_compare_seconds=$(( ${cron_compare_seconds} - ${cron_current_seconds} )) + fi + + local cron_minute=$(echo -n "${2}" | awk '{print $1}') + local cron_hour=$(echo -n "${2}" | awk '{print $2}') + local cron_day_of_month=$(echo -n "${2}" | awk '{print $3}') + local cron_month=$(echo -n "${2}" | awk '{print $4}') + local cron_day_of_week=$(echo -n "${2}" | awk '{print $5}') + + local cron_parsed=1 + + local cron_next_minute=$(date --date="@${cron_compare}" +"%-M") + local cron_next_hour=$(date --date="@${cron_compare}" +"%-H") + local cron_next_day_of_month=$(date --date="@${cron_compare}" +"%-d") + local cron_next_month=$(date --date="@${cron_compare}" +"%-m") + local cron_next_day_of_week=$(date --date="@${cron_compare}" +"%-u") + cron_next_day_of_week=$(( ${cron_next_day_of_week} % 7 )) + local cron_next_year=$(date --date="@${cron_compare}" +"%-Y") + + local cron_next= + + while [ "$cron_parsed" != "0" ]; do + cron_next=$(parse_expression "${cron_minute}" 59 "${cron_next_minute}") + if [ "${cron_next}" != "${cron_next_minute}" ]; then + if [ "${cron_next_minute}" -gt "${cron_next}" ]; then + cron_next_hour=$(( ${cron_next_hour} + 1 )) + fi + + cron_next_minute="${cron_next}" + fi + + cron_next=$(parse_expression "${cron_hour}" 23 "${cron_next_hour}") + if [ "${cron_next}" != "${cron_next_hour}" ]; then + if [ "${cron_next_hour}" -gt "${cron_next}" ]; then + cron_next_day_of_month=$(( ${cron_next_day_of_month} + 1 )) + fi + cron_next_hour="${cron_next}" + #cron_next_minute=0 + fi + + cron_next=$(parse_expression "${cron_day_of_week}" 6 "${cron_next_day_of_week}") + if [ "${cron_next}" != "${cron_next_day_of_week}" ]; then + day_of_week_difference=$(( ${cron_next} - ${cron_next_day_of_week} )) + + if [ "${day_of_week_difference}" -lt "0" ]; then + day_of_week_difference=$(( ${day_of_week_difference} + 7 )) + fi + + cron_next_day_of_month=$(( ${cron_next_day_of_month} + ${day_of_week_difference} )) + cron_next_hour=0 + cron_next_minute=0 + fi + + case "${cron_next_month}" in + 1|3|5|7|8|10|12) + last_day_of_month="31" + ;; + "2") + local divide_by_4=$(( ${cron_next_year} % 4 )) + local divide_by_100=$(( ${cron_next_year} % 100 )) + local divide_by_400=$(( ${cron_next_year} % 400 )) + last_day_of_month=28 + if [ "${divide_by_4}" = "0" ] && [ "${divide_by_100}" != "0" ]; then + last_day_of_month="29" + fi + if [ "${divide_by_400}" = "0" ]; then + last_day_of_month="29" + fi + ;; + *) + last_day_of_month="30" + ;; + esac + + cron_next=$(parse_expression "${cron_day_of_month}" 30 "${cron_next_day_of_month}") + + if [ "${cron_next}" != "${cron_next_day_of_month}" ]; then + cron_next_hour=0 + cron_next_minute=0 + fi + + if [ "${cron_next_day_of_month}" -gt "${cron_next}" ] || [ "${cron_next_day_of_month}" -gt "${last_day_of_month}" ]; then + cron_next_month=$(( ${cron_next_month} + 1 )) + if [ ${cron_next_month} -gt 12 ]; then + cron_next_month=$(( ${cron_next_month} - 12)) + cron_next_year=$(( ${cron_next_year} + 1 )) + fi + cron_next_day_of_month=1 + else + cron_next_day_of_month=$cron_next + fi + + cron_next=$(parse_expression "${cron_month}" 12 "${cron_next_month}") + if [ "${cron_next}" != "${cron_next_month}" ]; then + if [ "${cron_next}" -gt "12" ]; then + cron_next_year=$(( ${cron_next_year} + 1 )) + cron_next=$(( ${cron_next} - 12 )) + fi + if [ "${cron_next_month}" -gt "${cron_next}" ]; then + cron_next_year=$(( ${cron_next_year} + 1 )) + fi + cron_next_month="${cron_next}" + cron_next_day=1 + cron_next_minute=0 + cron_next_hour=0 + fi + cron_parsed=0 + done + + local cron_future=$(date --date="${cron_next_year}-$(printf "%02d" ${cron_next_month})-$(printf "%02d" ${cron_next_day_of_month})T$(printf "%02d" ${cron_next_hour}):$(printf "%02d" ${cron_next_minute}):00" "+%s") + local cron_future_difference=$((${cron_future} - ${cron_compare_seconds})) + time_cron=true + time_wait="${cron_future_difference}" + time_future=${cron_future} + ;; + datetime) + time_begin_year=${BASH_REMATCH[1]} + time_begin_month=${BASH_REMATCH[2]} + time_begin_day=${BASH_REMATCH[3]} + time_begin_hour=${BASH_REMATCH[4]} + time_begin_minute=${BASH_REMATCH[5]} + time_begin_second=${BASH_REMATCH[6]} + time_begin=$(date -d "${time_begin_year}-${time_begin_month}-${time_begin_day} ${time_begin_hour}:${time_begin_minute}:${time_begin_second}" +%s) + print_debug "BACKUP_BEGIN time = ${time_begin}" + time_wait=$((time_begin - time_current)) + print_debug "Difference in seconds: ${time_wait}" + + if (( ${time_wait} < 0 )); then + time_wait=$(( (${time_wait} + (${backup_job_backup_interval - 1)) / (${backup_job_backup_interval} * 60) )) + time_wait=$(( ${time_wait} * -1 )) + print_debug "Difference in seconds (rounded) time_wait is in the past : ${time_wait}" + fi + + time_future=$((${time_current} + ${time_wait})) + print_debug "Future execution time = ${time_future}" ;; job) case "${2}" in @@ -1355,9 +1557,20 @@ timer() { ;; esac ;; + plusvalue) + time_wait=$(( ${BASH_REMATCH[1]} * 60 )) + time_future=$(($time_current} + $time_wait)) + ;; + time) + time_future=$(date --date="$(date +"%Y%m%d") ${backup_job_backup_begin}" +"%s") + if [[ "${future_time}" < "${time_current}" ]]; then + time_future=$(($time_future + 24*60*60)) + fi + time_wait=$((${time_future} - ${time_current})) + ;; esac - } + prepare_dbbackup() { timer backup start now=$(run_as_user date +"%Y%m%d-%H%M%S") From 0ba19890831a72dab0b7c1e9de1c01928f54f2bf Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 07:45:52 -0800 Subject: [PATCH 31/68] modify MSSQL backups to get compressed post initial backup --- examples/post-script.sh | 2 +- install/assets/functions/10-db-backup | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/post-script.sh b/examples/post-script.sh index 4307134..98e8e09 100644 --- a/examples/post-script.sh +++ b/examples/post-script.sh @@ -4,7 +4,7 @@ # #### $1=EXIT_CODE (After running backup routine) # #### $2=DB_TYPE (Type of Backup) # #### $3=DB_HOST (Backup Host) -# #### #4=DB_NAME (Name of Database backed up +# #### #4=DB_NAME (Name of Database backed up) # #### $5=BACKUP START TIME (Seconds since Epoch) # #### $6=BACKUP FINISH TIME (Seconds since Epoch) # #### $7=BACKUP TOTAL TIME (Seconds between Start and Finish) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 57f9ae0..48e28e8 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -476,11 +476,14 @@ backup_mssql() { prepare_dbbackup target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.bak ltarget=mssql_${backup_job_db_name,,}_${backup_job_db_host,,} - compression pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? + target_original=${target} + compression + pre_dbbackup all + run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" check_exit_code backup $target timer backup finish file_encryption @@ -493,11 +496,14 @@ backup_mssql() { prepare_dbbackup target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.trn ltarget=mssql_${backup_job_db_name,,}_trn_${backup_job_db_host,,} - compression pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? + target_original=${target} + compression + pre_dbbackup all + run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" check_exit_code backup $target file_encryption timer backup finish From e090eeda3f64ee5ec69666b3f66ebbc02099fb23 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 07:55:40 -0800 Subject: [PATCH 32/68] feat - add MySQL Event backup support --- README.md | 33 +++++++++++++++------------ install/assets/defaults/10-db-backup | 1 + install/assets/functions/10-db-backup | 8 +++++-- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d033223..5b4bb89 100644 --- a/README.md +++ b/README.md @@ -257,16 +257,18 @@ If these are set and no other defaults or variables are set explicitly, they wil ###### MariaDB/MySQL -| Variable | Description | Default | `_FILE` | -| ---------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | -| `DEFAULT_PORT` | MariaDB Port | `3306` | x | -| `DEFAULT_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | -| `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | -| `DEFAULT_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | -| `DEFAULT_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet if backing up MySQL / MariaDB | `512M` | | -| `DEFAULT_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction with MySQL / MariaDB | `TRUE` | | -| `DEFAULT_MYSQL_STORED_PROCEDURES` | Backup stored procedures with MySQL / MariaDB | `TRUE` | | -| `DEFAULT_MYSQL_ENABLE_TLS` | Enable TLS functionality for MySQL/MariaDB client | `FALSE` | | +| Variable | Description | Default | `_FILE` | +| ---------------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | +| `DEFAULT_PORT` | MySQL / MariaDB Port | `3306` | x | +| `DEFAULT_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DEFAULT_MYSQL_EVENTS` | Backup Events | `TRUE` | | +| `DEFAULT_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet | `512M` | | +| `DEFAULT_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction | `TRUE` | | +| `DEFAULT_MYSQL_STORED_PROCEDURES` | Backup stored procedures | `TRUE` | | +| `DEFAULT_MYSQL_ENABLE_TLS` | Enable TLS functionality | `FALSE` | | + | `DEFAULT_MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | | `DEFAULT_MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | | `DEFAULT_MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | @@ -525,11 +527,12 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_NAME` | Schema Name e.g. `database` or `ALL` to backup all databases the user has access to. | | | | | Backup multiple by separating with commas eg `db1,db2` | | x | | `DB01_NAME_EXCLUDE` | If using `ALL` - use this as to exclude databases separated via commas from being backed up | | x | -| `DB01_PORT` | MariaDB Port | `3306` | x | -| `DB01_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet if backing up MySQL / MariaDB | `512M` | | -| `DB01_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction with MySQL / MariaDB | `TRUE` | | -| `DB01_MYSQL_STORED_PROCEDURES` | Backup stored procedures with MySQL / MariaDB | `TRUE` | | -| `DB01_MYSQL_ENABLE_TLS` | Enable TLS functionality for MySQL/MariaDB client | `FALSE` | | +| `DB01_PORT` | MySQL / MariaDB Port | `3306` | x | +| `DB01_MYSQL_EVENTS` | Backup Events for | `TRUE` | | +| `DB01_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet | `512M` | | +| `DB01_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction | `TRUE` | | +| `DB01_MYSQL_STORED_PROCEDURES` | Backup stored procedures | `TRUE` | | +| `DB01_MYSQL_ENABLE_TLS` | Enable TLS functionality | `FALSE` | | | `DB01_MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | | `DB01_MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | | `DB01_MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | diff --git a/install/assets/defaults/10-db-backup b/install/assets/defaults/10-db-backup index 86bb12e..4f2b465 100644 --- a/install/assets/defaults/10-db-backup +++ b/install/assets/defaults/10-db-backup @@ -17,6 +17,7 @@ DEFAULT_FILESYSTEM_PERMISSION=${DEFAULT_FILESYSTEM_PERMISSION:-"700"} DEFAULT_FILESYSTEM_ARCHIVE_PATH=${DEFAULT_FILESYSTEM_ARCHIVE_PATH:-"${DEFAULT_FILESYSTEM_PATH}/archive/"} DEFAULT_LOG_LEVEL=${DEFAULT_LOG_LEVEL:-"notice"} DEFAULT_MYSQL_ENABLE_TLS=${DEFAULT_MYSQL_ENABLE_TLS:-"FALSE"} +DEFAULT_MYSQL_EVENTS=${DEFAULT_MYSQL_EVENTS:-"TRUE"} DEFAULT_MYSQL_MAX_ALLOWED_PACKET=${DEFAULT_MYSQL_MAX_ALLOWED_PACKET:-"512M"} DEFAULT_MYSQL_SINGLE_TRANSACTION=${DEFAULT_MYSQL_SINGLE_TRANSACTION:-"TRUE"} DEFAULT_MYSQL_STORED_PROCEDURES=${DEFAULT_MYSQL_STORED_PROCEDURES:-"TRUE"} diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 48e28e8..5e0b687 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -188,6 +188,7 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" LOG_LEVEL backup_job_log_level transform_backup_instance_variable "${backup_instance_number}" MONGO_CUSTOM_URI backup_job_mongo_custom_uri transform_backup_instance_variable "${backup_instance_number}" MYSQL_ENABLE_TLS backup_job_mysql_enable_tls + transform_backup_instance_variable "${backup_instance_number}" MYSQL_EVENTS backup_job_mysql_events transform_backup_instance_variable "${backup_instance_number}" MYSQL_MAX_ALLOWED_PACKET backup_job_mysql_max_allowed_packet transform_backup_instance_variable "${backup_instance_number}" MYSQL_SINGLE_TRANSACTION backup_job_mysql_single_transaction transform_backup_instance_variable "${backup_instance_number}" MYSQL_STORED_PROCEDURES backup_job_mysql_stored_procedures @@ -516,6 +517,9 @@ backup_mssql() { } backup_mysql() { + if var_true "${backup_job_mysql_events}" ; then + events="--events" + fi if var_true "${backup_job_mysql_single_transaction}" ; then single_transaction="--single-transaction" fi @@ -547,7 +551,7 @@ backup_mysql() { compression pre_dbbackup $db write_log notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" - run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target timer backup finish @@ -565,7 +569,7 @@ backup_mysql() { compression pre_dbbackup all write_log notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null exit_code=$? check_exit_code backup $target timer backup finish From 59854c1715c43e1da3b541b8e23867e7966bf37f Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 08:35:40 -0800 Subject: [PATCH 33/68] Refactor timer/datetime --- install/assets/functions/10-db-backup | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 5e0b687..2d88476 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1536,13 +1536,7 @@ timer() { time_future=${cron_future} ;; datetime) - time_begin_year=${BASH_REMATCH[1]} - time_begin_month=${BASH_REMATCH[2]} - time_begin_day=${BASH_REMATCH[3]} - time_begin_hour=${BASH_REMATCH[4]} - time_begin_minute=${BASH_REMATCH[5]} - time_begin_second=${BASH_REMATCH[6]} - time_begin=$(date -d "${time_begin_year}-${time_begin_month}-${time_begin_day} ${time_begin_hour}:${time_begin_minute}:${time_begin_second}" +%s) + time_begin=$(date -d "${backup_job_backup_begin}" +%s) print_debug "BACKUP_BEGIN time = ${time_begin}" time_wait=$((time_begin - time_current)) print_debug "Difference in seconds: ${time_wait}" From da22217e7b50f7d0997b8a6258f4db0a7378b60d Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 08:36:15 -0800 Subject: [PATCH 34/68] Refactor timer/datetime --- install/assets/functions/10-db-backup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 2d88476..b0a6a88 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1542,7 +1542,7 @@ timer() { print_debug "Difference in seconds: ${time_wait}" if (( ${time_wait} < 0 )); then - time_wait=$(( (${time_wait} + (${backup_job_backup_interval - 1)) / (${backup_job_backup_interval} * 60) )) + time_wait=$(( (${time_wait} + (${backup_job_backup_interval} - 1)) / (${backup_job_backup_interval} * 60) )) time_wait=$(( ${time_wait} * -1 )) print_debug "Difference in seconds (rounded) time_wait is in the past : ${time_wait}" fi From ca6b045d7d1ffeb59b49cc6623ff284619226534 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 08:51:32 -0800 Subject: [PATCH 35/68] Optimize write_log function --- install/assets/functions/10-db-backup | 41 +++++++++++++-------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index b0a6a88..92013af 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1665,7 +1665,7 @@ EOF fi if [ -d "${backup_job_script_location_post}" ] && dir_notempty "${backup_job_script_location_post}" ; then - for f in $(run_as_user find ${backup_job_script_location_post} -name \*.sh -type f); do + for f in $(run_as_user find "${backup_job_script_location_post}" -name \*.sh -type f); do if var_true "${backup_job_post_script_x_verify}" ; then run_as_user ${f} "${exit_code}" "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${dbbackup_finish_time}" "${dbbackup_total_time}" "${target}" "${filesize}" "${checksum_value}" "${move_exit_code}" else @@ -1709,7 +1709,7 @@ run_as_user() { } setup_mode() { - if [ "${MODE,,}" = "auto" ] || [ ${MODE,,} = "default" ] ; then + if [ "${MODE,,}" = "auto" ] || [ "${MODE,,}" = "default" ] ; then write_log debug "Running in Auto / Default Mode - Letting Image control scheduling" else write_log info "Running in Manual mode - Execute 'backup_now' to perform a manual backup" @@ -1750,29 +1750,31 @@ symlink_log () { } write_log() { + local CONTAINER_LOG_LEVEL_ORIGINAL=${CONTAINER_LOG_LEVEL} + local _arg_log_level=${1} + shift 1 + local _arg_log_message="$@" case "${1}" in debug ) + output_off CONTAINER_LOG_LEVEL=DEBUG - shift 1 case "${backup_job_log_level,,}" in "debug" ) - - print_debug "$@" - output_off + print_debug "${_arg_log_message}" if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi - output_on ;; esac + CONTAINER_LOG_LEVEL=${CONTAINER_LOG_LEVEL_ORIGINAL} + output_on ;; error ) + output_off CONTAINER_LOG_LEVEL=ERROR - shift 1 case "${backup_job_log_level,,}" in "debug" | "notice" | "warn" | "error") - output_off - print_error "$@" + print_error "${_arg_log_message}" if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi @@ -1781,40 +1783,35 @@ write_log() { esac ;; info ) + output_off CONTAINER_LOG_LEVEL=INFO - shift 1 - print_info "$@" - output_off + print_info "${_arg_log_message}" if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi - output_on + ;; notice ) + output_off CONTAINER_LOG_LEVEL=NOTICE - shift 1 case "${backup_job_log_level,,}" in "debug" | "notice" ) - print_notice "$@" - output_off + print_notice "${_arg_log_message}" if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi - output_on ;; esac ;; warn ) + output_off CONTAINER_LOG_LEVEL=WARN - shift 1 case "${backup_job_log_level,,}" in "debug" | "notice" | "warn" ) - print_warn "$@" - output_off + print_warn "${_arg_log_message}" if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi - output_on ;; esac ;; From 5d8a7f1720776effb084810d8d780ba8090a39b1 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 08:54:43 -0800 Subject: [PATCH 36/68] Optimize write_log function --- install/assets/functions/10-db-backup | 37 ++++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 92013af..5a5f48c 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1750,70 +1750,71 @@ symlink_log () { } write_log() { + output_off local CONTAINER_LOG_LEVEL_ORIGINAL=${CONTAINER_LOG_LEVEL} local _arg_log_level=${1} shift 1 local _arg_log_message="$@" + + if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then + write_to_file=true + else + write_to_file=false + fi + case "${1}" in debug ) - output_off CONTAINER_LOG_LEVEL=DEBUG case "${backup_job_log_level,,}" in "debug" ) print_debug "${_arg_log_message}" - if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + if var_true "${write_to_file}" ; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi ;; esac CONTAINER_LOG_LEVEL=${CONTAINER_LOG_LEVEL_ORIGINAL} - output_on ;; error ) - output_off CONTAINER_LOG_LEVEL=ERROR case "${backup_job_log_level,,}" in "debug" | "notice" | "warn" | "error") print_error "${_arg_log_message}" - if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + if var_true "${write_to_file}" ; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi - output_on ;; esac ;; info ) - output_off CONTAINER_LOG_LEVEL=INFO print_info "${_arg_log_message}" - if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + if var_true "${write_to_file}" ; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi - ;; notice ) - output_off CONTAINER_LOG_LEVEL=NOTICE case "${backup_job_log_level,,}" in "debug" | "notice" ) print_notice "${_arg_log_message}" - if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + if var_true "${write_to_file}" ; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi ;; esac ;; warn ) - output_off CONTAINER_LOG_LEVEL=WARN case "${backup_job_log_level,,}" in "debug" | "notice" | "warn" ) print_warn "${_arg_log_message}" - if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] $@" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + if var_true "${write_to_file}" ; then + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null fi ;; esac ;; esac + output_on } \ No newline at end of file From 9e7904ce4dfbd29c3215538306993036adeabad9 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 09:47:59 -0800 Subject: [PATCH 37/68] Cleanup Formatting --- README.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5b4bb89..269efac 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Build Status](https://img.shields.io/github/actions/workflow/status/tiredofit/docker-db-backup/main.yml?branch=main&style=flat-square)](https://github.com/tiredofit/docker-db-backup/actions) [![Docker Stars](https://img.shields.io/docker/stars/tiredofit/db-backup.svg?style=flat-square&logo=docker)](https://hub.docker.com/r/tiredofit/db-backup/) [![Docker Pulls](https://img.shields.io/docker/pulls/tiredofit/db-backup.svg?style=flat-square&logo=docker)](https://hub.docker.com/r/tiredofit/db-backup/) -[![Become a sponsor](https://img.shields.io/badge/sponsor-tiredofit-181717.svg?logo=github&style=flat-square)](https://github.com/sponsors/tiredofit) +[![Become a sponsor](https://img.shields.io/badge/sponsor-tiredofit-181717.svg?logo=github&style=flat-square)](https://www.tiredofit.ca/sponsor) [![Paypal Donate](https://img.shields.io/badge/donate-paypal-00457c.svg?logo=paypal&style=flat-square)](https://www.paypal.me/tiredofit) --- @@ -13,7 +13,7 @@ This will build a container for backing up multiple types of DB Servers -Backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis servers. +Backs up CouchDB, InfluxDB, MySQL/MariaDB, Microsoft SQL, MongoDB, Postgres, Redis servers. - dump to local filesystem or backup to S3 Compatible services, and Azure. - multiple backup job support @@ -27,9 +27,10 @@ Backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis serve - checksum support choose to have an MD5 or SHA1 hash generated after backup for verification - compression support (none, gz, bz, xz, zstd) - encryption support (passphrase and public key) -- Zabbix Metrics support -- Hooks to execute pre and post backup job for customization purposes -- Companion script to aid in restores +- notify upon job failure to email, matrix, mattermost, rocketchat, custom script +- zabbix metrics support +- hooks to execute pre and post backup job for customization purposes +- companion script to aid in restores ## Maintainer @@ -101,7 +102,6 @@ Backs up CouchDB, InfluxDB, MySQL, Microsoft SQL, MongoDB, Postgres, Redis serve - [Maintenance](#maintenance) - [Shell Access](#shell-access) - [Manual Backups](#manual-backups) - - [Manual Backups](#manual-backups-1) - [Restoring Databases](#restoring-databases) - [Support](#support) - [Usage](#usage) @@ -142,7 +142,7 @@ docker pull docker.io/tiredofit/db-backup:(imagetag) #### Multi Architecture -Images are built primarily for `amd64` architecture, and may also include builds for `arm/v7`, `arm64` and others. These variants are all unsupported. Consider [sponsoring](https://github.com/sponsors/tiredofit) my work so that I can work with various hardware. To see if this image supports multiple architectures, type `docker manifest (image):(tag)` +Images are built primarily for `amd64` architecture, and may also include builds for `arm/v7`, `arm64` and others. These variants are all unsupported. Consider [sponsoring](https://www.tiredofit.ca/sponsor) my work so that I can work with various hardware. To see if this image supports multiple architectures, type `docker manifest (image):(tag)` ## Configuration @@ -161,6 +161,7 @@ The following directories are used for configuration and can be mapped for persi | `/backup` | Backups | | `/assets/scripts/pre` | _Optional_ Put custom scripts in this directory to execute before backup operations | | `/assets/scripts/post` | _Optional_ Put custom scripts in this directory to execute after backup operations | +| `/logs` | _Optional_ Logfiles for backup jobs | ### Environment Variables @@ -444,7 +445,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | `DB01_NAME` | Schema Name e.g. `database` | | x | | `DB01_USER` | username for the database(s) - Can use `root` for MySQL | | x | | `DB01_PASS` | (optional if DB doesn't require it) password for the database | | x | -| | | + | Variable | Description | Default | | ------------------------------ | --------------------------------------------------------------------------------------------------------- | ------------ | @@ -784,13 +785,10 @@ docker exec -it (whatever your container name is) bash ### Manual Backups -Manual Backups can be performed by entering the container and typing `backup-now` +Manual Backups can be performed by entering the container and typing `backup-now`. This will execute all the backup tasks that are scheduled by means of the `BACKUPXX_` variables. Alternatively if you wanted to execute a job on its own you could simply type `backup01-now` (or whatever your number would be). There is no concurrency, and jobs will be executed sequentially. - Recently there was a request to have the container work with Kubernetes cron scheduling. This can theoretically be accomplished by setting the container `MODE=MANUAL` and then setting `MANUAL_RUN_FOREVER=FALSE` - You would also want to disable a few features from the upstream base images specifically `CONTAINER_ENABLE_SCHEDULING` and `CONTAINER_ENABLE_MONITORING`. This should allow the container to start, execute a backup by executing and then exit cleanly. An alternative way to running the script is to execute `/etc/services.available/10-db-backup/run`. -### Manual Backups -Manual Backups can be performed by entering the container and typing `backup-now`. This will execute all the backup tasks that are scheduled by means of the `BACKUPXX_` variables. Alternatively if you wanted to execute a job on its own you could simply type `backup01-now` (or whatever your number would be). There is no concurrency, and jobs will be executed sequentially. - ### Restoring Databases Entering in the container and executing `restore` will execute a menu based script to restore your backups - MariaDB, Postgres, and Mongo supported. @@ -823,7 +821,7 @@ These images were built to serve a specific need in a production environment and ### Usage - The [Discussions board](../../discussions) is a great place for working with the community on tips and tricks of using this image. -- Consider [sponsoring me](https://github.com/sponsors/tiredofit) for personalized support +- [Sponsor me](https://www.tiredofit.ca/sponsor) for personalized support ### Bugfixes @@ -832,12 +830,12 @@ These images were built to serve a specific need in a production environment and ### Feature Requests - Feel free to submit a feature request, however there is no guarantee that it will be added, or at what timeline. -- Consider [sponsoring me](https://github.com/sponsors/tiredofit) regarding development of features. +- [Sponsor me](https://www.tiredofit.ca/sponsor) regarding development of features. ### Updates - Best effort to track upstream changes, More priority if I am actively using the image in a production environment. -- Consider [sponsoring me](https://github.com/sponsors/tiredofit) for up to date releases. +- [Sponsor me](https://www.tiredofit.ca/sponsor) for up to date releases. ## License From 2b446f1e4c839f6b103c57527d54fa4800fb10df Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 09:48:23 -0800 Subject: [PATCH 38/68] Add encrypt/checksum duration graphs --- zabbix_templates/db_backup.json | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/zabbix_templates/db_backup.json b/zabbix_templates/db_backup.json index 1e25523..a7680ac 100644 --- a/zabbix_templates/db_backup.json +++ b/zabbix_templates/db_backup.json @@ -94,7 +94,7 @@ }, { "uuid": "8ec2b2f44ddf4f36b3dbb2aa15e3a32f", - "name": "[{#NAME}] Duration", + "name": "[{#NAME}] Backup Duration", "type": "TRAP", "key": "dbbackup.backup.duration.[{#NAME}]", "delay": "0", @@ -201,6 +201,34 @@ } } ] + }, + { + "uuid": "8c641e33659e4c8b866da64e252cfc2a", + "name": "[{#NAME}] Checksum Duration", + "graph_items": [ + { + "color": "199C0D", + "calc_fnc": "ALL", + "item": { + "host": "DB Backup", + "key": "dbbackup.backup.checksum.duration.[{#NAME}]" + } + } + ] + }, + { + "uuid": "65b8770f71ed4cff9111b82c42b17571", + "name": "[{#NAME}] Encrypt Duration", + "graph_items": [ + { + "color": "199C0D", + "calc_fnc": "ALL", + "item": { + "host": "DB Backup", + "key": "dbbackup.backup.encrypt.duration.[{#NAME}]" + } + } + ] } ] } From da54cdf48bca8f023f061e3e7b4b6b61e1cffcc9 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 10:10:59 -0800 Subject: [PATCH 39/68] Code formatting and cleanup --- install/assets/dbbackup/template-dbbackup/run | 18 +-- install/assets/functions/10-db-backup | 126 +++++++++--------- 2 files changed, 72 insertions(+), 72 deletions(-) diff --git a/install/assets/dbbackup/template-dbbackup/run b/install/assets/dbbackup/template-dbbackup/run index 0fe9839..d4f6ee8 100755 --- a/install/assets/dbbackup/template-dbbackup/run +++ b/install/assets/dbbackup/template-dbbackup/run @@ -31,7 +31,7 @@ else elif [[ "${backup_job_backup_begin}" =~ ([0-9]{4})-([0-9]{2})-([0-9]{2})[[:space:]]([0-9]{2}):([0-9]{2}):([0-9]{2}) ]]; then print_debug "BACKUP_BEGIN is a full date timestamp" timer datetime - elif echo ${backup_job_backup_begin//\*/#} | grep -qP "^(((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|#) ?){5}$" ; then + elif echo "${backup_job_backup_begin//\*/#}" | grep -qP "^(((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|#) ?){5}$" ; then print_debug "BACKUP_BEGIN is a cron expression" time_last_run=$(date +"%s") timer cron "${backup_job_backup_begin}" "${time_current}" "${time_last_run}" @@ -52,8 +52,8 @@ EOF fi print_debug "Wait Time: ${time_wait} Future execution time: ${time_future} Current Time: ${time_current}" - print_info "Next Backup at $(date -d @${time_future} +'%Y-%m-%d %T %Z')" - silent sleep ${time_wait} + print_info "Next Backup at $(date -d @"${time_future}" +'%Y-%m-%d %T %Z')" + silent sleep "${time_wait}" fi while true; do @@ -75,11 +75,11 @@ while true; do print_debug "Backup {{BACKUP_NUMBER}} routines started time: $(date +'%Y-%m-%d %T %Z')" bootstrap_filesystem check_availability - backup_${dbtype,,} + backup_"${dbtype,,}" timer job stop if [ -z "${exitcode_backup}" ] ; then exitcode_backup="0" ; fi - print_info "Backup {{BACKUP_NUMBER}} routines finish time: $(date -d @${backup_job_finish_time} +'%Y-%m-%d %T %Z') with exit code ${exitcode_backup}" - print_notice "Backup {{BACKUP_NUMBER}} routines time taken: $(echo ${backup_job_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" + print_info "Backup {{BACKUP_NUMBER}} routines finish time: $(date -d @"${backup_job_finish_time}" +'%Y-%m-%d %T %Z') with exit code ${exitcode_backup}" + print_notice "Backup {{BACKUP_NUMBER}} routines time taken: $(echo "${backup_job_total_time}" | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" sed -i "/^{{BACKUP_NUMBER}}/d" /tmp/.container/db-backup-backups fi @@ -87,7 +87,7 @@ while true; do if var_false "${persist}" ; then print_debug "Exiting due to manual mode" - exit ${exitcode_backup}; + exit "${exitcode_backup}"; else if var_true "${stop_scheduler_backup}" ; then print_error "Stopping backup_scheduler {{BACKUP_NUMBER}} due to detected errors. Fix and restart container." @@ -99,8 +99,8 @@ while true; do else time_last_run=$(date +"%s") timer cron "${backup_job_backup_begin}" "${time_current}" "${time_last_run}" - print_notice "Sleeping for another ${time_wait} seconds. Waking up at $(date -d@${time_future} +'%Y-%m-%d %T %Z') " - silent sleep ${time_wait} + print_notice "Sleeping for another ${time_wait} seconds. Waking up at $(date -d@"${time_future}" +'%Y-%m-%d %T %Z') " + silent sleep "${time_wait}" fi fi fi diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 5a5f48c..7124bc9 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -5,11 +5,11 @@ bootstrap_filesystem() { mkdir -p "${backup_job_filesystem_path}" fi if [ "$(stat -c %U "${backup_job_filesystem_path}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${backup_job_filesystem_path}" ; fi - if [ "$(stat -c %a "${backup_job_filesystem_path}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R ${backup_job_filesystem_permission} "${backup_job_filesystem_path}" ; fi + if [ "$(stat -c %a "${backup_job_filesystem_path}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R "${backup_job_filesystem_permission}" "${backup_job_filesystem_path}" ; fi if [ -d "${backup_job_filesystem_archive}" ]; then if [ "$(stat -c %U "${backup_job_filesystem_archive}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${backup_job_filesystem_archive}" ; fi - if [ "$(stat -c %a "${backup_job_filesystem_archive}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R ${backup_job_filesystem_permission} "${backup_job_filesystem_archive}" ; fi + if [ "$(stat -c %a "${backup_job_filesystem_archive}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R "${backup_job_filesystem_permission}" "${backup_job_filesystem_archive}" ; fi fi if [ ! -d "${LOG_PATH}" ]; then @@ -17,7 +17,7 @@ bootstrap_filesystem() { fi if [ "$(stat -c %U "${LOG_PATH}")" != "dbbackup" ] ; then chown dbbackup:dbbackup "${LOG_PATH}" ; fi - if [ ! -d "${LOG_PATH}"/"$(date +'%Y%m%d')" ]; then run_as_user mkdir -p "${LOG_PATH}"/$(date +'%Y%m%d'); fi + if [ ! -d "${LOG_PATH}"/"$(date +'%Y%m%d')" ]; then run_as_user mkdir -p "${LOG_PATH}"/"$(date +'%Y%m%d')"; fi if [ "$(stat -c %a "${LOG_PATH}")" != "755" ] ; then chmod -R 755 "${LOG_PATH}" ; fi if [ ! -d "${TEMP_PATH}" ]; then @@ -110,34 +110,34 @@ bootstrap_variables() { ## Legacy checks from removed variables if [ -n "${ENABLE_CHECKSUM}" ]; then - print_warn "Deprecated and unsupported variable 'ENABLE_CHECKSUM' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'ENABLE_CHECKSUM' detected - Please upgrade your variables as they will be removed in version 4.3.0" if var_false "${ENABLE_CHECKSUM}" ; then DEFAULT_CHECKSUM=NONE fi fi if [ -n "${DB_DUMP_BEGIN}" ]; then - print_warn "Deprecated and unsupported variable 'DB_DUMP_BEGIN' dnow_date=$(run_as_user date +"%Y-%m-%d") + print_warn "Deprecated and unsupported variable 'DB_DUMP_BEGIN' detected - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_BACKUP_BEGIN=${DB_BACKUP_BEGIN} fi if [ -n "${DB_DUMP_FREQ}" ]; then - print_warn "Deprecated and unsupported variable 'DB_DUMP_FREQ' dnow_date=$(run_as_user date +"%Y-%m-%d") + print_warn "Deprecated and unsupported variable 'DB_DUMP_FREQ' detected - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_BACKUP_INTERVAL=${DB_BACKUP_INTERVAL} fi if [ -n "${DB_DUMP_TARGET}" ]; then - print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET' detected - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_FILESYSTEM_PATH="${DB_DUMP_TARGET}" fi if [ -n "${DB_DUMP_TARGET_ARCHIVE}" ]; then - print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET_ACRHIVE' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET_ACRHIVE' detected - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_FILESYSTEM_ARCHIVE_PATH="${DB_DUMP_TARGET_ARCHIVE}" fi if [ -n "${EXTRA_DUMP_OPTS}" ]; then - print_warn "Deprecated and unsupported variable 'EXTRA_DUMP_OPTS' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Deprecated and unsupported variable 'EXTRA_DUMP_OPTS' detected - Please upgrade your variables as they will be removed in version 4.3.0" DEFAULT_EXTRA_BACKUP_OPTS="${EXTRA_DUMP_OPTS}" fi ## @@ -147,10 +147,10 @@ bootstrap_variables() { export "$3"="$(grep "^DB${1}_${2}=" "${backup_instance_vars}" | cut -d = -f2)" elif grep -q "^DB_${2}=" "${backup_instance_vars}" && [ "$(grep "^DB_${2}=" "${backup_instance_vars}" | cut -d = -f2)" != "unset" ]; then # Allow old legacy work, perhaps remove old DB_ functionality in future? This should allow for seamless upgrades - #print_warn "Legacy Variable 'DB_${2}'' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + #print_warn "Legacy Variable 'DB_${2}'' detected - Please upgrade your variables as they will be removed in version 4.3.0" export "$3"="$(grep "^DB_${2}=" "${backup_instance_vars}" | cut -d = -f2)" elif grep -q "^${2}=" "${backup_instance_vars}" && [ "$(grep "^${2}=" "${backup_instance_vars}" | cut -d = -f2)" != "unset" ]; then - print_warn "Legacy Variable '${2}' detected being used - Please upgrade your variables as they will be removed in version 4.3.0" + print_warn "Legacy unsupported variable '${2}' detected - Please upgrade your variables as they will be removed in version 4.3.0" export "$3"="$(grep "^${2}=" "${backup_instance_vars}" | cut -d = -f2)" elif grep -q "^DEFAULT_${2}=" "${backup_instance_vars}" && [ "$(grep "^DEFAULT_${2}=" "${backup_instance_vars}" | cut -d = -f2)" != "unset" ]; then export "$3"="$(grep "^DEFAULT_${2}=" "${backup_instance_vars}" | cut -d = -f2)" @@ -252,20 +252,20 @@ bootstrap_variables() { print_debug "Looking for existence of $2 environment variable" if [ ! -v "$1" ]; then print_error "No '$3' Entered! - Set '\$$2' environment variable - Halting Backup Number ${v_instance}" - s6-svc -d /var/run/s6/legacy-services/dbbackup-${v_instance} + s6-svc -d /var/run/s6/legacy-services/dbbackup-"${v_instance}" exit 1 fi output_on } - check_var backup_job_db_type DB${v_instance}_TYPE "appropriate database type" + check_var backup_job_db_type DB"${v_instance}"_TYPE "appropriate database type" case "${backup_job_db_type,,}" in couch* ) dbtype=couch backup_job_backup_job_db_port=${backup_job_db_port:-5984} - check_var backup_job_db_user DB${v_instance}_USER "database username" - check_var backup_job_db_pass DB${v_instance}_PASS "database password" + check_var backup_job_db_user DB$"{v_instance}"_USER "database username" + check_var backup_job_db_pass DB$"{v_instance}"_PASS "database password" ;; influx* ) dbtype=influx @@ -273,9 +273,9 @@ bootstrap_variables() { 1) backup_job_db_port=${backup_job_db_port:-8088} ;; 2) backup_job_db_port=${backup_job_db_port:-8086} ;; esac - check_var backup_job_db_user DB${v_instance}_USER "database username" - check_var backup_job_db_pass DB${v_instance}_PASS "database password" - check_var backup_job_influx_version DB${v_instance}_INFLUX_VERSION "InfluxDB version you are backing up from" + check_var backup_job_db_user DB"${v_instance}"_USER "database username" + check_var backup_job_db_pass DB"${v_instance}"_PASS "database password" + check_var backup_job_influx_version DB"${v_instance}"_INFLUX_VERSION "InfluxDB version you are backing up from" ;; mongo* ) dbtype=mongo @@ -302,7 +302,7 @@ bootstrap_variables() { "mysql" | "mariadb" ) dbtype=mysql backup_job_db_port=${backup_job_db_port:-3306} - check_var backup_job_db_name DB${v_instance}_NAME "database name. Seperate multiple with commas" + check_var backup_job_db_name DB"${v_instance}"_NAME "database name. Seperate multiple with commas" if [ -n "${backup_job_db_pass}" ] ; then export MYSQL_PWD=${backup_job_db_pass} ; fi if var_true "${backup_job_mysql_enable_tls}" ; then @@ -1055,7 +1055,7 @@ file_encryption() { fi fi if [ -f "${TEMP_PATH}"/"${target}".gpg ]; then - rm -rf "${TEMP_PATH}"/"${target}" + rm -rf "${TEMP_PATH:?}"/"${target:?}" target="${target}.gpg" encrypt_routines_finish_time=$(date +'%s') @@ -1277,7 +1277,7 @@ move_dbbackup() { write_log notice "Backup of '${target}' created with the size of ${filesize}" fi - chmod ${backup_job_filesystem_permission} "${TEMP_PATH}"/"${target}" + chmod "${backup_job_filesystem_permission}" "${TEMP_PATH}"/"${target}" case "${backup_job_backup_location,,}" in "file" | "filesystem" ) write_log debug "Moving backup to filesystem" @@ -1386,7 +1386,7 @@ timer() { expression_end=${expression_number##*-} fi - validate_temp=$(seq ${expression_start} ${expression_end}) + validate_temp="$(seq "${expression_start}" "${expression_end}")" if [ "${expression_step}" != "${expression}" ]; then for step in ${validate_temp}; do @@ -1407,37 +1407,37 @@ timer() { fi done - echo ${validate_all%% *} + echo "${validate_all%% *}" } local cron_compare="${3}" local cron_compare_seconds=${cron_compare} - local cron_compare_difference=$((${cron_compare} - ${4})) + local cron_compare_difference=$(( cron_compare - ${4} )) if [ "${cron_compare_difference}" -lt 60 ]; then - cron_compare=$((${cron_compare} + $(( 60-${cron_compare_difference} )) )) + cron_compare=$((${cron_compare} + $(( 60 - cron_compare_difference )) )) fi - local cron_current_seconds=$(date --date="@${cron_compare_seconds}" +"%-S") - if [ $cron_current_seconds -ne 0 ]; then - cron_compare_seconds=$(( ${cron_compare_seconds} - ${cron_current_seconds} )) + local cron_current_seconds="$(date --date=@"${cron_compare_seconds}" +"%-S")" + if [ "${cron_current_seconds}" -ne 0 ]; then + cron_compare_seconds=$(( cron_compare_seconds - cron_current_seconds )) fi - local cron_minute=$(echo -n "${2}" | awk '{print $1}') - local cron_hour=$(echo -n "${2}" | awk '{print $2}') - local cron_day_of_month=$(echo -n "${2}" | awk '{print $3}') - local cron_month=$(echo -n "${2}" | awk '{print $4}') - local cron_day_of_week=$(echo -n "${2}" | awk '{print $5}') + local cron_minute="$(echo -n "${2}" | awk '{print $1}')" + local cron_hour="$(echo -n "${2}" | awk '{print $2}')" + local cron_day_of_month="$(echo -n "${2}" | awk '{print $3}')" + local cron_month="$(echo -n "${2}" | awk '{print $4}')" + local cron_day_of_week="$(echo -n "${2}" | awk '{print $5}')" local cron_parsed=1 - local cron_next_minute=$(date --date="@${cron_compare}" +"%-M") - local cron_next_hour=$(date --date="@${cron_compare}" +"%-H") - local cron_next_day_of_month=$(date --date="@${cron_compare}" +"%-d") - local cron_next_month=$(date --date="@${cron_compare}" +"%-m") - local cron_next_day_of_week=$(date --date="@${cron_compare}" +"%-u") - cron_next_day_of_week=$(( ${cron_next_day_of_week} % 7 )) - local cron_next_year=$(date --date="@${cron_compare}" +"%-Y") + local cron_next_minute="$(date --date=@"${cron_compare}" +"%-M")" + local cron_next_hour="$(date --date=@"${cron_compare}" +"%-H")" + local cron_next_day_of_month="$(date --date=@"${cron_compare}" +"%-d")" + local cron_next_month="$(date --date=@"${cron_compare}" +"%-m")" + local cron_next_day_of_week="$(date --date=@"${cron_compare}" +"%-u")" + cron_next_day_of_week=$(( cron_next_day_of_week % 7 )) + local cron_next_year="$(date --date=@"${cron_compare}" +"%-Y")" local cron_next= @@ -1445,7 +1445,7 @@ timer() { cron_next=$(parse_expression "${cron_minute}" 59 "${cron_next_minute}") if [ "${cron_next}" != "${cron_next_minute}" ]; then if [ "${cron_next_minute}" -gt "${cron_next}" ]; then - cron_next_hour=$(( ${cron_next_hour} + 1 )) + cron_next_hour=$(( cron_next_hour + 1 )) fi cron_next_minute="${cron_next}" @@ -1454,7 +1454,7 @@ timer() { cron_next=$(parse_expression "${cron_hour}" 23 "${cron_next_hour}") if [ "${cron_next}" != "${cron_next_hour}" ]; then if [ "${cron_next_hour}" -gt "${cron_next}" ]; then - cron_next_day_of_month=$(( ${cron_next_day_of_month} + 1 )) + cron_next_day_of_month=$(( cron_next_day_of_month + 1 )) fi cron_next_hour="${cron_next}" #cron_next_minute=0 @@ -1465,10 +1465,10 @@ timer() { day_of_week_difference=$(( ${cron_next} - ${cron_next_day_of_week} )) if [ "${day_of_week_difference}" -lt "0" ]; then - day_of_week_difference=$(( ${day_of_week_difference} + 7 )) + day_of_week_difference=$(( day_of_week_difference + 7 )) fi - cron_next_day_of_month=$(( ${cron_next_day_of_month} + ${day_of_week_difference} )) + cron_next_day_of_month=$(( cron_next_day_of_month + day_of_week_difference )) cron_next_hour=0 cron_next_minute=0 fi @@ -1478,9 +1478,9 @@ timer() { last_day_of_month="31" ;; "2") - local divide_by_4=$(( ${cron_next_year} % 4 )) - local divide_by_100=$(( ${cron_next_year} % 100 )) - local divide_by_400=$(( ${cron_next_year} % 400 )) + local divide_by_4=$(( cron_next_year % 4 )) + local divide_by_100=$(( cron_next_year % 100 )) + local divide_by_400=$(( cron_next_year % 400 )) last_day_of_month=28 if [ "${divide_by_4}" = "0" ] && [ "${divide_by_100}" != "0" ]; then last_day_of_month="29" @@ -1502,10 +1502,10 @@ timer() { fi if [ "${cron_next_day_of_month}" -gt "${cron_next}" ] || [ "${cron_next_day_of_month}" -gt "${last_day_of_month}" ]; then - cron_next_month=$(( ${cron_next_month} + 1 )) + cron_next_month=$(( cron_next_month + 1 )) if [ ${cron_next_month} -gt 12 ]; then - cron_next_month=$(( ${cron_next_month} - 12)) - cron_next_year=$(( ${cron_next_year} + 1 )) + cron_next_month=$(( cron_next_month - 12)) + cron_next_year=$(( cron_next_year + 1 )) fi cron_next_day_of_month=1 else @@ -1515,11 +1515,11 @@ timer() { cron_next=$(parse_expression "${cron_month}" 12 "${cron_next_month}") if [ "${cron_next}" != "${cron_next_month}" ]; then if [ "${cron_next}" -gt "12" ]; then - cron_next_year=$(( ${cron_next_year} + 1 )) - cron_next=$(( ${cron_next} - 12 )) + cron_next_year=$(( cron_next_year + 1 )) + cron_next=$(( cron_next - 12 )) fi if [ "${cron_next_month}" -gt "${cron_next}" ]; then - cron_next_year=$(( ${cron_next_year} + 1 )) + cron_next_year=$(( cron_next_year + 1 )) fi cron_next_month="${cron_next}" cron_next_day=1 @@ -1530,15 +1530,15 @@ timer() { done local cron_future=$(date --date="${cron_next_year}-$(printf "%02d" ${cron_next_month})-$(printf "%02d" ${cron_next_day_of_month})T$(printf "%02d" ${cron_next_hour}):$(printf "%02d" ${cron_next_minute}):00" "+%s") - local cron_future_difference=$((${cron_future} - ${cron_compare_seconds})) + local cron_future_difference=$(( cron_future - cron_compare_seconds )) time_cron=true time_wait="${cron_future_difference}" - time_future=${cron_future} + time_future="${cron_future}" ;; datetime) time_begin=$(date -d "${backup_job_backup_begin}" +%s) print_debug "BACKUP_BEGIN time = ${time_begin}" - time_wait=$((time_begin - time_current)) + time_wait=$(( time_begin - time_current )) print_debug "Difference in seconds: ${time_wait}" if (( ${time_wait} < 0 )); then @@ -1547,7 +1547,7 @@ timer() { print_debug "Difference in seconds (rounded) time_wait is in the past : ${time_wait}" fi - time_future=$((${time_current} + ${time_wait})) + time_future=$(( time_current + time_wait )) print_debug "Future execution time = ${time_future}" ;; job) @@ -1557,20 +1557,20 @@ timer() { ;; stop) backup_job_finish_time=$(date +'%s') - backup_job_total_time=$(echo $((backup_job_finish_time-backup_job_start_time))) + backup_job_total_time=$(echo $(( backup_job_finish_time - backup_job_start_time))) ;; esac ;; plusvalue) time_wait=$(( ${BASH_REMATCH[1]} * 60 )) - time_future=$(($time_current} + $time_wait)) + time_future=$(( time_current + time_wait )) ;; time) time_future=$(date --date="$(date +"%Y%m%d") ${backup_job_backup_begin}" +"%s") if [[ "${future_time}" < "${time_current}" ]]; then - time_future=$(($time_future + 24*60*60)) + time_future=$(( time_future + 24*60*60)) fi - time_wait=$((${time_future} - ${time_current})) + time_wait=$(( time_future - time_current )) ;; esac } @@ -1690,7 +1690,7 @@ process_limiter() { if [ "${process_amount}" -ge "${BACKUP_JOB_CONCURRENCY}" ] ; then if [ -z $text_concurrency_limit_initial ] ; then print_notice "Backup concurrency limit reached (${BACKUP_JOB_CONCURRENCY}). Waiting for other tasks to finish before backing up." - text_concurrency_limit_initial=done + text_concurrency_limit_initial=true fi if [[ "${counter}" =~ 45|90|135|180|225|270|315|360|405|450|495|540|585|630|675|720|765|810|855|900|945|990|1035|1080|1125|1170|1215|1260|1305|1350|1395|1440|1485|1530|1575|1620|1665|1710|1755|1800|1845|1890|1935|1980|2025|2070|2115|2160|2205|2250|2295|2340|2385|2430|2475|2520|2565|2610|2655|2700|2745|2790|2835|2880|2925|2970|3015|3060|3105|3150|3195|3240|3285|3330|3375|3420|3465|3510|3555|3600 ]] ; then if [ "${counter}" != 0 ] ; then counter_verbose=" (${counter} seconds so far)" ; fi @@ -1744,7 +1744,7 @@ symlink_log () { if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then local oldpwd=$(pwd) cd "${LOG_PATH}"/"$(date +'%Y%m%d')" - ln -sf $(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log ../latest-"${ltarget}".log + ln -sf "$(date +'%Y%m%d')"/"$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')"-"${ltarget}".log ../latest-"${ltarget}".log cd "${oldpwd}" fi } From 050cc3ef6236389cd88b72ace2394afff3316e1f Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 10:12:32 -0800 Subject: [PATCH 40/68] Cleanup Mongo compression routine --- install/assets/functions/10-db-backup | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 7124bc9..4413f49 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -443,8 +443,7 @@ backup_influx() { backup_mongo() { prepare_dbbackup - ## TODO REMOVE ENABLE_COMPRESSION - if [ "${ENABLE_COMPRESSION,,}" = "none" ] || [ "${ENABLE_COMPRESSION,,}" = "false" ] ; then + if [ "$backup_job_compression,,}" = "none" ] ; then target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} else From a62928209302ea6c2f2d5d53d2c4a51a3481067e Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Sun, 5 Nov 2023 10:42:51 -0800 Subject: [PATCH 41/68] Update dependencies --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 098bc10..8c734fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,10 +6,10 @@ LABEL maintainer="Dave Conroy (github.com/tiredofit)" ### Set Environment Variables ENV INFLUX1_CLIENT_VERSION=1.8.0 \ - INFLUX2_CLIENT_VERSION=2.4.0 \ + INFLUX2_CLIENT_VERSION=2.7.3 \ MSODBC_VERSION=18.3.2.1-1 \ MSSQL_VERSION=18.3.1.1-1 \ - AWS_CLI_VERSION=1.25.97 \ + AWS_CLI_VERSION=1.29.78 \ CONTAINER_ENABLE_MESSAGING=FALSE \ CONTAINER_ENABLE_MONITORING=TRUE \ CONTAINER_PROCESS_RUNAWAY_PROTECTOR=FALSE \ From c9fd36db72bd612563ebeb51453504290f7cb362 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Mon, 6 Nov 2023 11:09:15 -0800 Subject: [PATCH 42/68] Fix DEFAULT_MYSQL table --- Dockerfile | 1 - README.md | 23 +++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8c734fd..eb62348 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,6 @@ ENV INFLUX1_CLIENT_VERSION=1.8.0 \ AWS_CLI_VERSION=1.29.78 \ CONTAINER_ENABLE_MESSAGING=FALSE \ CONTAINER_ENABLE_MONITORING=TRUE \ - CONTAINER_PROCESS_RUNAWAY_PROTECTOR=FALSE \ IMAGE_NAME="tiredofit/db-backup" \ IMAGE_REPO_URL="https://github.com/tiredofit/docker-db-backup/" diff --git a/README.md b/README.md index 269efac..e35e98b 100644 --- a/README.md +++ b/README.md @@ -258,18 +258,17 @@ If these are set and no other defaults or variables are set explicitly, they wil ###### MariaDB/MySQL -| Variable | Description | Default | `_FILE` | -| ---------------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | -| `DEFAULT_PORT` | MySQL / MariaDB Port | `3306` | x | -| `DEFAULT_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | -| `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | -| `DEFAULT_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | -| `DEFAULT_MYSQL_EVENTS` | Backup Events | `TRUE` | | -| `DEFAULT_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet | `512M` | | -| `DEFAULT_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction | `TRUE` | | -| `DEFAULT_MYSQL_STORED_PROCEDURES` | Backup stored procedures | `TRUE` | | -| `DEFAULT_MYSQL_ENABLE_TLS` | Enable TLS functionality | `FALSE` | | - +| Variable | Description | Default | `_FILE` | +| ---------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------- | ------- | +| `DEFAULT_PORT` | MySQL / MariaDB Port | `3306` | x | +| `DEFAULT_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | +| `DEFAULT_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | +| `DEFAULT_MYSQL_EVENTS` | Backup Events | `TRUE` | | +| `DEFAULT_MYSQL_MAX_ALLOWED_PACKET` | Max allowed packet | `512M` | | +| `DEFAULT_MYSQL_SINGLE_TRANSACTION` | Backup in a single transaction | `TRUE` | | +| `DEFAULT_MYSQL_STORED_PROCEDURES` | Backup stored procedures | `TRUE` | | +| `DEFAULT_MYSQL_ENABLE_TLS` | Enable TLS functionality | `FALSE` | | | `DEFAULT_MYSQL_TLS_VERIFY` | (optional) If using TLS (by means of MYSQL_TLS_* variables) verify remote host | `FALSE` | | | `DEFAULT_MYSQL_TLS_VERSION` | What TLS `v1.1` `v1.2` `v1.3` version to utilize | `TLSv1.1,TLSv1.2,TLSv1.3` | | | `DEFAULT_MYSQL_TLS_CA_FILE` | Filename to load custom CA certificate for connecting via TLS | `/etc/ssl/cert.pem` | x | From c94a85b52346d6ed7e3c9871ed6f0718b84833de Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Mon, 6 Nov 2023 16:53:20 -0800 Subject: [PATCH 43/68] Change Zabbix autoregister string to dbbackup4 --- install/etc/cont-init.d/10-db-backup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/etc/cont-init.d/10-db-backup b/install/etc/cont-init.d/10-db-backup index 546eae5..23c855b 100755 --- a/install/etc/cont-init.d/10-db-backup +++ b/install/etc/cont-init.d/10-db-backup @@ -9,6 +9,6 @@ output_off setup_mode db_backup_container_init create_schedulers backup -create_zabbix dbbackup +create_zabbix dbbackup4 liftoff From 72f90876e3431593336cb3199c26b73fe74af4e7 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Mon, 6 Nov 2023 17:00:29 -0800 Subject: [PATCH 44/68] Fix writing logfiles to filessytem --- install/assets/functions/10-db-backup | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 4413f49..5add9f4 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1761,10 +1761,10 @@ write_log() { write_to_file=false fi - case "${1}" in + case "${_arg_log_level,,}" in debug ) CONTAINER_LOG_LEVEL=DEBUG - case "${backup_job_log_level,,}" in + case "${_arg_log_level,,}" in "debug" ) print_debug "${_arg_log_message}" if var_true "${write_to_file}" ; then @@ -1776,7 +1776,7 @@ write_log() { ;; error ) CONTAINER_LOG_LEVEL=ERROR - case "${backup_job_log_level,,}" in + case "${_arg_log_level,,}" in "debug" | "notice" | "warn" | "error") print_error "${_arg_log_message}" if var_true "${write_to_file}" ; then @@ -1794,7 +1794,7 @@ write_log() { ;; notice ) CONTAINER_LOG_LEVEL=NOTICE - case "${backup_job_log_level,,}" in + case "${_arg_log_level,,}" in "debug" | "notice" ) print_notice "${_arg_log_message}" if var_true "${write_to_file}" ; then @@ -1805,7 +1805,7 @@ write_log() { ;; warn ) CONTAINER_LOG_LEVEL=WARN - case "${backup_job_log_level,,}" in + case "${_arg_log_level,,}" in "debug" | "notice" | "warn" ) print_warn "${_arg_log_message}" if var_true "${write_to_file}" ; then From bb3c942b35b9dab717cdda3ab0d625d6cc1bb3c8 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 11:02:49 -0800 Subject: [PATCH 45/68] Properly output exit code for notifications --- install/assets/functions/10-db-backup | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 5add9f4..3dcfd02 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -832,7 +832,7 @@ check_exit_code() { notify \ "$(date -d @"${backup_job_start_time}" +'%Y%m%d_%H%M%S')" \ "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ - "{exit_code}" \ + "${exit_code}" \ "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed completely" \ "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." master_exit_code=1 @@ -849,7 +849,7 @@ check_exit_code() { notify \ "$(date -d @"${backup_job_start_time}" +'%Y%m%d_%H%M%S')" \ "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ - "{exit_code}" \ + "${move_exit_code}" \ "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed to move to destination" \ "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." master_exit_code=1 From 9863358469ccdbf13f844f782b90bb7cfb5b4932 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 11:05:39 -0800 Subject: [PATCH 46/68] Split debug statement for backup/move exit codes --- install/assets/functions/10-db-backup | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 3dcfd02..2494895 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -820,9 +820,9 @@ check_availability() { } check_exit_code() { - write_log debug "DB Backup Exit Code is ${exit_code}" case "${1}" in backup ) + write_log debug "DB Backup exit Code is ${exit_code}" case "${exit_code}" in 0 ) write_log info "DB Backup of '${2}' completed successfully" @@ -840,6 +840,7 @@ check_exit_code() { esac ;; move ) + write_log debug "Move exit Code is ${exit_code}" case "${move_exit_code}" in 0 ) write_log debug "Moving of backup '${2}' completed successfully" From 57193cc824a626c544608ba131e937b54e5188af Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 11:50:59 -0800 Subject: [PATCH 47/68] Properly translate legacy _DUMP var to DEFAULT_BACKUP --- install/assets/functions/10-db-backup | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 2494895..544447c 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -118,12 +118,12 @@ bootstrap_variables() { if [ -n "${DB_DUMP_BEGIN}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_BEGIN' detected - Please upgrade your variables as they will be removed in version 4.3.0" - DEFAULT_BACKUP_BEGIN=${DB_BACKUP_BEGIN} + DEFAULT_BACKUP_BEGIN=${DB_DUMP_BEGIN} fi if [ -n "${DB_DUMP_FREQ}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_FREQ' detected - Please upgrade your variables as they will be removed in version 4.3.0" - DEFAULT_BACKUP_INTERVAL=${DB_BACKUP_INTERVAL} + DEFAULT_BACKUP_INTERVAL=${DB_DUMP_INTERVAL} fi if [ -n "${DB_DUMP_TARGET}" ]; then From 11205be091a44dbb4a2590bc0c3152f01d076a39 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 12:00:19 -0800 Subject: [PATCH 48/68] Properly handle BACKUP_BEGIN plus values --- install/assets/dbbackup/template-dbbackup/run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/assets/dbbackup/template-dbbackup/run b/install/assets/dbbackup/template-dbbackup/run index d4f6ee8..e468c84 100755 --- a/install/assets/dbbackup/template-dbbackup/run +++ b/install/assets/dbbackup/template-dbbackup/run @@ -24,7 +24,7 @@ else if [[ "${backup_job_backup_begin}" =~ ^\+(.*)$ ]]; then print_debug "BACKUP_BEGIN is a jump of minute starting with +" - timer plus_value + timer plusvalue elif [[ "${backup_job_backup_begin}" =~ ^[0-9]{4}$ ]]; then print_debug "BACKUP_BEGIN is a HHMM value" timer time From 2ccc867a754f228041f38435639f4bcfcf6f63ba Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 12:13:04 -0800 Subject: [PATCH 49/68] Write Legacy vars to file instead --- install/assets/functions/10-db-backup | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 544447c..638af0d 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -112,33 +112,33 @@ bootstrap_variables() { if [ -n "${ENABLE_CHECKSUM}" ]; then print_warn "Deprecated and unsupported variable 'ENABLE_CHECKSUM' detected - Please upgrade your variables as they will be removed in version 4.3.0" if var_false "${ENABLE_CHECKSUM}" ; then - DEFAULT_CHECKSUM=NONE + echo "DEFAULT_CHECKSUM=NONE" >> "${backup_instance_vars}" fi fi if [ -n "${DB_DUMP_BEGIN}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_BEGIN' detected - Please upgrade your variables as they will be removed in version 4.3.0" - DEFAULT_BACKUP_BEGIN=${DB_DUMP_BEGIN} + echo "DEFAULT_BACKUP_BEGIN=${DB_DUMP_BEGIN}" >> "${backup_instance_vars}" fi if [ -n "${DB_DUMP_FREQ}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_FREQ' detected - Please upgrade your variables as they will be removed in version 4.3.0" - DEFAULT_BACKUP_INTERVAL=${DB_DUMP_INTERVAL} + echo "DEFAULT_BACKUP_INTERVAL=${DB_DUMP_INTERVAL}" >> "${backup_instance_vars}" fi if [ -n "${DB_DUMP_TARGET}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET' detected - Please upgrade your variables as they will be removed in version 4.3.0" - DEFAULT_FILESYSTEM_PATH="${DB_DUMP_TARGET}" + echo "DEFAULT_FILESYSTEM_PATH=${DB_DUMP_TARGET}" >> "${backup_instance_vars}" fi if [ -n "${DB_DUMP_TARGET_ARCHIVE}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET_ACRHIVE' detected - Please upgrade your variables as they will be removed in version 4.3.0" - DEFAULT_FILESYSTEM_ARCHIVE_PATH="${DB_DUMP_TARGET_ARCHIVE}" + echo "DEFAULT_FILESYSTEM_ARCHIVE_PATH=${DB_DUMP_TARGET_ARCHIVE}" >> "${backup_instance_vars}" fi if [ -n "${EXTRA_DUMP_OPTS}" ]; then print_warn "Deprecated and unsupported variable 'EXTRA_DUMP_OPTS' detected - Please upgrade your variables as they will be removed in version 4.3.0" - DEFAULT_EXTRA_BACKUP_OPTS="${EXTRA_DUMP_OPTS}" + echo "DEFAULT_EXTRA_BACKUP_OPTS=${EXTRA_DUMP_OPTS}" >> "${backup_instance_vars}" fi ## @@ -159,8 +159,10 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" ARCHIVE_TIME backup_job_archive_time transform_backup_instance_variable "${backup_instance_number}" AUTH backup_job_db_auth + set -x transform_backup_instance_variable "${backup_instance_number}" BACKUP_BEGIN backup_job_backup_begin transform_backup_instance_variable "${backup_instance_number}" BACKUP_INTERVAL backup_job_backup_interval + set +x transform_backup_instance_variable "${backup_instance_number}" BACKUP_LOCATION backup_job_backup_location transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_BEGIN backup_job_snapshot_blackout_start transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_END backup_job_snapshot_blackout_finish From b8a825a3afe35b2ee3cb881e11da701bde05cd1c Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 12:33:03 -0800 Subject: [PATCH 50/68] Rework write_log to not output more than it needs to --- install/assets/functions/10-db-backup | 104 +++++++++++--------------- 1 file changed, 43 insertions(+), 61 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 638af0d..a30ac71 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -112,32 +112,38 @@ bootstrap_variables() { if [ -n "${ENABLE_CHECKSUM}" ]; then print_warn "Deprecated and unsupported variable 'ENABLE_CHECKSUM' detected - Please upgrade your variables as they will be removed in version 4.3.0" if var_false "${ENABLE_CHECKSUM}" ; then + sed -i "/DEFAULT_CHECKSUM=/d" "${backup_instance_vars}" echo "DEFAULT_CHECKSUM=NONE" >> "${backup_instance_vars}" fi fi if [ -n "${DB_DUMP_BEGIN}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_BEGIN' detected - Please upgrade your variables as they will be removed in version 4.3.0" + sed -i "/DEFAULT_BACKUP_BEGIN=/d" "${backup_instance_vars}" echo "DEFAULT_BACKUP_BEGIN=${DB_DUMP_BEGIN}" >> "${backup_instance_vars}" fi if [ -n "${DB_DUMP_FREQ}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_FREQ' detected - Please upgrade your variables as they will be removed in version 4.3.0" - echo "DEFAULT_BACKUP_INTERVAL=${DB_DUMP_INTERVAL}" >> "${backup_instance_vars}" + sed -i "/DEFAULT_BACKUP_INTERVAL=/d" "${backup_instance_vars}" + echo "DEFAULT_BACKUP_INTERVAL=${DB_DUMP_FREQ}" >> "${backup_instance_vars}" fi if [ -n "${DB_DUMP_TARGET}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET' detected - Please upgrade your variables as they will be removed in version 4.3.0" + sed -i "/DEFAULT_FILESYSTEM_PATH=/d" "${backup_instance_vars}" echo "DEFAULT_FILESYSTEM_PATH=${DB_DUMP_TARGET}" >> "${backup_instance_vars}" fi if [ -n "${DB_DUMP_TARGET_ARCHIVE}" ]; then print_warn "Deprecated and unsupported variable 'DB_DUMP_TARGET_ACRHIVE' detected - Please upgrade your variables as they will be removed in version 4.3.0" + sed -i "/DEFAULT_FILESYSTEM_ARCHIVE_PATH=/d" "${backup_instance_vars}" echo "DEFAULT_FILESYSTEM_ARCHIVE_PATH=${DB_DUMP_TARGET_ARCHIVE}" >> "${backup_instance_vars}" fi if [ -n "${EXTRA_DUMP_OPTS}" ]; then print_warn "Deprecated and unsupported variable 'EXTRA_DUMP_OPTS' detected - Please upgrade your variables as they will be removed in version 4.3.0" + sed -i "/DEFAULT_EXTRA_BACKUP_OPTS=/d" "${backup_instance_vars}" echo "DEFAULT_EXTRA_BACKUP_OPTS=${EXTRA_DUMP_OPTS}" >> "${backup_instance_vars}" fi ## @@ -159,10 +165,8 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" ARCHIVE_TIME backup_job_archive_time transform_backup_instance_variable "${backup_instance_number}" AUTH backup_job_db_auth - set -x transform_backup_instance_variable "${backup_instance_number}" BACKUP_BEGIN backup_job_backup_begin transform_backup_instance_variable "${backup_instance_number}" BACKUP_INTERVAL backup_job_backup_interval - set +x transform_backup_instance_variable "${backup_instance_number}" BACKUP_LOCATION backup_job_backup_location transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_BEGIN backup_job_snapshot_blackout_start transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_END backup_job_snapshot_blackout_finish @@ -229,6 +233,7 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" USER backup_job_db_user backup_job_backup_begin=$(echo "${backup_job_backup_begin}" | sed -e "s|'||g" -e 's|"||g') + rm -rf "${backup_instance_vars}" } upgrade_lonely_variables() { @@ -1753,70 +1758,47 @@ symlink_log () { write_log() { output_off - local CONTAINER_LOG_LEVEL_ORIGINAL=${CONTAINER_LOG_LEVEL} local _arg_log_level=${1} shift 1 local _arg_log_message="$@" if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - write_to_file=true - else - write_to_file=false + case "${_arg_log_level,,}" in + debug ) + case "${_arg_log_level,,}" in + "debug" ) + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + ;; + esac + ;; + error ) + case "${_arg_log_level,,}" in + "debug" | "notice" | "warn" | "error") + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + ;; + esac + ;; + info ) + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + ;; + notice ) + case "${_arg_log_level,,}" in + "debug" | "notice" ) + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + ;; + esac + ;; + warn ) + case "${_arg_log_level,,}" in + "debug" | "notice" | "warn" ) + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + ;; + esac + ;; + esac + fi - case "${_arg_log_level,,}" in - debug ) - CONTAINER_LOG_LEVEL=DEBUG - case "${_arg_log_level,,}" in - "debug" ) - print_debug "${_arg_log_message}" - if var_true "${write_to_file}" ; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null - fi - ;; - esac - CONTAINER_LOG_LEVEL=${CONTAINER_LOG_LEVEL_ORIGINAL} - ;; - error ) - CONTAINER_LOG_LEVEL=ERROR - case "${_arg_log_level,,}" in - "debug" | "notice" | "warn" | "error") - print_error "${_arg_log_message}" - if var_true "${write_to_file}" ; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null - fi - ;; - esac - ;; - info ) - CONTAINER_LOG_LEVEL=INFO - print_info "${_arg_log_message}" - if var_true "${write_to_file}" ; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null - fi - ;; - notice ) - CONTAINER_LOG_LEVEL=NOTICE - case "${_arg_log_level,,}" in - "debug" | "notice" ) - print_notice "${_arg_log_message}" - if var_true "${write_to_file}" ; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null - fi - ;; - esac - ;; - warn ) - CONTAINER_LOG_LEVEL=WARN - case "${_arg_log_level,,}" in - "debug" | "notice" | "warn" ) - print_warn "${_arg_log_message}" - if var_true "${write_to_file}" ; then - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null - fi - ;; - esac - ;; - esac + print_${_arg_log_level} "${_arg_log_message}" output_on } \ No newline at end of file From 50039f8d0ca92738bdbd7660268dec33225d1156 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 13:03:01 -0800 Subject: [PATCH 51/68] Rearrange timer() --- install/assets/functions/10-db-backup | 356 +++++++++++++------------- 1 file changed, 175 insertions(+), 181 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index a30ac71..f47b38a 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1353,6 +1353,180 @@ move_dbbackup() { run_as_user rm -rf "${TEMP_PATH}"/"${target}" } +prepare_dbbackup() { + timer backup start + now=$(run_as_user date +"%Y%m%d-%H%M%S") + now_date=$(run_as_user date +"%Y-%m-%d") + now_time=$(run_as_user date +"%H:%M:%S") + ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} + target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.sql +} + +pre_dbbackup() { + ### Pre Script Support + if [ -n "${backup_job_pre_script}" ] ; then + if var_true "${backup_job_pre_script_x_verify}" ; then + run_as_user eval "${backup_job_pre_script}" "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" + else + if [ -x "${backup_job_pre_script}" ] ; then + write_log notice "Found PRE_SCRIPT environment variable. Executing '${backup_job_pre_script}" + run_as_user eval "${backup_job_pre_script}" "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" + else + write_log error "Can't execute PRE_SCRIPT environment variable '${backup_job_pre_script}' as its filesystem bit is not executible!" + fi + fi + fi + + ### Pre Backup Custom Script Support + if [ -d "/assets/custom-scripts/pre" ] && dir_notempty "/assets/custom-scripts/pre" ; then + write_log warning "Found Custom Post Scripts in /assets/custom-scripts/pre - Automatically moving them to '${backup_job_script_location_pre}'" + run_as_user mkdir -p "${backup_job_script_location_pre}" + silent run_as_user cp /assets/custom-scripts/pre/* "${backup_job_script_location_pre}" + fi + + if [ -d "${backup_job_script_location_pre}" ] && dir_notempty "${backup_job_script_location_pre}" ; then + for f in $(find ${backup_job_script_location_pre} -name \*.sh -type f); do + if var_true "${backup_job_pre_script_x_verify}" ; then + run_as_user ${f} "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" + else + if [ -x "${f}" ] ; then + write_log notice "Executing pre backup custom script : '${f}'" + ## script DB_TYPE DB_HOST DB_NAME STARTEPOCH BACKUP_FILENAME + run_as_user ${f} "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" + else + write_log error "Can't run pre backup custom script: '${f}' as its filesystem bit is not executible!" + fi + fi + done + fi +} + +post_dbbackup() { + dbbackup_finish_time=$(run_as_user date +"%s") + dbbackup_total_time=$(run_as_user echo $((dbbackup_finish_time-dbbackup_start_time))) + + if var_true "${CONTAINER_ENABLE_MONITORING}" && [ "${CONTAINER_MONITORING_BACKEND,,}" = "zabbix" ]; then + source /assets/defaults/03-monitoring + write_log notice "Sending Backup Statistics to Zabbix" + silent zabbix_sender -c -c "${ZABBIX_CONFIG_PATH}"/"${ZABBIX_CONFIG_FILE}" -k dbbackup.backup -o '[{"{#NAME}":"'${backup_job_db_host}.${backup_job_db_name}'"}]' + cat < /etc/services.d/99-run_forever/run +#!/bin/bash +while true; do + sleep 86400 +done +EOF + chmod +x /etc/services.d/99-run_forever/run + else + if var_true "${CONTAINER_ENABLE_SCHEDULING}" ; then + write_log error "Manual / Exit after execution mode doesn't work with 'CONTAINER_ENABLE_SCHEDULING=TRUE'" + exit 1 + fi + if var_true "${CONTAINER_ENABLE_MONITORING}" ; then + write_log error "Manual / Exit after execution mode doesn't work with 'CONTAINER_ENABLE_MONITORING=TRUE'" + exit 1 + fi + if var_true "${CONTAINER_ENABLE_LOGSHIPPING}" ; then + write_log error "Manual / Exit after execution mode doesn't work with 'CONTAINER_ENABLE_LOGSHIPPING=TRUE'" + exit 1 + fi + fi + fi +} + +symlink_log () { + if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then + local oldpwd=$(pwd) + cd "${LOG_PATH}"/"$(date +'%Y%m%d')" + ln -sf "$(date +'%Y%m%d')"/"$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')"-"${ltarget}".log ../latest-"${ltarget}".log + cd "${oldpwd}" + fi +} + timer() { case "${1}" in backup) @@ -1582,180 +1756,6 @@ timer() { esac } -prepare_dbbackup() { - timer backup start - now=$(run_as_user date +"%Y%m%d-%H%M%S") - now_date=$(run_as_user date +"%Y-%m-%d") - now_time=$(run_as_user date +"%H:%M:%S") - ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} - target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.sql -} - -pre_dbbackup() { - ### Pre Script Support - if [ -n "${backup_job_pre_script}" ] ; then - if var_true "${backup_job_pre_script_x_verify}" ; then - run_as_user eval "${backup_job_pre_script}" "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" - else - if [ -x "${backup_job_pre_script}" ] ; then - write_log notice "Found PRE_SCRIPT environment variable. Executing '${backup_job_pre_script}" - run_as_user eval "${backup_job_pre_script}" "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" - else - write_log error "Can't execute PRE_SCRIPT environment variable '${backup_job_pre_script}' as its filesystem bit is not executible!" - fi - fi - fi - - ### Pre Backup Custom Script Support - if [ -d "/assets/custom-scripts/pre" ] && dir_notempty "/assets/custom-scripts/pre" ; then - write_log warning "Found Custom Post Scripts in /assets/custom-scripts/pre - Automatically moving them to '${backup_job_script_location_pre}'" - run_as_user mkdir -p "${backup_job_script_location_pre}" - silent run_as_user cp /assets/custom-scripts/pre/* "${backup_job_script_location_pre}" - fi - - if [ -d "${backup_job_script_location_pre}" ] && dir_notempty "${backup_job_script_location_pre}" ; then - for f in $(find ${backup_job_script_location_pre} -name \*.sh -type f); do - if var_true "${backup_job_pre_script_x_verify}" ; then - run_as_user ${f} "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" - else - if [ -x "${f}" ] ; then - write_log notice "Executing pre backup custom script : '${f}'" - ## script DB_TYPE DB_HOST DB_NAME STARTEPOCH BACKUP_FILENAME - run_as_user ${f} "${dbtype}" "${backup_job_db_host}" "${1}" "${dbbackup_start_time}" "${target}" - else - write_log error "Can't run pre backup custom script: '${f}' as its filesystem bit is not executible!" - fi - fi - done - fi -} - -post_dbbackup() { - dbbackup_finish_time=$(run_as_user date +"%s") - dbbackup_total_time=$(run_as_user echo $((dbbackup_finish_time-dbbackup_start_time))) - - if var_true "${CONTAINER_ENABLE_MONITORING}" && [ "${CONTAINER_MONITORING_BACKEND,,}" = "zabbix" ]; then - source /assets/defaults/03-monitoring - write_log notice "Sending Backup Statistics to Zabbix" - silent zabbix_sender -c -c "${ZABBIX_CONFIG_PATH}"/"${ZABBIX_CONFIG_FILE}" -k dbbackup.backup -o '[{"{#NAME}":"'${backup_job_db_host}.${backup_job_db_name}'"}]' - cat < /etc/services.d/99-run_forever/run -#!/bin/bash -while true; do - sleep 86400 -done -EOF - chmod +x /etc/services.d/99-run_forever/run - else - if var_true "${CONTAINER_ENABLE_SCHEDULING}" ; then - write_log error "Manual / Exit after execution mode doesn't work with 'CONTAINER_ENABLE_SCHEDULING=TRUE'" - exit 1 - fi - if var_true "${CONTAINER_ENABLE_MONITORING}" ; then - write_log error "Manual / Exit after execution mode doesn't work with 'CONTAINER_ENABLE_MONITORING=TRUE'" - exit 1 - fi - if var_true "${CONTAINER_ENABLE_LOGSHIPPING}" ; then - write_log error "Manual / Exit after execution mode doesn't work with 'CONTAINER_ENABLE_LOGSHIPPING=TRUE'" - exit 1 - fi - fi - fi -} - -symlink_log () { - if [ -n "${backup_job_db_type}" ] && [ -n "${backup_job_db_name}" ] && [ -n "${backup_job_db_host}" ] && [ -n "${ltarget}" ]; then - local oldpwd=$(pwd) - cd "${LOG_PATH}"/"$(date +'%Y%m%d')" - ln -sf "$(date +'%Y%m%d')"/"$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')"-"${ltarget}".log ../latest-"${ltarget}".log - cd "${oldpwd}" - fi -} - write_log() { output_off local _arg_log_level=${1} @@ -1776,13 +1776,7 @@ write_log() { "debug" | "notice" | "warn" | "error") echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null ;; - esac - ;; - info ) - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null - ;; - notice ) - case "${_arg_log_level,,}" in + esacexterna "debug" | "notice" ) echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null ;; From f5b92854dadcce26c48d6f722805f9f1bd21bd16 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Tue, 7 Nov 2023 13:06:06 -0800 Subject: [PATCH 52/68] Cleanup cron expression --- install/assets/functions/10-db-backup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index f47b38a..d2e21f7 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1571,7 +1571,7 @@ timer() { if [ "${expression_step}" != "${expression}" ]; then for step in ${validate_temp}; do - if [ $(( (${step} - ${expression_start}) % ${expression_step} )) -eq 0 ]; then + if [ $( (( step - expression_start ) % expression_step )) -eq 0 ]; then validate_all="$validate_all ${step}" fi done From 633cc8410ec5c620c8adf6166a825b435db67907 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 07:43:37 -0800 Subject: [PATCH 53/68] Fix cron expression stepping --- install/assets/functions/10-db-backup | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index d2e21f7..30de02f 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1557,8 +1557,10 @@ timer() { expression_step=${expression##*\/} expression_number=${expression%%\/*} validate_temp="" + local expression_start= local expression_end= + if [ "${expression_number}" = "#" ]; then expression_start=0 expression_end="${2}" @@ -1571,7 +1573,7 @@ timer() { if [ "${expression_step}" != "${expression}" ]; then for step in ${validate_temp}; do - if [ $( (( step - expression_start ) % expression_step )) -eq 0 ]; then + if [ $(( ( step - expression_start ) % expression_step )) -eq 0 ]; then validate_all="$validate_all ${step}" fi done @@ -1580,7 +1582,7 @@ timer() { fi done - validate_all=$(echo $validate_all | tr ' ' '\n' | sort -n -u | tr '\n' ' ') + validate_all=$(echo "${validate_all}" | tr ' ' '\n' | sort -n -u | tr '\n' ' ') for entry in $validate_all; do if [ "${entry}" -ge "${3}" ]; then echo "${entry}" From 77f54d06fa568a007cf372d23944e5c0466c53ea Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 08:00:21 -0800 Subject: [PATCH 54/68] Rearrange variables and polish: --- install/assets/functions/10-db-backup | 314 +++++++++++++------------- 1 file changed, 160 insertions(+), 154 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 30de02f..335d8f1 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -48,7 +48,7 @@ bootstrap_variables() { DEFAULT_ENCRYPT_PUBKEY \ DEFAULT_MONGO_CUSTOM_URI \ DEFAULT_MYSQL_TLS_CA_FILE \ - DEFAULT_MYSQL_TLS_CERT_FILE \ + DEFAULT_MYSQL_TLS_backup_job_filenameCERT_FILE \ DEFAULT_MYSQL_TLS_KEY_FILE \ DEFAULT_S3_BUCKET \ DEFAULT_S3_KEY_ID \ @@ -271,8 +271,8 @@ bootstrap_variables() { couch* ) dbtype=couch backup_job_backup_job_db_port=${backup_job_db_port:-5984} - check_var backup_job_db_user DB$"{v_instance}"_USER "database username" - check_var backup_job_db_pass DB$"{v_instance}"_PASS "database password" + check_var backup_job_db_user DB"${v_instance}"_USER "database username" + check_var backup_job_db_pass DB"${v_instance}"_PASS "database password" ;; influx* ) dbtype=influx @@ -345,7 +345,7 @@ bootstrap_variables() { dbtype=pgsql backup_job_db_port=${backup_job_db_port:-5432} [[ ( -n "${backup_job_db_pass}" ) ]] && POSTGRES_PASS_STR="PGPASSWORD=${backup_job_db_pass}" - check_var backup_job_db_name DB${v_instance}_NAME "database name. Seperate multiple with commas" + check_var backup_job_db_name DB"${v_instance}"_NAME "database name. Seperate multiple with commas" ;; "redis" ) dbtype=redis @@ -373,19 +373,19 @@ bootstrap_variables() { backup_couch() { prepare_dbbackup - target=couch_${backup_job_db_name}_${backup_job_db_host#*//}_${now}.txt - ltarget=couch_${backup_job_db_name}_${backup_job_db_host#*//} + backup_job_filename=couch_${backup_job_db_name}_${backup_job_db_host#*//}_${now}.txt + backup_job_filename_base=couch_${backup_job_db_name}_${backup_job_db_host#*//} compression pre_dbbackup ${backup_job_db_name} write_log notice "Dumping CouchDB database: '${backup_job_db_name}' ${compression_string}" - run_as_user curl -sSL -X GET ${backup_job_db_host}:${backup_job_db_port}/${backup_job_db_name}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user curl -sSL -X GET ${backup_job_db_host}:${backup_job_db_port}/${backup_job_db_name}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? - check_exit_code backup $target + check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup ${backup_job_db_name} } @@ -402,47 +402,47 @@ backup_influx() { for db in ${db_names}; do prepare_dbbackup if [ "${db}" != "justbackupeverything" ] ; then bucket="-db ${db}" ; else db=all ; fi - target=influx_${db}_${backup_job_db_host#*//}_${now} - ltarget=influx_${db}_${backup_job_db_host#*//} + backup_job_filename=influx_${db}_${backup_job_db_host#*//}_${now} + backup_job_filename_base=influx_${db}_${backup_job_db_host#*//} compression - pre_dbbackup $db + pre_dbbackup "${db}" write_log notice "Dumping Influx database: '${db}'" - run_as_user influxd backup ${influx_compression} ${bucket} -portable -host ${backup_job_db_host}:${backup_job_db_port} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} "${TEMP_PATH}"/"${target_dir}" + run_as_user influxd backup ${influx_compression} ${bucket} -portable -host ${backup_job_db_host}:${backup_job_db_port} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} "${TEMP_PATH}"/"${backup_job_filename_dir}" exit_code=$? - check_exit_code backup $target_dir - write_log notice "Creating archive file of '${target_dir}' with tar ${compression_string}" - run_as_user tar cf - "${TEMP_PATH}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target_dir}".tar"${extension}" > /dev/null - target=influx_${db}_${backup_job_db_host#*//}_${now}.tar${extension} - ltarget=influx_${db}_${backup_job_db_host#*//} + check_exit_code backup "${backup_job_filename_dir}" + write_log notice "Creating archive file of '${backup_job_filename_dir}' with tar ${compression_string}" + run_as_user tar cf - "${TEMP_PATH}"/"${backup_job_filename_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename_dir}".tar"${extension}" > /dev/null + backup_job_filename=influx_${db}_${backup_job_db_host#*//}_${now}.tar${extension} + backup_job_filename_base=influx_${db}_${backup_job_db_host#*//} timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target_dir - post_dbbackup $db + check_exit_code move "${backup_job_filename_dir}" + post_dbbackup "${db}" done ;; 2 ) for db in ${db_names}; do prepare_dbbackup if [ "${db}" != "justbackupeverything" ] ; then bucket="--bucket $db" ; else db=all ; fi - target=influx2_${db}_${backup_job_db_host#*//}_${now} - ltarget=influx2_${db}_${backup_job_db_host#*//} + backup_job_filename=influx2_${db}_${backup_job_db_host#*//}_${now} + backup_job_filename_base=influx2_${db}_${backup_job_db_host#*//} compression - pre_dbbackup $db + pre_dbbackup "${db}" write_log notice "Dumping Influx2 database: '${db}'" - run_as_user influx backup --org ${backup_job_db_user} ${bucket} --host ${backup_job_db_host}:${backup_job_db_port} --token ${backup_job_db_pass} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --compression none "${TEMP_PATH}"/"${target_dir}" + run_as_user influx backup --org ${backup_job_db_user} ${bucket} --host ${backup_job_db_host}:${backup_job_db_port} --token ${backup_job_db_pass} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --compression none "${TEMP_PATH}"/"${backup_job_filename_dir}" exit_code=$? - check_exit_code backup $target_dir + check_exit_code backup "${backup_job_filename_dir}" create_archive - target=influx2_${db}_${backup_job_db_host#*//}_${now}.tar${extension} - ltarget=influx2_${db}_${backup_job_db_host#*//} + backup_job_filename=influx2_${db}_${backup_job_db_host#*//}_${now}.tar${extension} + backup_job_filename_base=influx2_${db}_${backup_job_db_host#*//} timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target_dir - post_dbbackup $db + check_exit_code move "${backup_job_filename_dir}" + post_dbbackup "${db}" done ;; esac @@ -450,12 +450,12 @@ backup_influx() { backup_mongo() { prepare_dbbackup - if [ "$backup_job_compression,,}" = "none" ] ; then - target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive - ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} + if [ "${backup_job_compression,,}" = "none" ] ; then + backup_job_filename=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive + backup_job_filename_base=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} else - target=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive.gz - ltarget=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} + backup_job_filename=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive.gz + backup_job_filename_base=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} mongo_compression="--gzip" compression_string="and compressing with gzip" fi @@ -466,14 +466,14 @@ backup_mongo() { fi pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" - silent run_as_user ${play_fair} mongodump --archive=${TEMP_PATH}/${target} ${mongo_compression} ${mongo_backup_parameter} + silent run_as_user ${play_fair} mongodump --archive=${TEMP_PATH}/${backup_job_filename} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? - check_exit_code backup $target + check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup "${backup_job_db_name}" } @@ -481,42 +481,42 @@ backup_mssql() { case "${backup_job_mssql_mode,,}" in db|database ) prepare_dbbackup - target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.bak - ltarget=mssql_${backup_job_db_name,,}_${backup_job_db_host,,} + backup_job_filename=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.bak + backup_job_filename_base=mssql_${backup_job_db_name,,}_${backup_job_db_host,,} pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${backup_job_filename}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? - target_original=${target} + backup_job_filename_original=${backup_job_filename} compression pre_dbbackup all - run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" - check_exit_code backup $target + run_as_user ${compress_cmd} "${TEMP_PATH}/${backup_job_filename_original}" + check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup "${backup_job_db_name}" ;; trn|transaction ) prepare_dbbackup - target=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.trn - ltarget=mssql_${backup_job_db_name,,}_trn_${backup_job_db_host,,} + backup_job_filename=mssql_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.trn + backup_job_filename_base=mssql_${backup_job_db_name,,}_trn_${backup_job_db_host,,} pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" - silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${target}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" + silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${backup_job_filename}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? - target_original=${target} + backup_job_filename_original=${backup_job_filename} compression pre_dbbackup all - run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" - check_exit_code backup $target + run_as_user ${compress_cmd} "${TEMP_PATH}/${backup_job_filename_original}" + check_exit_code backup "${backup_job_filename}" file_encryption timer backup finish generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup "${backup_job_db_name}" ;; esac @@ -552,37 +552,37 @@ backup_mysql() { if var_true "${backup_job_split_db}" ; then for db in ${db_names} ; do prepare_dbbackup - target=mysql_${db}_${backup_job_db_host,,}_${now}.sql - ltarget=mysql_${db}_${backup_job_db_host,,} + backup_job_filename=mysql_${db}_${backup_job_db_host,,}_${now}.sql + backup_job_filename_base=mysql_${db}_${backup_job_db_host,,} compression - pre_dbbackup $db + pre_dbbackup "${db}" write_log notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" - run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? - check_exit_code backup $target + check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target - post_dbbackup $db + check_exit_code move "${backup_job_filename}" + post_dbbackup "${db}" done else write_log debug "Not splitting database dumps into their own files" prepare_dbbackup - target=mysql_all_${backup_job_db_host,,}_${now}.sql - ltarget=mysql_all_${backup_job_db_host,,} + backup_job_filename=mysql_all_${backup_job_db_host,,}_${now}.sql + backup_job_filename_base=mysql_all_${backup_job_db_host,,} compression pre_dbbackup all write_log notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" - run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? - check_exit_code backup $target + check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup all fi } @@ -613,40 +613,40 @@ backup_pgsql() { if var_true "${backup_job_split_db}" ; then for db in ${db_names} ; do prepare_dbbackup - target=pgsql_${db}_${backup_job_db_host,,}_${now}.sql - ltarget=pgsql_${db}_${backup_job_db_host,,} + backup_job_filename=pgsql_${db}_${backup_job_db_host,,}_${now}.sql + backup_job_filename_base=pgsql_${db}_${backup_job_db_host,,} compression - pre_dbbackup $db + pre_dbbackup "${db}" write_log notice "Dumping PostgresSQL database: '${db}' ${compression_string}" - run_as_user ${play_fair} pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? - check_exit_code backup $target + check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target - post_dbbackup $db + check_exit_code move "${backup_job_filename}" + post_dbbackup "${db}" done prepare_dbbackup - target=pgsql_globals_${backup_job_db_host,,}_${now}.sql + backup_job_filename=pgsql_globals_${backup_job_db_host,,}_${now}.sql compression pre_dbbackup "globals" print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" - run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? - check_exit_code $target + check_exit_code $backup_job_filename timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup "globals" else write_log debug "Not splitting database dumps into their own files" prepare_dbbackup - target=pgsql_all_${backup_job_db_host,,}_${now}.sql - ltarget=pgsql_${db}_${backup_job_db_host,,} + backup_job_filename=pgsql_all_${backup_job_db_host,,}_${now}.sql + backup_job_filename_base=pgsql_${db}_${backup_job_db_host,,} compression pre_dbbackup all write_log notice "Dumping all PostgreSQL databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" @@ -658,14 +658,14 @@ backup_pgsql() { for x_db_name in ${tmp_db_names} ; do pgexclude_arg=$(echo ${pgexclude_arg} --exclude-database=${x_db_name}) done - run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target}" > /dev/null + run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? - check_exit_code backup $target + check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup all fi } @@ -673,9 +673,9 @@ backup_pgsql() { backup_redis() { prepare_dbbackup write_log notice "Dumping Redis - Flushing Redis Cache First" - target=redis_all_${backup_job_db_host,,}_${now}.rdb - ltarget=redis_${backup_job_db_host,,} - echo bgsave | silent run_as_user ${play_fair} redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${target} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} + backup_job_filename=redis_all_${backup_job_db_host,,}_${now}.rdb + backup_job_filename_base=redis_${backup_job_db_host,,} + echo bgsave | silent run_as_user ${play_fair} redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${backup_job_filename} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} sleep 10 try=5 while [ $try -gt 0 ] ; do @@ -690,16 +690,16 @@ backup_redis() { write_log warn "Redis Busy - Waiting and retrying in 5 seconds" sleep 5 done - target_original=${target} + backup_job_filename_original=${backup_job_filename} compression pre_dbbackup all - run_as_user ${compress_cmd} "${TEMP_PATH}/${target_original}" + run_as_user ${compress_cmd} "${TEMP_PATH}/${backup_job_filename_original}" timer backup finish - check_exit_code backup $target + check_exit_code backup "${backup_job_filename}" file_encryption generate_checksum move_dbbackup - check_exit_code move $target + check_exit_code move "${backup_job_filename}" post_dbbackup all } @@ -707,21 +707,21 @@ backup_sqlite3() { prepare_dbbackup db=$(basename "${backup_job_db_host}") db="${db%.*}" - target=sqlite3_${db}_${now}.sqlite3 - ltarget=sqlite3_${db}.sqlite3 + backup_job_filename=sqlite3_${db}_${now}.sqlite3 + backup_job_filename_base=sqlite3_${db}.sqlite3 compression - pre_dbbackup $db + pre_dbbackup "${db}" write_log notice "Dumping sqlite3 database: '${backup_job_db_host}' ${compression_string}" silent run_as_user ${play_fair} sqlite3 "${backup_job_db_host}" ".backup '${TEMP_PATH}/backup.sqlite3'" exit_code=$? - check_exit_code backup $target - run_as_user ${play_fair} cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${target}" > /dev/null + check_exit_code backup "${backup_job_filename}" + run_as_user ${play_fair} cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${backup_job_filename}" > /dev/null timer backup finish file_encryption generate_checksum move_dbbackup - check_exit_code move $target - post_dbbackup $db + check_exit_code move "${backup_job_filename}" + post_dbbackup "${db}" } check_availability() { @@ -838,7 +838,7 @@ check_exit_code() { write_log error "DB Backup of '${2}' reported errors" notify \ "$(date -d @"${backup_job_start_time}" +'%Y%m%d_%H%M%S')" \ - "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ + "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${backup_job_filename_base}.log" \ "${exit_code}" \ "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed completely" \ "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." @@ -856,7 +856,7 @@ check_exit_code() { write_log error "Moving of backup '${2}' reported errors" notify \ "$(date -d @"${backup_job_start_time}" +'%Y%m%d_%H%M%S')" \ - "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" \ + "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${backup_job_filename_base}.log" \ "${move_exit_code}" \ "[FATAL] Host: ${backup_job_db_host} Name: ${backup_job_db_name} - Backup failed to move to destination" \ "DB Backup is failing to backup the '${backup_job_db_host}-${backup_job_db_name}' job." @@ -874,14 +874,14 @@ cleanup_old_data() { "blobxfer" ) write_log info "Cleaning up old backups on filesystem" run_as_user mkdir -p "${backup_job_filesystem_path}" - find "${backup_job_filesystem_path}"/ -type f -mmin +"${backup_job_cleanup_time}" -iname "${ltarget}*" -exec rm -f {} \; + find "${backup_job_filesystem_path}"/ -type f -mmin +"${backup_job_cleanup_time}" -iname "${backup_job_filename_base}*" -exec rm -f {} \; write_log info "Syncing changes via blobxfer" silent run_as_user blobxfer upload --mode file --remote-path ${backup_job_blobxfer_remote_path} --local-path ${backup_job_filesystem_path} --delete --delete-only ;; "file" | "filesystem" ) write_log info "Cleaning up old backups on filesystem" run_as_user mkdir -p "${backup_job_filesystem_path}" - run_as_user find "${backup_job_filesystem_path}"/ -type f -mmin +"${backup_job_cleanup_time}" -iname "${ltarget}*" -exec rm -f {} \; + run_as_user find "${backup_job_filesystem_path}"/ -type f -mmin +"${backup_job_cleanup_time}" -iname "${backup_job_filename_base}*" -exec rm -f {} \; ;; "s3" | "minio" ) write_log info "Cleaning up old backups on S3 storage" @@ -920,38 +920,38 @@ compression() { compression_type="bzip2" dir_compress_cmd=${compress_cmd} extension=".bz2" - target_dir=${target} - target=${target}.bz2 + backup_job_filename_dir=${backup_job_filename} + backup_job_filename=${backup_job_filename}.bz2 ;; gz* ) compress_cmd="${play_fair} pigz -q -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="gzip" extension=".gz" dir_compress_cmd=${compress_cmd} - target_dir=${target} - target=${target}.gz + backup_job_filename_dir=${backup_job_filename} + backup_job_filename=${backup_job_filename}.gz ;; xz* ) compress_cmd="${play_fair} pixz -${backup_job_compression_level} -p ${backup_job_parallel_compression_threads} " compression_type="xzip" dir_compress_cmd=${compress_cmd} extension=".xz" - target_dir=${target} - target=${target}.xz + backup_job_filename_dir=${backup_job_filename} + backup_job_filename=${backup_job_filename}.xz ;; zst* ) compress_cmd="${play_fair} zstd -q -q --rm -${backup_job_compression_level} -T${backup_job_parallel_compression_threads} ${gz_rsyncable}" compression_type="zstd" dir_compress_cmd=${compress_cmd} extension=".zst" - target_dir=${target} - target=${target}.zst + backup_job_filename_dir=${backup_job_filename} + backup_job_filename=${backup_job_filename}.zst ;; "none" | "false") compress_cmd="cat " compression_type="none" dir_compress_cmd="cat " - target_dir=${target} + backup_job_filename_dir=${backup_job_filename} ;; esac @@ -973,8 +973,8 @@ compression() { create_archive() { if [ "${exit_code}" = "0" ] ; then - write_log notice "Creating archive file of '${target_dir}' with tar ${compression_string}" - run_as_user tar cf - "${TEMP_PATH}"/"${target_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${target_dir}".tar"${extension}" > /dev/null + write_log notice "Creating archive file of '${backup_job_filename_dir}' with tar ${compression_string}" + run_as_user tar cf - "${TEMP_PATH}"/"${backup_job_filename_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename_dir}".tar"${extension}" > /dev/null else write_log error "Skipping creating archive file because backup did not complete successfully" fi @@ -1050,20 +1050,20 @@ file_encryption() { print_notice "Encrypting with GPG Passphrase" encrypt_routines_start_time=$(date +'%s') encrypt_tmp_dir=$(run_as_user mktemp -d) - echo "${backup_job_encrypt_passphrase}" | silent run_as_user ${play_fair} gpg --batch --home ${encrypt_tmp_dir} --yes --passphrase-fd 0 -c "${TEMP_PATH}"/"${target}" + echo "${backup_job_encrypt_passphrase}" | silent run_as_user ${play_fair} gpg --batch --home ${encrypt_tmp_dir} --yes --passphrase-fd 0 -c "${TEMP_PATH}"/"${backup_job_filename}" rm -rf "${encrypt_tmp_dir}" elif [ -z "${backup_job_encrypt_passphrase}" ] && [ -n "${backup_job_encrypt_pubkey}" ]; then if [ -f "${backup_job_encrypt_pubkey}" ]; then encrypt_routines_start_time=$(date +'%s') print_notice "Encrypting with GPG Public Key" encrypt_tmp_dir=$(run_as_user mktemp -d) - silent run_as_user ${play_fair} gpg --batch --yes --home "${encrypt_tmp_dir}" --recipient-file "${backup_job_encrypt_pubkey}" -c "${TEMP_PATH}"/"${target}" + silent run_as_user ${play_fair} gpg --batch --yes --home "${encrypt_tmp_dir}" --recipient-file "${backup_job_encrypt_pubkey}" -c "${TEMP_PATH}"/"${backup_job_filename}" rm -rf "${encrypt_tmp_dir}" fi fi - if [ -f "${TEMP_PATH}"/"${target}".gpg ]; then - rm -rf "${TEMP_PATH:?}"/"${target:?}" - target="${target}.gpg" + if [ -f "${TEMP_PATH}"/"${backup_job_filename}".gpg ]; then + rm -rf "${TEMP_PATH:?}"/"${backup_job_filename:?}" + backup_job_filename="${backup_job_filename}.gpg" encrypt_routines_finish_time=$(date +'%s') encrypt_routines_total_time=$(echo $((encrypt_routines_finish_time-encrypt_routines_start_time))) @@ -1095,11 +1095,11 @@ generate_checksum() { esac checksum_routines_start_time=$(date +'%s') - write_log notice "Generating ${checksum_extension^^} for '${target}'" + write_log notice "Generating ${checksum_extension^^} for '${backup_job_filename}'" cd "${TEMP_PATH}" - run_as_user ${checksum_command} "${target}" | run_as_user tee "${target}"."${checksum_extension}" > /dev/null - chmod ${backup_job_filesystem_permission} "${target}"."${checksum_extension}" - checksum_value=$(run_as_user cat "${target}"."${checksum_extension}" | awk '{print $1}') + run_as_user ${checksum_command} "${backup_job_filename}" | run_as_user tee "${backup_job_filename}"."${checksum_extension}" > /dev/null + chmod ${backup_job_filesystem_permission} "${backup_job_filename}"."${checksum_extension}" + checksum_value=$(run_as_user cat "${backup_job_filename}"."${checksum_extension}" | awk '{print $1}') checksum_routines_finish_time=$(date +'%s') checksum_routines_total_time=$(echo $((checksum_routines_finish_time-checksum_routines_start_time))) zabbix_checksum_time=$(cat < /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [debug] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${backup_job_filename_base}.log" > /dev/null ;; esac ;; error ) case "${_arg_log_level,,}" in "debug" | "notice" | "warn" | "error") - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [error] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${backup_job_filename_base}.log" > /dev/null ;; - esacexterna + esac + ;; + info ) + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [info] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${backup_job_filename_base}.log" > /dev/null + ;; + notice ) + case "${_arg_log_level,,}" in "debug" | "notice" ) - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [notice] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${backup_job_filename_base}.log" > /dev/null ;; esac ;; warn ) case "${_arg_log_level,,}" in "debug" | "notice" | "warn" ) - echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${ltarget}.log" > /dev/null + echo "$(date +'%Y-%m-%d %H:%M:%S %Z') [warn] ${_arg_log_message}" | run_as_user tee -a "${LOG_PATH}/$(date +'%Y%m%d')/$(date -d @${backup_job_start_time} +'%Y%m%d_%H%M%S')-${backup_job_filename_base}.log" > /dev/null ;; esac ;; From ff96f09e33ef0896f3268be8c067201fb7eb09c6 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 08:25:08 -0800 Subject: [PATCH 55/68] future_time is time_future --- install/assets/functions/10-db-backup | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 335d8f1..24b8648 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -535,7 +535,7 @@ backup_mysql() { if [ "${backup_job_db_name,,}" = "all" ] ; then write_log debug "Preparing to back up everything except for information_schema and _* prefixes" - db_names=$(run_as_user mysql -h ${backup_job_db_host} -P $backup_job_db_port -u$backup_job_db_user ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_enumeration_opts} --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema ) + db_names=$(run_as_user mysql -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_enumeration_opts} --batch -e "SHOW DATABASES;" | grep -v Database | grep -v schema ) if [ -n "${backup_job_db_name_exclude}" ] ; then db_names_exclusions=$(echo "${backup_job_db_name_exclude}" | tr ',' '\n') for db_exclude in ${db_names_exclusions} ; do @@ -618,7 +618,7 @@ backup_pgsql() { compression pre_dbbackup "${db}" write_log notice "Dumping PostgresSQL database: '${db}' ${compression_string}" - run_as_user ${play_fair} pg_dump -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null + run_as_user ${play_fair} pg_dump -h "${backup_job_db_host}" -p "${backup_job_db_port}" -U "${backup_job_db_user}" $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? check_exit_code backup "${backup_job_filename}" timer backup finish @@ -633,9 +633,9 @@ backup_pgsql() { compression pre_dbbackup "globals" print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" - run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -U ${backup_job_db_user} -p ${backup_job_db_port} -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null + run_as_user ${play_fair} pg_dumpall -h "${backup_job_db_host}" -U "${backup_job_db_user}" -p "${backup_job_db_port}" -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? - check_exit_code $backup_job_filename + check_exit_code "${backup_job_filename}" timer backup finish file_encryption generate_checksum @@ -1095,7 +1095,7 @@ generate_checksum() { esac checksum_routines_start_time=$(date +'%s') - write_log notice "Generating ${checksum_extension^^} for '${backup_job_filename}'" + write_log notice "Generating ${checksum_extension^^} sum for '${backup_job_filename}'" cd "${TEMP_PATH}" run_as_user ${checksum_command} "${backup_job_filename}" | run_as_user tee "${backup_job_filename}"."${checksum_extension}" > /dev/null chmod ${backup_job_filesystem_permission} "${backup_job_filename}"."${checksum_extension}" @@ -1609,12 +1609,7 @@ timer() { local cron_minute="$(echo -n "${2}" | awk '{print $1}')" local cron_hour="$(echo -n "${2}" | awk '{print $2}')" local cron_day_of_month="$(echo -n "${2}" | awk '{print $3}')" - local cron_month="$(echo -n "${2}" | awk '{print $4}')" - local cron_day_of_week="$(echo -n "${2}" | awk '{print $5}')" - - local cron_parsed=1 - - local cron_next_minute="$(date --date=@"${cron_compare}" +"%-M")" + local cron_month="$(echo -n "${2}" | awk '{print $4}')"Generating local cron_next_hour="$(date --date=@"${cron_compare}" +"%-H")" local cron_next_day_of_month="$(date --date=@"${cron_compare}" +"%-d")" local cron_next_month="$(date --date=@"${cron_compare}" +"%-m")" @@ -1750,7 +1745,7 @@ timer() { ;; time) time_future=$(date --date="$(date +"%Y%m%d") ${backup_job_backup_begin}" +"%s") - if [[ "${future_time}" < "${time_current}" ]]; then + if [[ "${time_future}" < "${time_current}" ]]; then time_future=$(( time_future + 24*60*60)) fi time_wait=$(( time_future - time_current )) From 3ecb24c603f9a0c21d5dcf47ff154951f6099cb4 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 08:29:35 -0800 Subject: [PATCH 56/68] Fix naming issue with backup_job_filesystem_archive --- install/assets/functions/10-db-backup | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 24b8648..edc5242 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1296,8 +1296,8 @@ move_dbbackup() { run_as_user ln -sfr "${backup_job_filesystem_path}"/"${backup_job_filename}" "${backup_job_filesystem_path}"/latest-"${backup_job_filename_base}" fi if [ -n "${backup_job_archive_time}" ] ; then - run_as_user mkdir -p "${backup_job_filesystem_archive}" - run_as_user find "${backup_job_filesystem_path}"/ -type f -maxdepth 1 -mmin +"${backup_job_archive_time}" -iname "${backup_job_filename_base}*" -exec mv {} "${backup_job_filesystem_archive}" \; + run_as_user mkdir -p "${backup_job_filesystem_archive_path}" + run_as_user find "${backup_job_filesystem_path}"/ -type f -maxdepth 1 -mmin +"${backup_job_archive_time}" -iname "${backup_job_filename_base}*" -exec mv {} "${backup_job_filesystem_archive_path}" \; fi ;; "s3" | "minio" ) @@ -1413,7 +1413,7 @@ post_dbbackup() { - dbbackup.backup.size.[${backup_job_db_host}.${backup_job_db_name}] "${dbbackup_size}" - dbbackup.backup.datetime.[${backup_job_db_host}.${backup_job_db_name}] "${dbbackup_date}" - dbbackup.backup.status.[${backup_job_db_host}.${backup_job_db_name}] "${exit_code}" -- dbbackup.backup.duration.[${backup_job_db_host}.${backup_job_db_name}] "$(echo $((dbbackup_finish_time-dbbackup_start_time)))" +- dbbackup.backup.duration.[${backup_job_db_host}.${backup_job_db_name}] "$((dbbackup_finish_time-dbbackup_start_time))" - dbbackup.backup.filename.[${backup_job_db_host}.${backup_job_db_name}] "${backup_job_filename}" ${zabbix_encrypt_time} ${zabbix_checksum_time} @@ -1735,7 +1735,7 @@ timer() { ;; stop) backup_job_finish_time=$(date +'%s') - backup_job_total_time=$(echo $(( backup_job_finish_time - backup_job_start_time))) + backup_job_total_time=$(( backup_job_finish_time - backup_job_start_time)) ;; esac ;; From 40ad7abac4af124ae58b3367ff8f525d7b6aea8f Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 08:35:22 -0800 Subject: [PATCH 57/68] No more extra now --- install/assets/functions/10-db-backup | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index edc5242..323bc63 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -7,9 +7,9 @@ bootstrap_filesystem() { if [ "$(stat -c %U "${backup_job_filesystem_path}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${backup_job_filesystem_path}" ; fi if [ "$(stat -c %a "${backup_job_filesystem_path}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R "${backup_job_filesystem_permission}" "${backup_job_filesystem_path}" ; fi - if [ -d "${backup_job_filesystem_archive}" ]; then - if [ "$(stat -c %U "${backup_job_filesystem_archive}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${backup_job_filesystem_archive}" ; fi - if [ "$(stat -c %a "${backup_job_filesystem_archive}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R "${backup_job_filesystem_permission}" "${backup_job_filesystem_archive}" ; fi + if [ -d "${backup_job_filesystem_archive_path}" ]; then + if [ "$(stat -c %U "${backup_job_filesystem_archive_path}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${backup_job_filesystem_archive_path}" ; fi + if [ "$(stat -c %a "${backup_job_filesystem_archive_path}")" != "${backup_job_filesystem_permission}" ] ; then chmod -R "${backup_job_filesystem_permission}" "${backup_job_filesystem_archive_path}" ; fi fi if [ ! -d "${LOG_PATH}" ]; then @@ -1356,8 +1356,6 @@ move_dbbackup() { prepare_dbbackup() { timer backup start now=$(run_as_user date +"%Y%m%d-%H%M%S") - now_date=$(run_as_user date +"%Y-%m-%d") - now_time=$(run_as_user date +"%H:%M:%S") backup_job_filename_base=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,} backup_job_filename=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.sql } From 80e407d81dd5449ff62021a1213d668f79f6fda9 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 08:55:21 -0800 Subject: [PATCH 58/68] switch globals to only get backed up if using all --- install/assets/functions/10-db-backup | 48 ++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 323bc63..e88bf83 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -588,6 +588,23 @@ backup_mysql() { } backup_pgsql() { + backup_pgsql_globals() { + prepare_dbbackup + backup_job_filename=pgsql_globals_${backup_job_db_host,,}_${now}.sql + compression + pre_dbbackup "globals" + print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" + run_as_user ${play_fair} pg_dumpall -h "${backup_job_db_host}" -U "${backup_job_db_user}" -p "${backup_job_db_port}" -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null + exit_code=$? + check_exit_code "${backup_job_filename}" + timer backup finish + file_encryption + generate_checksum + move_dbbackup + check_exit_code move "${backup_job_filename}" + post_dbbackup "globals" + } + export PGPASSWORD=${backup_job_db_pass} if [ -n "${backup_job_db_auth}" ] ; then authdb=${backup_job_db_auth} @@ -603,9 +620,11 @@ backup_pgsql() { write_log debug "Excluding '${db_exclude}' from ALL DB_NAME backups" db_names=$(echo "$db_names" | sed "/${db_exclude}/d" ) done + _postgres_backup_globals=true fi else db_names=$(echo "${backup_job_db_name}" | tr ',' '\n') + _postgres_backup_globals=false fi write_log debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" @@ -628,20 +647,7 @@ backup_pgsql() { check_exit_code move "${backup_job_filename}" post_dbbackup "${db}" done - prepare_dbbackup - backup_job_filename=pgsql_globals_${backup_job_db_host,,}_${now}.sql - compression - pre_dbbackup "globals" - print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" - run_as_user ${play_fair} pg_dumpall -h "${backup_job_db_host}" -U "${backup_job_db_user}" -p "${backup_job_db_port}" -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null - exit_code=$? - check_exit_code "${backup_job_filename}" - timer backup finish - file_encryption - generate_checksum - move_dbbackup - check_exit_code move "${backup_job_filename}" - post_dbbackup "globals" + if var_true "${_postgres_backup_globals}" ; then backup_pgsql_globals; fi else write_log debug "Not splitting database dumps into their own files" prepare_dbbackup @@ -667,6 +673,7 @@ backup_pgsql() { move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup all + if var_true "${_postgres_backup_globals}" ; then backup_pgsql_globals; fi fi } @@ -1407,7 +1414,7 @@ post_dbbackup() { source /assets/defaults/03-monitoring write_log notice "Sending Backup Statistics to Zabbix" silent zabbix_sender -c -c "${ZABBIX_CONFIG_PATH}"/"${ZABBIX_CONFIG_FILE}" -k dbbackup.backup -o '[{"{#NAME}":"'${backup_job_db_host}.${backup_job_db_name}'"}]' - cat < /tmp/zabbix_output - dbbackup.backup.size.[${backup_job_db_host}.${backup_job_db_name}] "${dbbackup_size}" - dbbackup.backup.datetime.[${backup_job_db_host}.${backup_job_db_name}] "${dbbackup_date}" - dbbackup.backup.status.[${backup_job_db_host}.${backup_job_db_name}] "${exit_code}" @@ -1416,6 +1423,17 @@ post_dbbackup() { ${zabbix_encrypt_time} ${zabbix_checksum_time} EOF + + cat < Date: Wed, 8 Nov 2023 09:11:24 -0800 Subject: [PATCH 59/68] Add PGSQL to override and force backing up globals --- README.md | 2 ++ install/assets/functions/10-db-backup | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index e35e98b..bec7b82 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,7 @@ If these are set and no other defaults or variables are set explicitly, they wil | Variable | Description | Default | `_FILE` | | -------------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | | `DEFAULT_AUTH` | (Optional) Authentication Database | | x | +| `DEFAULT_BACKUP_GLOBALS` | Backup Globals as part of backup procedure | | | | `DEFAULT_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | | `DEFAULT_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | | `DEFAULT_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | @@ -562,6 +563,7 @@ Otherwise, override them per backup job. Additional backup jobs can be scheduled | Variable | Description | Default | `_FILE` | | ----------------------------- | --------------------------------------------------------------------------------------------------------- | ------- | ------- | | `DB01_AUTH` | (Optional) Authentication Database | | | +| `DB01_BACKUP_GLOBALS` | Backup Globals after backing up database (forces `TRUE` if `_NAME=ALL``) | `FALSE` | | | `DB01_EXTRA_OPTS` | Pass extra arguments to the backup and database enumeration command, add them here e.g. `--extra-command` | | | | `DB01_EXTRA_BACKUP_OPTS` | Pass extra arguments to the backup command only, add them here e.g. `--extra-command` | | | | `DB01_EXTRA_ENUMERATION_OPTS` | Pass extra arguments to the database enumeration command only, add them here e.g. `--extra-command` | | | diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index e88bf83..87206cf 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -167,6 +167,7 @@ bootstrap_variables() { transform_backup_instance_variable "${backup_instance_number}" AUTH backup_job_db_auth transform_backup_instance_variable "${backup_instance_number}" BACKUP_BEGIN backup_job_backup_begin transform_backup_instance_variable "${backup_instance_number}" BACKUP_INTERVAL backup_job_backup_interval + transform_backup_instance_variable "${backup_instance_number}" BACKUP_GLOBALS backup_job_backup_pgsql_globals transform_backup_instance_variable "${backup_instance_number}" BACKUP_LOCATION backup_job_backup_location transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_BEGIN backup_job_snapshot_blackout_start transform_backup_instance_variable "${backup_instance_number}" BLACKOUT_END backup_job_snapshot_blackout_finish From 440b24da8dbc84f00aac32993a59f205238c69d5 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 10:06:19 -0800 Subject: [PATCH 60/68] Update Zabbix Template --- zabbix_templates/db_backup.json | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/zabbix_templates/db_backup.json b/zabbix_templates/db_backup.json index a7680ac..831a0de 100644 --- a/zabbix_templates/db_backup.json +++ b/zabbix_templates/db_backup.json @@ -14,8 +14,8 @@ "templates": [ { "uuid": "5a16c1bd694145389eed5ee803d954cc", - "template": "DB Backup", - "name": "DB Backup", + "template": "DB Backup4", + "name": "DB Backup4", "description": "Template for Docker DB Backup Image\n\nMeant for use specifically with https://github.com/tiredofit/docker-db-backup Version > 4.0.0\n\nSupports auto discovery of backup jobs and creates graphs and triggers", "groups": [ { @@ -30,7 +30,7 @@ "uuid": "94bb6f862e1841f8b2834b04c41c1d86", "name": "Backup", "type": "TRAP", - "key": "dbbackup.backup.[{#NAME}]", + "key": "dbbackup.backup", "delay": "0", "item_prototypes": [ { @@ -64,28 +64,28 @@ "trigger_prototypes": [ { "uuid": "3681b56bb882466fb304a48b4beb15f0", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 2 days", "priority": "HIGH", "manual_close": "YES" }, { "uuid": "6c70136c84994197b6396a143b4e956f", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 3 days", "priority": "DISASTER", "manual_close": "YES" }, { "uuid": "d2038025cab643019cb9610c301f0cb9", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 4 days", "priority": "DISASTER", "manual_close": "YES" }, { "uuid": "ea85f02d032c4a1dbc1b6e91a3b2b37b", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)=0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)=0", "name": "[{#NAME}] No backups detected in 5 days", "priority": "DISASTER", "manual_close": "YES" @@ -132,20 +132,20 @@ "trigger_prototypes": [ { "uuid": "849f8660bee04427aff55af47b6f509c", - "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>1.2", + "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)>1.2", "name": "[{#NAME}] Backup 20% Greater in size", "priority": "WARNING", "manual_close": "YES" }, { "uuid": "74d16a7680544c65af22cc568ce3d59d", - "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>0.2", + "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)>0.2", "name": "[{#NAME}] Backup 20% Smaller in Size", "priority": "WARNING" }, { "uuid": "5595d769c73f4eaeadda95c84c2c0f17", - "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])<1K", + "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])<1K", "name": "[{#NAME}] Backup Empty", "priority": "HIGH" } @@ -165,7 +165,7 @@ "trigger_prototypes": [ { "uuid": "74b91e28453b4c2a84743f5e371495c1", - "expression": "last(/DB Backup/dbbackup.backup.status.[{#NAME}])=1", + "expression": "last(/DB Backup4/dbbackup.backup.status.[{#NAME}])=1", "name": "[{#NAME}] Backup - Failed with errors", "priority": "WARNING", "manual_close": "YES" @@ -182,7 +182,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.duration.[{#NAME}]" } } @@ -196,7 +196,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.size.[{#NAME}]" } } @@ -210,7 +210,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.checksum.duration.[{#NAME}]" } } @@ -224,7 +224,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.encrypt.duration.[{#NAME}]" } } From 45eba40360583d18a9a8ba0369969f9b28856cf4 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 10:40:02 -0800 Subject: [PATCH 61/68] Update Zabbix Template --- zabbix_templates/db_backup.json | 48 ++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/zabbix_templates/db_backup.json b/zabbix_templates/db_backup.json index 831a0de..ab40051 100644 --- a/zabbix_templates/db_backup.json +++ b/zabbix_templates/db_backup.json @@ -14,8 +14,8 @@ "templates": [ { "uuid": "5a16c1bd694145389eed5ee803d954cc", - "template": "DB Backup4", - "name": "DB Backup4", + "template": "DB Backup", + "name": "DB Backup", "description": "Template for Docker DB Backup Image\n\nMeant for use specifically with https://github.com/tiredofit/docker-db-backup Version > 4.0.0\n\nSupports auto discovery of backup jobs and creates graphs and triggers", "groups": [ { @@ -35,16 +35,16 @@ "item_prototypes": [ { "uuid": "5a2c4d1cacf844829bc1fbf912e071c5", - "name": "[{#NAME}] Checksum duration", + "name": "[{#NAME}] Checksum - Duration", "type": "TRAP", "key": "dbbackup.backup.checksum.duration.[{#NAME}]", "delay": "0", "history": "7d", - "units": "unixtime" + "units": "uptime" }, { "uuid": "6e49769ec07344a4974b13dab00c3539", - "name": "[{#NAME}] Checksum Hash", + "name": "[{#NAME}] Checksum - Hash", "type": "TRAP", "key": "dbbackup.backup.checksum.hash.[{#NAME}]", "delay": "0", @@ -54,7 +54,7 @@ }, { "uuid": "bb6472e30bff4d9c908b1d34b893e622", - "name": "[{#NAME}] Last Backup", + "name": "[{#NAME}] Backup - Last Backup", "type": "TRAP", "key": "dbbackup.backup.datetime.[{#NAME}]", "delay": "0", @@ -64,28 +64,28 @@ "trigger_prototypes": [ { "uuid": "3681b56bb882466fb304a48b4beb15f0", - "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 2 days", "priority": "HIGH", "manual_close": "YES" }, { "uuid": "6c70136c84994197b6396a143b4e956f", - "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 3 days", "priority": "DISASTER", "manual_close": "YES" }, { "uuid": "d2038025cab643019cb9610c301f0cb9", - "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 4 days", "priority": "DISASTER", "manual_close": "YES" }, { "uuid": "ea85f02d032c4a1dbc1b6e91a3b2b37b", - "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)=0", + "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)=0", "name": "[{#NAME}] No backups detected in 5 days", "priority": "DISASTER", "manual_close": "YES" @@ -94,7 +94,7 @@ }, { "uuid": "8ec2b2f44ddf4f36b3dbb2aa15e3a32f", - "name": "[{#NAME}] Backup Duration", + "name": "[{#NAME}] Backup - Duration", "type": "TRAP", "key": "dbbackup.backup.duration.[{#NAME}]", "delay": "0", @@ -104,16 +104,16 @@ }, { "uuid": "3f0dc3c75261447c93482815c3d69524", - "name": "[{#NAME}] Encrypt Duration", + "name": "[{#NAME}] Encrypt - Duration", "type": "TRAP", "key": "dbbackup.backup.encrypt.duration.[{#NAME}]", "delay": "0", "history": "7d", - "units": "unixtime" + "units": "uptime" }, { "uuid": "c3d5ad0789c443859d6a673e03db9cec", - "name": "[{#NAME}] Filename", + "name": "[{#NAME}] Backup - Filename", "type": "TRAP", "key": "dbbackup.backup.filename.[{#NAME}]", "delay": "0", @@ -123,7 +123,7 @@ }, { "uuid": "43b700c03897465eb7e49bbfe8fc9fc5", - "name": "[{#NAME}] Size", + "name": "[{#NAME}] Backup - Size", "type": "TRAP", "key": "dbbackup.backup.size.[{#NAME}]", "delay": "0", @@ -132,20 +132,20 @@ "trigger_prototypes": [ { "uuid": "849f8660bee04427aff55af47b6f509c", - "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)>1.2", + "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>1.2", "name": "[{#NAME}] Backup 20% Greater in size", "priority": "WARNING", "manual_close": "YES" }, { "uuid": "74d16a7680544c65af22cc568ce3d59d", - "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)>0.2", + "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>0.2", "name": "[{#NAME}] Backup 20% Smaller in Size", "priority": "WARNING" }, { "uuid": "5595d769c73f4eaeadda95c84c2c0f17", - "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])<1K", + "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])<1K", "name": "[{#NAME}] Backup Empty", "priority": "HIGH" } @@ -153,7 +153,7 @@ }, { "uuid": "a6fc542a565c4baba8429ed9ab31b5ae", - "name": "[{#NAME}] Status", + "name": "[{#NAME}] Backup - Status", "type": "TRAP", "key": "dbbackup.backup.status.[{#NAME}]", "delay": "0", @@ -165,7 +165,7 @@ "trigger_prototypes": [ { "uuid": "74b91e28453b4c2a84743f5e371495c1", - "expression": "last(/DB Backup4/dbbackup.backup.status.[{#NAME}])=1", + "expression": "last(/DB Backup/dbbackup.backup.status.[{#NAME}])=1", "name": "[{#NAME}] Backup - Failed with errors", "priority": "WARNING", "manual_close": "YES" @@ -182,7 +182,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup4", + "host": "DB Backup", "key": "dbbackup.backup.duration.[{#NAME}]" } } @@ -196,7 +196,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup4", + "host": "DB Backup", "key": "dbbackup.backup.size.[{#NAME}]" } } @@ -210,7 +210,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup4", + "host": "DB Backup", "key": "dbbackup.backup.checksum.duration.[{#NAME}]" } } @@ -224,7 +224,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup4", + "host": "DB Backup", "key": "dbbackup.backup.encrypt.duration.[{#NAME}]" } } From 140e3183a4cb1c487d3c7ca1ad7dc4c7e8e95760 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 10:43:25 -0800 Subject: [PATCH 62/68] Update Zabbix Template --- zabbix_templates/db_backup.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/zabbix_templates/db_backup.json b/zabbix_templates/db_backup.json index ab40051..2062433 100644 --- a/zabbix_templates/db_backup.json +++ b/zabbix_templates/db_backup.json @@ -141,13 +141,15 @@ "uuid": "74d16a7680544c65af22cc568ce3d59d", "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>0.2", "name": "[{#NAME}] Backup 20% Smaller in Size", - "priority": "WARNING" + "priority": "WARNING", + "manual_close": "YES" }, { "uuid": "5595d769c73f4eaeadda95c84c2c0f17", "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])<1K", "name": "[{#NAME}] Backup Empty", - "priority": "HIGH" + "priority": "HIGH", + "manual_close": "YES" } ] }, From fd59daf12503346b5bea6e88aed894ac41f67e90 Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 10:48:06 -0800 Subject: [PATCH 63/68] Update Zabbix Template --- zabbix_templates/db_backup.json | 86 +++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/zabbix_templates/db_backup.json b/zabbix_templates/db_backup.json index 2062433..d5ad3a6 100644 --- a/zabbix_templates/db_backup.json +++ b/zabbix_templates/db_backup.json @@ -14,8 +14,8 @@ "templates": [ { "uuid": "5a16c1bd694145389eed5ee803d954cc", - "template": "DB Backup", - "name": "DB Backup", + "template": "DB Backup4", + "name": "DB Backup4", "description": "Template for Docker DB Backup Image\n\nMeant for use specifically with https://github.com/tiredofit/docker-db-backup Version > 4.0.0\n\nSupports auto discovery of backup jobs and creates graphs and triggers", "groups": [ { @@ -40,7 +40,13 @@ "key": "dbbackup.backup.checksum.duration.[{#NAME}]", "delay": "0", "history": "7d", - "units": "uptime" + "units": "uptime", + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ] }, { "uuid": "6e49769ec07344a4974b13dab00c3539", @@ -50,7 +56,13 @@ "delay": "0", "history": "30d", "trends": "0", - "value_type": "TEXT" + "value_type": "TEXT", + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ] }, { "uuid": "bb6472e30bff4d9c908b1d34b893e622", @@ -61,31 +73,37 @@ "history": "7d", "units": "unixtime", "description": "Datestamp of last database backup", + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ], "trigger_prototypes": [ { "uuid": "3681b56bb882466fb304a48b4beb15f0", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 2 days", "priority": "HIGH", "manual_close": "YES" }, { "uuid": "6c70136c84994197b6396a143b4e956f", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 3 days", "priority": "DISASTER", "manual_close": "YES" }, { "uuid": "d2038025cab643019cb9610c301f0cb9", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)=0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)=0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)<>0", "name": "[{#NAME}] No backups detected in 4 days", "priority": "DISASTER", "manual_close": "YES" }, { "uuid": "ea85f02d032c4a1dbc1b6e91a3b2b37b", - "expression": "fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup/dbbackup.backup.datetime.[{#NAME}],432800s)=0", + "expression": "fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],172800s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],259200s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],345600s)<>0 and fuzzytime(/DB Backup4/dbbackup.backup.datetime.[{#NAME}],432800s)=0", "name": "[{#NAME}] No backups detected in 5 days", "priority": "DISASTER", "manual_close": "YES" @@ -100,7 +118,13 @@ "delay": "0", "history": "7d", "units": "uptime", - "description": "How long the DB Backup job took" + "description": "How long the DB Backup job took", + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ] }, { "uuid": "3f0dc3c75261447c93482815c3d69524", @@ -109,7 +133,13 @@ "key": "dbbackup.backup.encrypt.duration.[{#NAME}]", "delay": "0", "history": "7d", - "units": "uptime" + "units": "uptime", + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ] }, { "uuid": "c3d5ad0789c443859d6a673e03db9cec", @@ -119,7 +149,13 @@ "delay": "0", "history": "30d", "trends": "0", - "value_type": "TEXT" + "value_type": "TEXT", + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ] }, { "uuid": "43b700c03897465eb7e49bbfe8fc9fc5", @@ -129,24 +165,30 @@ "delay": "0", "history": "7d", "description": "Backup Size", + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ], "trigger_prototypes": [ { "uuid": "849f8660bee04427aff55af47b6f509c", - "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>1.2", + "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)>1.2", "name": "[{#NAME}] Backup 20% Greater in size", "priority": "WARNING", "manual_close": "YES" }, { "uuid": "74d16a7680544c65af22cc568ce3d59d", - "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])/last(/DB Backup/dbbackup.backup.size.[{#NAME}],#2)>0.2", + "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)>0.2", "name": "[{#NAME}] Backup 20% Smaller in Size", "priority": "WARNING", "manual_close": "YES" }, { "uuid": "5595d769c73f4eaeadda95c84c2c0f17", - "expression": "last(/DB Backup/dbbackup.backup.size.[{#NAME}])<1K", + "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])<1K", "name": "[{#NAME}] Backup Empty", "priority": "HIGH", "manual_close": "YES" @@ -164,10 +206,16 @@ "valuemap": { "name": "Backup Status" }, + "tags": [ + { + "tag": "Application", + "value": "DB Backup" + } + ], "trigger_prototypes": [ { "uuid": "74b91e28453b4c2a84743f5e371495c1", - "expression": "last(/DB Backup/dbbackup.backup.status.[{#NAME}])=1", + "expression": "last(/DB Backup4/dbbackup.backup.status.[{#NAME}])=1", "name": "[{#NAME}] Backup - Failed with errors", "priority": "WARNING", "manual_close": "YES" @@ -184,7 +232,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.duration.[{#NAME}]" } } @@ -198,7 +246,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.size.[{#NAME}]" } } @@ -212,7 +260,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.checksum.duration.[{#NAME}]" } } @@ -226,7 +274,7 @@ "color": "199C0D", "calc_fnc": "ALL", "item": { - "host": "DB Backup", + "host": "DB Backup4", "key": "dbbackup.backup.encrypt.duration.[{#NAME}]" } } From 3010a4d1878d26aaa5803f85b85f422670fd780e Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 10:48:18 -0800 Subject: [PATCH 64/68] Rework Zabbix Payloads --- install/assets/functions/10-db-backup | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 87206cf..d2cb70a 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -628,6 +628,8 @@ backup_pgsql() { _postgres_backup_globals=false fi + if var_false "${_postgres_backup_globals}" && var_true "${backup_job_backup_pgsql_globals}" ; then _postgres_backup_globals=true; fi + write_log debug "Databases Found: $(echo ${db_names} | xargs | tr ' ' ',')" if var_true "${backup_job_split_db}" ; then @@ -1414,28 +1416,26 @@ post_dbbackup() { if var_true "${CONTAINER_ENABLE_MONITORING}" && [ "${CONTAINER_MONITORING_BACKEND,,}" = "zabbix" ]; then source /assets/defaults/03-monitoring write_log notice "Sending Backup Statistics to Zabbix" - silent zabbix_sender -c -c "${ZABBIX_CONFIG_PATH}"/"${ZABBIX_CONFIG_FILE}" -k dbbackup.backup -o '[{"{#NAME}":"'${backup_job_db_host}.${backup_job_db_name}'"}]' - cat < /tmp/zabbix_output -- dbbackup.backup.size.[${backup_job_db_host}.${backup_job_db_name}] "${dbbackup_size}" -- dbbackup.backup.datetime.[${backup_job_db_host}.${backup_job_db_name}] "${dbbackup_date}" -- dbbackup.backup.status.[${backup_job_db_host}.${backup_job_db_name}] "${exit_code}" -- dbbackup.backup.duration.[${backup_job_db_host}.${backup_job_db_name}] "$((dbbackup_finish_time-dbbackup_start_time))" -- dbbackup.backup.filename.[${backup_job_db_host}.${backup_job_db_name}] "${backup_job_filename}" -${zabbix_encrypt_time} -${zabbix_checksum_time} -EOF + silent run_as_user zabbix_sender -c "${ZABBIX_CONFIG_PATH}"/"${ZABBIX_CONFIG_FILE}" -k dbbackup.backup -o '[{"{#NAME}":"'${backup_job_db_host}.${backup_job_db_name}'"}]' + local zabbix_payload=$(run_as_user mktemp) - cat < Date: Wed, 8 Nov 2023 13:14:09 -0800 Subject: [PATCH 65/68] Update Zabbix Template to fix trigger --- zabbix_templates/db_backup.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix_templates/db_backup.json b/zabbix_templates/db_backup.json index d5ad3a6..3d4a181 100644 --- a/zabbix_templates/db_backup.json +++ b/zabbix_templates/db_backup.json @@ -181,7 +181,7 @@ }, { "uuid": "74d16a7680544c65af22cc568ce3d59d", - "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)>0.2", + "expression": "last(/DB Backup4/dbbackup.backup.size.[{#NAME}])/last(/DB Backup4/dbbackup.backup.size.[{#NAME}],#2)<0.2", "name": "[{#NAME}] Backup 20% Smaller in Size", "priority": "WARNING", "manual_close": "YES" From 089687dc556dbbf135c6da7d8efb2391fbc7dd3c Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 14:48:27 -0800 Subject: [PATCH 66/68] Add DEBUG_ statements --- install/assets/functions/10-db-backup | 65 ++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index d2cb70a..376f2f2 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1,6 +1,7 @@ #!/command/with-contenv bash bootstrap_filesystem() { + if var_true "${DEBUG_BOOTSTRAP_FILESYSTEM}" ; then debug on; fi if [ ! -d "${backup_job_filesystem_path}" ]; then mkdir -p "${backup_job_filesystem_path}" fi @@ -24,9 +25,11 @@ bootstrap_filesystem() { mkdir -p "${TEMP_PATH}" fi if [ "$(stat -c %U "${TEMP_PATH}")" != "dbbackup" ] ; then chown -R dbbackup:dbbackup "${TEMP_PATH}" ; fi + if var_true "${DEBUG_BOOTSTRAP_FILESYSTEM}" ; then debug off; fi } bootstrap_variables() { + if var_true "${DEBUG_BOOTSTRAP_VARIABLES}" ; then debug on; fi backup_init() { backup_instance_number=${1} backup_instance_vars=$(mktemp) @@ -370,9 +373,12 @@ bootstrap_variables() { parse_variables) parse_variables "$2" ;; upgrade ) upgrade_lonely_variables "$2" ;; esac + + if var_true "${DEBUG_BOOTSTRAP_VARIABLES}" ; then debug off; fi } backup_couch() { + if var_true "${DEBUG_BACKUP_COUCH}" ; then debug on; fi prepare_dbbackup backup_job_filename=couch_${backup_job_db_name}_${backup_job_db_host#*//}_${now}.txt backup_job_filename_base=couch_${backup_job_db_name}_${backup_job_db_host#*//} @@ -388,9 +394,11 @@ backup_couch() { move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup ${backup_job_db_name} + if var_true "${DEBUG_BACKUP_COUCH}" ; then debug off; fi } backup_influx() { + if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug on; fi if [ "${backup_job_db_name,,}" = "all" ] ; then write_log debug "Preparing to back up everything" db_names=justbackupeverything @@ -447,9 +455,11 @@ backup_influx() { done ;; esac + if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug off; fi } backup_mongo() { + if var_true "${DEBUG_BACKUP_MONGO}" ; then debug on; fi prepare_dbbackup if [ "${backup_job_compression,,}" = "none" ] ; then backup_job_filename=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive @@ -476,9 +486,11 @@ backup_mongo() { move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup "${backup_job_db_name}" + if var_true "${DEBUG_BACKUP_MONGO}" ; then debug off; fi } backup_mssql() { + if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug on; fi case "${backup_job_mssql_mode,,}" in db|database ) prepare_dbbackup @@ -521,9 +533,11 @@ backup_mssql() { post_dbbackup "${backup_job_db_name}" ;; esac + if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug off; fi } backup_mysql() { + if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug on; fi if var_true "${backup_job_mysql_events}" ; then events="--events" fi @@ -586,9 +600,11 @@ backup_mysql() { check_exit_code move "${backup_job_filename}" post_dbbackup all fi + if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug off; fi } backup_pgsql() { + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug on; fi backup_pgsql_globals() { prepare_dbbackup backup_job_filename=pgsql_globals_${backup_job_db_host,,}_${now}.sql @@ -678,9 +694,11 @@ backup_pgsql() { post_dbbackup all if var_true "${_postgres_backup_globals}" ; then backup_pgsql_globals; fi fi + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug on; fi } backup_redis() { + if var_true "${DEBUG_BACKUP_REDIS}" ; then debug on; fi prepare_dbbackup write_log notice "Dumping Redis - Flushing Redis Cache First" backup_job_filename=redis_all_${backup_job_db_host,,}_${now}.rdb @@ -711,9 +729,11 @@ backup_redis() { move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup all + if var_true "${DEBUG_BACKUP_REDIS}" ; then debug off; fi } backup_sqlite3() { + if var_true "${DEBUG_BACKUP_SQLITE3}" ; then debug on; fi prepare_dbbackup db=$(basename "${backup_job_db_host}") db="${db%.*}" @@ -732,10 +752,12 @@ backup_sqlite3() { move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup "${db}" + if var_true "${DEBUG_BACKUP_SQLITE3}" ; then debug off; fi } check_availability() { -### Set the Database Type + if var_true "${DEBUG_CHECK_AVAILABILITY}" ; then debug on; fi + ### Set the Database Type if var_false "${backup_job_skip_availability_check}" ; then case "${dbtype}" in "couch" ) @@ -834,9 +856,11 @@ check_availability() { ;; esac fi + if var_true "${DEBUG_CHECK_AVAILABILITY}" ; then debug off; fi } check_exit_code() { + if var_true "${DEBUG_CHECK_EXIT_CODE}" ; then debug on; fi case "${1}" in backup ) write_log debug "DB Backup exit Code is ${exit_code}" @@ -875,9 +899,11 @@ check_exit_code() { esac ;; esac + if var_true "${DEBUG_CHECK_EXIT_CODE}" ; then debug off; fi } cleanup_old_data() { + if var_true "${DEBUG_CLEANUP_OLD_DATA}" ; then debug on; fi if [ -n "${backup_job_cleanup_time}" ]; then if [ "${master_exit_code}" != 1 ]; then case "${backup_job_backup_location,,}" in @@ -913,9 +939,11 @@ cleanup_old_data() { write_log error "Skipping Cleaning up old backups because there were errors in backing up" fi fi + if var_true "${DEBUG_CLEANUP_OLD_DATA}" ; then debug off; fi } compression() { + if var_true "${DEBUG_COMPRESSION}" ; then debug on; fi if var_false "${backup_job_parallel_compression}" ; then backup_job_parallel_compression_threads=1 fi @@ -979,18 +1007,22 @@ compression() { fi ;; esac + if var_true "${DEBUG_COMPRESSION}" ; then debug off; fi } create_archive() { + if var_true "${DEBUG_CREATE_ARCHIVE}" ; then debug on; fi if [ "${exit_code}" = "0" ] ; then write_log notice "Creating archive file of '${backup_job_filename_dir}' with tar ${compression_string}" run_as_user tar cf - "${TEMP_PATH}"/"${backup_job_filename_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename_dir}".tar"${extension}" > /dev/null else write_log error "Skipping creating archive file because backup did not complete successfully" fi + if var_true "${DEBUG_CREATE_ARCHIVE}" ; then debug off; fi } create_schedulers() { + if var_true "${DEBUG_CREATE_SCHEDULERS}" ; then debug on; fi backup() { bootstrap_variables upgrade BACKUP local backup_instances=$(printenv | sort | grep -c "^DB[0-9]._HOST") @@ -1033,6 +1065,7 @@ EOF case "${1}" in backup ) backup ;; esac + if var_true "${DEBUG_CREATE_SCHEDULERS}" ; then debug off; fi } ctrl_c() { @@ -1048,7 +1081,20 @@ db_backup_container_init() { touch /tmp/.container/db-backup-backups } +debug() { + case "${1}" in + off) + DEBUG_MODE=${OLD_DEBUG_MODE} + ;; + on) + OLD_DEBUG_MODE=${DEBUG_MODE} + DEBUG_MODE=TRUE + ;; + esac +} + file_encryption() { + if var_true "${DEBUG_FILE_ENCRYPTION}" ; then debug on; fi if var_true "${backup_job_encrypt}" ; then if [ "${exit_code}" = "0" ] ; then print_debug "Encrypting" @@ -1086,9 +1132,11 @@ EOF write_log error "Skipping encryption because backup did not complete successfully" fi fi + if var_true "${DEBUG_FILE_ENCRYPTION}" ; then debug off; fi } generate_checksum() { + if var_true "${DEBUG_GENERATE_CHECKSUM}" ; then debug on; fi if [ "${exit_code}" = "0" ] ; then case "${backup_job_checksum,,}" in "md5" ) @@ -1122,9 +1170,11 @@ EOF else write_log error "Skipping Checksum creation because backup did not complete successfully" fi + if var_true "${DEBUG_GENERATE_CHECKSUM}" ; then debug off; fi } notify() { + if var_true "${DEBUG_NOTIFY}" ; then debug on; fi notification_custom() { if [ -n "${NOTIFICATION_SCRIPT}" ] ; then if var_true "${NOTIFICATION_SCRIPT_SKIP_X_VERIFY}" ; then @@ -1268,9 +1318,11 @@ EOF esac done fi + if var_true "${DEBUG_NOTIFY}" ; then debug off; fi } move_dbbackup() { + if var_true "${DEBUG_MOVE_DBBACKUP}" ; then debug on; fi if [ "${exit_code}" = "0" ] ; then dbbackup_size="$(run_as_user stat -c%s "${TEMP_PATH}"/"${backup_job_filename}")" dbbackup_date="$(run_as_user date -r "${TEMP_PATH}"/"${backup_job_filename}" +'%s')" @@ -1361,6 +1413,7 @@ move_dbbackup() { fi run_as_user rm -rf "${TEMP_PATH}"/"${backup_job_filename}" + if var_true "${DEBUG_MOVE_DBBACKUP}" ; then debug off; fi } prepare_dbbackup() { @@ -1371,6 +1424,7 @@ prepare_dbbackup() { } pre_dbbackup() { + if var_true "${DEBUG_PRE_DBBACKUP}" ; then debug on; fi ### Pre Script Support if [ -n "${backup_job_pre_script}" ] ; then if var_true "${backup_job_pre_script_x_verify}" ; then @@ -1407,9 +1461,11 @@ pre_dbbackup() { fi done fi + if var_true "${DEBUG_PRE_DBBACKUP}" ; then debug off; fi } post_dbbackup() { + if var_true "${DEBUG_POST_DBBACKUP}" ; then debug on; fi dbbackup_finish_time=$(run_as_user date +"%s") dbbackup_total_time=$(run_as_user echo $((dbbackup_finish_time-dbbackup_start_time))) @@ -1476,9 +1532,11 @@ EOZP fi write_log notice "DB Backup for '${1}' time taken: $(echo ${dbbackup_total_time} | awk '{printf "Hours: %d Minutes: %02d Seconds: %02d", $1/3600, ($1/60)%60, $1%60}')" + if var_true "${DEBUG_POST_DBBACKUP}" ; then debug on; fi } process_limiter() { + if var_true "${DEBUG_PROCESS_LIMITER}" ; then debug on; fi while true ; do counter=0 process_amount="$(wc -l /tmp/.container/db-backup-backups | awk '{print $1}')" @@ -1497,6 +1555,7 @@ process_limiter() { break fi done + if var_true "${DEBUG_PROCESS_LIMITER}" ; then debug off; fi } run_as_user() { @@ -1545,6 +1604,7 @@ symlink_log () { } timer() { + if var_true "${DEBUG_TIMER}" ; then debug on; fi case "${1}" in backup) case "${2}" in @@ -1768,9 +1828,11 @@ timer() { time_wait=$(( time_future - time_current )) ;; esac + if var_true "${DEBUG_TIMER}" ; then debug off; fi } write_log() { + if var_true "${DEBUG_WRITE_LOG}" ; then debug on; fi output_off local _arg_log_level=${1} shift 1 @@ -1815,4 +1877,5 @@ write_log() { print_${_arg_log_level} "${_arg_log_message}" output_on + if var_true "${DEBUG_WRITE_LOG}" ; then debug off; fi } \ No newline at end of file From 89e6956cdd87c79b52908c3fd912e0784aae3bcc Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 15:08:32 -0800 Subject: [PATCH 67/68] Adjust debug statements --- install/assets/functions/10-db-backup | 50 ++++++++++++++++++--------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 376f2f2..27ce875 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -378,15 +378,16 @@ bootstrap_variables() { } backup_couch() { - if var_true "${DEBUG_BACKUP_COUCH}" ; then debug on; fi prepare_dbbackup backup_job_filename=couch_${backup_job_db_name}_${backup_job_db_host#*//}_${now}.txt backup_job_filename_base=couch_${backup_job_db_name}_${backup_job_db_host#*//} compression pre_dbbackup ${backup_job_db_name} write_log notice "Dumping CouchDB database: '${backup_job_db_name}' ${compression_string}" + if var_true "${DEBUG_BACKUP_COUCH}" ; then debug on; fi run_as_user curl -sSL -X GET ${backup_job_db_host}:${backup_job_db_port}/${backup_job_db_name}/_all_docs?include_docs=true | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? + if var_true "${DEBUG_BACKUP_COUCH}" ; then debug off; fi check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption @@ -394,11 +395,9 @@ backup_couch() { move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup ${backup_job_db_name} - if var_true "${DEBUG_BACKUP_COUCH}" ; then debug off; fi } backup_influx() { - if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug on; fi if [ "${backup_job_db_name,,}" = "all" ] ; then write_log debug "Preparing to back up everything" db_names=justbackupeverything @@ -416,6 +415,7 @@ backup_influx() { compression pre_dbbackup "${db}" write_log notice "Dumping Influx database: '${db}'" + if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug on; fi run_as_user influxd backup ${influx_compression} ${bucket} -portable -host ${backup_job_db_host}:${backup_job_db_port} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} "${TEMP_PATH}"/"${backup_job_filename_dir}" exit_code=$? check_exit_code backup "${backup_job_filename_dir}" @@ -423,6 +423,7 @@ backup_influx() { run_as_user tar cf - "${TEMP_PATH}"/"${backup_job_filename_dir}" | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename_dir}".tar"${extension}" > /dev/null backup_job_filename=influx_${db}_${backup_job_db_host#*//}_${now}.tar${extension} backup_job_filename_base=influx_${db}_${backup_job_db_host#*//} + if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug off; fi timer backup finish file_encryption generate_checksum @@ -440,12 +441,14 @@ backup_influx() { compression pre_dbbackup "${db}" write_log notice "Dumping Influx2 database: '${db}'" + if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug on; fi run_as_user influx backup --org ${backup_job_db_user} ${bucket} --host ${backup_job_db_host}:${backup_job_db_port} --token ${backup_job_db_pass} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --compression none "${TEMP_PATH}"/"${backup_job_filename_dir}" exit_code=$? check_exit_code backup "${backup_job_filename_dir}" create_archive backup_job_filename=influx2_${db}_${backup_job_db_host#*//}_${now}.tar${extension} backup_job_filename_base=influx2_${db}_${backup_job_db_host#*//} + if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug off; fi timer backup finish file_encryption generate_checksum @@ -455,11 +458,9 @@ backup_influx() { done ;; esac - if var_true "${DEBUG_BACKUP_INFLUX}" ; then debug off; fi } backup_mongo() { - if var_true "${DEBUG_BACKUP_MONGO}" ; then debug on; fi prepare_dbbackup if [ "${backup_job_compression,,}" = "none" ] ; then backup_job_filename=${dbtype}_${backup_job_db_name,,}_${backup_job_db_host,,}_${now}.archive @@ -477,8 +478,10 @@ backup_mongo() { fi pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MongoDB database: '${DB_NAME}' ${compression_string}" + if var_true "${DEBUG_BACKUP_MONGO}" ; then debug on; fi silent run_as_user ${play_fair} mongodump --archive=${TEMP_PATH}/${backup_job_filename} ${mongo_compression} ${mongo_backup_parameter} exit_code=$? + if var_true "${DEBUG_BACKUP_MONGO}" ; then debug off; fi check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption @@ -490,7 +493,6 @@ backup_mongo() { } backup_mssql() { - if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug on; fi case "${backup_job_mssql_mode,,}" in db|database ) prepare_dbbackup @@ -498,8 +500,10 @@ backup_mssql() { backup_job_filename_base=mssql_${backup_job_db_name,,}_${backup_job_db_host,,} pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" + if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug on; fi silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP DATABASE [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${backup_job_filename}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? + if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug off; fi backup_job_filename_original=${backup_job_filename} compression pre_dbbackup all @@ -518,8 +522,10 @@ backup_mssql() { backup_job_filename_base=mssql_${backup_job_db_name,,}_trn_${backup_job_db_host,,} pre_dbbackup "${backup_job_db_name}" write_log notice "Dumping MSSQL database: '${DB_NAME}'" + if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug on; fi silent run_as_user ${play_fair} /opt/mssql-tools18/bin/sqlcmd -C -S ${backup_job_db_host}\,${backup_job_db_port} -U ${backup_job_db_user} -P ${backup_job_db_pass} -Q "BACKUP LOG [${backup_job_db_name}] TO DISK = N'${TEMP_PATH}/${backup_job_filename}' WITH NOFORMAT, NOINIT, NAME = '${backup_job_db_name}-log', SKIP, NOREWIND, NOUNLOAD, STATS = 10" exit_code=$? + if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug off; fi backup_job_filename_original=${backup_job_filename} compression pre_dbbackup all @@ -533,11 +539,10 @@ backup_mssql() { post_dbbackup "${backup_job_db_name}" ;; esac - if var_true "${DEBUG_BACKUP_MSSQL}" ; then debug off; fi } backup_mysql() { - if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug on; fi + if var_true "${backup_job_mysql_events}" ; then events="--events" fi @@ -572,8 +577,10 @@ backup_mysql() { compression pre_dbbackup "${db}" write_log notice "Dumping MySQL/MariaDB database: '${db}' ${compression_string}" + if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug on; fi run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} $db | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? + if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug off; fi check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption @@ -590,8 +597,10 @@ backup_mysql() { compression pre_dbbackup all write_log notice "Dumping all MySQL / MariaDB databases: '$(echo ${db_names} | xargs | tr ' ' ',')' ${compression_string}" + if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug on; fi run_as_user ${play_fair} mysqldump --max-allowed-packet=${backup_job_mysql_max_allowed_packet} -h ${backup_job_db_host} -P ${backup_job_db_port} -u${backup_job_db_user} ${events} ${single_transaction} ${stored_procedures} ${mysql_tls_args} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} --databases $(echo ${db_names} | xargs) | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? + if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug off; fi check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption @@ -600,19 +609,19 @@ backup_mysql() { check_exit_code move "${backup_job_filename}" post_dbbackup all fi - if var_true "${DEBUG_BACKUP_MYSQL}" ; then debug off; fi } backup_pgsql() { - if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug on; fi backup_pgsql_globals() { prepare_dbbackup backup_job_filename=pgsql_globals_${backup_job_db_host,,}_${now}.sql compression pre_dbbackup "globals" print_notice "Dumping PostgresSQL globals: with 'pg_dumpall -g' ${compression_string}" + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug on; fi run_as_user ${play_fair} pg_dumpall -h "${backup_job_db_host}" -U "${backup_job_db_user}" -p "${backup_job_db_port}" -g ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug off; fi check_exit_code "${backup_job_filename}" timer backup finish file_encryption @@ -656,8 +665,10 @@ backup_pgsql() { compression pre_dbbackup "${db}" write_log notice "Dumping PostgresSQL database: '${db}' ${compression_string}" + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug on; fi run_as_user ${play_fair} pg_dump -h "${backup_job_db_host}" -p "${backup_job_db_port}" -U "${backup_job_db_user}" $db ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug off; fi check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption @@ -683,8 +694,10 @@ backup_pgsql() { for x_db_name in ${tmp_db_names} ; do pgexclude_arg=$(echo ${pgexclude_arg} --exclude-database=${x_db_name}) done + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug on; fi run_as_user ${play_fair} pg_dumpall -h ${backup_job_db_host} -p ${backup_job_db_port} -U ${backup_job_db_user} ${pgexclude_arg} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} | ${compress_cmd} | run_as_user tee "${TEMP_PATH}"/"${backup_job_filename}" > /dev/null exit_code=$? + if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug off; fi check_exit_code backup "${backup_job_filename}" timer backup finish file_encryption @@ -694,15 +707,14 @@ backup_pgsql() { post_dbbackup all if var_true "${_postgres_backup_globals}" ; then backup_pgsql_globals; fi fi - if var_true "${DEBUG_BACKUP_PGSQL}" ; then debug on; fi } backup_redis() { - if var_true "${DEBUG_BACKUP_REDIS}" ; then debug on; fi prepare_dbbackup write_log notice "Dumping Redis - Flushing Redis Cache First" backup_job_filename=redis_all_${backup_job_db_host,,}_${now}.rdb backup_job_filename_base=redis_${backup_job_db_host,,} + if var_true "${DEBUG_BACKUP_REDIS}" ; then debug on; fi echo bgsave | silent run_as_user ${play_fair} redis-cli -h ${backup_job_db_host} -p ${backup_job_db_port} ${REDIS_PASS_STR} --rdb ${TEMP_PATH}/${backup_job_filename} ${backup_job_extra_opts} ${backup_job_extra_dump_opts} sleep 10 try=5 @@ -723,17 +735,16 @@ backup_redis() { pre_dbbackup all run_as_user ${compress_cmd} "${TEMP_PATH}/${backup_job_filename_original}" timer backup finish + if var_true "${DEBUG_BACKUP_REDIS}" ; then debug on; fi check_exit_code backup "${backup_job_filename}" file_encryption generate_checksum move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup all - if var_true "${DEBUG_BACKUP_REDIS}" ; then debug off; fi } backup_sqlite3() { - if var_true "${DEBUG_BACKUP_SQLITE3}" ; then debug on; fi prepare_dbbackup db=$(basename "${backup_job_db_host}") db="${db%.*}" @@ -742,17 +753,18 @@ backup_sqlite3() { compression pre_dbbackup "${db}" write_log notice "Dumping sqlite3 database: '${backup_job_db_host}' ${compression_string}" + if var_true "${DEBUG_BACKUP_SQLITE3}" ; then debug on; fi silent run_as_user ${play_fair} sqlite3 "${backup_job_db_host}" ".backup '${TEMP_PATH}/backup.sqlite3'" exit_code=$? check_exit_code backup "${backup_job_filename}" run_as_user ${play_fair} cat "${TEMP_PATH}"/backup.sqlite3 | ${dir_compress_cmd} | run_as_user tee "${TEMP_PATH}/${backup_job_filename}" > /dev/null timer backup finish + if var_true "${DEBUG_BACKUP_SQLITE3}" ; then debug off; fi file_encryption generate_checksum move_dbbackup check_exit_code move "${backup_job_filename}" post_dbbackup "${db}" - if var_true "${DEBUG_BACKUP_SQLITE3}" ; then debug off; fi } check_availability() { @@ -1085,10 +1097,16 @@ debug() { case "${1}" in off) DEBUG_MODE=${OLD_DEBUG_MODE} + if var_true "${DEBUG_MODE}" ; then + set -x + else + set +x + fi + ;; on) OLD_DEBUG_MODE=${DEBUG_MODE} - DEBUG_MODE=TRUE + set -x ;; esac } From 41b518f2f0b6e4e0768e3b734fef1f26cc97b12b Mon Sep 17 00:00:00 2001 From: Dave Conroy Date: Wed, 8 Nov 2023 18:18:05 -0800 Subject: [PATCH 68/68] Add a failsafe for _original_debug_mode --- install/assets/functions/10-db-backup | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/install/assets/functions/10-db-backup b/install/assets/functions/10-db-backup index 27ce875..f617ebb 100644 --- a/install/assets/functions/10-db-backup +++ b/install/assets/functions/10-db-backup @@ -1096,16 +1096,17 @@ db_backup_container_init() { debug() { case "${1}" in off) - DEBUG_MODE=${OLD_DEBUG_MODE} + DEBUG_MODE=${_original_debug_mode} if var_true "${DEBUG_MODE}" ; then set -x else set +x fi - ;; on) - OLD_DEBUG_MODE=${DEBUG_MODE} + if [ -z "${_original_debug_mode}" ]; then + _original_debug_mode="${DEBUG_MODE}" + fi set -x ;; esac