Commit 8567a2c0 by tingweiwang

mysql备份脚本

0 parents
Showing with 57 additions and 0 deletions
#!/bin/bash
# author: wangtingwei
#全备和每个库的备份
#########################################指定多个库单独备份#################################################
HOST="172.24.14.41" #测试环境数据库
BACK_PATH="/data/mysql_back"
DATE=`date +%F-%H-%M`
USER="root"
PASSWD="seetatech"
DB_NAME=(
autodl-core
user-center
seetaas
autocnn
)
#######################################数据库单备##############################################################
single_back() {
echo "创建mysql单库备份所需目录"
test -d $BACK_PATH/$DATE-single || mkdir $BACK_PATH/$DATE-single -pv
for db_name in ${DB_NAME[@]}
do
mysqldump -u${USER} -p${PASSWD} -h $HOST --routines --single-transaction --master-data=2 -B -F --databases $db_name |gzip >$BACK_PATH/$DATE-single/$db_name.sql.gz
done
find $BACK_PATH/ -mmin +1 -type d |xargs rm -rf
}
#搭配计划任务,执行频率为1小时1次,只保留一天的备份。
#######################################数据库全备############################################################
all_back() {
echo "创建mysql全库备份所需目录"
test -d $BACK_PATH/$DATE-all || mkdir $BACK_PATH/$DATE-all -pv
mysqldump -u${USER} -p${PASSWD} -h $HOST --routines --single-transaction --master-data=2 -A -B -F |gzip >$BACK_PATH/$DATE-all/all.sql.gz
find $BACK_PATH/ -mmin +1 -type d |xargs rm -rf
}
#搭配计划任务,执行频率为1天1次,只保留一天的备份。
###################################################################################################
case $1 in
single_back)
single_back
;;
all_back)
all_back
;;
*)
echo "please input (single_back|all_back)"
esac
#################################################添加计划任务如下##############################
#* */1 * * * root sh /root/common-script/mysql_backup.sh single_back #单库备份频率,每小时一次
#12 12 * * * root sh /root/common-script/mysql_backup.sh all_back #全库备份频率,每天一次。
###################################数据恢复操作########################################################
#gunzip < /path/xxxx.sql.gz | mysql -uroot -proot databasename 单库
#gunzip < /path/xxxx.sql.gz | mysql -uroot -proot 全库
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!