In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how the Docker container backs up the database regularly and sends it to the designated mailbox. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
1. Background:
At first, I wanted to write a script to monitor the server's high occupancy process and email it to me, and then I had a whim that I could use this way to back up my database and get started!
Second, the design idea:
By writing a shell script, calling linux's mail tool, saving the sql file of the database by mysqldump, adding it to the attachment through the mail tool, and finally sending it to my mailbox.
Third, write startup scripts
First, let's write a startup script.
To facilitate future personalized configuration, we extract all the variables in the script into an application.yml file, as follows:
RUNTIME: 084900 # # Startup time. Due to the container time zone problem, you need to subtract 8 hours from the current time HOST: 172.17.0.3 # # Database IP address USER: root # # Database user PASSWORD: 123456 # # Database password DATABASE: solo # # Database name TARGETMAIL: 1849539179@qq.com # # email address sent
Next, let's write a shell script. The logic is also very simple. When the current time is the same as the startup time, the sendmail function is called to send the mail.
#! / bin/bash#author: chentengRUNTIME=$ (cat. / application.yml | grep RUNTIME | awk'{print $2}') HOST=$ (cat. / application.yml | grep HOST | awk'{print $2}') USER=$ (cat. / application.yml | grep USER | awk'{print $2}') PASSWORD=$ (cat. / application.yml | grep PASSWORD | awk'{print $2}') DATABASE=$ (cat. / application.yml | grep DATABASE | awk'{print $2}') TARGETMAIL=$ (cat. / application.yml | grep TARGETMAIL | awk'{awk $2) ) function sendmail () {mysqldump-h$HOST-u$USER-p$PASSWORD-- complete-insert-- skip-add-drop-table-- hex-blob $DATABASE > $DATABASE.sql echo-e "mysqlbak_$CURRENT_TIME" | mail-s "mysqlbak_$CURRENT_TIME"-a $DATABASE.sql $TARGETMAIL sleep 1} while truedo CURRENT_TIME=$ (date +% H%M%S) if [$CURRENT_TIME=$ RUNTIME] Then echo "starting bak mysql database" sendmail continue else echo $CURRENT_TIME sleep 1 fidone IV. Build an image
Since we will finally put it on the K8s platform, we are going to build an image. Before building the image, please put the application.yml demo.sh Dockerfile in the same directory.
Dockerfile is as follows:
PS: client with mysql added, mail mail client
FROM centosRUN mkdir / app & & yum install-y mysql.x86_64 sendmail mailx libreport-plugin-mailx WORKDIR / appCOPY demo.sh .copy application.yml .CMD ["/ bin/sh", "demo.sh"]
Use the docker build command to build the image, remember to add the last point
Docker build-t mysqlmail-bak:1.0.1. Fifth, add side car containers
Side car container (sidecar): a side car container is a container that runs in a pod together with the main container, empowers the business container and shares a cyberspace, so you can connect to the database of the main container with 127.0.0.1 3306.
5.1 create a profile
To facilitate debugging, I also mount the shell script inside.
Create two configmap corresponding to the configuration file and shell script in the container. Later, if you do not need to debug, you can unmount the mysqlshell.
ApiVersion: v1kind: ConfigMapmetadata: name: mysqlmail-conf namespace: solodata: application.yml: | RUNTIME: 105800 HOST: 127.0.0.1 USER: root PASSWORD: 123456 DATABASE: solo TARGETMAIL: 1849539179@qq.com---apiVersion: v1kind: ConfigMapmetadata: name: mysqlmail-shell namespace: solodata: demo.sh: | #! / bin/bash # author: chenteng RUNTIME=$ (cat. / application.yml | grep RUNTIME | awk'{print $2}') HOST=$ (cat . / application.yml | grep HOST | awk'{print $2}') USER=$ (cat. / application.yml | grep USER | awk'{print $2}') PASSWORD=$ (cat. / application.yml | grep PASSWORD | awk'{print $2}') DATABASE=$ (cat. / application.yml | grep DATABASE | awk'{print $2}') TARGETMAIL=$ (cat. / application.yml | grep TARGETMAIL | awk'{print $2}') function sendmail () {mysqldump-h$HOST-u$USER-p$PASSWORD- -complete-insert-- skip-add-drop-table-- column-statistics=0-- hex-blob $DATABASE > $DATABASE.sql echo-e "mysqlbak_$CURRENT_TIME" | mail-s "mysqlbak_$CURRENT_TIME"-a $DATABASE.sql $TARGETMAIL sleep 1} while true do CURRENT_TIME=$ (date +% H%M%S) if [$CURRENT_TIME=$ RUNTIME] Then echo "starting bak mysql database" sendmail continue else echo $CURRENT_TIME sleep 1 fi done5.2 creates a stateful service deployment file
Our deploy file uses the yaml of mysql stateful service created in the previous article. If you are interested, please take a look at my last migration article.
ApiVersion: apps/v1kind: StatefulSetmetadata: name: mysql namespace: solospec: serviceName: mysql-service selector: matchLabels: mysql replicas: 1 template: metadata: labels: app: mysql spec: containers:-name: mysqlmail-bak imagePullPolicy: IfNotPresent image: mysqlmail-bak:1.0.1 volumeMounts:-name: mysqlmail-conf mountPath: / app/application.yml SubPath: application.yml-name: mysqlmail-shell mountPath: / app/demo.sh subPath: demo.sh-name: mysql-pod imagePullPolicy: IfNotPresent image: mysql:5.7 env:-name: MYSQL_ROOT_PASSWORD value: "123456" ports:-containerPort: 3306 name: msyql-listin volumeMounts :-name: mysql-data mountPath: / var/lib/mysql subPath: mysql-data-name: mysql-conf mountPath: / etc/mysql/conf.d/my.cnf subPath: my.cnf volumes:-name: mysql-data hostPath: path: / data/mysql-name: mysql-conf configMap: Name: mysql-conf-name: mysqlmail-conf configMap: name: mysqlmail-conf-name: mysqlmail-shell configMap: name: mysqlmail-shell---apiVersion: v1kind: Servicemetadata: mysql-service namespace: solo labels: app: mysqlspec: ports:-targetPort: 3306 port: 3306 clusterIP: None selector: app: MySQL VI, test
The time we set for him above is RUNTIME: 105800, the Shanghai time zone is 18:58. Let's take a look at the effect.
View the log
Note: when a pod contains multiple containers, use the-c parameter to specify which container to view
[root@VM-24-15-centos solo] # kubectl logs-n solo mysql-0-c mysqlmail-bak | grep mysql- C 5105755105756105757105758105759starting bak mysql databasemysqldump: [Warning] Using a password on the command line interface can be insecure.105801105802
As you can see from the log, the mail has been sent successfully! Let's go to the mailbox and find that it has been successful, so our experiment has been completed perfectly!
After reading the above, do you have any further understanding of how the Docker container backs up the database regularly and sends it to the specified mailbox? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.