码迷,mamicode.com
首页 > 数据库 > 详细

mysql分库分表备份脚本

时间:2019-08-26 11:30:37      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:DBName   table   author   tin   create   tab   define   root   http   

#!/bin/sh
#######################################################
#ShellName:mysql database and tables backup
#Author:zkg
#Created Time:2019-08-26
#Blog Address:https://blog.51cto.com/1009516
#######################################################

#调用系统函数库
. /etc/init.d/functions
#Define variables
BACKUPDIR=/data/mysqlbak/
MYSQLUSER=root
MYSQLPASSWD=DbApp
MYSQLSOCK=/data/mysql/tmp/mysql.sock
MYSQLCMD="/data/mysql/bin/mysql -u$MYSQLUSER -p$MYSQLPASSWD -S $MYSQLSOCK"
MYSQLDUMP="/data/mysql/bin/mysqldump -u$MYSQLUSER -p$MYSQLPASSWD -S $MYSQLSOCK -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 "Create a backup directory successfully and start backing up the database" /bin/true
else
action "The backup directory already exists. Start backing up the database." /bin/true &>/dev/null
fi
echo -e "\033[32mstarting backup $DBNAME databases\033[0m"
$MYSQLDUMP $DBNAME|gzip > $BACKUPDIR/$DBNAME/${DBNAME}$(date +%F%H%M%S).gz
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "$DBNAME database Successful backup" /bin/true
else
action "$DBNAME database failed backup" /bin/false
continue
fi
echo "starting backup tables from $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 table Successful backup" /bin/true
else
action "$TABLENAME table failed backup" /bin/false
continue
fi
done
done

mysql分库分表备份脚本

标签:DBName   table   author   tin   create   tab   define   root   http   

原文地址:https://blog.51cto.com/1009516/2432524

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!