自己编写自动同步脚本

Step1:

运行脚本,将结果保存到sync_date.log下;

执行的时候,将地址修改为slave ip

--databases 指定为需要同步的db_name

user 和password修改为对应的账号密码;

/*

#!/bin/bash


#define sync function
sync(){
a=`pt-table-sync --execute --sync-to-master --lock 1 --print --charset=utf8 --verbose --databases itdb u=copy2,p=password,h=x.x.x.x,P=3306`
echo -e "$a"
}


#output sync to log file
path=sync_`date +%Y%m%d.log`


sync > $path




#print sync result 

 

echo "Sync OK, plese check $path"

*/

Step 2:

运行 sh sync.sh  slave_address  db_name  slave_user_name  slave_password的方式:

一共四个参数,依次进行;

/*

#!/bin/bash


#define sync function
#h is host ,this is slave address
#the charset is setted utf8, if your table is not utf8, remove it
#lock could be 0 - 3, 0=don't lock; 1 lock sync part; 2 lock table ;3 lock server;


slave_add=$1
slave_db=$2
slave_user=$3
slave_pass=$4


sync(){
a=`pt-table-sync --execute --sync-to-master --lock 1 --print --charset=utf8 --verbose --databases $slave_db u=$slave_user,p=$slave_pass,h=$slave_add,P=3306`
echo -e "$a"
}


#output sync to log file
path=sync_`date +%Y%m%d.log`


sync > $path




#print sync result 


echo "Sync OK, plese check $path"

*/

Step3:

当用户未输入4个参数的时候,给出提示;

/*

#!/bin/bash


#define sync function
#h is host ,this is slave address
#the charset is setted utf8, if your table is not utf8, remove it
#lock could be 0 - 3, 0=don't lock; 1 lock sync part; 2 lock table ;3 lock server;


slave_add=$1
slave_db=$2
slave_user=$3
slave_pass=$4


sync(){
a=`pt-table-sync --execute --sync-to-master --lock 1 --print --charset=utf8 --verbose --databases $slave_db u=$slave_user,p=$slave_pass,h=$slave_add,P=3306`
echo -e "$a"
}




if [ $# -ne 4  ]
then
echo 
echo Usage: sh script.sh Slave_Address db_name Slave_username Slave_password;
echo There has 4 parameters. No error in the order of the parameters;
echo Plese try again
echo
else
#output sync to log file
path=sync_`date +%Y%m%d.log`
sync > $path


#print sync result 
echo "Sync OK, plese check $path"
fi

 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值