You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/database/dbBak.sh

59 lines
2.0 KiB

#!/usr/bin/bash
DBUSER=root
DBPASSWORD=123
BACKUPDIR=/dback
HISTLOG=/tmp/history.log
RULE=(0 1 2 1 2 2 2) # 0 full 1 diff 2 incr
DATE=$(date "+%Y-%m-%d_%H-%M-%S")
logdev=local2
full="innobackupex --user=$DBUSER --password=$DBPASSWORD $BACKUPDIR"
diff="innobackupex --user=$DBUSER --password=$DBPASSWORD --incremental $BACKUPDIR --incremental-basedir=${BACKUPDIR}/"
envCheck(){
mysql -u$DBUSER -p$DBPASSWORD -e "select user()" &>/dev/null
if [ $? -ne 0 ];then
logger -p ${logdev}.error "mysql 连接异常" && exit 2
fi
if [ ! -e $BACKUPDIR ];then
logger -p ${logdev}.error "备份目录未正常初始化" && \
mkdir $BACKUPDIR
fi
}
work(){
datetime=$[`date "+%u"`-1]
case ${RULE[$datetime]} in
0)
mv $HISTLOG $BACKUPDIR/ && \
tar cvzf /tmp/${DATE}.tar.gz $BACKUPDIR && \
logger -p ${logdev}.info "历史备份文件打包成功 /tmp/${DATE}.tar.gz" && \
rm -rf $BACKUPDIR $HISTLOG && mkdir $BACKUPDIR || \
logger -p ${logdev}.warn "历史备份文件打包异常 ${DATE}"
eval $full && \
echo "full `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
logger -p ${logdev}.info "完整备份完成 `ls $BACKUPDIR |tail -n1`" || \
logger -p ${logdev}.error "完整备份异常 ${DATE}"
;;
1)
BEFOREDIR=$(awk '$1 ~ /full/{print $2}' $HISTLOG)
eval $diff${BEFOREDIR} && \
echo "diff `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
logger -p ${logdev}.info "差异备份完成 `ls $BACKUPDIR |tail -n1`" || \
logger -p ${logdev}.error "差异备份异常 ${DATE}"
;;
2)
BEFOREDIR=$(tail -n1 $HISTLOG|awk '{print $2}')
eval $diff${BEFOREDIR} && \
echo "incr `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
logger -p ${logdev}.info "增量备份完成 `ls $BACKUPDIR |tail -n1`" || \
logger -p ${logdev}.error "增量备份异常 ${DATE}"
;;
*)
echo "异常操作"
;;
esac
}
envCheck
work