项目作者: appleboy

项目描述 :
Docker image to periodically backup your database (MySQL, Postgres, or MongoDB) to S3 or local disk.
高级语言: Go
项目地址: git://github.com/appleboy/docker-backup-database.git
创建时间: 2020-11-18T22:22:52Z
项目社区:https://github.com/appleboy/docker-backup-database

开源协议:MIT License

下载


docker-backup-database

GoDoc
codecov
Go Report Card
Docker Image

English | 繁體中文 | 簡體中文

Docker image to periodically backup a your database (MySQL, Postgres or MongoDB) to Local Disk or S3 (AWS S3 or Minio).

中文 Youtube 影片

Support Database

see the docker hub page.

  • Postgres (9, 10, 11, 12, 13, 14, 15, 16, 17)
    • 9: appleboy/docker-backup-database:postgres9
    • 10: appleboy/docker-backup-database:postgres10
    • 11: appleboy/docker-backup-database:postgres11
    • 12: appleboy/docker-backup-database:postgres12
    • 13: appleboy/docker-backup-database:postgres13
    • 14: appleboy/docker-backup-database:postgres14
    • 15: appleboy/docker-backup-database:postgres15
    • 16: appleboy/docker-backup-database:postgres16
    • 17: appleboy/docker-backup-database:postgres17
  • MySQL (8, 9)
    • 8: appleboy/docker-backup-database:mysql8
    • 9: appleboy/docker-backup-database:mysql9
  • Mongo (4.4)
    • 4.4: appleboy/docker-backup-database:mongo4.4

Docker Image

You can pull the latest image of the project from the Docker Hub Registry.

  1. docker pull appleboy/docker-backup-database:postgres12

Or you can pull the latest image of the project from the GitHub Container Registry.

  1. docker pull ghcr.io/appleboy/docker-backup-database:postgres12

Usage

First steps: Setup the Minio and Postgres 12 Server using docker-compose command.

  1. services:
  2. minio:
  3. image: quay.io/minio/minio
  4. restart: always
  5. volumes:
  6. - data1-1:/data1
  7. ports:
  8. - 9000:9000
  9. - 9001:9001
  10. environment:
  11. MINIO_ROOT_USER: minioadmin
  12. MINIO_ROOT_PASSWORD: minioadmin
  13. command: server /data --console-address ":9001"
  14. healthcheck:
  15. test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
  16. interval: 30s
  17. timeout: 20s
  18. retries: 3
  19. postgres:
  20. image: postgres:12
  21. restart: always
  22. volumes:
  23. - pg-data:/var/lib/postgresql/data
  24. logging:
  25. options:
  26. max-size: "100k"
  27. max-file: "3"
  28. environment:
  29. POSTGRES_USER: db
  30. POSTGRES_DB: db
  31. POSTGRES_PASSWORD: db

Second Steps: Backup your database and upload the dump file to S3 storage.

  1. backup_postgres:
  2. image: appleboy/docker-backup-database:postgres12
  3. logging:
  4. options:
  5. max-size: "100k"
  6. max-file: "3"
  7. environment:
  8. STORAGE_DRIVER: s3
  9. STORAGE_ENDPOINT: minio:9000
  10. STORAGE_BUCKET: test
  11. STORAGE_REGION: ap-northeast-1
  12. STORAGE_PATH: backup_postgres
  13. STORAGE_SSL: "false"
  14. STORAGE_INSECURE_SKIP_VERIFY: "false"
  15. ACCESS_KEY_ID: minioadmin
  16. SECRET_ACCESS_KEY: minioadmin
  17. DATABASE_DRIVER: postgres
  18. DATABASE_HOST: postgres:5432
  19. DATABASE_USERNAME: db
  20. DATABASE_PASSWORD: db
  21. DATABASE_NAME: db
  22. DATABASE_OPTS:

The default lifecycle policy is disabled. You can enable it by setting the STORAGE_DAYS environment variable. You can change the STORAGE_DAYS environment variable to keep the backup files for a different number of days. You also can change the STORAGE_PATH environment variable to save the backup files in a different directory.

  1. STORAGE_DAYS: 30
  2. STORAGE_PATH: backup_postgres

Cron schedule to run periodic backups. See the TIME_SCHEDULE and TIME_LOCATION

  1. backup_mysql:
  2. image: appleboy/docker-backup-database:mysql8
  3. logging:
  4. options:
  5. max-size: "100k"
  6. max-file: "3"
  7. environment:
  8. STORAGE_DRIVER: s3
  9. STORAGE_ENDPOINT: minio:9000
  10. STORAGE_BUCKET: test
  11. STORAGE_REGION: ap-northeast-1
  12. STORAGE_PATH: backup_mysql
  13. STORAGE_SSL: "false"
  14. STORAGE_INSECURE_SKIP_VERIFY: "false"
  15. ACCESS_KEY_ID: 1234567890
  16. SECRET_ACCESS_KEY: 1234567890
  17. DATABASE_DRIVER: mysql
  18. DATABASE_HOST: mysql:3306
  19. DATABASE_USERNAME: root
  20. DATABASE_PASSWORD: db
  21. DATABASE_NAME: db
  22. DATABASE_OPTS:
  23. TIME_SCHEDULE: "@daily"
  24. TIME_LOCATION: Asia/Taipei

Each line of a crontab file represents a job, and looks like this:

  1. # ┌───────────── minute (0 - 59)
  2. # │ ┌───────────── hour (0 - 23)
  3. # │ │ ┌───────────── day of the month (1 - 31)
  4. # │ │ │ ┌───────────── month (1 - 12)
  5. # │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
  6. # │ │ │ │ │ 7 is also Sunday on some systems)
  7. # │ │ │ │ │
  8. # │ │ │ │ │
  9. # * * * * * <command to execute>

A cron expression represents a set of times, using 5 space-separated fields.

Field name Mandatory? Allowed values Allowed special characters
Minutes Yes 0-59 * / , -
Hours Yes 0-23 * / , -
Day of month Yes 1-31 * / , - ?
Month Yes 1-12 or JAN-DEC * / , -
Day of week Yes 0-6 or SUN-SAT * / , - ?

You may use one of several pre-defined schedules in place of a cron expression.

  1. | Entry | Description | Equivalent To |
  2. | ---------------------- | ------------------------------------------ | ------------- |
  3. | @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 * |
  4. | @monthly | Run once a month, midnight, first of month | 0 0 1 * * |
  5. | @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0 |
  6. | @daily (or @midnight) | Run once a day, midnight | 0 0 * * * |
  7. | @hourly | Run once an hour, beginning of hour | 0 * * * * |

Setup Webhook Notification

You can setup the webhook notification to send the backup status to the slack channel.

  1. backup_mysql:
  2. image: appleboy/docker-backup-database:mysql8
  3. logging:
  4. options:
  5. max-size: "100k"
  6. max-file: "3"
  7. environment:
  8. STORAGE_DRIVER: s3
  9. STORAGE_ENDPOINT: minio:9000
  10. STORAGE_BUCKET: test
  11. STORAGE_REGION: ap-northeast-1
  12. STORAGE_PATH: backup_mysql
  13. STORAGE_SSL: "false"
  14. STORAGE_INSECURE_SKIP_VERIFY: "false"
  15. ACCESS_KEY_ID: 1234567890
  16. SECRET_ACCESS_KEY: 1234567890
  17. DATABASE_DRIVER: mysql
  18. DATABASE_HOST: mysql:3306
  19. DATABASE_USERNAME: root
  20. DATABASE_PASSWORD: db
  21. DATABASE_NAME: db
  22. DATABASE_OPTS:
  23. TIME_SCHEDULE: "@daily"
  24. TIME_LOCATION: Asia/Taipei
  25. + WEBHOOK_URL: https://example.com/webhook
  26. + WEBHOOK_INSECURE: "false"

Envionment Variables

Database Section

  • DATABASE_DRIVER - support postgres, mysql or mongo. default is postgres
  • DATABASE_USERNAME - database username
  • DATABASE_PASSWORD - database password
  • DATABASE_NAME - database name
  • DATABASE_HOST - database host
  • DATABASE_OPTS - see the pg_dump, mysqldump or mongodump command

Storage Section

  • STORAGE_DRIVER - support s3 or disk. default is s3
  • ACCESS_KEY_ID - Minio or AWS S3 ACCESS Key ID
  • SECRET_ACCESS_KEY - Minio or AWS S3 SECRET ACCESS Key
  • STORAGE_ENDPOINT - S3 Endpoint. default is s3.amazonaws.com
  • STORAGE_BUCKET - S3 bucket name
  • STORAGE_REGION - S3 Region. default is ap-northeast-1
  • STORAGE_PATH - backup folder path in bucket. default is backup and all dump file will save in bucket_name/backup directory
  • STORAGE_SSL - default is false
  • STORAGE_INSECURE_SKIP_VERIFY - default is false
  • STORAGE_DAYS - The number of days to keep the backup files. default is 7

Schedule Section

  • TIME_SCHEDULE - You may use one of several pre-defined schedules in place of a cron expression.
  • TIME_LOCATION - By default, all interpretation and scheduling is done in the machine’s local time zone. You can specify a different time zone on construction.

File Section

  • FILE_PREFIX - Prefix name of file, default is storage driver name.
  • FILE_SUFFIX - Suffix name of file
  • FILE_FORMAT - Format string of file, default is 20060102150405.

Webhook Section

  • WEBHOOK_URL - Webhook URL
  • WEBHOOK_INSECURE - default is false