以下为mysql备份脚本,因使用的mysqlhotcopy命令,所以只适用于MYISAM引擎数据库,并且建议在100G容量以内的数据库使用。本脚本做的是每天完全备份。
[root@mysql mysql_backup]# cat mysqlbackup.sh
#!/bin/bash
#Shell Name: mysqlbackup.sh.
#MySQL Daily Backup by mysqlhotcopy tools.
#Last Modify: 2008-7-30 Writen by Navy Dai.
#crontab -e
#0 4 * * * /databak/mysql_backup/mysqlbackup.sh
#Shell will run at 4:00 everyday.
MYSQLBACKUPDIR=/databak/mysql_backup
SHELLDIR=$MYSQLBACKUPDIR/mysqlbackup.sh
MYSQLDATADIR=/var/lib/mysql
MYSQLBACKUPLOG=$MYSQLBACKUPDIR/MySQLBackup.log
DATE=`date -I`
mkdir -p $MYSQLBACKUPDIR/MySQL-$DATE
if [ -e "$MYSQLBACKUPLOG" ];then
continue
else
touch $MYSQLBACKUPLOG
fi
echo "------------Backup start at `date +%Y-%m-%d_%H:%M:%S`-----\
---------" >>$MYSQLBACKUPLOG
for DB in `find $MYSQLDATADIR -type d | \
grep -v '^/var/lib/mysql$'| \
sed "s/\/var\/lib\/mysql\///" | \
sed "s/test//"`;do
if [ "x$DB" == "x" ];then
continue
else
/usr/bin/mysqlhotcopy -q $DB $MYSQLBACKUPDIR/MySQL-$DATE
echo "`date +%Y-%m-%d_%H:%M:%S` Database:$DB mysqlhotcopy backup success!" >>$MYSQLBACKUPLOG
fi;
done
#tar
for BAKDB in `find $MYSQLBACKUPDIR/MySQL-$DATE/ -type d | \
grep -v '^/databak/mysql_backup/MySQL-'$DATE'$'| \
sed "s/\/databak\/mysql_backup\/MySQL-$DATE\///"`;
do
cd $MYSQLBACKUPDIR/MySQL-$DATE
tar -zcf $BAKDB-$DATE.tar.gz $BAKDB
echo "`date +%Y-%m-%d_%H:%M:%S` Database:$BAKDB tar success!" >>$MYSQLBACKUPLOG
rm -fr $MYSQLBACKUPDIR/MySQL-$DATE/$BAKDB
echo "`date +%Y-%m-%d_%H:%M:%S` Database:$BAKDB Remove success!" >>$MYSQLBACKUPLOG;
done
echo "------------Backup end at `date +%Y-%m-%d_%H:%M:%S`-----\
---------" >>$MYSQLBACKUPLOG
exit 0
阅读(1048) | 评论(0) | 转发(0) |