当前位置:网站首页>[docker]mysql scheduled backup

[docker]mysql scheduled backup

2022-06-10 00:23:00 Djc8 small yard farmer

Preface

Today, I adjusted the parameters of the machine , Make a backup , I think I haven't done automatic backup yet , If the data is lost , There's nothing left . So I studied , Use mysqldump To schedule backups mysql The data of .

analysis

My database runs in a container , Cannot run directly on the host mysqldump Command backup ( The host does not care about the database instance type , And will not install these things ), If you want to back up, you need to use docker exec Enter the container for backup , This obviously provides us with an idea . I now need to mount a path to the container mysql example , So that the container can store the backed up files directly . Then you need to write a crontab Automated task scripts .

Container mount Directory

Because when containers are used , Adopt the principle of least modification , Try not to automatically generate the configuration of the moving capacitor , So I'm going to stop the container here , And remove . The container performs a stop operation first docker stop mysql. Container removal docker rm mysql'. Add mount directory -v /bak/mysqlbak/:/tmp/mysqlbak` For example, the following script

sudo docker run  \
 --network=djc8net --name mysql \
 -v /database/data/:/var/lib/mysql   \
 -v /database/mysql/conf:/etc/mysql/conf.d \
 -v  /bak/mysqlbak/:/tmp/mysqlbak \
 -e MYSQL_ROOT_PASSWORD=test \
 -d mysql:8.0.22

After the operation is completed , You can see in the container that it contains /tmp/mysqlbak Catalog , This directory is the path where we need to store the backup .

Backup scripts

Use mysqldump The data can be flexibly backed up as sql Script , Easy to distribute . First create a sh file :touch /tool/autojob/bak_mysql.sh The contents are as follows :

#!/bin/bash
export NOW="$(date +"%Y-%m-%d")"
export DATA_DIR=/tmp/mysqlbak
docker exec -it mysql /bin/bash -c "mysqldump -uroot -ptest --set-gtid-purged=OFF --all-databases > $DATA_DIR/$NOW.sql"

I back up all the databases here .

Grant authority :chmod +x /tool/autojob/bak_mysql.sh Debugging operation :./tool/autojob/./bak_mysql.sh If there is no problem, check /bak/mysqlbak You will see a with the current date as the name sql The script .

Automatically run backup scripts

perform :crontab -e. Add a line at the bottom :20 01 * * * /tool/autojob/bak_mysql.sh preservation , And perform :crontab -l. This line will appear in the list . The backup time is every morning 01 spot 20 branch .

原网站

版权声明
本文为[Djc8 small yard farmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091637365610.html