Mad World #Save_GAZA: Free Palestine  

Search

One-click container database export

  • Share this:
One-click container database export

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
 

ParameterFunction
docker execa 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
mysqlcontainer name
--defaults-extra-file=/etc/mysql/conf.d/my.cnfRead this extra option file after all other option files are read.
-u rootroot is the name of the user who has permission to export the database.
testdbthe name of the database you'd like to export (ex testdb)
gziputility 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-general
 

Task: Database backup
User : root

  • Schedule
     

task-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
     

task-settings
 

Run command : bash location of the .sh script

Finally, I hope that this post will be useful to anyone who wants to know how to quickly and efficiently export databases from containers.
 Yassine

Yassine

Yassine founded IGProd to educate the visitors with technological tutorials. These helpful guides are available for all sorts of professionals and novices, whether they work in technology or not!

Surf freely 💗

Leave a comment

Your email address will not be published. Required fields are marked *