bakfile=/data/dbbackup
logfile=/data/bak.log
dbuser=xxx
dbpasswd=xxx
ip=ifconfig | grep "inet addr"| grep Bcast| awk ‘{print $2}‘| awk -F":" ‘{print $2}‘
NowTime=$(date +%Y%m%d)
#ser_port=cat /usr/jyinit/serverlist | awk ‘{print $3}‘ | tr "\n" " "
serverlist=/usr/jyinit/serverlist
#time=13
read -p "请输入需要恢复的数据的时间:(备份的小时数):" time ;echo $time
recovery_databases()
{
#库删除或重建
mysql -u$dbuser -p$dbpasswd -e "drop database if exists xmxyshxy${zoneid}"
echo "批量删除xmxyshxy${zoneid} 完成"
mysql -u$dbuser -p$dbpasswd -e "create database if not exists xmxyshxy${zoneid}"
echo "批量创建xmxyshxy${zoneid} 完成"
#数据还原
cd $bakfile
#解压备份文件
gzip -cdv ${NowTime}xmxy${pf}${zoneid}${time}.FullyBak.tar.gz > ${NowTime}xmxy${pf}${zoneid}${time}.FullyBak.sql
if (test -e ${NowTime}xmxy${pf}${zoneid}${time}.FullyBak.sql)
then
#解压的sql导入到库
mysql -u$dbuser -p$dbpasswd xmxy${pf}${zoneid} < ${NowTime}xmxy${pf}${zoneid}${time}.FullyBak.sql
echo xmxy${pf}${zoneid} backup success >> $logfile
else
echo xmxy${pf}${zoneid} backup fail >>$logfile
exit 1
fi
}
while read noteline
do
if [ -z "echo $noteline | grep -v ^$
" ]; then
continue
fi
commentflag=${noteline:0:1}
if [ "${commentflag}"x == "#"x ]; then
continue
fi
pf=`echo $noteline | awk ‘{print $1}‘`
zoneid=`printf "$noteline" | awk ‘{print $2}‘ `
echo $zoneid
echo $pf
recovery_databases
done<$serverlist
原文地址:http://blog.51cto.com/yzg784534398/2085893