定时导出mysql本地数据替换远程数据库数据脚本分享
代码如下:
需求在每天的5点将192.168.3.5上ser_a库中的tb_a表导入到192.168.3.6上的ser_b库中,表名还叫tb_a 服务端脚本/usr/shell_clubs/auto_sql/server.sh #!/bin/sh #code by scpman /usr/bin/find /usr/shell_clubs/auto_sql/ -name "*.sql" -mtime +3|xargs rm #del old sql #get new sql time=`date +%Y%m%d` mysqldump -uroot -p123456 ser_a tb_a > /usr/shell_clubs/auto_sql/`date +%Y%m%d`_tb_a.sql sleep 2 ssh -p10022 192.168.3.6 <<EEE /usr/shell_clubs/auto_sql/client.sh EEE 说明:服务端脚本会导出指定表,并通过公钥ssh到目标机器上,调用客户端脚本 客户端脚本/usr/shell_clubs/auto_sql/client.sh #!/bin/sh #code by scpman #client.sh time=`date +%Y%m%d` /usr/bin/find /usr/shell_clubs/auto_sql/ -name "*.sql" -mtime +3|xargs rm #del old sql /usr/local/bin/rsync -vzrtopgl --progress [email protected]::all/usr/shell_clubs/auto_sql/`date +%Y%m%d`_tb_a.sql /usr/shell_clubs/auto_sql/; counts=`ls -l /usr/shell_clubs/auto_sql/ | grep "$time" | awk '{print $NF}'|wc -l` echo $counts if [ "$counts" -eq 1 ] then echo sql:$counts mysql -uroot -ptest123 <<FFF use ser_b; drop table tb_a; FFF mysql -uroot -ptest123 ser_b < /usr/shell_clubs/auto_sql/`date +%Y%m%d`_tb_a.sql --default-character-set=utf8 echo --------------done else echo sql wrong fi 说明:客户端脚本被调用时,会先删除当前路径下的旧sql记录,然后通过rsync将服务端导出的表,同步过来,并检查是否同步过来,如果同步成功,则清空现有旧tb_a,将新的tb_a导入进库,如果没同步成功,则脚本退出(此处可以先发个邮件通知再退出) 然后流程通了 现在再加一条定时在192.168.3.5上(server端) ##send sql to 192.168.3.6 0 5 * * * su - root -c "/usr/shell_clubs/auto_sql/server.sh" >>/usr/shell_clubs/auto_sql/res.log
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30