当前位置:网站首页>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 !
边栏推荐
- Scientific computing library numpy Foundation & Improvement (Understanding + explanation of important functions)
- (15) [driver development] over written copy
- HTB-Devel
- Netease game Flink SQL platform practice
- Classic cases of static keywords and block blocks
- (2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
- Blocking Queue Analysis
- Req.body in node.express is always undefind
- 【datawhale202207】强化学习:策略梯度和近端策略优化
- New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China
猜你喜欢

EOL offline sequence based on iso13209 (Otx)

Use abp Zero builds a third-party login module (4): wechat applet development

Dry goods | training AI model can't find data? Collect 20 selected open source communities!

Data too long for column 'data' at row 1 and the garbled code caused by setting to longblob are solved. node-mysql

Use abp Zero builds a third-party login module (III): web side development

Koa2 learning

四、MFC工具栏、运行时类信息机制、运行时创建机制

HTB-Optimum

SAP FICO 第三节 BDC和LTMC导入S4财务科目

(牛客多校二)G-Link with Monotonic Subsequence(构造题)
随机推荐
Detailed annotation and analysis of start.s of uboot
JVM tuning summary -xms -xmx -xmn -xss
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
Dry goods | training AI model can't find data? Collect 20 selected open source communities!
Data too long for column ‘data‘ at row 1以及设置成longblob造成的乱码解决。node-mysql
Brief tutorial of vbs script syntax (1)
【每日一练】day(14)
Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
target_compile_features specified unknown feature “cxx_std_14“ for target
VSCode 如何开启多个终端?如何横向显示?
Special episode of Goddess Festival | exclusive interview with Chinese AI goddess Zhang Qingqing's transformation from a female learning tyrant to a female entrepreneur
How does vscode enable multiple terminals? How to display horizontally?
Multithreading programming under Win32 API
Draw Bezier curve through screen interaction
Siggraph 2022 -- rendering iridescent rock dove neck feathers
MySQL index collation summary
Mysql database backup and recovery
Sword finger offer 36. binary search tree and bidirectional linked list
JS how to delete an element without deleting its child elements