Sign inSign up

psychomantys/database-backup

By psychomantys

•Updated about 1 year ago

Backup Databases(maria, postgres, mongo, ldap, etc)periodically, can optionally send to an S3-like

Image
0

10K+

psychomantys/database-backup repository overview

Docker Pulls

⁠database-backup

Backup Databases periodically, based on idea of prodrigestivill/docker-postgres-backup-local⁠.

Supports the following Docker architectures: linux/amd64, linux/arm64, linux/arm/v7.

⁠Usage

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:
⁠Environment Variables
env variableDescription
DB_TYPEType of database to backup. Can be mariadb, mongo, mysql, postgresql or openldap
DB_EXCLUDEList of database names splited by whitespace to exclude from backup
DB_NAMESSpace separated list of database names. If empty, backup all. Default is empty.
DB_USERUser to bind and make the backup
DB_PASSWORDPassword for bind user
DB_HOSTHost of database server
DB_PORTPort of running database server. Default to default port used by DB_TYPE.
DB_BACKUP_DIRDirectory to save the backup at. Defaults to /data. If start with s3://, stream dumps to a bucket.
DB_BACKUP_SUFFIXFilename suffix to save the backup. Defaults to .sql.xz.
DB_BACKUP_KEEP_DAYSNumber of daily backups to keep before removal. Defaults to 7. If 0, disable.
DB_BACKUP_KEEP_WEEKSNumber of weekly backups to keep before removal. Defaults to 4. If 0, disable.
DB_BACKUP_KEEP_MONTHSNumber of monthly backups to keep before removal. Defaults to 6. If 0, disable.
DB_BACKUP_SCHEDULECron like string to set daily execution of backup script. Defaults to 00 03 * * *.
DB_SPLITIf set to false, no split dump in individual files for each dbname
DB_COMPRESS_EXTRA_ARGSExtra args to compress
DB_DUMP_EXTRA_ARGSExtra args to dump database
DB_LIST_EXTRA_ARGSExtra args to list database to backup
DB_BACKUP_PREFIXPrefix for backup file. Default is "${db_name}-${DB_HOST}"
⁠S3/Bucket Variables
env variableDescription
S3_ENABLEEnable S3 backup
S3_ACCESS_KEYAccess key to access bucket
S3_SECRET_KEYSecret Key of bucket
S3_ENDPOINTS3 Endpoint, can be a minio host. Default to AWS
S3_BUCKETBucket name
S3_BUCKET_DIRBucket Directory to upload backups
S3_REGIONRegion of bucket, no defaults for now
S3_WRITEONLYIf "true", not delete anything on S3, for delete files in bucket policy for example
S3_CLEANUPIf "true", enable cleanup of s3. Used if bucket has no lifecycle policy.
⁠Manual Backups

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
⁠Automatic Periodic Backups

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.

⁠Restore examples

Some examples to restore/apply the backups.

⁠Restore to a remote server

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.

⁠Softwares

A relevant list of installed softwares:

NameVersion
mariadb10.11
mongo8.0
postgresql17.0
openldap2.5.13

Tag summary

Content type

Image

Digest

sha256:5f2313c6d…

Size

241 MB

Last updated

about 1 year ago

docker pull psychomantys/database-backup