将数据库直接复制一份,自动加锁,只适用于MYISAM表,脚本如下:
#! /bin/bash
# use like this : hotcopydb db1 db2 db3 ...
dbsum=$#
backdir=/opt/hotbackup/myisam
if [ "${dbsum}" -eq 0 ];then
echo "Error:no database chosed"
exit 1
fi
if [ -d ${backdir} ];then
echo "${backdir} exist!"
else
mkdir -p ${backdir}
fi
LogFile=/var/log/hotcopy_backup.log
touch $LogFile
datetime=`date +"%Y%m%d%H%M%S"`
hotcopypath=${backdir}${datetime}
mkdir -p ${hotcopypath}
echo "------------------------------------------------------------------" >> $LogFile
cd ${backdir}
while [ "`ls ${backdir} | wc -l`" -gt 2 ]
do
rm -rf `ls | head -1`
done
echo "hotcopy start at [`date +"%Y-%m-%d %H:%M:%S"`]" >> $LogFile
for i in $*;do
echo "backing up for database $i starting ..."
/usr/local/mysql/bin/mysqlhotcopy -uroot -p123456 --flushlog $i ${hotcopypath}
echo "backing up for database $i completed"
done
echo "hotcopy end at [`date +"%Y-%m-%d %H:%M:%S"`]" >> $LogFile
echo "tar and gzip the backed file now ..." >> $LogFile
cd ${backdir}
tar zcvf ${datetime}.tar.gz ${datetime}
rm -rf ${datetime}
echo "all success ! you can find the backed file in ${backdir} suffixed by .tar.gz" >> $LogFile
exit 0
阅读(1767) | 评论(0) | 转发(0) |