mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-22 05:33:53 +01:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
859ce5ffa3 | ||
|
|
4d1577e553 | ||
|
|
4f103a5b36 | ||
|
|
0472cba83d | ||
|
|
fe6fab857f | ||
|
|
6113bf64b2 | ||
|
|
95d1129a12 | ||
|
|
f8bab5f045 | ||
|
|
71802d2a28 | ||
|
|
c96a2179b5 | ||
|
|
42d3aa0fef | ||
|
|
e6009e7a1e | ||
|
|
b5466d5b97 | ||
|
|
06b6e685c7 |
53
CHANGELOG.md
53
CHANGELOG.md
@@ -1,3 +1,56 @@
|
||||
## 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
|
||||
- Rework to allow passwords with spaces in them for MariaDB / MySQL
|
||||
|
||||
## 1.18.1 2020-03-14 <dave at tiredofit dot ca>
|
||||
|
||||
### Changed
|
||||
- Allow for passwords with spaces in them for MariaDB / MySQL
|
||||
|
||||
|
||||
## 1.18.0 2019-12-29 <dave at tiredofit dot ca>
|
||||
|
||||
### Added
|
||||
- Update image to support new tiredofit/alpine base images
|
||||
|
||||
|
||||
## 1.17.3 2019-12-12 <dave at tiredofit dot ca>
|
||||
|
||||
### Changed
|
||||
- Quiet down Zabbix Agent
|
||||
|
||||
|
||||
## 1.17.2 2019-12-12 <dave at tiredofit dot ca>
|
||||
|
||||
### Changed
|
||||
- Re Enable ZABBIX
|
||||
|
||||
|
||||
## 1.17.1 2019-12-10 <dave at tiredofit dot ca>
|
||||
|
||||
### Changed
|
||||
- Fix spelling mistake in container initialization
|
||||
|
||||
|
||||
## 1.17.0 2019-12-09 <dave at tiredofit dot ca>
|
||||
|
||||
### Changed
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019 Dave Conroy
|
||||
Copyright (c) 2020 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
|
||||
|
||||
33
README.md
33
README.md
@@ -10,7 +10,7 @@
|
||||
|
||||
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
|
||||
* select database user and password
|
||||
@@ -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
|
||||
@@ -105,7 +108,8 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co
|
||||
|
||||
## 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 +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: ${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`
|
||||
|
||||
@@ -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
|
||||
|
||||
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}"
|
||||
@@ -1,26 +1,19 @@
|
||||
#!/usr/bin/with-contenv bash
|
||||
|
||||
for s in /assets/functions/*; do source $s; done
|
||||
PROCESS_NAME="db-backup"
|
||||
|
||||
date >/dev/null
|
||||
|
||||
if [ "$1" != "NOW" ]; then
|
||||
sleep 10
|
||||
fi
|
||||
|
||||
### Set Debug Mode
|
||||
if [ "$DEBUG_MODE" = "TRUE" ] || [ "$DEBUG_MODE" = "true" ]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
### Sanity Test
|
||||
if [ ! -n "$DB_TYPE" ]; then
|
||||
echo '** [db-backup] ERROR: No Database Type Selected! '
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -n "$DB_HOST" ]; then
|
||||
echo '** [db-backup] ERROR: No Database Host Entered! '
|
||||
exit 1
|
||||
fi
|
||||
sanity_var DB_TYPE "Database Type"
|
||||
sanity_var DB_HOST "Database Host"
|
||||
file_env 'DB_USER'
|
||||
file_env 'DB_PASS'
|
||||
|
||||
### Set Defaults
|
||||
COMPRESSION=${COMPRESSION:-GZ}
|
||||
@@ -34,6 +27,7 @@ DBPASS=${DB_PASS}
|
||||
DBUSER=${DB_USER}
|
||||
DBTYPE=${DB_TYPE}
|
||||
MD5=${MD5:-TRUE}
|
||||
SIZE_VALUE=${SIZE_VALUE:-"bytes"}
|
||||
SPLIT_DB=${SPLIT_DB:-FALSE}
|
||||
TMPDIR=/tmp/backups
|
||||
|
||||
@@ -74,7 +68,7 @@ fi
|
||||
"mysql" | "MYSQL" | "mariadb" | "MARIADB")
|
||||
DBTYPE=mysql
|
||||
DBPORT=${DB_PORT:-3306}
|
||||
[[ ( -n "${DB_PASS}" ) ]] && MYSQL_PASS_STR=" -p${DBPASS}"
|
||||
[[ ( -n "${DB_PASS}" ) ]] && export MYSQL_PWD=${DBPASS}
|
||||
;;
|
||||
"postgres" | "postgresql" | "pgsql" | "POSTGRES" | "POSTGRESQL" | "PGSQL" )
|
||||
DBTYPE=pgsql
|
||||
@@ -105,20 +99,20 @@ function backup_couch() {
|
||||
|
||||
function backup_mysql() {
|
||||
if [ "$SPLIT_DB" = "TRUE" ] || [ "$SPLIT_DB" = "true" ]; then
|
||||
DATABASES=`mysql -h $DBHOST -P $DBPORT -u$DBUSER -p$DBPASS --batch -e "SHOW DATABASES;" | grep -v Database|grep -v schema`
|
||||
DATABASES=`mysql -h ${DBHOST} -P $DBPORT -u$DBUSER --batch -e "SHOW DATABASES;" | grep -v Database|grep -v schema`
|
||||
|
||||
for db in $DATABASES; do
|
||||
if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
|
||||
echo "** [db-backup] Dumping database: $db"
|
||||
TARGET=mysql_${db}_${DBHOST}_${now}.sql
|
||||
mysqldump --max-allowed-packet=512M -h $DBHOST -P $DBPORT -u$DBUSER ${MYSQL_PASS_STR} --databases $db > ${TMPDIR}/${TARGET}
|
||||
mysqldump --max-allowed-packet=512M -h $DBHOST -P $DBPORT -u$DBUSER --databases $db > ${TMPDIR}/${TARGET}
|
||||
generate_md5
|
||||
compression
|
||||
move_backup
|
||||
fi
|
||||
done
|
||||
else
|
||||
mysqldump --max-allowed-packet=512M -A -h $DBHOST -P $DBPORT -u$DBUSER ${MYSQL_PASS_STR} > ${TMPDIR}/${TARGET}
|
||||
mysqldump --max-allowed-packet=512M -A -h $DBHOST -P $DBPORT -u$DBUSER > ${TMPDIR}/${TARGET}
|
||||
generate_md5
|
||||
compression
|
||||
move_backup
|
||||
@@ -149,7 +143,7 @@ function backup_pgsql() {
|
||||
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
|
||||
echo "** [db-backup] Dumping database: $db"
|
||||
print_info "Dumping database: $db"
|
||||
TARGET=pgsql_${db}_${DBHOST}_${now}.sql
|
||||
pg_dump -h ${DBHOST} -p ${DBPORT} -U ${DBUSER} $db > ${TMPDIR}/${TARGET}
|
||||
generate_md5
|
||||
@@ -168,17 +162,17 @@ function backup_pgsql() {
|
||||
function backup_redis() {
|
||||
TARGET=redis_${db}_${DBHOST}_${now}.rdb
|
||||
echo bgsave | redis-cli -h ${DBHOST} -p ${DBPORT} ${REDIS_PASS_STR} --rdb ${TMPDIR}/${TARGET}
|
||||
echo "** [db-backup] Dumping Redis - Flushing Redis Cache First"
|
||||
print_info "Dumping Redis - Flushing Redis Cache First"
|
||||
sleep 10
|
||||
try=5
|
||||
while [ $try -gt 0 ] ; do
|
||||
saved=$(echo 'info Persistence' | redis-cli -h ${DBHOST} -p ${DBPORT} ${REDIS_PASS_STR} | awk '/rdb_bgsave_in_progress:0/{print "saved"}')
|
||||
ok=$(echo 'info Persistence' | redis-cli -h ${DBHOST} -p ${DBPORT} ${REDIS_PASS_STR} | awk '/rdb_last_bgsave_status:ok/{print "ok"}')
|
||||
if [[ "$saved" = "saved" ]] && [[ "$ok" = "ok" ]]; then
|
||||
echo "** [db-backup] Redis Backup Complete"
|
||||
print_info "Redis Backup Complete"
|
||||
fi
|
||||
try=$((try - 1))
|
||||
echo "** [db-backup] Redis Busy - Waiting and retrying in 5 seconds"
|
||||
print_info "Redis Busy - Waiting and retrying in 5 seconds"
|
||||
sleep 5
|
||||
done
|
||||
generate_md5
|
||||
@@ -188,7 +182,7 @@ function backup_redis() {
|
||||
|
||||
function backup_rethink() {
|
||||
TARGET=rethink_${db}_${DBHOST}_${now}.tar.gz
|
||||
echo "** [db-backup] Dumping rethink Database: $db"
|
||||
print_info "Dumping rethink Database: $db"
|
||||
rethinkdb dump -f ${TMPDIR}/${TARGET} -c ${DBHOST}:${DBPORT} ${RETHINK_PASS_STR} ${RETHINK_DB_STR}
|
||||
move_backup
|
||||
}
|
||||
@@ -201,7 +195,7 @@ function check_availability() {
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] CouchDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "CouchDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"influx" )
|
||||
@@ -209,7 +203,7 @@ function check_availability() {
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] InfluxDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "InfluxDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"mongo" )
|
||||
@@ -217,7 +211,7 @@ function check_availability() {
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] Mongo Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "Mongo Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"mysql" )
|
||||
@@ -230,7 +224,7 @@ function check_availability() {
|
||||
:
|
||||
break
|
||||
fi
|
||||
echo "** [db-backup] MySQL/MariaDB Server "$DBHOST" is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "MySQL/MariaDB Server "$DBHOST" is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
done
|
||||
@@ -243,7 +237,7 @@ function check_availability() {
|
||||
do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] Postgres Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "Postgres Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"redis" )
|
||||
@@ -251,7 +245,7 @@ function check_availability() {
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] Redis Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "Redis Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"rethink" )
|
||||
@@ -259,7 +253,7 @@ function check_availability() {
|
||||
while ! (nc -z ${DBHOST} ${DBPORT}) ; do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] RethinkDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
print_warn "RethinkDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
esac
|
||||
@@ -288,6 +282,7 @@ function generate_md5() {
|
||||
if [ "$MD5" = "TRUE" ] || [ "$MD5" = "true" ] ; then
|
||||
cd $TMPDIR
|
||||
md5sum ${TARGET} > ${TARGET}.md5
|
||||
MD5VALUE=$(md5sum ${TARGET} | awk '{ print $1}')
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -295,11 +290,27 @@ 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
|
||||
}
|
||||
|
||||
|
||||
### Container Startup
|
||||
echo '** [db-backup] Initialized at at '$(date)
|
||||
print_info "Initialized on `date`"
|
||||
|
||||
### Wait for Next time to start backup
|
||||
current_time=$(date +"%s")
|
||||
@@ -325,6 +336,8 @@ echo '** [db-backup] Initialized at at '$(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,15 +374,26 @@ echo '** [db-backup] Initialized at at '$(date)
|
||||
|
||||
### Zabbix
|
||||
if [ "$ENABLE_ZABBIX" = "TRUE" ] || [ "$ENABLE_ZABBIX" = "true" ]; then
|
||||
zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.size -o `stat -c%s ${DB_DUMP_TARGET}/${TARGET}`
|
||||
zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k dbbackup.datetime -o `date -r ${DB_DUMP_TARGET}/${TARGET} +'%s'`
|
||||
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
|
||||
exit 1;
|
||||
|
||||
Reference in New Issue
Block a user