Hello, I simply wanted to share this tip with anyone who would be interested in how to export a database from a mariadb / mysql docker container with a single click, as well as how to schedule this action on the NAS. The processes for exporting your database from Task Scheduler are as follows.
Note: This operation is applicable to any platform that has a mysql / maria server, whether it's on Docker or on a different OS. The only need is that the server has mysql / mariadb installed.
Declare your password like this in my.cnf
[client]
password = your database password
Make a sh script in a secure location in your file system (example bkp.sh)
- Include this command in your script.
#!/bin/bash
docker exec -i mysql mysqldump --defaults-extra-file=/etc/mysql/conf.d/my.cnf -u root testdb | gzip -c > /volume1/docker/databasebkp/testdb_$(date +%F-%H:%M).gz
Explanation
Parameter | Function |
---|---|
docker exec | a command that allows the execution of any given command within a Docker container |
-i | (interactive) keeps stdin open |
mysqldump | is a part of the mysql relational database package to "dump" a database, or collection of databases |
mysql | container name |
--defaults-extra-file=/etc/mysql/conf.d/my.cnf | Read this extra option file after all other option files are read. |
-u root | root is the name of the user who has permission to export the database. |
testdb | the name of the database you'd like to export (ex testdb) |
gzip | utility which is used to compress and decompress files |
-c | option which tells gzip to write on standard output and redirect the output to a file |
$(date +%F-%H:%M) | add timestap at the end of the file |
Add the script to your task scheduler as seen in the diagram below
- General
Task: Database backup
User : root
- Schedule
Run on the following days : Daily
First run time : 00:00
Frequency : Every 8 hours
Note:depending on the policy you want to go with
- Task Settings
Run command : bash location of the .sh script
Leave a comment
Your email address will not be published. Required fields are marked *