mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-21 21:33:28 +01:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd141cc865 | ||
|
|
abf2a877f7 | ||
|
|
3115cb3440 | ||
|
|
859ce5ffa3 | ||
|
|
4d1577e553 | ||
|
|
4f103a5b36 | ||
|
|
0472cba83d | ||
|
|
fe6fab857f | ||
|
|
6113bf64b2 | ||
|
|
95d1129a12 | ||
|
|
f8bab5f045 | ||
|
|
b5466d5b97 |
34
CHANGELOG.md
34
CHANGELOG.md
@@ -1,3 +1,37 @@
|
||||
## 1.21.1 2020-06-04 <dave at tiredofit dot ca>
|
||||
|
||||
### Changed
|
||||
- Bugfix to initalization routine
|
||||
|
||||
|
||||
## 1.21.0 2020-06-03 <dave at tiredofit dot ca>
|
||||
|
||||
### Added
|
||||
- Add S3 Compatible Storage Support
|
||||
|
||||
### Changed
|
||||
- Switch some variables to support tiredofit/alpine base image better
|
||||
- Fix issue with parallel compression not working correctly
|
||||
|
||||
|
||||
## 1.20.1 2020-04-24 <dave at tiredofit dot ca>
|
||||
|
||||
### Changed
|
||||
- Fix Auto Cleanup routines when using `root` as username
|
||||
|
||||
|
||||
## 1.20.0 2020-04-22 <dave at tiredofit dot ca>
|
||||
|
||||
### Added
|
||||
- Docker Secrets Support for DB_USER and DB_PASS variables
|
||||
|
||||
|
||||
## 1.19.0 2020-04-22 <dave at tiredofit dot ca>
|
||||
|
||||
### Added
|
||||
- Custom Script support to execute upon compleition of backup
|
||||
|
||||
|
||||
## 1.18.2 2020-04-08 <hyun007 @ github>
|
||||
|
||||
### Changed
|
||||
|
||||
49
README.md
49
README.md
@@ -10,9 +10,9 @@
|
||||
|
||||
This will build a container for backing up multiple type of DB Servers
|
||||
|
||||
Currently backs up CouchDB, InfluxDB, MySQL, MongoDB Postgres, Redis, Rethink servers.
|
||||
Currently backs up CouchDB, InfluxDB, MySQL, MongoDB, Postgres, Redis, Rethink servers.
|
||||
|
||||
* dump to local filesystem
|
||||
* dump to local filesystem or backup to S3 Compatible services
|
||||
* select database user and password
|
||||
* backup all databases
|
||||
* choose to have an MD5 sum after backup for verification
|
||||
@@ -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
|
||||
@@ -85,6 +88,7 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co
|
||||
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `BACKUP_LOCATION` | Backup to `FILESYSTEM` or `S3` compatible services like S3, Minio, Wasabi - Default `FILESYSTEM`
|
||||
| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, or none `NONE` - Default `GZ`
|
||||
| `DB_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `pgsql` `mongo` `redis` `rethink`
|
||||
| `DB_HOST` | Server Hostname e.g. `mariadb`
|
||||
@@ -102,10 +106,24 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co
|
||||
| `PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` - Default `TRUE` |
|
||||
| `SPLIT_DB` | If using root as username and multiple DBs on system, set to TRUE to create Seperate DB Backups instead of all in one. - Default `FALSE` |
|
||||
|
||||
**Backing Up to S3 Compatible Services**
|
||||
|
||||
If `BACKUP_LOCATION` = `S3` then the following options are used.
|
||||
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `S3_BUCKET` | S3 Bucket name e.g. 'mybucket' |
|
||||
| `S3_HOSTNAME` | Hostname of S3 Server e.g "s3.amazonaws.com" - You can also include a port if necessary
|
||||
| `S3_KEY_ID` | S3 Key ID |
|
||||
| `S3_KEY_SECRET` | S3 Key Secret |
|
||||
| `S3_PATH` | S3 Pathname to save to e.g. '`backup`' |
|
||||
| `S3_PROTOCOL` | Use either `http` or `https` to access service - Default `https` |
|
||||
| `S3_URI_STYLE` | Choose either `VIRTUALHOST` or `PATH` style - Default `VIRTUALHOST`
|
||||
|
||||
## Maintenance
|
||||
|
||||
Manual Backups can be perforemd by entering the container and typing `backup-now`
|
||||
Manual Backups can be performed by entering the container and typing `backup-now`
|
||||
|
||||
|
||||
#### Shell Access
|
||||
|
||||
@@ -115,3 +133,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: ${6} Size: ${7} bytes MD5: ${8}"
|
||||
````
|
||||
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`
|
||||
|
||||
@@ -20,6 +20,7 @@ services:
|
||||
- example-db
|
||||
volumes:
|
||||
- ./backups:/backup
|
||||
- ./post-script.sh:/assets/custom-scripts/post-script.sh
|
||||
environment:
|
||||
- DB_TYPE=mariadb
|
||||
- DB_HOST=example-db
|
||||
|
||||
13
examples/post-script.sh
Executable file
13
examples/post-script.sh
Executable file
@@ -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}"
|
||||
@@ -12,29 +12,46 @@ fi
|
||||
### Sanity Test
|
||||
sanity_var DB_TYPE "Database Type"
|
||||
sanity_var DB_HOST "Database Host"
|
||||
file_env 'DB_USER'
|
||||
file_env 'DB_PASS'
|
||||
|
||||
### Set Defaults
|
||||
BACKUP_LOCATION=${BACKUP_LOCATION:-"FILESYSTEM"}
|
||||
COMPRESSION=${COMPRESSION:-GZ}
|
||||
PARALLEL_COMPRESSION=${PARALLEL_COMPRESSION:-TRUE}
|
||||
DB_DUMP_FREQ=${DB_DUMP_FREQ:-1440}
|
||||
DB_DUMP_BEGIN=${DB_DUMP_BEGIN:-+0}
|
||||
DB_DUMP_FREQ=${DB_DUMP_FREQ:-1440}
|
||||
DB_DUMP_TARGET=${DB_DUMP_TARGET:-/backup}
|
||||
DBHOST=${DB_HOST}
|
||||
DBNAME=${DB_NAME}
|
||||
DBPASS=${DB_PASS}
|
||||
DBUSER=${DB_USER}
|
||||
DBTYPE=${DB_TYPE}
|
||||
DBUSER=${DB_USER}
|
||||
MD5=${MD5:-TRUE}
|
||||
PARALLEL_COMPRESSION=${PARALLEL_COMPRESSION:-TRUE}
|
||||
SIZE_VALUE=${SIZE_VALUE:-"bytes"}
|
||||
SPLIT_DB=${SPLIT_DB:-FALSE}
|
||||
TMPDIR=/tmp/backups
|
||||
|
||||
if [ "BACKUP_TYPE" = "S3" ] || [ "BACKUP_TYPE" = "s3" ] || [ "BACKUP_TYPE" = "MINIO" ] || [ "BACKUP_TYPE" = "minio" ] ; then
|
||||
S3_PROTOCOL=${S3_PROTOCOL:-"https"}
|
||||
sanity_var S3_HOST "S3 Host"
|
||||
sanity_var S3_BUCKET "S3 Bucket"
|
||||
sanity_var S3_KEY_ID "S3 Key ID"
|
||||
sanity_var S3_KEY_SECRET "S3 Key Secret"
|
||||
sanity_var S3_URI_STYLE "S3 URI Style (Virtualhost or Path)"
|
||||
sanity_var S3_PATH "S3 Path"
|
||||
file_env 'S3_KEY_ID'
|
||||
file_env 'S3_KEY_SECRET'
|
||||
|
||||
fi
|
||||
|
||||
if [ "$1" = "NOW" ]; then
|
||||
DB_DUMP_BEGIN=+0
|
||||
MANUAL=TRUE
|
||||
fi
|
||||
|
||||
### Set Compression Options
|
||||
if [ "$PARALLEL_COMPRESSION" = "TRUE " ]; then
|
||||
if var_true $PARALLEL_COMPRESSION ; then
|
||||
BZIP="pbzip2"
|
||||
GZIP="pigz"
|
||||
XZIP="pixz"
|
||||
@@ -95,7 +112,7 @@ function backup_couch() {
|
||||
}
|
||||
|
||||
function backup_mysql() {
|
||||
if [ "$SPLIT_DB" = "TRUE" ] || [ "$SPLIT_DB" = "true" ]; then
|
||||
if var_true $SPLIT_DB ; then
|
||||
DATABASES=`mysql -h ${DBHOST} -P $DBPORT -u$DBUSER --batch -e "SHOW DATABASES;" | grep -v Database|grep -v schema`
|
||||
|
||||
for db in $DATABASES; do
|
||||
@@ -136,7 +153,7 @@ function backup_mongo() {
|
||||
}
|
||||
|
||||
function backup_pgsql() {
|
||||
if [ "$SPLIT_DB" = "TRUE" ] || [ "$SPLIT_DB" = "true" ]; then
|
||||
if var_true $SPLIT_DB ; then
|
||||
export PGPASSWORD=${DBPASS}
|
||||
DATABASES=`psql -h $DBHOST -U $DBUSER -p ${DBPORT} -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' `
|
||||
for db in $DATABASES; do
|
||||
@@ -276,16 +293,71 @@ function compression() {
|
||||
}
|
||||
|
||||
function generate_md5() {
|
||||
if [ "$MD5" = "TRUE" ] || [ "$MD5" = "true" ] ; then
|
||||
if var_true $MD5 ; then
|
||||
cd $TMPDIR
|
||||
md5sum ${TARGET} > ${TARGET}.md5
|
||||
MD5VALUE=$(md5sum ${TARGET} | awk '{ print $1}')
|
||||
fi
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
case "${BACKUP_LOCATION}" in
|
||||
"FILE" | "file" | "filesystem" | "FILESYSTEM" )
|
||||
mkdir -p ${DB_DUMP_TARGET}
|
||||
mv ${TMPDIR}/*.md5 ${DB_DUMP_TARGET}/
|
||||
mv ${TMPDIR}/${TARGET} ${DB_DUMP_TARGET}/${TARGET}
|
||||
;;
|
||||
"S3" | "s3" | "MINIO" | "minio" )
|
||||
s3_content_type="application/octet-stream"
|
||||
if [ "$S3_URI_STYLE" = "VIRTUALHOST" ] || [ "$S3_URI_STYLE" = "VHOST" ] [ "$S3_URI_STYLE" = "virtualhost" ] [ "$S3_URI_STYLE" = "vhost" ] ; then
|
||||
s3_url="${S3_BUCKET}.${S3_HOST}"
|
||||
else
|
||||
s3_url="${S3_HOST}/${S3_BUCKET}"
|
||||
fi
|
||||
|
||||
if var_true $MD5 ; then
|
||||
s3_date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
|
||||
s3_md5="$(libressl md5 -binary < "${TMPDIR}/${TARGET}.md5" | base64)"
|
||||
sig="$(printf "PUT\n$s3_md5\n${s3_content_type}\n$s3_date\n/$S3_BUCKET/$S3_PATH/${TARGET}.md5" | libressl sha1 -binary -hmac "${S3_KEY_SECRET}" | base64)"
|
||||
print_debug "Uploading ${TARGET}.md5 to S3"
|
||||
curl -T "${TMPDIR}/${TARGET}.md5" ${S3_PROTOCOL}://${s3_url}/${S3_PATH}/${TARGET}.md5 \
|
||||
-H "Date: $date" \
|
||||
-H "Authorization: AWS ${S3_KEY_ID}:$sig" \
|
||||
-H "Content-Type: ${s3_content_type}" \
|
||||
-H "Content-MD5: ${s3_md5}"
|
||||
fi
|
||||
|
||||
s3_date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
|
||||
s3_md5="$(libressl md5 -binary < "${TMPDIR}/${TARGET}" | base64)"
|
||||
sig="$(printf "PUT\n$s3_md5\n${s3_content_type}\n$s3_date\n/$S3_BUCKET/$S3_PATH/${TARGET}" | libressl sha1 -binary -hmac "${S3_KEY_SECRET}" | base64)"
|
||||
print_debug "Uploading ${TARGET} to S3"
|
||||
curl -T ${TMPDIR}/${TARGET} ${S3_PROTOCOL}://${s3_url}/${S3_PATH}/${TARGET} \
|
||||
-H "Date: $s3_date" \
|
||||
-H "Authorization: AWS ${S3_KEY_ID}:$sig" \
|
||||
-H "Content-Type: ${s3_content_type}" \
|
||||
-H "Content-MD5: ${s3_md5}"
|
||||
|
||||
rm -rf ${TMPDIR}/*.md5
|
||||
rm -rf ${TMPDIR}/${TARGET}
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
@@ -316,6 +388,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
|
||||
@@ -351,18 +425,29 @@ print_info "Initialized on `date`"
|
||||
esac
|
||||
|
||||
### Zabbix
|
||||
if [ "$ENABLE_ZABBIX" = "TRUE" ] || [ "$ENABLE_ZABBIX" = "true" ]; then
|
||||
if var_true $ENABLE_ZABBIX ; then
|
||||
silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.size -o `stat -c%s ${DB_DUMP_TARGET}/${TARGET}`
|
||||
silent zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.datetime -o `date -r ${DB_DUMP_TARGET}/${TARGET} +'%s'`
|
||||
fi
|
||||
|
||||
### Automatic Cleanup
|
||||
if [[ -n "$DB_CLEANUP_TIME" ]]; then
|
||||
find $DB_DUMP_TARGET/ -mmin +$DB_CLEANUP_TIME -iname "$DBTYPE_$DBNAME_*.*" -exec rm {} \;
|
||||
find $DB_DUMP_TARGET/ -mmin +$DB_CLEANUP_TIME -iname "*" -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
|
||||
if var_true $MANUAL ; then
|
||||
exit 1;
|
||||
else
|
||||
sleep $(($DB_DUMP_FREQ*60))
|
||||
|
||||
Reference in New Issue
Block a user