mirror of
https://github.com/tiredofit/docker-db-backup.git
synced 2025-12-22 21:53:42 +01:00
Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6655d5a12a | ||
|
|
bd141cc865 | ||
|
|
abf2a877f7 | ||
|
|
3115cb3440 | ||
|
|
859ce5ffa3 | ||
|
|
4d1577e553 | ||
|
|
4f103a5b36 | ||
|
|
0472cba83d | ||
|
|
fe6fab857f | ||
|
|
6113bf64b2 | ||
|
|
95d1129a12 | ||
|
|
f8bab5f045 | ||
|
|
71802d2a28 | ||
|
|
c96a2179b5 | ||
|
|
42d3aa0fef | ||
|
|
e6009e7a1e | ||
|
|
b5466d5b97 | ||
|
|
06b6e685c7 | ||
|
|
d7bdcbd0dc | ||
|
|
62ee7ad3dc | ||
|
|
65c879172f | ||
|
|
33c942551a | ||
|
|
36e4d9a2a2 | ||
|
|
aa1c8b3591 | ||
|
|
243bbb9709 | ||
|
|
78e7434a85 | ||
|
|
d1d093b87d | ||
|
|
ea00709aa1 | ||
|
|
219a6463f3 | ||
|
|
0ed89369a9 | ||
|
|
6bd534258e | ||
|
|
48bea7aeee | ||
|
|
c3179d58ba | ||
|
|
fcafa1753d | ||
|
|
d74a516967 | ||
|
|
b0a5fafc4c | ||
|
|
9051ba559a | ||
|
|
bf672c0fda | ||
|
|
9993ad2970 | ||
|
|
dc17d60b7b | ||
|
|
49356629fe | ||
|
|
b938c8a761 | ||
|
|
5807ba07e3 | ||
|
|
96f3120e35 |
109
CHANGELOG.md
109
CHANGELOG.md
@@ -1,3 +1,111 @@
|
|||||||
|
## 1.21.2 2020-06-08 <dave at tiredofit dot ca>
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Change to support tiredofit/alpine base image 5.0.0
|
||||||
|
|
||||||
|
|
||||||
|
## 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
|
||||||
|
- 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
|
||||||
|
- Stop compiling mongodb-tools as it is back in Alpine:edge repositories
|
||||||
|
- Cleanup Code
|
||||||
|
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
|
## 1.13 - 2019-03-09 - <dave at tiredofit dot ca>
|
||||||
|
|
||||||
|
* Fixed Postgres backup without SPLIT_DB enabled (credit MelwinKfr@github)
|
||||||
|
* Added DB_PORT reference to properly backup Postgres with non default ports (thanks Maxximus007@github)
|
||||||
|
|
||||||
|
## 1.12 - 2019-03-01 - <stevetodorov at github>
|
||||||
|
|
||||||
|
* Fix for XZ Compression failing
|
||||||
|
|
||||||
|
## 1.11 - 2018-11-19 - <skylord123 at github>
|
||||||
|
|
||||||
|
* Fix for Urnary Operator Error
|
||||||
|
|
||||||
## 1.10 - 2018-11-19 - <dave at tiredofit dot ca>
|
## 1.10 - 2018-11-19 - <dave at tiredofit dot ca>
|
||||||
|
|
||||||
* Fix for InfluxDB for backing up and supporting DB_PORT variable - Thanks skylord123@github
|
* Fix for InfluxDB for backing up and supporting DB_PORT variable - Thanks skylord123@github
|
||||||
@@ -47,3 +155,4 @@
|
|||||||
|
|
||||||
* Initial Release
|
* Initial Release
|
||||||
* Alpine:Edge
|
* Alpine:Edge
|
||||||
|
|
||||||
|
|||||||
26
Dockerfile
26
Dockerfile
@@ -2,25 +2,28 @@ FROM tiredofit/alpine:edge
|
|||||||
LABEL maintainer="Dave Conroy (dave at tiredofit dot ca)"
|
LABEL maintainer="Dave Conroy (dave at tiredofit dot ca)"
|
||||||
|
|
||||||
### Set Environment Variables
|
### Set Environment Variables
|
||||||
ENV ENABLE_CRON=FALSE \
|
ENV ENABLE_CRON=FALSE \
|
||||||
ENABLE_SMTP=FALSE
|
ENABLE_SMTP=FALSE \
|
||||||
|
ENABLE_ZABBIX=FALSE \
|
||||||
|
ZABBIX_HOSTNAME=db-backup
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
RUN set -ex && \
|
RUN set -ex && \
|
||||||
echo "@testing http://nl.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
|
echo "@testing http://nl.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
|
||||||
apk update && \
|
apk update && \
|
||||||
apk upgrade && \
|
apk upgrade && \
|
||||||
apk add --virtual .db-backup-build-deps \
|
apk add -t .db-backup-build-deps \
|
||||||
build-base \
|
build-base \
|
||||||
bzip2-dev \
|
bzip2-dev \
|
||||||
git \
|
git \
|
||||||
xz-dev \
|
xz-dev \
|
||||||
&& \
|
&& \
|
||||||
\
|
\
|
||||||
apk add --virtual .db-backup-run-deps \
|
apk add -t .db-backup-run-deps \
|
||||||
bzip2 \
|
bzip2 \
|
||||||
mongodb-tools \
|
influxdb \
|
||||||
mariadb-client \
|
mariadb-client \
|
||||||
|
mongodb-tools \
|
||||||
libressl \
|
libressl \
|
||||||
pigz \
|
pigz \
|
||||||
postgresql \
|
postgresql \
|
||||||
@@ -28,21 +31,20 @@ LABEL maintainer="Dave Conroy (dave at tiredofit dot ca)"
|
|||||||
redis \
|
redis \
|
||||||
xz \
|
xz \
|
||||||
&& \
|
&& \
|
||||||
|
\
|
||||||
apk add \
|
apk add \
|
||||||
influxdb@testing \
|
|
||||||
pixz@testing \
|
pixz@testing \
|
||||||
&& \
|
&& \
|
||||||
\
|
\
|
||||||
cd /usr/src && \
|
mkdir -p /usr/src/pbzip2 && \
|
||||||
mkdir -p pbzip2 && \
|
|
||||||
curl -ssL https://launchpad.net/pbzip2/1.1/1.1.13/+download/pbzip2-1.1.13.tar.gz | tar xvfz - --strip=1 -C /usr/src/pbzip2 && \
|
curl -ssL https://launchpad.net/pbzip2/1.1/1.1.13/+download/pbzip2-1.1.13.tar.gz | tar xvfz - --strip=1 -C /usr/src/pbzip2 && \
|
||||||
cd pbzip2 && \
|
cd /usr/src/pbzip2 && \
|
||||||
make && \
|
make && \
|
||||||
make install && \
|
make install && \
|
||||||
\
|
\
|
||||||
# Cleanup
|
### Cleanup
|
||||||
rm -rf /usr/src/* && \
|
|
||||||
apk del .db-backup-build-deps && \
|
apk del .db-backup-build-deps && \
|
||||||
|
rm -rf /usr/src/* && \
|
||||||
rm -rf /tmp/* /var/cache/apk/*
|
rm -rf /tmp/* /var/cache/apk/*
|
||||||
|
|
||||||
### S6 Setup
|
### S6 Setup
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2016 Dave Conroy
|
Copyright (c) 2020 Dave Conroy
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
67
README.md
67
README.md
@@ -1,4 +1,4 @@
|
|||||||
# tiredofit/db-backup
|
# hub.docker.com/r/tiredofit/db-backup
|
||||||
|
|
||||||
|
|
||||||
[](https://hub.docker.com/r/tiredofit/db-backup)
|
[](https://hub.docker.com/r/tiredofit/db-backup)
|
||||||
@@ -6,14 +6,13 @@
|
|||||||
[](https://hub.docker.com/r/tiredofit/db-backup)
|
[](https://hub.docker.com/r/tiredofit/db-backup)
|
||||||
[](https://microbadger.com/images/tiredofit/db-backup)
|
[](https://microbadger.com/images/tiredofit/db-backup)
|
||||||
|
|
||||||
|
|
||||||
# Introduction
|
# Introduction
|
||||||
|
|
||||||
This will build a container for backing up multiple type of DB Servers
|
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
|
* select database user and password
|
||||||
* backup all databases
|
* backup all databases
|
||||||
* choose to have an MD5 sum after backup for verification
|
* choose to have an MD5 sum after backup for verification
|
||||||
@@ -22,8 +21,9 @@ Currently backs up CouchDB, InfluxDB, MySQL, MongoDB Postgres, Redis, Rethink se
|
|||||||
* connect to any container running on the same system
|
* connect to any container running on the same system
|
||||||
* select how often to run a dump
|
* select how often to run a dump
|
||||||
* select when to start the first dump, whether time of day or relative to container start time
|
* 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 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) 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)
|
[Changelog](CHANGELOG.md)
|
||||||
|
|
||||||
@@ -43,6 +43,7 @@ This Container uses Alpine:Edge as a base.
|
|||||||
- [Environment Variables](#environmentvariables)
|
- [Environment Variables](#environmentvariables)
|
||||||
- [Maintenance](#maintenance)
|
- [Maintenance](#maintenance)
|
||||||
- [Shell Access](#shell-access)
|
- [Shell Access](#shell-access)
|
||||||
|
- [Custom Scripts](#custom-scripts)
|
||||||
|
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ method of installation.
|
|||||||
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker pull tiredofit/db-backup
|
docker pull tiredofit/db-backup:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
# Quick Start
|
# Quick Start
|
||||||
@@ -77,6 +78,7 @@ The following directories are used for configuration and can be mapped for persi
|
|||||||
| Directory | Description |
|
| Directory | Description |
|
||||||
|-----------|-------------|
|
|-----------|-------------|
|
||||||
| `/backup` | Backups |
|
| `/backup` | Backups |
|
||||||
|
| `/assets/custom-scripts` | *Optional* Put custom scripts in this directory to execute after backup operations
|
||||||
|
|
||||||
|
|
||||||
## Environment Variables
|
## Environment Variables
|
||||||
@@ -86,28 +88,42 @@ Along with the Environment Variables from the [Base image](https://hub.docker.co
|
|||||||
|
|
||||||
| Parameter | Description |
|
| 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_TYPE` | Type of DB Server to backup `couch` `influx` `mysql` `pgsql` `mongo` `redis` `rethink`
|
||||||
| `DB_SERVER` | Server Hostname e.g. `mariadb`
|
| `DB_HOST` | Server Hostname e.g. `mariadb`
|
||||||
| `DB_NAME` | Schema Name e.g. `database`
|
| `DB_NAME` | Schema Name e.g. `database`
|
||||||
| `DB_USER` | username for the database - use `root` to backup all MySQL of them.
|
| `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_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_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
|
| `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`
|
| | 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
|
| | 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_DEBUG` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed.
|
|
||||||
| `DB_DUMP_TARGET` | Where to put the dump file, should be a directory. Supports three formats |
|
|
||||||
| | Local If the value of `DB_DUMP_TARGET` starts with a `/` character, will dump to a local path, which should be volume-mounted.
|
|
||||||
| `DB_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when dump freqency fires). 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything.
|
| `DB_CLEANUP_TIME` | Value in minutes to delete old backups (only fired when dump freqency fires). 1440 would delete anything above 1 day old. You don't need to set this variable if you want to hold onto everything.
|
||||||
| `COMPRESSION` | Use either Gzip `GZ`, Bzip2 `BZ`, XZip `XZ`, or none `NONE` - Default `GZ`
|
| `DEBUG_MODE` | If set to `true`, print copious shell script messages to the container log. Otherwise only basic messages are printed.
|
||||||
| `MD5` | Generate MD5 Sum in Directory, `TRUE` or `FALSE` - Default `TRUE`
|
| `MD5` | Generate MD5 Sum in Directory, `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` |
|
|
||||||
| `PARALLEL_COMPRESSION` | Use multiple cores when compressing backups `TRUE` or `FALSE` - Default `TRUE` |
|
| `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
|
## 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
|
#### Shell Access
|
||||||
|
|
||||||
@@ -117,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
|
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`
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ services:
|
|||||||
links:
|
links:
|
||||||
- example-db
|
- example-db
|
||||||
volumes:
|
volumes:
|
||||||
- ./backups:/backups
|
- ./backups:/backup
|
||||||
|
- ./post-script.sh:/assets/custom-scripts/post-script.sh
|
||||||
environment:
|
environment:
|
||||||
- DB_TYPE=mariadb
|
- DB_TYPE=mariadb
|
||||||
- DB_HOST=example-db
|
- DB_HOST=example-db
|
||||||
@@ -32,7 +33,6 @@ services:
|
|||||||
- MD5=TRUE
|
- MD5=TRUE
|
||||||
- COMPRESSION=XZ
|
- COMPRESSION=XZ
|
||||||
- SPLIT_DB=FALSE
|
- SPLIT_DB=FALSE
|
||||||
|
|
||||||
restart: always
|
restart: always
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
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,301 +0,0 @@
|
|||||||
#!/usr/bin/with-contenv bash
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
### Set Defaults
|
|
||||||
COMPRESSION=${COMPRESSION:-GZ}
|
|
||||||
PARALLEL_COMPRESSION=${PARALLEL_COMPRESSION:-TRUE}
|
|
||||||
DB_DUMP_FREQ=${DB_DUMP_FREQ:-1440}
|
|
||||||
DB_DUMP_BEGIN=${DB_DUMP_BEGIN:-+0}
|
|
||||||
DB_DUMP_TARGET=${DB_DUMP_TARGET:-/backup}
|
|
||||||
DBHOST=${DB_HOST}
|
|
||||||
DBNAME=${DB_NAME}
|
|
||||||
DBPASS=${DB_PASS}
|
|
||||||
DBUSER=${DB_USER}
|
|
||||||
DBTYPE=${DB_TYPE}
|
|
||||||
MD5=${MD5:-TRUE}
|
|
||||||
SPLIT_DB=${SPLIT_DB:-FALSE}
|
|
||||||
TMPDIR=/tmp/backups
|
|
||||||
|
|
||||||
if [ $1 = "NOW" ]; then
|
|
||||||
DB_DUMP_BEGIN=+0
|
|
||||||
MANUAL=TRUE
|
|
||||||
fi
|
|
||||||
|
|
||||||
### Set Compression Options
|
|
||||||
if [ "$PARALLEL_COMPRESSION" = "TRUE " ]; then
|
|
||||||
BZIP="pbzip2"
|
|
||||||
GZIP="pigz"
|
|
||||||
XZIP="pixz"
|
|
||||||
else
|
|
||||||
BZIP="bzip2"
|
|
||||||
GZIP="gzip"
|
|
||||||
XZIP=="xz"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
### Set the Database Type
|
|
||||||
case "$DBTYPE" in
|
|
||||||
"couch" | "couchdb" | "COUCH" | "COUCHDB" )
|
|
||||||
DBTYPE=couch
|
|
||||||
DBPORT=${DB_PORT:-5984}
|
|
||||||
;;
|
|
||||||
"influx" | "influxdb" | "INFLUX" | "INFLUXDB" )
|
|
||||||
DBTYPE=influx
|
|
||||||
DBPORT=${DB_PORT:-8088}
|
|
||||||
;;
|
|
||||||
"mongo" | "mongodb" | "MONGO" | "MONGODB" )
|
|
||||||
DBTYPE=mongo
|
|
||||||
DBPORT=${DB_PORT:-27017}
|
|
||||||
[[ ( -n "${DB_USER}" ) ]] && MONGO_USER_STR=" --username ${DBUSER}"
|
|
||||||
[[ ( -n "${DB_PASS}" ) ]] && MONGO_PASS_STR=" --password ${DBPASS}"
|
|
||||||
[[ ( -n "${DB_NAME}" ) ]] && MONGO_DB_STR=" --db ${DBNAME}"
|
|
||||||
;;
|
|
||||||
"mysql" | "MYSQL" | "mariadb" | "MARIADB")
|
|
||||||
DBTYPE=mysql
|
|
||||||
DBPORT=${DB_PORT:-3306}
|
|
||||||
[[ ( -n "${DB_PASS}" ) ]] && MYSQL_PASS_STR=" -p${DBPASS}"
|
|
||||||
;;
|
|
||||||
"postgres" | "postgresql" | "pgsql" | "POSTGRES" | "POSTGRESQL" | "PGSQL" )
|
|
||||||
DBTYPE=pgsql
|
|
||||||
DBPORT=${DB_PORT:-5432}
|
|
||||||
[[ ( -n "${DB_PASS}" ) ]] && POSTGRES_PASS_STR="PGPASSWORD=${DBPASS}"
|
|
||||||
;;
|
|
||||||
"redis" | "REDIS" )
|
|
||||||
DBTYPE=redis
|
|
||||||
DBPORT=${DB_PORT:-6379}
|
|
||||||
;;
|
|
||||||
"rethink" | "RETHINK" )
|
|
||||||
DBTYPE=rethink
|
|
||||||
DBPORT=${DB_PORT:-28015}
|
|
||||||
[[ ( -n "${DB_PASS}" ) ]] && echo $DB_PASS>/tmp/.rethink.auth; RETHINK_PASS_STR=" --password-file /tmp/.rethink.auth"
|
|
||||||
[[ ( -n "${DB_NAME}" ) ]] && RETHINK_DB_STR=" -e ${DBNAME}"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
### Functions
|
|
||||||
function backup_couch() {
|
|
||||||
TARGET=couch_${DBNAME}_${DBHOST}_${now}.txt
|
|
||||||
curl -X GET http://${DBHOST}:${DBPORT}/${DBNAME}/ all docs? include docs=true >${TMPDIR}/${TARGET}
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
}
|
|
||||||
|
|
||||||
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`
|
|
||||||
|
|
||||||
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}
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
else
|
|
||||||
mysqldump --max-allowed-packet=512M -A -h $DBHOST -u$DBUSER ${MYSQL_PASS_STR} > ${TMPDIR}/${TARGET}
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
function backup_influx() {
|
|
||||||
for DB in $DB_NAME; do
|
|
||||||
influxd backup -database $DB -host ${DBHOST} -p ${DBPORT} ${TMPDIR}/${TARGET}
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
}
|
|
||||||
|
|
||||||
function backup_pgsql() {
|
|
||||||
if [ "$SPLIT_DB" = "TRUE" ] || [ "$SPLIT_DB" = "true" ]; then
|
|
||||||
export PGPASSWORD=${DBPASS}
|
|
||||||
DATABASES=`psql -h $DBHOST -U $DBUSER -c 'COPY (SELECT datname FROM pg_database WHERE datistemplate = false) TO STDOUT;' `
|
|
||||||
for db in $DATABASES; do
|
|
||||||
echo "** [db-backup] Dumping database: $db"
|
|
||||||
TARGET=pgsql_${db}_${DBHOST}_${now}.sql
|
|
||||||
pg_dump -h ${DBHOST} -p ${DBPORT} -U ${DBUSER} $db > ${TMPDIR}/${TARGET}
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
done
|
|
||||||
else
|
|
||||||
export PGPASSWORD=${DBPASS}
|
|
||||||
pg_dump -h ${DBHOST} -U ${DBUSER} $db > ${TMPDIR}/${TARGET}
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
function backup_redis() {
|
|
||||||
TARGET=redis_${db}_${DBHOST}_${now}.rdb
|
|
||||||
echo bgsave | redis-cli -h ${DBHOST} -p ${DBPORT} --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"}')
|
|
||||||
if [[ "$saved" = "saved" ]] && [[ "$ok" = "ok" ]]; then
|
|
||||||
echo "** [db-backup] Redis Backup Complete"
|
|
||||||
fi
|
|
||||||
try=$((try - 1))
|
|
||||||
echo "** [db-backup] Redis Busy - Waiting and retrying in 5 seconds"
|
|
||||||
sleep 5
|
|
||||||
done
|
|
||||||
generate_md5
|
|
||||||
compression
|
|
||||||
move_backup
|
|
||||||
}
|
|
||||||
|
|
||||||
function backup_rethink() {
|
|
||||||
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 compression() {
|
|
||||||
case "$COMPRESSION" in
|
|
||||||
"GZ" | "gz" | "gzip" | "GZIP")
|
|
||||||
$GZIP ${TMPDIR}/${TARGET}
|
|
||||||
TARGET=${TARGET}.gz
|
|
||||||
;;
|
|
||||||
"BZ" | "bz" | "bzip2" | "BZIP2" | "bzip" | "BZIP" | "bz2" | "BZ2")
|
|
||||||
$BZIP ${TMPDIR}/${TARGET}
|
|
||||||
TARGET=${TARGET}.bz2
|
|
||||||
;;
|
|
||||||
"XZ" | "xz" | "XZIP" | "xzip" )
|
|
||||||
$XZIP ${TMPDIR}/${TARGET}
|
|
||||||
TARGET=${TARGET}.xz
|
|
||||||
;;
|
|
||||||
"NONE" | "none" | "FALSE" | "false")
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
function generate_md5() {
|
|
||||||
if [ "$MD5" = "TRUE" ] || [ "$MD5" = "true" ] ; then
|
|
||||||
cd $TMPDIR
|
|
||||||
md5sum ${TARGET} > ${TARGET}.md5
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
function move_backup() {
|
|
||||||
mkdir -p ${DB_DUMP_TARGET}
|
|
||||||
mv ${TMPDIR}/*.md5 ${DB_DUMP_TARGET}/
|
|
||||||
mv ${TMPDIR}/${TARGET} ${DB_DUMP_TARGET}/${TARGET}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
### 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
|
|
||||||
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
|
|
||||||
|
|
||||||
sleep $waittime
|
|
||||||
|
|
||||||
|
|
||||||
### Commence Backup
|
|
||||||
while true; do
|
|
||||||
# make sure the directory exists
|
|
||||||
mkdir -p $TMPDIR
|
|
||||||
|
|
||||||
### Define Target name
|
|
||||||
now=$(date +"%Y%m%d-%H%M%S")
|
|
||||||
TARGET=${DBTYPE}_${DBNAME}_${DBHOST}_${now}.sql
|
|
||||||
|
|
||||||
### Take a Dump
|
|
||||||
case "$DBTYPE" in
|
|
||||||
"couch" )
|
|
||||||
backup_couch
|
|
||||||
;;
|
|
||||||
"influx" )
|
|
||||||
backup_influx
|
|
||||||
;;
|
|
||||||
"mysql" )
|
|
||||||
backup_mysql
|
|
||||||
;;
|
|
||||||
"mongo" )
|
|
||||||
backup_mongo
|
|
||||||
;;
|
|
||||||
"pgsql" )
|
|
||||||
backup_pgsql
|
|
||||||
;;
|
|
||||||
"redis" )
|
|
||||||
backup_redis
|
|
||||||
;;
|
|
||||||
"rethink" )
|
|
||||||
backup_rethink
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
### 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 {} \;
|
|
||||||
fi
|
|
||||||
|
|
||||||
### Go back to Sleep until next Backup time
|
|
||||||
if [ "$MANUAL" = "TRUE" ]; then
|
|
||||||
exit 1;
|
|
||||||
else
|
|
||||||
sleep $(($DB_DUMP_FREQ*60))
|
|
||||||
fi
|
|
||||||
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
458
install/etc/services.available/10-db-backup/run
Executable file
458
install/etc/services.available/10-db-backup/run
Executable file
@@ -0,0 +1,458 @@
|
|||||||
|
#!/usr/bin/with-contenv bash
|
||||||
|
|
||||||
|
source /assets/functions/00-container
|
||||||
|
|
||||||
|
PROCESS_NAME="db-backup"
|
||||||
|
|
||||||
|
date >/dev/null
|
||||||
|
|
||||||
|
if [ "$1" != "NOW" ]; then
|
||||||
|
sleep 10
|
||||||
|
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}
|
||||||
|
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}
|
||||||
|
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 var_true $PARALLEL_COMPRESSION ; then
|
||||||
|
BZIP="pbzip2"
|
||||||
|
GZIP="pigz"
|
||||||
|
XZIP="pixz"
|
||||||
|
else
|
||||||
|
BZIP="bzip2"
|
||||||
|
GZIP="gzip"
|
||||||
|
XZIP="xz"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
### Set the Database Type
|
||||||
|
case "$DBTYPE" in
|
||||||
|
"couch" | "couchdb" | "COUCH" | "COUCHDB" )
|
||||||
|
DBTYPE=couch
|
||||||
|
DBPORT=${DB_PORT:-5984}
|
||||||
|
;;
|
||||||
|
"influx" | "influxdb" | "INFLUX" | "INFLUXDB" )
|
||||||
|
DBTYPE=influx
|
||||||
|
DBPORT=${DB_PORT:-8088}
|
||||||
|
;;
|
||||||
|
"mongo" | "mongodb" | "MONGO" | "MONGODB" )
|
||||||
|
DBTYPE=mongo
|
||||||
|
DBPORT=${DB_PORT:-27017}
|
||||||
|
[[ ( -n "${DB_USER}" ) ]] && MONGO_USER_STR=" --username ${DBUSER}"
|
||||||
|
[[ ( -n "${DB_PASS}" ) ]] && MONGO_PASS_STR=" --password ${DBPASS}"
|
||||||
|
[[ ( -n "${DB_NAME}" ) ]] && MONGO_DB_STR=" --db ${DBNAME}"
|
||||||
|
;;
|
||||||
|
"mysql" | "MYSQL" | "mariadb" | "MARIADB")
|
||||||
|
DBTYPE=mysql
|
||||||
|
DBPORT=${DB_PORT:-3306}
|
||||||
|
[[ ( -n "${DB_PASS}" ) ]] && export MYSQL_PWD=${DBPASS}
|
||||||
|
;;
|
||||||
|
"postgres" | "postgresql" | "pgsql" | "POSTGRES" | "POSTGRESQL" | "PGSQL" )
|
||||||
|
DBTYPE=pgsql
|
||||||
|
DBPORT=${DB_PORT:-5432}
|
||||||
|
[[ ( -n "${DB_PASS}" ) ]] && POSTGRES_PASS_STR="PGPASSWORD=${DBPASS}"
|
||||||
|
;;
|
||||||
|
"redis" | "REDIS" )
|
||||||
|
DBTYPE=redis
|
||||||
|
DBPORT=${DB_PORT:-6379}
|
||||||
|
[[ ( -n "${DB_PASS}" ) ]] && REDIS_PASS_STR=" -a ${DBPASS}"
|
||||||
|
;;
|
||||||
|
"rethink" | "RETHINK" )
|
||||||
|
DBTYPE=rethink
|
||||||
|
DBPORT=${DB_PORT:-28015}
|
||||||
|
[[ ( -n "${DB_PASS}" ) ]] && echo $DB_PASS>/tmp/.rethink.auth; RETHINK_PASS_STR=" --password-file /tmp/.rethink.auth"
|
||||||
|
[[ ( -n "${DB_NAME}" ) ]] && RETHINK_DB_STR=" -e ${DBNAME}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
### Functions
|
||||||
|
function backup_couch() {
|
||||||
|
TARGET=couch_${DBNAME}_${DBHOST}_${now}.txt
|
||||||
|
curl -X GET http://${DBHOST}:${DBPORT}/${DBNAME}/_all_docs?include_docs=true >${TMPDIR}/${TARGET}
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
}
|
||||||
|
|
||||||
|
function backup_mysql() {
|
||||||
|
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
|
||||||
|
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 --databases $db > ${TMPDIR}/${TARGET}
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
mysqldump --max-allowed-packet=512M -A -h $DBHOST -P $DBPORT -u$DBUSER > ${TMPDIR}/${TARGET}
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function backup_influx() {
|
||||||
|
for DB in $DB_NAME; do
|
||||||
|
influxd backup -database $DB -host ${DBHOST}:${DBPORT} ${TMPDIR}/${TARGET}
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
}
|
||||||
|
|
||||||
|
function backup_pgsql() {
|
||||||
|
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
|
||||||
|
print_info "Dumping database: $db"
|
||||||
|
TARGET=pgsql_${db}_${DBHOST}_${now}.sql
|
||||||
|
pg_dump -h ${DBHOST} -p ${DBPORT} -U ${DBUSER} $db > ${TMPDIR}/${TARGET}
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
done
|
||||||
|
else
|
||||||
|
export PGPASSWORD=${DBPASS}
|
||||||
|
pg_dump -h ${DBHOST} -U ${DBUSER} -p ${DBPORT} ${DBNAME} > ${TMPDIR}/${TARGET}
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function backup_redis() {
|
||||||
|
TARGET=redis_${db}_${DBHOST}_${now}.rdb
|
||||||
|
echo bgsave | redis-cli -h ${DBHOST} -p ${DBPORT} ${REDIS_PASS_STR} --rdb ${TMPDIR}/${TARGET}
|
||||||
|
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
|
||||||
|
print_info "Redis Backup Complete"
|
||||||
|
fi
|
||||||
|
try=$((try - 1))
|
||||||
|
print_info "Redis Busy - Waiting and retrying in 5 seconds"
|
||||||
|
sleep 5
|
||||||
|
done
|
||||||
|
generate_md5
|
||||||
|
compression
|
||||||
|
move_backup
|
||||||
|
}
|
||||||
|
|
||||||
|
function backup_rethink() {
|
||||||
|
TARGET=rethink_${db}_${DBHOST}_${now}.tar.gz
|
||||||
|
print_info "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
|
||||||
|
print_warn "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
|
||||||
|
print_warn "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
|
||||||
|
print_warn "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
|
||||||
|
print_warn "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
|
||||||
|
print_warn "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
|
||||||
|
print_warn "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
|
||||||
|
print_warn "RethinkDB Host '"$DBHOST"' is not accessible, retrying.. ($COUNTER seconds so far)"
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
function compression() {
|
||||||
|
case "$COMPRESSION" in
|
||||||
|
"GZ" | "gz" | "gzip" | "GZIP")
|
||||||
|
$GZIP ${TMPDIR}/${TARGET}
|
||||||
|
TARGET=${TARGET}.gz
|
||||||
|
;;
|
||||||
|
"BZ" | "bz" | "bzip2" | "BZIP2" | "bzip" | "BZIP" | "bz2" | "BZ2")
|
||||||
|
$BZIP ${TMPDIR}/${TARGET}
|
||||||
|
TARGET=${TARGET}.bz2
|
||||||
|
;;
|
||||||
|
"XZ" | "xz" | "XZIP" | "xzip" )
|
||||||
|
$XZIP ${TMPDIR}/${TARGET}
|
||||||
|
TARGET=${TARGET}.xz
|
||||||
|
;;
|
||||||
|
"NONE" | "none" | "FALSE" | "false")
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
function generate_md5() {
|
||||||
|
if var_true $MD5 ; then
|
||||||
|
cd $TMPDIR
|
||||||
|
md5sum ${TARGET} > ${TARGET}.md5
|
||||||
|
MD5VALUE=$(md5sum ${TARGET} | awk '{ print $1}')
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function move_backup() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### Container Startup
|
||||||
|
print_info "Initialized on `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
|
||||||
|
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
|
||||||
|
|
||||||
|
sleep $waittime
|
||||||
|
|
||||||
|
|
||||||
|
### Commence Backup
|
||||||
|
while true; do
|
||||||
|
# make sure the directory exists
|
||||||
|
mkdir -p $TMPDIR
|
||||||
|
|
||||||
|
### 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
|
||||||
|
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
|
||||||
|
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 "*" -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 var_true $MANUAL ; then
|
||||||
|
exit 1;
|
||||||
|
else
|
||||||
|
sleep $(($DB_DUMP_FREQ*60))
|
||||||
|
fi
|
||||||
|
|
||||||
|
done
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user