├── .github └── dependabot.yml ├── Dockerfile ├── LICENSE └── README.md /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "docker" 9 | directory: "/" 10 | schedule: 11 | interval: "weekly" 12 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.18 2 | 3 | RUN apk add --no-cache \ 4 | mysql-client \ 5 | tzdata 6 | ENTRYPOINT ["crond", "-f"] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Alexander Schnitzler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Supported tags and respective Dockerfile links 2 | latest ([Dockerfile](https://github.com/alexanderschnitzler/docker-mysqldump/blob/master/Dockerfile)) 3 | 4 | The container is based on `alpine:3.18`, thus it is very small. 5 | 6 | 7 | ## What is schnitzler/mysqldump? 8 | 9 | This container can be used in two ways. It is prepared to run `crond` by default, so you can integrate this container in your `docker-compose.yml` and do regularly backups defined by a `crontab` and backup `script`. 10 | 11 | Additionaly you can use this container to create single backups. In this case scroll to the very bottom of this documentation. 12 | 13 | ## Use as cronjob container 14 | 15 | Here is an example configuration for using the container with a crontab. 16 | 17 | ##### Directory structure: 18 | ``` 19 | . 20 | ├── backup 21 | ├── bin 22 | │   ├── backup 23 | │   └── crontab 24 | └── docker-compose.yml 25 | ``` 26 | 27 | ##### Directory listing of the `bin` folder: 28 | ``` 29 | $ ls -al 30 | total 16 31 | drwxrwxr-x 2 whoami whoami 4096 Feb 25 17:51 . 32 | drwxrwxr-x 4 whoami whoami 4096 Feb 25 17:51 .. 33 | -rwx------ 1 root root 175 Feb 25 17:51 backup 34 | -rw------- 1 root root 69 Feb 25 17:51 crontab 35 | ``` 36 | 37 | Mind the file permissions! 38 | `chown 0:0 backup && chmod 700 backup` 39 | `chown 0:0 backup && chmod 600 crontab` 40 | 41 | ##### docker-compose.yml 42 | ``` 43 | version: '2' 44 | services: 45 | db: 46 | image: mariadb:10.1 47 | restart: always 48 | environment: 49 | MYSQL_ROOT_PASSWORD: password 50 | MYSQL_USER: user 51 | MYSQL_PASSWORD: password 52 | MYSQL_DATABASE: database 53 | cron: 54 | image: schnitzler/mysqldump 55 | restart: always 56 | volumes: 57 | - ./bin/crontab:/var/spool/cron/crontabs/root 58 | - ./bin/backup:/usr/local/bin/backup 59 | volumes_from: 60 | - backup 61 | command: ["-l", "8", "-d", "8"] 62 | environment: 63 | MYSQL_HOST: db 64 | MYSQL_USER: user 65 | MYSQL_PASSWORD: password 66 | MYSQL_DATABASE: database 67 | backup: 68 | image: busybox 69 | volumes: 70 | - ./backup:/backup 71 | ``` 72 | 73 | ##### bin/crontab 74 | ``` 75 | #minute hour day month week command 76 | 0 0 * * * /usr/local/bin/backup 77 | ``` 78 | 79 | ##### bin/backup 80 | ``` 81 | #!/bin/sh 82 | 83 | now=$(date +"%s_%Y-%m-%d") 84 | /usr/bin/mysqldump --opt -h ${MYSQL_HOST} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE} > "/backup/${now}_${MYSQL_DATABASE}.sql" 85 | ``` 86 | 87 | ## Use as cronjob container (without overwriting bin/crontab) 88 | 89 | The container has a proper crontab by default: 90 | 91 | ``` 92 | # do daily/weekly/monthly maintenance 93 | # min hour day month weekday command 94 | */15 * * * * run-parts /etc/periodic/15min 95 | 0 * * * * run-parts /etc/periodic/hourly 96 | 0 2 * * * run-parts /etc/periodic/daily 97 | 0 3 * * 6 run-parts /etc/periodic/weekly 98 | 0 5 1 * * run-parts /etc/periodic/monthly 99 | ``` 100 | 101 | If these execution times suffice, you can simply mount your backup script into the proper folder: 102 | 103 | ``` 104 | version: '2' 105 | services: 106 | ... 107 | cron: 108 | image: schnitzler/mysqldump 109 | restart: always 110 | volumes: 111 | - ./bin/backup:/etc/periodic/daily/backup 112 | volumes_from: 113 | - backup 114 | command: ["-l", "8", "-d", "8"] 115 | environment: 116 | MYSQL_HOST: db 117 | MYSQL_USER: user 118 | MYSQL_PASSWORD: password 119 | MYSQL_DATABASE: database 120 | ... 121 | ``` 122 | 123 | ## Use for a single backup 124 | 125 | In this case you simply empty the `entrypoint` and run the mysqlump `command`. 126 | 127 | ``` 128 | docker run \ 129 | --rm --entrypoint "" \ 130 | -v `pwd`/backup:/backup \ 131 | --link="container:alias" \ 132 | schnitzler/mysqldump \ 133 | mysqldump --opt -h alias -u user -p"password" "--result-file=/backup/dumps.sql" database 134 | ``` 135 | --------------------------------------------------------------------------------