diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc4b11..9482895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.19.0 2020-04-22 + + ### Added + - Custom Script support to execute upon compleition of backup + + ## 1.18.2 2020-04-08 ### Changed diff --git a/README.md b/README.md index fa839ed..266d720 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Currently backs up CouchDB, InfluxDB, MySQL, MongoDB Postgres, Redis, Rethink se * connect to any container running on the same system * 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 * This Container uses a [customized Alpine Linux base](https://hub.docker.com/r/tiredofit/alpine) which includes [s6 overlay](https://github.com/just-containers/s6-overlay) enabled for PID 1 Init capabilities, [zabbix-agent](https://zabbix.org) for individual container monitoring, Cron also installed along with other tools (bash,curl, less, logrotate, nano, vim) for easier management. It also supports sending to external SMTP servers. @@ -42,6 +43,7 @@ Currently backs up CouchDB, InfluxDB, MySQL, MongoDB Postgres, Redis, Rethink se - [Environment Variables](#environmentvariables) - [Maintenance](#maintenance) - [Shell Access](#shell-access) + - [Custom Scripts](#custom-scripts) # Prerequisites @@ -76,6 +78,7 @@ The following directories are used for configuration and can be mapped for persi | Directory | Description | |-----------|-------------| | `/backup` | Backups | +| `/assets/custom-scripts | *Optional* Put custom scripts in this directory to execute after backup operations` ## Environment Variables @@ -107,6 +110,7 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co Manual Backups can be perforemd by entering the container and typing `backup-now` + #### Shell Access For debugging and maintenance purposes you may want access the containers shell. @@ -115,3 +119,28 @@ For debugging and maintenance purposes you may want access the containers shell. docker exec -it (whatever your container name is e.g.) db-backup bash ``` +#### Custom Scripts + +If you want to execute a custom script at the end of backup, you can drop bash scripts with the extension of `.sh` in this directory. See the following example to utilize: + +````bash +$ cat post-script.sh +##!/bin/bash + +## Example Post Script +## $1=DB_TYPE (Type of Backup) +## $2=DB_HOST (Backup Host) +## #3=DB_NAME (Name of Database backed up +## $4=DATE (Date of Backup) +## $5=TIME (Time of Backup) +## $6=BACKUP_FILENAME (Filename of Backup) +## $7=FILESIZE (Filesize of backup) +## $8=MD5_RESULT (MD5Sum if enabled) + +echo "${1} Backup Completed on ${2} for ${3} on ${4} ${5}. Filename: ${5} Size: ${6} MD5: ${7}" +```` +Outputs the following on the console: + +`mysql Backup Completed on example-db for example on 2020-04-22 05:19:10. Filename: mysql_example_example-db_20200422-051910.sql.bz2 Size: 7795 bytes MD5: 952fbaafa30437494fdf3989a662cd40` + +If you wish to change the size value from bytes to megabytes set environment variable `SIZE_VALUE=megabytes` \ No newline at end of file diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml index cf43339..bd2da49 100644 --- a/examples/docker-compose.yml +++ b/examples/docker-compose.yml @@ -6,6 +6,7 @@ services: image: mariadb:latest volumes: - ./db:/var/lib/mysql + - ./post-script.sh:/assets/custom-scripts/post-script.sh environment: - MYSQL_ROOT_PASSWORD=examplerootpassword - MYSQL_DATABASE=example diff --git a/examples/post-script.sh b/examples/post-script.sh new file mode 100755 index 0000000..016ab4f --- /dev/null +++ b/examples/post-script.sh @@ -0,0 +1,13 @@ +##!/bin/bash + +## Example Post Script +## $1=DB_TYPE (Type of Backup) +## $2=DB_HOST (Backup Host) +## #3=DB_NAME (Name of Database backed up +## $4=DATE (Date of Backup) +## $5=TIME (Time of Backup) +## $6=BACKUP_FILENAME (Filename of Backup) +## $7=FILESIZE (Filesize of backup) +## $8=MD5_RESULT (MD5Sum if enabled) + +echo "${1} Backup Completed on ${2} for ${3} on ${4} ${5}. Filename: ${6} Size: ${7} bytes MD5: ${8}" diff --git a/install/etc/s6/services/10-db-backup/run b/install/etc/s6/services/10-db-backup/run index 619f612..f0d53da 100755 --- a/install/etc/s6/services/10-db-backup/run +++ b/install/etc/s6/services/10-db-backup/run @@ -27,6 +27,7 @@ DBTYPE=${DB_TYPE} MD5=${MD5:-TRUE} SPLIT_DB=${SPLIT_DB:-FALSE} TMPDIR=/tmp/backups +SIZE_VALUE=${SIZE_VALUE:-"bytes"} if [ "$1" = "NOW" ]; then DB_DUMP_BEGIN=+0 @@ -279,6 +280,7 @@ function generate_md5() { if [ "$MD5" = "TRUE" ] || [ "$MD5" = "true" ] ; then cd $TMPDIR md5sum ${TARGET} > ${TARGET}.md5 + MD5VALUE=$(md5sum ${TARGET} | awk '{ print $1}') fi } @@ -286,6 +288,22 @@ function move_backup() { mkdir -p ${DB_DUMP_TARGET} mv ${TMPDIR}/*.md5 ${DB_DUMP_TARGET}/ mv ${TMPDIR}/${TARGET} ${DB_DUMP_TARGET}/${TARGET} + case "$SIZE_VALUE" in + "b" | "bytes" ) + SIZE_VALUE=1 + ;; + "[kK]" | "[kK][bB]" | "kilobytes" | "[mM]" | "[mM][bB]" | "megabytes" ) + SIZE_VALUE="-h" + ;; + *) + SIZE_VALUE=1 + ;; + esac + if [ "$SIZE_VALUE" = "1" ] ; then + FILESIZE=$(stat -c%s "${DB_DUMP_TARGET}/${TARGET}") + else + FILESIZE=$(du -h "${DB_DUMP_TARGET}/${TARGET}" | awk '{ print $1}') + fi } @@ -316,6 +334,8 @@ print_info "Initialized on `date`" ### Define Target name now=$(date +"%Y%m%d-%H%M%S") + now_time=$(date +"%H:%M:%S") + now_date=$(date +"%Y-%m-%d") TARGET=${DBTYPE}_${DBNAME}_${DBHOST}_${now}.sql ### Take a Dump @@ -361,6 +381,17 @@ print_info "Initialized on `date`" find $DB_DUMP_TARGET/ -mmin +$DB_CLEANUP_TIME -iname "$DBTYPE_$DBNAME_*.*" -exec rm {} \; fi +### Post Backup Custom Script Support + if [ -d /assets/custom-scripts/ ] ; then + print_info "Found Custom Scripts to Execute" + for f in $(find /assets/custom-scripts/ -name \*.sh -type f); do + print_info "Running Script ${f}" + ## script DB_TYPE DB_HOST DB_NAME DATE BACKUP_FILENAME FILESIZE MD5_VALUE + chmod +x ${f} + ${f} "${DBTYPE}" "${DBHOST}" "${DBNAME}" "${now_date}" "${now_time}" "${TARGET}" "${FILESIZE}" "${MD5VALUE}" + done + fi + ### Go back to Sleep until next Backup time if [ "$MANUAL" = "TRUE" ]; then exit 1;