docker mysql crontab backup
在既有的 mysql 中加入 crontab 的方式去定期備份 data。
Step 1. init database
希望一開始就幫我們把資料加入,因此 mount 一個 docker-entrypoint-initdb.d 到 container 內的。可參考[Mysql Docker Hub]
Initializing a fresh instance
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
1 | version: '2' |
而在 docker-entrypoint-initdb.d 內放入一小段 sql。
init.sql
1 | CREATE DATABASE IF NOT EXISTS develop; |
Step 2. Add crontab
特地把 mysql_backup mount 到 local 的 backup_sql。
1 | crontab: |
調整你希望備份的時間區間,可修改 crontab.txt
。
1 | */1 * * * * /script.sh |
調整你的執行 mysqldump 的 sh,可修改 script.sh
1 | #!/bin/sh |
全部的東西可在 Github 找到。