1. Problems encounteredWe all know that after using mysqldump, we need to manually enter the mysql password, so we cannot use mysqldump directly in crontab to achieve periodic backup. In fact, we can use the expect script to automatically enter the password , thereby achieving true periodic backup. If you don’t know what expect is, I suggest you read this article first: https://www.jb51.net/article/197865.htm 2. Ideas
The idea is as follows: 3. Code3.1. Single-machine cold backup (1) mysqldump shell script #!/bin/bash mysql_username=$1 backup_databases=$2 backup_path=$3 mysqldump -u ${mysql_username} -p --databases ${backup_databases} > ${backup_path} (2) Executing the expect script of mysqldump can help us automatically enter the mysql code #!/usr/bin/expect set timeout 5 #Set local information set mysql_username [lindex $argv 0] set backup_database [lindex $argv 1] set backup_path [lindex $argv 2] #utils path set utils_path /home/hadoop/backup_script/utils spawn bash ${utils_path}/backup.sh ${mysql_username} ${backup_database} ${backup_path} expect { "*assword*" {send "nimabidecao1\r"} #Enter password} expect eof (3) Drive the script and execute expect, where you can pass in the required parameters #!/bin/bash #The data here can be written as mysql_username=root backup_databases=school backup_path=$HOME/backup_data/${backup_databases}.sql #Run the expect script expect $HOME/backup_script/single_cold_backup/single_cold_backup_service.exp ${mysql_username} ${backup_databases} ${backup_path} You must pay close attention to your path here. It is strongly recommended to use an absolute path to execute (4) Use crontab to periodically execute the driver script and enter the crontab editor: 0 9 * * 1 bash /home/hadoop/backup_script/single_cold_backup/single_cold_backup_driver.sh This means: perform a backup every Monday at 9:00 AM. If you want to confirm the time you want, you can go to this website: https://crontab-generator.org/ and select the time you want.
(1) Copy remote files to copy the MySQL backup files on the local machine #!/bin/bash local_backup_path=$1 another_user=$2 another_ip=$3 another_backup_path=$4 scp ${local_backup_path} ${another_user}@${another_ip}:${another_backup_path} (2) Executing the expect script of mysqldump can help us automatically enter the mysql code #!/usr/bin/expect set timeout 5 #Host information set mysql_username [lindex $argv 0] set backup_database [lindex $argv 1] set backup_path [lindex $argv 2] #Slave information set slave_user [lindex $argv 3] set slave_ip [lindex $argv 4] set slave_backup_path [lindex $argv 5] #utils path set utils_path /home/hadoop/backup_script/utils spawn bash ${utils_path}/backup.sh ${mysql_username} ${backup_database} ${backup_path} expect { "*assword*" {send "nimabidecao1\r"} #Enter password} spawn bash ${utils_path}/scp.sh ${backup_path} ${slave_user} ${slave_ip} ${slave_backup_path} expect { "*assword*" {send "nimabidecao1\r"} #Enter password} expect eof (3) Drive the script and execute expect, where you can pass in the required parameters #!/bin/bash #Local machine information mysql_username=root backup_databases=school backup_path=$HOME/backup_data/${backup_databases}.sql #Slave information slave_user=meizhaowei slave_ip=172.20.10.14 slave_backup_path=backup_data/doule_cold_backup_data/${backup_databases}.sql #Execute write absolute path expect $HOME/backup_script/double_cold_backup/double_cold_backup_service.exp ${mysql_username} ${backup_databases} ${backup_path} ${slave_user} ${slave_ip} ${slave_backup_path} (4) Use crontab to periodically execute the driver script 0 9 * * 1 bash /home/hadoop/backup_script/double_cold_backup/double_cold_backup_driver.sh This means: perform a backup every week at 9 am This is the end of this article about how to use mysqldump+expect+crontab in Linux to implement MySQL periodic cold backup. For more information about MySQL periodic cold backup, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Vue project realizes login and registration effect
>>: Detailed explanation of the use of Docker commit
Article Structure 1. Preparation 2. Install Java ...
Write to the css file Copy code The code is as fol...
The MySQL query result row field splicing can be ...
Table of contents Install mockjs in your project ...
The conversion between time, string and timestamp...
Part 1: Basics 1. Unlike pseudo-classes such as :...
What we are simulating now is a master-slave syst...
vmware vsphere 6.5 is the classic version of vsph...
As shown below: Mainly execute authorization comm...
<br />Without any warning, I saw news on cnB...
Table of contents 1. What is an event? 2. Enable ...
Exporting Data Report an error SHOW VARIABLES LIK...
Use of clip-path polygon The value is composed of...
Table of contents 1. Problem Description 2. Probl...
Usage of MySQL memory tables and temporary tables...