#/!bin/bash
HOST=127.0.0.1 #ip (127.0.0.1表示本机地址)
USER=root #数据库用户名
PASSWORD=password #数据库密码
DATABASE=pig #数据库名
BACKUP_PATH=/home/hdkg/bkdata/ #备份目录
logfile=/home/hdkg/bklog/data.log #记录日志
TABLES="test test1" #同步的表名
SQLFILE="export.sql" #导出的SQL
#源库
DST_HOST=127.0.0.2 #ip (127.0.0.1表示本机地址)
DST_USER=root #数据库用户名
DST_PASSWORD=password #数据库密码
DST_DATABASE="test" #数据库名
#删除目标库数据
mysql -h $DST_HOST -u $DST_USER -p$DST_PASSWORD -s $DST_DATABASE <<EOF
delete from test;
delete from test1;
EOF
#将日志打印再日志文件中
if [ $? -ne 0 ]
then
echo "delete data fail!">>$logfile
exit 4
fi
echo "delete data Successful!">>$logfile
#如果不存在备份目录就创建
if [ ! -d $BACKUP_PATH ]
then
mkdir $BACKUP_PATH
fi
#进入备份目录
cd $BACKUP_PATH
#导出原始数据表,忽略创建表的语句
mysqldump -h $HOST -u $USER -p$PASSWORD $DATABASE --no-create-info $TABLES > $SQLFILE
if [ $? = 0 ]
then
echo "export success"
echo "export data Successful!" >> $logfile
#导入到目标库
mysql -h $DST_HOST -u $DST_USER -p$DST_PASSWORD -s $DST_DATABASE <<EOF
source $BACKUP_PATH$SQLFILE
EOF
if [ $? = 0 ]
then
echo "import data Successful!" >> $logfile
else
echo "import data fail!" >> $logfile
exit 4
fi
else
echo "export data fail!" >> $logfile
fi