Docker image to periodically backup your database (MySQL, Postgres, or MongoDB) to S3 or local disk.
- star 数
- 135
- watcher 数
- 3
- fork 数
- 34
- issue 数
- 6
- 最終更新
- 2025/03/06
Docker image to periodically backup your database (MySQL, Postgres, or MongoDB) to S3 or local disk.
Docker image to periodically backup a your database (MySQL, Postgres or MongoDB) to Local Disk or S3 (AWS S3 or Minio).
see the docker hub page.
You can pull the latest image of the project from the Docker Hub Registry.
docker pull appleboy/docker-backup-database:postgres12
Or you can pull the latest image of the project from the GitHub Container Registry.
docker pull ghcr.io/appleboy/docker-backup-database:postgres12
First steps: Setup the Minio and Postgres 12 Server using docker-compose command.
services: minio: image: quay.io/minio/minio restart: always volumes: - data1-1:/data1 ports: - 9000:9000 - 9001:9001 environment: MINIO_ROOT_USER: minioadmin MINIO_ROOT_PASSWORD: minioadmin command: server /data --console-address ":9001" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] interval: 30s timeout: 20s retries: 3 postgres: image: postgres:12 restart: always volumes: - pg-data:/var/lib/postgresql/data logging: options: max-size: "100k" max-file: "3" environment: POSTGRES_USER: db POSTGRES_DB: db POSTGRES_PASSWORD: db
Second Steps: Backup your database and upload the dump file to S3 storage.
backup_postgres: image: appleboy/docker-backup-database:postgres12 logging: options: max-size: "100k" max-file: "3" environment: STORAGE_DRIVER: s3 STORAGE_ENDPOINT: minio:9000 STORAGE_BUCKET: test STORAGE_REGION: ap-northeast-1 STORAGE_PATH: backup_postgres STORAGE_SSL: "false" STORAGE_INSECURE_SKIP_VERIFY: "false" ACCESS_KEY_ID: minioadmin SECRET_ACCESS_KEY: minioadmin DATABASE_DRIVER: postgres DATABASE_HOST: postgres:5432 DATABASE_USERNAME: db DATABASE_PASSWORD: db DATABASE_NAME: db 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.
STORAGE_DAYS: 30 STORAGE_PATH: backup_postgres
Cron schedule to run periodic backups. See the TIME_SCHEDULE and TIME_LOCATION
backup_mysql: image: appleboy/docker-backup-database:mysql8 logging: options: max-size: "100k" max-file: "3" environment: STORAGE_DRIVER: s3 STORAGE_ENDPOINT: minio:9000 STORAGE_BUCKET: test STORAGE_REGION: ap-northeast-1 STORAGE_PATH: backup_mysql STORAGE_SSL: "false" STORAGE_INSECURE_SKIP_VERIFY: "false" ACCESS_KEY_ID: 1234567890 SECRET_ACCESS_KEY: 1234567890 DATABASE_DRIVER: mysql DATABASE_HOST: mysql:3306 DATABASE_USERNAME: root DATABASE_PASSWORD: db DATABASE_NAME: db DATABASE_OPTS: TIME_SCHEDULE: "@daily" TIME_LOCATION: Asia/Taipei
Each line of a crontab file represents a job, and looks like this:
# ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) # │ │ ┌───────────── day of the month (1 - 31) # │ │ │ ┌───────────── month (1 - 12) # │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday; # │ │ │ │ │ 7 is also Sunday on some systems) # │ │ │ │ │ # │ │ │ │ │ # * * * * * <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.
| Entry | Description | Equivalent To | | ---------------------- | ------------------------------------------ | ------------- | | @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 * | | @monthly | Run once a month, midnight, first of month | 0 0 1 * * | | @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0 | | @daily (or @midnight) | Run once a day, midnight | 0 0 * * * | | @hourly | Run once an hour, beginning of hour | 0 * * * * |
You can setup the webhook notification to send the backup status to the slack channel.
backup_mysql:
image: appleboy/docker-backup-database:mysql8
logging:
options:
max-size: "100k"
max-file: "3"
environment:
STORAGE_DRIVER: s3
STORAGE_ENDPOINT: minio:9000
STORAGE_BUCKET: test
STORAGE_REGION: ap-northeast-1
STORAGE_PATH: backup_mysql
STORAGE_SSL: "false"
STORAGE_INSECURE_SKIP_VERIFY: "false"
ACCESS_KEY_ID: 1234567890
SECRET_ACCESS_KEY: 1234567890
DATABASE_DRIVER: mysql
DATABASE_HOST: mysql:3306
DATABASE_USERNAME: root
DATABASE_PASSWORD: db
DATABASE_NAME: db
DATABASE_OPTS:
TIME_SCHEDULE: "@daily"
TIME_LOCATION: Asia/Taipei
+ WEBHOOK_URL: https://example.com/webhook
+ WEBHOOK_INSECURE: "false"postgres, mysql or mongo. default is postgrespg_dump, mysqldump or mongodump commands3 or disk. default is s3s3.amazonaws.comap-northeast-1backup and all dump file will save in bucket_name/backup directoryfalsefalse7storage driver name.20060102150405.false