详解mysql数据备份脚本~mysqldump和Innobackupex

目录

1.mysqldump数据库备份脚本:

2.mysqldump表备份脚本:

3.基于Innobackupex的MySQL备份脚本:


1.mysqldump数据库备份脚本:

#! /bin/bash

mysql_user=root
mysql_password=110119
mysql_host=127.0.0.1
ignore_db="Database|information_schema|mysql|performance_schema|sys"
back_path=/dump/db

mkdir -p ${back_path} > /dev/null;cd ${back_path}
echo "保存目录:`pwd`"
back_list=`mysql -h${mysql_host} -u${mysql_user} -p${mysql_password} -e "show databases;" | grep -Ev ${ignore_db}`
#echo ${back_list[*]}
for db in ${back_list[*]};do
	echo "保存文件:${db}_`date "+%F"`.sql"
	mysqldump -h${mysql_host} -u${mysql_user} -p${mysql_password} --databases ${db} > ${back_path}/${db}_`date "+%F"`.sql
done

详解:一个数据库一个sql文件

# 获取的数据库列表所需要忽略的字段
ignore_db="Database|information_schema|mysql|performance_schema|sys"
# 备份保存目录
back_path=/dump/db
# 获取年-月-日
date "+%F

2.mysqldump表备份脚本:

#! /bin/bash

mysql_user=root
mysql_password=110119
mysql_host=127.0.0.1
ignore_db="Database|information_schema|mysql|performance_schema|sys"
back_path=/dump/db

mkdir -p ${back_path} &> /dev/null
cd ${back_path}

back_dbs_list=`mysql -h${mysql_host} -u${mysql_user} -p${mysql_password} -e "show databases;" | grep -Ev ${ignore_db}`
#echo ${back_list[*]}
for db in ${back_dbs_list[*]};do
	mkdir -p ${back_path}/${back_dbs_list}/${db} &> /dev/null
	echo "保存表目录:${back_path}/${db}"
	
	back_tables_list=`mysql -h${mysql_host} -u${mysql_user} -p${mysql_password} -D${db} -e "show tables;" | grep -Ev "Tables_in_test1"`
	for tb in ${back_tables_list};do
		echo "${back_path}/${db}/${tb}_`date '+%F'`.sql"
		mysqldump -h${mysql_host} -u${mysql_user} -p${mysql_password} ${db} ${tb} > ${back_path}/${db}/${tb}_`date "+%F"`.sql
	done
done

详解:一张表一个sql文件,就是多了层循环(循环的是某个数据库的表 列表),没有其他东西

3.基于Innobackupex的MySQL备份脚本:

#! /bin/bash

innobackupex -v
if [ `echo $?` -ne 0 ];then
	echo "innobackupex未安装!!!"
	exit
fi

mysql_host=127.0.0.1
mysql_user=root
mysql_password=110119

time_today=`date '+%F'`
back_path=/innoback
mkdir -p ${back_path} &> /dev/null
echo "保存目录:${back_path}"

rm -rf ${back_path}/${time_today}
if [ `date '+%u'` -eq 7 ];then
	echo "今天周天,完全备份"
	innobackupex --host=${mysql_host} --user=${mysql_user} --password=${mysql_password} ${back_path}/${time_today}
else
	echo "今天不是周天,增量备份"
	if [ -d ${back_path}/`date -d '-1 day' '+%F'` ];then
		basedir_cmd="--incremental-basedir=${back_path}/`date -d '-1 day' '+%F'`/"
		echo ${basedir_cmd}
	fi
	innobackupex --incremental --host=${mysql_host} --user=${mysql_user} --password=${mysql_password} ${back_path}/${time_today} ${basedir_cmd} --no-timestamp | sed -n "$p"
fi

详解:

# 获取今天的年-月-日
time_today=`date '+%F'`
# 备份保存目录
back_path=/innoback
# 删除今天已经备份的内容,不删除在原备份过的目录中再备份报错
rm -rf ${back_path}/${time_today}
# 判断今天是不是星期天,如果是就完全备份,如果不是就增量备份
if [ `date '+%u'` -eq 7 ]
# 先判断是否有昨天的文件
if [ -d ${back_path}/`date -d '-1 day' '+%F'` ]

你可能感兴趣的:(shell,mysql,数据库,php)