Sign inSign up

psychomantys/mysql-backup

By psychomantys

•Updated almost 4 years ago

Image
0

1.8K

psychomantys/mysql-backup repository overview

Docker Pulls

⁠mysql-backup

Backup MySQL 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 mysql data directory to the container, set the envs and bind the backup directory.

Docker:

docker run -v ${MYSQL_BACKUP_OUT}:/data -e MYSQL_BACKUP_KEEP_DAYS=7 -e MYSQL_BACKUP_KEEP_WEEKS=4 -e MYSQL_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/mysql-backup

Docker Compose:

version: '3.8'
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/mysql-backup:latest
    environment:
      MYSQL_BACKUP_KEEP_DAYS: 7
      MYSQL_BACKUP_KEEP_WEEKS: 4
      MYSQL_BACKUP_KEEP_MONTHS: 12
      MYSQL_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_NAMESSpace separated list of tables. If empty, backup all database. Default is empty.
DB_USERUser to bind and make the backup
DB_PASSWORDPassword for bind user
DB_HOSTHost of mysql server
DB_PORTPort of running mysql server. Default to 3306
MYSQL_BACKUP_DIRDirectory to save the backup at. Defaults to /data.
MYSQL_BACKUP_SUFFIXFilename suffix to save the backup. Defaults to .ldif.xz.
MYSQL_BACKUP_KEEP_DAYSNumber of daily backups to keep before removal. Defaults to 7.
MYSQL_BACKUP_KEEP_WEEKSNumber of weekly backups to keep before removal. Defaults to 4.
MYSQL_BACKUP_KEEP_MONTHSNumber of monthly backups to keep before removal. Defaults to 6.
MYSQL_BACKUP_SCHEDULECron like string to set daily execution of backup script. Defaults to 00 03 * * *.
⁠Manual Backups

By default, the backup is make daily at 03:00. But, you can force a backup running the script /backup_mysql.sh.

To force a backup now with a container created on first section, you can make:

docker exec -it "${CONTAINER}" /backup_mysql.sh
⁠Automatic Periodic Backups

You can change the MYSQL_BACKUP_SCHEDULE environment variable in -e MYSQL_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.

⁠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"

Tag summary

Content type

Image

Digest

sha256:7db97c4e1…

Size

81.3 MB

Last updated

almost 4 years ago

docker pull psychomantys/mysql-backup