mysql5.7全/增量备份脚本

#!/bin/bash

MODE=$1
MYSQLUSER=root
BINLOGPATH=/var/lib/mysql
BINLOGNAME=mysql-bin
ARCHIVEPATH=/tmp/mysql-backup

copyBinlogs() {
# copy binlogs to archive dir
echo "Copying binlogs"
setRead;
for FILE in `cat $BINLOGPATH/$BINLOGNAME.index`
do
SFILE=(${FILE/*\//})
if [ ! -f $ARCHIVEPATH/$SFILE.gz ]
then
echo "- binlog $SFILE"
cd $BINLOGPATH
cp $FILE $ARCHIVEPATH
gzip $ARCHIVEPATH/$SFILE
fi
done
resetRead;
}
saveBinlogs() {
# move latest binlogs to savedir
echo "Saving binlogs"
SAVEDIR=`date +%Y%m%d`
echo $SAVEDIR
mkdir -p $ARCHIVEPATH/$SAVEDIR
mv $ARCHIVEPATH/*.gz $ARCHIVEPATH/$SAVEDIR
}


# --- Main ---
case "$MODE" in
#全备份
'full')
# Weekly backup
# - copy all bin logs to backup directory
# - clean path of backup directory
echo "Weekly backup"
DATE=`date +%Y%m%d`
mysqldump -u $MYSQLUSER -p --single-transaction --flush-logs --master-data=2 \
--all-databases  | gzip > $ARCHIVEPATH/$SAVEDIR/full-$DATE.sql.gz
copyBinlogs;
saveBinlogs;
;;

#增量备份
'incremental')
# Daily backup
# - flush logs
# - copy all bin logs to backup directory if not already done
echo "Mysql daily backup"
mysqladmin -u $MYSQLUSER -p flush-logs
copyBinlogs;
;;


*)
echo "Usage: mysql_backup [full|incremental]"
;;


esac

你可能感兴趣的:(mysql)