采用shell脚本方案,从生产环境取数据,备份文件到本地;将备份文件远程复制到客户服务器,再通过定时器还原文件到数据库。
同步的3步操作
1.从数据库上下载脚本到当前服务器【生产服务器】
调用脚本:
syncfrom.sh
#! /bin/bash
source /etc/profile
db=test
for table in `cat /opt/tmp.txt`
do
echo $table
mysqldump --defaults-extra-file=.my.cnf $db $table --where="1= 1" --set-gtid-purged=OFF > /opt/backup/$table.sql
2.将当前备份脚本传输到客户服务器【生产服务器】
调用脚本:
sync_file_to_nice2.sh
#!/bin/bash
for table in `cat /opt/tmp.txt`
do
echo $table
/opt/sync_file_to_nice.sh $table
done
这里面因为需要expect脚本
所以或循环调用sync_file_to_nice.sh脚本
#!/usr/bin/expect -f
set passwd "abcabc"
set table [lindex $argv 0]
spawn scp /opt/backup/$table.sql root@192.1.1.1:/opt/backup/$table.sql
expect {
"pass"
{
send "$passwd\n"
}
"yes/no"
{
sleep 5
send_user "send yes"
send "yes\n"
}
eof
{
sleep 5
send_user "eof\n"
}
}
set timeout 3000
send "exit\r"
expect eof
3.客户服务器将脚本进行还原【客户服务器】
调用脚本:nice_sync.sh
#!/bin/bash
db=backup
echo "start"
for table in `cat tmp.txt`
do
echo $table
mysql --defaults-extra-file=.my.cnf $db < /opt/backup/$table.sql
done