场景:需要同步线上mysql指定表中数据到新版本开发环境的mysql库
初步计划用shell脚本同步。
#!/bin/bash
db1_host=""
db1_port="3306"
db1_username=""
db1_password=""
db1_dbname="source"
db2_host=""
db2_port="3306"
db2_username=""
db2_password=""
db2_dbname="dest"
commond1="mysql -h${db1_host} -P${db1_port} -u${db1_username} -p${db1_password} ${db1_dbname} -N"
commond2="mysql -h${db2_host} -P${db2_port} -u${db2_username} -p${db2_password} ${db2_dbname}"
tenant_field="create_time update_time charge_time expired_time delivery_time open_time tenant_code tenant_name address contacts phone_code telephone email status logo"
tenant_field2="REPLACE(IFNULL(create_time,'1970-01-01 00:00:00'),' ','##') as create_time,REPLACE(IFNULL(update_time,'1970-01-01 00:00:00'),' ','##') as update_time, REPLACE(IFNULL(charge_time,'1970-01-01 00:00:00'),' ','##') as charge_time,REPLACE(IFNULL(expired_time,'1970-01-01 00:00:00'),' ','##') as expired_time,REPLACE(IFNULL(delivery_time,'1970-01-01 00:00:00'),' ','##') as delivery_time,REPLACE(IFNULL(open_time,'1970-01-01 00:00:00'),' ','##') as open_time, tenant_code,REPLACE(tenant_name,' ','##') as tenant_name,address,contacts,phone_code,telephone,email,status,logo"
tenant_field3="create_time,update_time,charge_time,expired_time,delivery_time,open_time,tenant_code,tenant_name,address,contacts,phone_code,telephone,email,status,logo"
while read ${tenant_field}
do
${commond2} -e "insert into t_tenant(${tenant_field3}) values('`echo "${create_time}" | sed 's/##/ /g'`','`echo "${update_time}" | sed 's/##/ /g'`','`echo "${charge_time}" | sed 's/##/ /g'`','`echo "${expired_time}" | sed 's/##/ /g'`','`echo "${delivery_time}" | sed 's/##/ /g'`','`echo "${open_time}" | sed 's/##/ /g'`','${tenant_code}','${tenantName}','${address}','${contacts}','${phone_code}','${telephone}','${email}',${status},'${logo}