MySQL备份脚本

MYSQL备份,每天全量备份(排除由于GTID的缘故造成恢复问题的系统库),然后通过Gzip进行压缩。

#!/bin/bash

#DEFINE VARIABLES
BACKUPUSER=root
BACKUPPASS=root密码 
BACKUPPORT=mysql端口
#BACKUPSOCKET=/home/data/$BACKUPPORT/logs/mysql.sock
BACKUPIP=localhost
MYSQL=/usr/local/mysql/bin/mysql
MYSQLDUMP=/usr/local/mysql/bin/mysqldump
#CONFIG_FILE=/home/data/$BACKUPPORT/conf/my.cnf
BINLOGPATH=/home/dbbak/logs
BACKUPPATH=/home/dbbak/backupdb
DEL_DIRECTORY=`date +%Y%m%d -d "-14 day"`
BACKUPDATE=`date +%Y%m%d`

mkdir $BACKUPPATH/$BACKUPDATE

#connection  to mysql db
mycmd="$MYSQL -u$BACKUPUSER -p$BACKUPPASS -h$BACKUPIP -P$BACKUPPORT"

#backup metadata
DUMP_Table="$MYSQLDUMP -u$BACKUPUSER -p$BACKUPPASS -h$BACKUPIP  -P$BACKUPPORT  -R --events -d"

# backup database for InnoDB
DUMP_InnoDB="$MYSQLDUMP -u$BACKUPUSER -p$BACKUPPASS -h$BACKUPIP -P$BACKUPPORT   --flush-privileges --single-transaction --master-data=2 --set-gtid-purged=OFF --triggers -f -R -E --hex-blob
 "

# backup database for MyISAM
DUMP_MYISAM="$MYSQLDUMP -u$BACKUPUSER -p$BACKUPPASS -h$BACKUPIP -P$BACKUPPORT   --flush-privileges --lock-all-tables --master-data=2 --triggers -f -F -R -E --hex-blob "

echo  `date +%F" "%r` "Start backup  Mysql  databases" >> $BINLOGPATH/$BACKUPDATE.log


#flush logs
$mycmd -e 'flush logs'

#backup config file
#/bin/cp  $CONFIG_FILE  $BACKUPPATH/$BACKUPDATE


#backup binary log file
BINLOG=`find  $BINLOGPATH  -type  f  -name "mysql_bin*" -mtime -2 |grep -v index `
for  i in  $BINLOG  
do
 /bin/cp $i  $BACKUPPATH/$BACKUPDATE/$BACKUPPORT
done


# backup  and compress 
#有些时候,我们要对数据库进行备份的时候,由于GTID的缘故,导出系统库后,再次导入其他环境的数据库时,就会出问题。所以,我们需要排掉一些系统库,排除GTID对于数据库迁移的影响。

db=`$mycmd  -e "show databases;"|grep -Evw "Database|information_schema|performance_schema|test|mysql|sys"`
${DUMP_InnoDB} --databases $db --ignore-table=要忽略的表 |gzip > $BACKUPPATH/$BACKUPDATE/full_$(date +%w).sql.gz

#flush logs
$mycmd -e 'flush logs'

解压缩方法:使用gunzip gunzip full_2.sql.gz

posted @ 2024-12-17 12:35  李济宏(Amadeus)  阅读(1)  评论(0编辑  收藏  举报