MySQL增量恢復

MySQL增量恢復

本文連接:http://www.javashuo.com/article/p-okaomqyb-ds.htmlmysql

HOSTNAME=mariadb
hostname $HOSTNAME
echo "$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "$(ifconfig eth0|grep inet|awk -F'[ :]' '{print $13}') $(hostname)">>/etc/hosts
sed -i "s/^HOSTNAME.*$/HOSTNAME=$HOSTNAME/g" /etc/sysconfig/network
ping -c 3 $(hostname)sql

在配置文件[mysqld]標籤下追加binlog配置

cat >>/etc/my.cnf<<EOF
log-bin=/var/lib/mysql/binlog
binlog_cache_size=32K
max_binlog_cache_size=60M
max_binlog_size=50M
binlog-format=ROW
expire_logs_days=7
EOF
/etc/init.d/mysql restartbash

查看是否生效

mysql -uroot -pvincent
show variables like 'log_bin%';
system du -sh /var/lib/mysql/binlog*
exitide

建庫,跑腳本模擬業務:

建立兩個業務庫和對應的業務表:

mysql -uroot -pvincent
create database vincent1;
create database vincent2;
create table vincent1.test1
(id int,dt timestamp NOT NULL DEFAULT NOW());
create table vincent2.test1
(id int,dt timestamp NOT NULL DEFAULT NOW());
GRANT ALL PRIVILEGES ON vincent1. TO 'vincent1'@'%' IDENTIFIED BY 'vincent1';
GRANT ALL PRIVILEGES ON vincent2.
TO 'vincent2'@'%' IDENTIFIED BY 'vincent2';
flush privileges;
exit測試

後臺執行腳本模擬生產:

cat >/tmp/test1.sh<<EOF
SQL1='insert into vincent1.test1(id) select count(id) from vincent1.test1;'
while true;do echo \${SQL1}|mysql -uroot -pvincent;sleep 1;done &
EOF
cat >/tmp/test2.sh<<EOF
SQL2='insert into vincent2.test1(id) select count(id) from vincent2.test1;'
while true;do echo \${SQL2}|mysql -uroot -pvincent;sleep 1;done &
EOF
bash /tmp/test1.sh &
bash /tmp/test2.sh &.net

源庫備份方法:

全庫備份:

mysqldump -uroot -pvincent --flush-privileges \
--single-transaction --master-data=2 --triggers \
--routines --events --hex-blob -A >/tmp/Full_Backup.sqlrest

多業務庫備份:

mysqldump -uroot -pvincent --flush-privileges \
--single-transaction --master-data=2 --triggers \
--routines --events --hex-blob -B vincent1 vincent2 >/tmp/Muti_Backup.sql日誌

單庫備份:

mysqldump -uroot -pvincent --flush-privileges \
--single-transaction --master-data=2 --triggers \
--routines --events --hex-blob vincent1 >/tmp/Vincent1_Backup.sqlorm

注意,未使用-B參數,不包含建庫語句,恢復時須要提早建庫,而後指定該庫

基於binlog的全庫恢復實驗:

依據簡單說明中的實驗機部署部分部署另一臺MySQL庫

源庫停掉腳本,傳輸備份到恢復庫主機:

ps -ef|grep test|grep -v grep|awk '{print $2}'|xargs kill -9
mysql -uroot -pvincent
select max(id) from vincent1.test1;
select max(id) from vincent2.test1;
exit
3867
3757
scp /tmp/.sql /var/lib/mysql/binlog. 192.168.77.11:/tmp/blog

目標庫全庫恢復:

依據全備文件,定位起始binlog日誌文件和pos

cd /tmp/
head -30 Full_Backup.sql|grep CHANGE

-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.000002', MASTER_LOG_POS=268403;

抽取全備點以後的binlog日誌

mysqlbinlog binlog.000002 --start-position=268403 >Full_binlog.sql

若是有其餘binlog日誌,則追加便可:

mysqlbinlog binlog.000003 >>Full_binlog.sql

全庫恢復

mysql -uroot -pvincent < Full_Backup.sql

追加binlog,增量恢復

mysql -uroot -pvincent < Full_binlog.sql

測試:

mysql -uroot -pvincent
select max(id) from vincent1.test1;
select max(id) from vincent2.test1;
select User,Host from mysql.user where user!='root';
exit
3867
3757

vincent1

vincent2

由於是全庫恢復,所以建立的帳號和倆業務庫所有恢復

業務庫中的表數據和源端保持一致,增量恢復成功

基於binlog的單庫恢復實驗:

使用單庫備份,選擇vincent1庫作恢復測試

依據單備文件,定位起始binlog日誌文件和pos

cd /tmp/
head -30 Vincent1_Backup.sql|grep CHANGE

-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.000002', MASTER_LOG_POS=362938;

抽取全備點以後的binlog日誌,使用參數-d只抽取vincent1庫相關操做

mysqlbinlog binlog.000002 -d vincent1 --start-position=362938 >Vincent1_binlog.sql

若是有其餘binlog日誌,則追加便可:

mysqlbinlog -d vincent1 binlog.000003 >>Vincent1_binlog.sql

刪除已經恢復的數據,作單庫恢復

mysql -uroot -pvincent -e "drop database vincent1;"
mysql -uroot -pvincent -e "drop database vincent2;"
mysql -uroot -pvincent -e "drop user vincent1;"
mysql -uroot -pvincent -e "drop user vincent2;"
mysql -uroot -pvincent -e "flush privileges;"

由於單庫備份腳本沒有使用-B參數,無建庫語句,所以要手動建庫

mysql -uroot -pvincent -e "create database vincent1;"
mysql -uroot -pvincent -o vincent1 < Vincent1_Backup.sql

追加binlog,增量恢復

mysql -uroot -pvincent < Vincent1_binlog.sql

測試:

mysql -uroot -pvincent
select max(id) from vincent1.test1;
select max(id) from vincent2.test1;
select User,Host from mysql.user where user!='root';
exit
3867

表不存在

無root帳號以外的帳號

由於是單庫恢復,所以建立的業務帳號和第二個業務庫所有沒有恢復

業務庫1中的表數據和源端保持一致,增量恢復成功

相關文章
相關標籤/搜索