docker MySQL realizes automatic backup every day!
This article mainly introduces how to back up and restore the docker MySQL database, and how to implement automatic backup every day. I hope it will be helpful to friends in need!
Docker MySQL database backup and restore, as well as daily scheduled automatic backup
1: Backup
View docker mysql
ubuntu@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a30ead8a26fc mysql:8.0.27 "docker-entrypoint.s…" 10 seconds ago Up 7 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
Method 1:
Step by step, perform regular backup, enter the container, and use mysqldump to back up. [Recommended learning: docker video tutorial]
sudo docker exec -it mysql /bin/bash #进入mysql容器 mysqldump -uroot -p123456 --all-databases > emp_`date +%F`.sql #在容器中备份所有数据库,并以日期命名 exit #退出容器 sudo docker cp mysql:emp_2022-03-17.sql /var/backup/ #将容器中的备份文件,复制到本地备份文件夹中
is displayed as follows:
ubuntu@ubuntu:~$ sudo docker exec -it mysql /bin/bash root@a30ead8a26fc:/# mysqldump -uroot -p123456 --all-databases > emp_`date +%F`.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. root@a30ead8a26fc:/# exit exit ubuntu@ubuntu:~$ sudo docker cp mysql:emp_2022-03-17.sql /var/backup/ ubuntu@ubuntu:~$ ll /var/backup/ total 11492 drwxrwxrwx 2 root root 4096 Mar 17 15:45 ./ drwxr-xr-x 14 root root 4096 Mar 14 17:49 ../ -rw-r--r-- 1 root root 3915599 Mar 17 15:42 emp_2022-03-17.sql
Method 2:
One step, there are two possible commands here
1): (Recommended, there is no problem with restore)
sudo docker exec mysql(容器名) sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +%F`.sql
2): (Not recommended, there is a problem with restore)
Most of the online backup methods are this, but I There is a problem with the restore. You can try version 5.7.
sudo docker exec -it mysql(容器名) mysqldump -uroot -p123456 --all-databases > /var/backup/music_`date +%F`.sql
Note: It may be due to the version (my version is 8.0). There is no problem with this kind of backup, but there will be problems with the restore. The error is reported as follows:
ubuntu@ubuntu:~$ sudo mysql -uroot -p < emp_2022-03-15.sql Enter password: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump: [Warning] Using a password on the command line interface can be insec' at line 1
You can also enter the database and use the command source to restore, but the error will still appear, but will be ignored, resulting in incomplete data recovery in the end. All this is not recommended.
2: Restore
1): Restore in non-container
1: Direct command to restore
mysql -uroot -p123456 < /var/backup/emp_2022-03-15.sql
2: Source restore after entering mysql
mysql -uroot -p source /var/backup/emp_2022-03-15.sql
2): Container mysql restore
needs to be restored in the mysql container.
#复制备份文件到容器中的var目录 sudo docker cp /var/backup/emp_2022-03-17.sql mysql:/var #进入容器 sudo docker exec -it mysql bin/bash #进入mysql mysql -uroot -p #使用备份sql恢复数据库 source /var/backup/emp_2022-03-15.sql #或者不进入mysql直接容器中还原 mysql -uroot -p123456 < /var/backup/emp_2022-03-15.sql
Three: Scheduled tasks
1): Set scheduled tasks
crontab -e
The content is as follows:
0 2 * * * find /backup/ -mtime +1 -name "*.sql" -delete && sudo docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +%F`.sql
Command description:
Every day Executed at 2 o'clock in the morning
find /backup/ -mtime +1 -name "*.sql" -delete #删除2天前的备份任务,即保留3个版本。根据需要自己选择。
2): The scheduled task is not executed
Update: The next day it was found that there was no backup file, check the cron log. If not, please enable cron log. The method is as follows:
#编辑系统日志文件 vim /etc/rsyslog.d/50-default.conf #去掉前面的#注释符号 cron.* /var/log/cron.log #重启日志服务 systemctl restart rsyslog.service
View the scheduled task display
cat /var/log/cron.log .... CRON[2015636]: (root) CMD (find /backup/ -mtime +1 -name "*.sql" -delete && sudo docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +) ....
It is found that %F`.sql after the command is not displayed. The reason is that % is a variable in it. Add the \ escape character, so that Keep the original characters.
0 2 * * * find /backup/ -mtime +1 -name "*.sql" -delete && sudo docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +\%F`.sql
Save and the test will take effect.
The above is the detailed content of docker MySQL realizes automatic backup every day!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database
