#!/bin/sh
#调用系统函数库
. /etc/init.d/functions
#Define variables
BACKUPDIR=/backup/sort/
MYSQLUSER=root
MYSQLPASSWD=RedHat@123
MYSQLCMD="mysql -u$MYSQLUSER -p$MYSQLPASSWD"
MYSQLDUMP="mysqldump -u$MYSQLUSER -p$MYSQLPASSWD -x -F -R"
DATABASELIST=" $MYSQLCMD -e show databases; |sed 1d|egrep -v information_schema|bin|innodb|mysql|soc "
for DBNAME in $DATABASELIST
do
if [ ! -d $BACKUPDIR/$DBNAME ];then
mkdir -p $BACKUPDIR/$DBNAME && \
action "创建成功,开始备份" /bin/true
else
action "已经创建,开始备份" /bin/true &>/dev/null
fi
echo -e "\033[32m 开始备份 $DBNAME 数据库 \033[0m"
$MYSQLDUMP $DBNAME | gzip > $BACKUPDIR/$DBNAME/${DBNAME}$(date +%F%H%M%S).gz
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "$DBNAME 数据库备份成功" /bin/true
else
action "$DBNAME 数据库备份失败" /bin/false
continue
fi
echo "开始备份表 $DBNAME"
TABLELIST="$MYSQLCMD -e show tables from $DBNAME |sed 1d"
for TABLENAME in $TABLELIST
do
$MYSQLDUMP $DBNAME $TABLENAME | gzip > $BACKUPDIR/$DBNAME/${DBNAME}${TABLENAME}_$(date +%F%H%M%S).gz
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "$TABLENAME 表备份成功" /bin/true
else
action "$TABLENAME 表备份失败" /bin/false
continue
fi
done
done