当前位置:网站首页>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 !
边栏推荐
- VB variable types and control statements (basic)
- Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
- Detailed explanation of arm instruction CMP
- Use abp Zero builds a third-party login module (III): web side development
- SAP FICO 第三节 BDC和LTMC导入S4财务科目
- New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China
- “font/woff“ and “font/woff2“ in file “mime.types“
- Multithreading programming under Win32 API
- (Niuke multi School II) G-LINK with monotonic subsequence (construction question)
- Tutorial: encryption keys in cloud (using golang and CLI)
猜你喜欢

VSCode 如何开启多个终端?如何横向显示?

ROI pooling and ROI align

Koa2 learning

Ffmpeg notes (I) fundamentals of audio and video

Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)

【Node】服务端口被占用Error: listen EADDRINUSE: address already in use :::9000-如何关闭node启动的端口

(2022 Niuke multi school) D-Link with game glitch (SPFA)

(2022牛客多校)D-Link with Game Glitch(spfa)
![(15) [driver development] over written copy](/img/1c/68dfff5671add1fe91567e4df34135.png)
(15) [driver development] over written copy

【datawhale202207】强化学习:强化学习基础
随机推荐
VO, dto, do, Po distinction and use
Qt 5界面修改无效的问题解决QtDesigner修改之后无效的解决办法
MySQL queries the table name under the current database
How programmers write bugs
(2022 Niuke multi school) D-Link with game glitch (SPFA)
Review of three traversal methods of map
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
Ffmpeg notes (I) fundamentals of audio and video
Machine learning keras fitting sine function
Seekbar attribute reference
Typedef usage and template
Classic cases of static keywords and block blocks
VSCode 如何开启多个终端?如何横向显示?
(14)[驱动开发]配置环境 VS2019 + WDK10 写 xp驱动
MySQL中建表时 pk、nn、qu、b、un、zf、ai、g代表的意思
Using JS to realize the linkage effect of form form's secondary menu
(牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)
Blocking Queue Analysis
(16)[系统调用]追踪系统调用(3环)
Leetcode/ integer division