Creating a backup MySQL container
Docker run--name mysql-back-e mysql_root_password=root-v/srv/mysql/backup:/mysql/backup-d mysql:5.7.17
View virtual networks, where bridge is the virtual network that Docker uses by default:
Docker Network Inspect Bridge
In the returned results, locate the Containers section. The contents are as follows:
"Containers": { "asdf2334a": {" Name": "Mysql-a" "EndpointId": "sadfas234" "MacAddress": "...." "ipv4address": "192.168.0.2" "ipv6address": "" }}
Containers lists all the containers that use the bridge network. Because I named the MySQL server container as mysql-a, I looked at the configuration of name Mysql-a. IPv4Address indicates that the virtual IP of the container mysql-a is 192.168.0.2. Record this IP.
Bash into the container:
Docker exec-it Mysql-b Bash
Install Vim and cron timed tasks:
Apt-get Update && apt-get install vimapt-get update && apt-get install cron
Generate a shell script file to be backed up.
Cat >/zc/mysql/backup.sh <<eof#!/bin/shzcdate=\$ (date +%y%m%d) mkdir/zc/mysql/backup/\ $zcDATEmysqldump-H ' 192.168.0.2 '-uroot-p ' 123456 '--databases db1 >/zc/mysql/backup/\ $zcDATE/db1.sqlmysqldump-h ' 192.168.0.2 '-uroot- P ' 123456 '--databases DB2 >/zc/mysql/backup/\ $zcDATE/db2.sqleof
There are two databases on MySQL, named DB1 and DB2, which use this method to back up data on the hard disk. -H indicates the remote server IP. -U and-P are the user names and passwords for the remote server, respectively. It is not recommended to use the –all-database option for mysqldump. Because this option will also back up the system database in remote MySQL, including the root user's password and so on. If you want to import the exported content to another machine, these system settings (such as user name and password) will overwrite the original settings on your machine.
To set the start time for a scheduled task:
CRONTAB-E0 * * * sh/zc/mysql/backup.sh
Start a scheduled task
Service Cron Start
Docker MySQL Backup