Backup Databases(maria, postgres, mongo, ldap, etc)periodically, can optionally send to an S3-like
10K+
Backup Databases periodically, based on idea of prodrigestivill/docker-postgres-backup-local.
Supports the following Docker architectures: linux/amd64, linux/arm64, linux/arm/v7.
Bind the database data directory to the container, set the envs and bind the backup directory.
Docker:
docker run -v ${DB_BACKUP_OUT}:/data -e DB_BACKUP_KEEP_DAYS=7 -e DB_BACKUP_KEEP_WEEKS=4 -e DB_BACKUP_KEEP_MONTHS=6 -e "DB_USER=${DB_USER}" -e "DB_PASSWORD=${DB_PASSWORD}" -e DB_HOST=${DB_HOST} -e DB_PORT=${DB_PORT} psychomantys/database-backup
Docker Compose:
version: '3.9'
services:
app-db:
image: mariadb
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_TCP_PORT: ${DB_PORT}
volumes:
- app_db_data:/var/lib/mysql
app-backup-db:
image: psychomantys/database-backup:latest
environment:
DB_TYPE: "maria"
DB_BACKUP_KEEP_DAYS: 7
DB_BACKUP_KEEP_WEEKS: 4
DB_BACKUP_KEEP_MONTHS: 12
DB_BACKUP_SCHEDULE: "00 03 * * * "
DB_NAMES: ${DB_NAME}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
volumes:
- type: bind
source: /home/app/backup/mariadb/
target: /data
volumes:
app_db_data:
| env variable | Description |
|---|---|
DB_TYPE | Type of database to backup. Can be mariadb, mongo, mysql, postgresql or openldap |
DB_EXCLUDE | List of database names splited by whitespace to exclude from backup |
DB_NAMES | Space separated list of database names. If empty, backup all. Default is empty. |
DB_USER | User to bind and make the backup |
DB_PASSWORD | Password for bind user |
DB_HOST | Host of database server |
DB_PORT | Port of running database server. Default to default port used by DB_TYPE. |
DB_BACKUP_DIR | Directory to save the backup at. Defaults to /data. If start with s3://, stream dumps to a bucket. |
DB_BACKUP_SUFFIX | Filename suffix to save the backup. Defaults to .sql.xz. |
DB_BACKUP_KEEP_DAYS | Number of daily backups to keep before removal. Defaults to 7. If 0, disable. |
DB_BACKUP_KEEP_WEEKS | Number of weekly backups to keep before removal. Defaults to 4. If 0, disable. |
DB_BACKUP_KEEP_MONTHS | Number of monthly backups to keep before removal. Defaults to 6. If 0, disable. |
DB_BACKUP_SCHEDULE | Cron like string to set daily execution of backup script. Defaults to 00 03 * * *. |
DB_SPLIT | If set to false, no split dump in individual files for each dbname |
DB_COMPRESS_EXTRA_ARGS | Extra args to compress |
DB_DUMP_EXTRA_ARGS | Extra args to dump database |
DB_LIST_EXTRA_ARGS | Extra args to list database to backup |
DB_BACKUP_PREFIX | Prefix for backup file. Default is "${db_name}-${DB_HOST}" |
| env variable | Description |
|---|---|
S3_ENABLE | Enable S3 backup |
S3_ACCESS_KEY | Access key to access bucket |
S3_SECRET_KEY | Secret Key of bucket |
S3_ENDPOINT | S3 Endpoint, can be a minio host. Default to AWS |
S3_BUCKET | Bucket name |
S3_BUCKET_DIR | Bucket Directory to upload backups |
S3_REGION | Region of bucket, no defaults for now |
S3_WRITEONLY | If "true", not delete anything on S3, for delete files in bucket policy for example |
S3_CLEANUP | If "true", enable cleanup of s3. Used if bucket has no lifecycle policy. |
By default, the backup is make daily at 03:00. But, you can force a backup running the script /backup_database.sh.
To force a backup now with a container created on first section, you can make:
docker exec -it "${CONTAINER}" /backup_database.sh start
You can change the DB_BACKUP_SCHEDULE environment variable in -e DB_BACKUP_SCHEDULE= to alter the default frequency. Default is 00 03 * * *.
More information about the scheduling can be found here.
Folders daily, weekly and monthly are created and populated using hard links to save disk space. If S3 stream is used, no files on disk is created.
Some examples to restore/apply the backups.
Configure the container like in compose example. Replace the ${BACKUP_FILENAME} and ${CONTAINER}, from the following command:
docker exec -ti "${CONTAINER}" db_restore "${BACKUP_FILENAME}"
For example:
docker exec -ti mysql-backup_app-backup-db_1 db_restore "/data/daily/opa-2020-10-31-02:20:00.sql.xz"
In some ${DB_TYPE}, db_restore is not available.
A relevant list of installed softwares:
| Name | Version |
|---|---|
| mariadb | 10.11 |
| mongo | 8.0 |
| postgresql | 17.0 |
| openldap | 2.5.13 |
Content type
Image
Digest
sha256:5f2313c6d…
Size
241 MB
Last updated
about 1 year ago
docker pull psychomantys/database-backup