mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-21 13:23:12 +01:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78e7434a85 | ||
|
|
d1d093b87d | ||
|
|
ea00709aa1 | ||
|
|
219a6463f3 | ||
|
|
0ed89369a9 | ||
|
|
6bd534258e | ||
|
|
48bea7aeee | ||
|
|
c3179d58ba |
@@ -1,3 +1,12 @@
|
||||
## 1.16 - 2019-06-16 - <dave at tiredofit dot ca>
|
||||
|
||||
* Check to see if Database Exists before performing backup
|
||||
* Fix for MysQL/MariaDB custom ports - Credit to <spumer@github>
|
||||
|
||||
## 1.15 - 2019-05-24 - <claudioaltamura @ github>
|
||||
|
||||
* Added abaility to backup password protected Redis Hosts
|
||||
|
||||
## 1.14 - 2019-04-20 - <dave at tiredofit dot ca>
|
||||
|
||||
* Switch to using locally built mongodb-tools from tiredofit/mongo-builder due to Alpine removing precompiled packages from repositories
|
||||
|
||||
@@ -36,7 +36,7 @@ COPY --from=mongo-packages / /usr/src/apk
|
||||
apk add \
|
||||
pixz@testing \
|
||||
&& \
|
||||
|
||||
\
|
||||
## Locally Install Mongo Package
|
||||
cd /usr/src/apk && \
|
||||
apk add -t .db-backup-mongo-deps --allow-untrusted \
|
||||
|
||||
21
README.md
21
README.md
@@ -1,4 +1,4 @@
|
||||
# tiredofit/db-backup
|
||||
# hub.docker.com/r/tiredofit/db-backup
|
||||
|
||||
|
||||
[](https://hub.docker.com/r/tiredofit/db-backup)
|
||||
@@ -13,7 +13,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.
|
||||
|
||||
* dump to local filesystem
|
||||
* dump to local filesystem
|
||||
* select database user and password
|
||||
* backup all databases
|
||||
* choose to have an MD5 sum after backup for verification
|
||||
@@ -23,7 +23,7 @@ Currently backs up CouchDB, InfluxDB, MySQL, MongoDB Postgres, Redis, Rethink se
|
||||
* select how often to run a dump
|
||||
* select when to start the first dump, whether time of day or relative to container start time
|
||||
|
||||
This Container uses Alpine:Edge as a base.
|
||||
* 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) based on `3.4` compiled 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
|
||||
|
||||
[Changelog](CHANGELOG.md)
|
||||
|
||||
@@ -40,7 +40,7 @@ This Container uses Alpine:Edge as a base.
|
||||
- [Quick Start](#quick-start)
|
||||
- [Configuration](#configuration)
|
||||
- [Data Volumes](#data-volumes)
|
||||
- [Environment Variables](#environmentvariables)
|
||||
- [Environment Variables](#environmentvariables)
|
||||
- [Maintenance](#maintenance)
|
||||
- [Shell Access](#shell-access)
|
||||
|
||||
@@ -51,12 +51,12 @@ You must have a working DB server or container available for this to work proper
|
||||
|
||||
# Installation
|
||||
|
||||
Automated builds of the image are available on [Docker Hub](https://hub.docker.com/r/tiredofit/db-backup) and is the recommended
|
||||
Automated builds of the image are available on [Docker Hub](https://hub.docker.com/r/tiredofit/db-backup) and is the recommended
|
||||
method of installation.
|
||||
|
||||
|
||||
```bash
|
||||
docker pull tiredofit/db-backup
|
||||
docker pull tiredofit/db-backup:latest
|
||||
```
|
||||
|
||||
# Quick Start
|
||||
@@ -77,7 +77,7 @@ The following directories are used for configuration and can be mapped for persi
|
||||
| Directory | Description |
|
||||
|-----------|-------------|
|
||||
| `/backup` | Backups |
|
||||
|
||||
|
||||
|
||||
## Environment Variables
|
||||
|
||||
@@ -87,11 +87,12 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co
|
||||
| Parameter | Description |
|
||||
|-----------|-------------|
|
||||
| `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_SERVER` | Server Hostname e.g. `mariadb`
|
||||
| `DB_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `pgsql` `mongo` `redis` `rethink`
|
||||
| `DB_HOST` | Server Hostname e.g. `mariadb`
|
||||
| `DB_NAME` | Schema Name e.g. `database`
|
||||
| `DB_USER` | username for the database - use `root` to backup all MySQL of them.
|
||||
| `DB_PASS` | (optional if DB doesn't require it) password for the database
|
||||
| `DB_PORT` | (optional) Set port to connect to DB_HOST. Defaults are provided
|
||||
| `DB_DUMP_FREQ` | How often to do a dump, in minutes. Defaults to 1440 minutes, or once per day.
|
||||
| `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`
|
||||
@@ -109,7 +110,7 @@ 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.
|
||||
For debugging and maintenance purposes you may want access the containers shell.
|
||||
|
||||
```bash
|
||||
docker exec -it (whatever your container name is e.g.) db-backup bash
|
||||
|
||||
@@ -84,6 +84,7 @@ fi
|
||||
"redis" | "REDIS" )
|
||||
DBTYPE=redis
|
||||
DBPORT=${DB_PORT:-6379}
|
||||
[[ ( -n "${DB_PASS}" ) ]] && REDIS_PASS_STR=" -a ${DBPASS}"
|
||||
;;
|
||||
"rethink" | "RETHINK" )
|
||||
DBTYPE=rethink
|
||||
@@ -103,14 +104,14 @@ function backup_couch() {
|
||||
}
|
||||
|
||||
function backup_mysql() {
|
||||
if [ "$SPLIT_DB" = "TRUE" ] || [ "$SPLIT_DB" = "true" ]; then
|
||||
DATABASES=`mysql -h $DBHOST -u$DBUSER -p$DBPASS --batch -e "SHOW DATABASES;" | grep -v Database|grep -v schema`
|
||||
|
||||
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`
|
||||
|
||||
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 -u$DBUSER ${MYSQL_PASS_STR} --databases $db > ${TMPDIR}/${TARGET}
|
||||
mysqldump --max-allowed-packet=512M -h $DBHOST -P $DBPORT -u$DBUSER ${MYSQL_PASS_STR} --databases $db > ${TMPDIR}/${TARGET}
|
||||
generate_md5
|
||||
compression
|
||||
move_backup
|
||||
@@ -137,7 +138,7 @@ function backup_mongo() {
|
||||
mongodump --out ${TMPDIR}/${TARGET} --host ${DBHOST} --port ${DBPORT} ${MONGO_USER_STR}${MONGO_PASS_STR}${MONGO_DB_STR} ${EXTRA_OPTS}
|
||||
cd ${TMPDIR}
|
||||
tar cf ${TARGET}.tar ${TARGET}/*
|
||||
TARGET=${TARGET}.tar
|
||||
TARGET=${TARGET}.tar
|
||||
generate_md5
|
||||
compression
|
||||
move_backup
|
||||
@@ -166,13 +167,13 @@ function backup_pgsql() {
|
||||
|
||||
function backup_redis() {
|
||||
TARGET=redis_${db}_${DBHOST}_${now}.rdb
|
||||
echo bgsave | redis-cli -h ${DBHOST} -p ${DBPORT} --rdb ${TMPDIR}/${TARGET}
|
||||
echo bgsave | redis-cli -h ${DBHOST} -p ${DBPORT} ${REDIS_PASS_STR} --rdb ${TMPDIR}/${TARGET}
|
||||
echo "** [db-backup] 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} | awk '/rdb_bgsave_in_progress:0/{print "saved"}')
|
||||
ok=$(echo 'info Persistence' | redis-cli -h ${DBHOST} -p ${DBPORT} | awk '/rdb_last_bgsave_status:ok/{print "ok"}')
|
||||
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"
|
||||
fi
|
||||
@@ -186,12 +187,84 @@ function backup_redis() {
|
||||
}
|
||||
|
||||
function backup_rethink() {
|
||||
TARGET=rethink_${db}_${DBHOST}_${now}.tar.gz
|
||||
TARGET=rethink_${db}_${DBHOST}_${now}.tar.gz
|
||||
echo "** [db-backup] Dumping rethink Database: $db"
|
||||
rethinkdb dump -f ${TMPDIR}/${TARGET} -c ${DBHOST}:${DBPORT} ${RETHINK_PASS_STR} ${RETHINK_DB_STR}
|
||||
move_backup
|
||||
}
|
||||
|
||||
function check_availability() {
|
||||
### Set the Database Type
|
||||
case "$DBTYPE" in
|
||||
"couch" )
|
||||
COUNTER=0
|
||||
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)"
|
||||
done
|
||||
;;
|
||||
"influx" )
|
||||
COUNTER=0
|
||||
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)"
|
||||
done
|
||||
;;
|
||||
"mongo" )
|
||||
COUNTER=0
|
||||
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)"
|
||||
done
|
||||
;;
|
||||
"mysql" )
|
||||
COUNTER=0
|
||||
while true; do
|
||||
mysqlcmd='mysql -u'${DBUSER}' -P '${DBPORT}' -h '${DBHOST}' -p'${DBPASS}
|
||||
out="`$mysqlcmd -e "SELECT COUNT(*) FROM information_schema.FILES;" 2>&1`"
|
||||
echo "$out" | grep -E "COUNT|Enter" 2>&1 > /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
:
|
||||
break
|
||||
fi
|
||||
echo "** [db-backup] MySQL/MariaDB Server "$DBHOST" is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
done
|
||||
;;
|
||||
"pgsql" )
|
||||
# Wait until mongo logs that it's ready (or timeout after 60s)
|
||||
COUNTER=0
|
||||
export PGPASSWORD=${DBPASS}
|
||||
until pg_isready --dbname=${DBNAME} --host=${DBHOST} --port=${DBPORT} --username=${DBUSER} -q
|
||||
do
|
||||
sleep 5
|
||||
let COUNTER+=5
|
||||
echo "** [db-backup] Postgres Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||
done
|
||||
;;
|
||||
"redis" )
|
||||
COUNTER=0
|
||||
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)"
|
||||
done
|
||||
;;
|
||||
"rethink" )
|
||||
COUNTER=0
|
||||
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)"
|
||||
done
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
function compression() {
|
||||
case "$COMPRESSION" in
|
||||
"GZ" | "gz" | "gzip" | "GZIP")
|
||||
@@ -212,7 +285,7 @@ function compression() {
|
||||
}
|
||||
|
||||
function generate_md5() {
|
||||
if [ "$MD5" = "TRUE" ] || [ "$MD5" = "true" ] ; then
|
||||
if [ "$MD5" = "TRUE" ] || [ "$MD5" = "true" ] ; then
|
||||
cd $TMPDIR
|
||||
md5sum ${TARGET} > ${TARGET}.md5
|
||||
fi
|
||||
@@ -225,13 +298,13 @@ function move_backup() {
|
||||
}
|
||||
|
||||
|
||||
### Container Startup
|
||||
### Container Startup
|
||||
echo '** [db-backup] Initialized at at '$(date)
|
||||
|
||||
### Wait for Next time to start backup
|
||||
current_time=$(date +"%s")
|
||||
today=$(date +"%Y%m%d")
|
||||
|
||||
|
||||
if [[ $DB_DUMP_BEGIN =~ ^\+(.*)$ ]]; then
|
||||
waittime=$(( ${BASH_REMATCH[1]} * 60 ))
|
||||
else
|
||||
@@ -257,34 +330,41 @@ echo '** [db-backup] Initialized at at '$(date)
|
||||
### Take a Dump
|
||||
case "$DBTYPE" in
|
||||
"couch" )
|
||||
check_availability
|
||||
backup_couch
|
||||
;;
|
||||
"influx" )
|
||||
check_availability
|
||||
backup_influx
|
||||
;;
|
||||
"mysql" )
|
||||
check_availability
|
||||
backup_mysql
|
||||
;;
|
||||
"mongo" )
|
||||
check_availability
|
||||
backup_mongo
|
||||
;;
|
||||
"pgsql" )
|
||||
check_availability
|
||||
backup_pgsql
|
||||
;;
|
||||
"redis" )
|
||||
check_availability
|
||||
backup_redis
|
||||
;;
|
||||
"rethink" )
|
||||
check_availability
|
||||
backup_rethink
|
||||
;;
|
||||
esac
|
||||
|
||||
### Zabbix
|
||||
### 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'`
|
||||
fi
|
||||
|
||||
|
||||
### Automatic Cleanup
|
||||
if [[ -n "$DB_CLEANUP_TIME" ]]; then
|
||||
find $DB_DUMP_TARGET/ -mmin +$DB_CLEANUP_TIME -iname "$DBTYPE_$DBNAME_*.*" -exec rm {} \;
|
||||
|
||||
Reference in New Issue
Block a user