当前位置:网站首页>Shell script realizes the scheduled backup of MySQL database on two computers
Shell script realizes the scheduled backup of MySQL database on two computers
2022-07-25 06:17:00 【roc98】
1 Origin and purpose
There's a need recently , It is required to realize dual computer backup of a database , Back up every morning , The main machine is required to keep only the records of the last 15 days . A thoughtful study ( insane baidu), We decided to use shell Scripts add timed tasks to achieve this requirement .
Considering some friends mysql Use docker Deploy , Finally, we will post docker Deployment script in environment .
Environmental preparation :
- Both are installed ssh Machine , Ensure that the network usually . The cross library replication command we use scp be based on openssh-clients-XXX, So basic ssh Environmental Science .
- mysql The machine where the service is located Expect The command is normal
- mysql Normal service .(docker Containers can also be used )
Here is a section of offline automatic installation expect Command script
#!/bin/bash
CURRENT_DIR=`pwd`
function install_expect()
{
echo "install start..."
cp expect-5.45.0.tar.gz tcl8.4.11-src.tar.gz /usr/
cd /usr/
tar -zxvf expect-5.45.0.tar.gz
tar -zxvf tcl8.4.11-src.tar.gz
cd tcl8.4.11/unix
./configure --prefix=/usr/tcl --enable-shared
make -j8
make install
cp tclUnixPort.h ../generic/
cd ../../expect-5.45
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.11/generic
make -j8
make install
ln -s /usr/expect/bin/expect /usr/tcl/bin/expect
ln -s /usr/expect/bin/expect /usr/bin/expect
cd ..
rm -rf expect-5.45.0.tar.gz tcl8.4.11-src.tar.gz
cd $CURRENT_DIR
echo "install finished..."
}
install_expect
Copy code In the download address of the two packages needed to put the above script
nchc.dl.sourceforge.net/sourceforge…
2 Confirm environment
In this step, let's manually confirm the key commands of a script , Ensure that the script can execute normally .
2.1 Export database sql Script
mysqldump -u Database user name -p Database password The name of the database to be exported > /mnt/data/mysql_backup/ Exported sql Script name .sql
Copy code sql The script is exported normally .
2.2 Cross machine copy
scp -P ssh Port number /mnt/data/mysql_backup/ Exported sql Script name .sql The user name of another machine, such as [email protected] Another machine IP:/mnt/data/mysql_backup/
Copy code The first copy failed , Cause another machine needs to be guaranteed /mnt/data/mysql_backup/ This path exists , Execute again to succeed .
2.3 docker Container confirmation environment
# Database backup
docker exec -u root mysql bash -c 'rm -rf /usr/local/pm/* && mysqldump -u user name -p password Database name > /usr/local/pm/pm_yantai_$(date "+%Y-%m-%d").sql'
#sql Copy out
docker cp mysql:/usr/local/pm/pm_yantai_$(date "+%Y-%m-%d").sql
# Cross machine copy
scp -P ssh Port number /mnt/data/mysql_backup/ Exported sql Script name .sql The user name of another machine, such as [email protected] Another machine IP:/mnt/data/mysql_backup/
Copy code 3 Build script
3.1 The script content
1. Not dockermysql
#!/bin/bash
# export sql Script
echo $(date "+%Y-%m-%d") backup start
echo mysql backup start
mysqldump -u Database user name -p Database password pm_prod2.0 > /mnt/data/mysql_backup/pm_shandong_$(date "+%Y-%m-%d").sql
echo mysql backup finish
#scp Cross machine backup
echo sql scp start
/usr/bin/expect <<-EOF
set timeout -1;
spawn scp -P ssh Port number /mnt/data/mysql_backup/pm_shandong_$(date "+%Y-%m-%d").sql User name of another machine @ Another machine IP:/mnt/data/mysql_backup/
expect {
"*password:" {send " Another machine password \r";exp_continue;}
"yes/no" {send "yes\r";}
}
EOF
# Delete expired sql
echo remove file /mnt/data/mysql_backup/pm_shandong_$(date -d "7 day ago" +%Y-%m-%d).sql
rm -rf /mnt/data/mysql_backup/pm_shandong_$(date -d "7 day ago" +%Y-%m-%d).sql
echo finish! The file is pm_shandong_$(date "+%Y-%m-%d").sql
Copy code 2.docker Containers mysql
#!/bin/bash
echo $(date "+%Y-%m-%d") backup start
echo mysql docker backup start
docker exec -u root mysql bash -c 'rm -rf /usr/local/pm/* && mysqldump -u user name -p password Database name > /usr/local/pm/pm_yantai_$(date "+%Y-%m-%d").sql'
echo mysql docker backup finish
echo sql copy start
docker cp mysql:/usr/local/pm/pm_yantai_$(date "+%Y-%m-%d").sql /usr/local/sdyy/pm/mysql_backup/pm_yantai_$(date "+%Y-%m-%d").sql
echo sql scp start
/usr/bin/expect <<-EOF
set timeout -1;
spawn scp -P ssh Port number /usr/local/sdyy/pm/mysql_backup/pm_yantai_$(date "+%Y-%m-%d").sql User name of another machine @ Another machine IP:/usr/local/sdyy/pm/mysql_backup/
expect {
"*password:" {send " Another machine login password \r";exp_continue;}
"yes/no" {send "yes\r";}
}
EOF
echo remove file /usr/local/sdyy/pm/mysql_backup/pm_yantai_$(date -d "15 day ago" +%Y-%m-%d).sql
rm -rf /usr/local/sdyy/pm/mysql_backup/pm_yantai_$(date -d "15 day ago" +%Y-%m-%d).sql
echo finish! The file is pm_yantai_$(date "+%Y-%m-%d").sql
Copy code 3.2 Manually execute the script to confirm that it is normal
sh mysql_backup.sh
Copy code Be careful : If you are from windows Directly uploaded shell Script , The following error should be reported .
This is because you have more backup files ?
The general reason is windows The file format created is dos Format reason .
The solution is as follows :
1. stay Windows Down conversion :
Use some editors such as UltraEdit or EditPlus Tools such as the script code conversion , Then put it in Linux In the implementation of . The conversion method is as follows (UltraEdit):File-->Conversions-->DOS->UNIX that will do .
2. stay Linux Down conversion use vim Open the sh file , Input : :set ff enter , Show fileformat=dos, Reset the file format : :set ff=unix Save and exit : :wq
Execute the order again
4 Timing task
- Edit scheduled tasks
crontab -e
Copy code Add the following line at the end
0 3 * * * /mnt/data/mysql_backup/mysql_backup.sh >> /mnt/data/mysql_backup/backup.log
Copy code The front is corn expression , On behalf of three o'clock every day .
Here is a paragraph that may be used cron expression .
And then shell Script absolute path , The execution log will be appended to the log file later .
Save and exit .
- Query the current scheduled tasks
crontab -l
Copy code - Add executable rights
chmod +x /mnt/data/mysql_backup/mysql_backup.sh
Copy code thus , Task to complete !
边栏推荐
- GF Securities online account opening? Is it safe?
- Pdf snapshot artifact
- What does PK, NN, Qu, B, UN, ZF, AI, G mean when creating tables in MySQL
- EOL offline sequence based on iso13209 (Otx)
- Sword finger offer 32 - I. print binary tree from top to bottom
- (2022牛客多校)D-Link with Game Glitch(spfa)
- [C language] in depth understanding of pointers and arrays (phase I)
- Msys2 common configuration
- Jstat command summary [easy to understand]
- Using JS to realize the linkage effect of form form's secondary menu
猜你喜欢

(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)

Prometheus operator configures promethesrule alarm rules

R奇怪语法总结

ROI pooling and ROI align

Sword finger offer 45. arrange the array into the smallest number

JTAG debugging source level debugging of arm bare board debugging

VO, dto, do, Po distinction and use

In depth analysis: is the hottest business model in 2022 linked by 2+1 a legal model?

Data too long for column ‘data‘ at row 1以及设置成longblob造成的乱码解决。node-mysql
![[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node](/img/06/b90fa310158669696f94f79ef4cc5a.png)
[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node
随机推荐
4、 MFC toolbar, runtime class information mechanism, runtime creation mechanism
Pdf snapshot artifact
Classic cases of static keywords and block blocks
Bug notes
Leetcode/ integer division
leetcode/整数除法
A little experience about von Mises distribution
(Niuke multi School II) j-link with arithmetic progress (least square method / three points)
VBA common objects
JS 获取鼠标选中的文字并处于选中状态
10. Rendering Basics
【Unity3D】UGUI回调函数
SAP FICO 第三节 BDC和LTMC导入S4财务科目
VSCode 如何开启多个终端?如何横向显示?
[QT] solve the problem of Chinese garbled code output from QT console
Unity model simplification / consolidation one click plug-in
This is how the permission system is designed, yyds
【C语言】指针和数组的深入理解(第一期)
Seekbar attribute reference
[Luogu p6629] string (runs) (tree array)