#!/bin/bash
#Author:heyic.m@gmail.com
#按数据库分库备份,可排除指定库
#2016-06-08 create.
#2017-02-21 add expire_days.
#2017-02-27 redifine backupPath's name style.
#备份用户与密码
mysql_user="root"
mysql_pwd="pass"
mysql_host="localhost"
cmd_mysql="/data/apps/mysql/bin/mysql"
cmd_mysqldump="/data/apps/mysql/bin/mysqldump"
#备份目录
#backuppath="/data/backup/mysql/`date +%F`/"
backup_basedir="/data/backup/mysql"
day=`date +%F`
#备份保存时间
backuppath="${backup_basedir}/${day}"
expire_days=30
if [ ! -d $backuppath ];then
mkdir $backuppath -p
fi
bk_time=`date +%y%m%d%H%M%S`
#排除不备份的数据库,多个库用'|'隔开
exclude_db="mysql|test|information_schema|performance_schema"
#备份功能
if [ -z $mysql_pwd ];then
if [ -z $exclude_db ];then
backup_db=`$cmd_mysql -u$mysql_user -h$mysql_host -e "show databases;"|grep -vi database`
else
backup_db=`$cmd_mysql -u$mysql_user -h$mysql_host -e "show databases;"|grep -vi database |grep -Ev $exclude_db`
fi
else
if [ -z $exclude_db ];then
backup_db=`$cmd_mysql -u$mysql_user -p$mysql_pwd -h$mysql_host -e "show databases;"|grep -vi database`
else
backup_db=`$cmd_mysql -u$mysql_user -p$mysql_pwd -h$mysql_host -e "show databases;"|grep -vi database |grep -Ev $exclude_db`
fi
fi
for db in $backup_db;do
if [ -z $mysql_pwd ];then
echo "`date` Is backuping $db,please wait..."
$cmd_mysqldump -u$mysql_user -h$mysql_host \
--single-transaction \
--hex-blob \
--events \
--routines \
--triggers \
--master-data=2 \
--set-gtid-purged=off \
--databases \
$db | gzip -9 > $backuppath/$db.$bk_time.sql.gz
else
echo "`date` Is backuping $db,please wait..."
$cmd_mysqldump -u$mysql_user -p$mysql_pwd -h$mysql_host \
--single-transaction \
--hex-blob \
--events \
--routines \
--triggers \
--master-data=2 \
--set-gtid-purged=off \
--databases \
$db | gzip -9 > $backuppath/$db.$bk_time.sql.gz
fi
done
echo "`date` The backup is completed."
#清除过期备份
#获取备份目录路径
#截取长度(res_len)=总长度(total_len)-12(/2017-02-21/)
#${backuppath:0:$res_len}
#total_len=${#backuppath}
#res_len=$[ $total_len - 12 ]
#clean_path=${backuppath:0:$res_len}
clean_path=${backup_basedir}
#echo $clean_path
#清除
echo "start to remove backupfiles of more than $expire_days days."
find $clean_path -mtime +$expire_days | xargs rm -rfv
echo "done."
企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,
成都创新互联公司面向各种领域:
白乌鱼等
成都网站设计、
营销型网站建设解决方案、网站设计等建站排名服务。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻名称:mysql分库备份脚本-创新互联
网页网址:
http://njwzjz.com/article/dgjceo.html