MySQL备份
MySQL备份
1、MySQL备份过程:
MyISAM
全备:
a. 锁表 -- LOCK TABLE -- --lock-all-tables
b. 同步表 -- FLUSH TABLES
c. 刷新二进制日志 -- FLUSH LOGS -- --flush-logs
d. 记录二进制日志位置 -- SHOW MASTER STATUS -- --master-data=2
e. 备份数据 -- -- mysqldump --databases
f. 释放锁 -- UNLOCK TABLES
g. 删除旧二进制日志 -- PURGE BINARY LOGS TO
总结命令:
mysqldump --databases --lock-all-tables --master-data=2 --flush-logs DB_NAME > db_name-datetime.sql
增量备份:
a. 刷新二进制日志 -- FLUSH LOGS
b. 复制旧的二进制日志即可
InnoDB
全备:
a. 刷新二进制日志
b. 记录当前二进制日志位置
c. 删除旧二进制日志
***相关锁表等操作数据库自动完成,由参数--single-transaction即可完成
总结命令:
mysqldump --databases --master-data=2 --flush-logs --single-transaction DB_NAME > db_name-datetime.sql
增量备份:
a. 刷新日志 -- FLUSH LOGS
b. 复制旧的二进制日志即可
备份脚本
[root@ibm01 ~]# cat sqlbackup.sh
#!/bin/bash
DB_Name="$1"
DB_Egine="$2"
Backup_Type="$3"
Backup_Time=date +%Y%M%d%H%M%S
Backup_File=$DB_Name-$Backup_Time.sql
Data_Dir="/data/mysql/logs/"
function FullBackup() {
if [ $DB_Egine == "MyISAM" ]
then
mysqldump --databases --lock-all-tables --flush-logs --master-data=2 $DB_Name > $Backup_File
elif [ $DB_Egine == "InnoDB" ]
then
mysqldump --databases --single-transaction --flush-logs --master-data=2 $DB_Name > $Backup_File
fi
Current_BinLog_File=egrep -o "MASTER_LOG_FILE=.*," $Backup_File |cut -d"=" -f2|tr -d ","
mysql -e "PURGE BINARY LOGS TO $Current_BinLog_File;"
}
function IncrBackup() {
BinLog_Files=mysql -N -e "SHOW BINARY LOGS;"|cut -f1 |tr "\n" " "
mysql -e "FLUSH LOGS;"
mkdir /root/binlogs/$Backup_Time
cd $Data_Dir
cp $BinLog_Files /root/binlogs/$Backup_Time
}
if [ $Backup_Type == "Full" ]
then
FullBackup
elif [ $Backup_Type == "Incr" ]
then
IncrBackup
fi
xtrabackup备份:
1、对于innodb可以实现增量备份,对于myISAM只能全备
2、innobackupex /backuppath(备份)
3、innobackupex --apply-log /backuppath/backuptime(应用事务日志)
4、innobackupex --copy-back /backuppath/backuptime(恢复)
xtrabackup增量备份:
备份:
1、innobackupex --incremental --incremental-basedir=/上一次备份路径 /备份路径
增量备份的恢复需要有3个步骤:
1、恢复完全备份
2、恢复增量备份到完全备份(开始恢复的增量备份要添加--redo-only参数,到最后一次增量备份要去掉--redo-only)
3、对整体的完全备份进行恢复,回滚未提交的数据
恢复过程:
##准备一个全备##
[root@centos6 pxb]# innobackupex --apply-log --redo-only /data/pxb/2017-04-24_02-46-11/
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2628145
InnoDB: Number of pools: 1
01:33:52 completed OK!
##将增量1应用到完全备份##
[root@centos6 pxb]# innobackupex --apply-log --redo-only /data/pxb/2017-04-24_02-46-11/ --incremental-dir=/data/pxb/inc/2017-04-28_01-09-40/
##将增量2应用到完全备份,注意不加 --redo-only 参数了##
[root@centos6 pxb]# innobackupex --apply-log /data/pxb/2017-04-24_02-46-11/ --incremental-dir=/data/pxb/inc/2017-04-28_01-27-46/
##把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据##
[root@centos6 pxb]# innobackupex --apply-log /data/pxb/2017-04-24_02-46-11/
xtrabackup: Log applied to lsn 2628145
xtrabackup: The intended lsn is 2643563