标签:
L008-oldboy-mysql-dba-lesson08
xtrabackup安装
[root@web01 installer]#
wget
https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.3/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
[root@web01 installer]# wget
ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libev-4.15-1.el6.rf.x86_64.rpm
[root@web01 installer]# yum -y install perl
perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL
[root@web01 installer]# rpm -ivh
libev-4.15-1.el6.rf.x86_64.rpm
[root@web01 installer]# rpm -ivh
percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
# yum localinstall -y percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
[root@web01 installer]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
[root@web01 installer]# mysqladmin -uroot
-ptestpassword shutdown
[root@web01 ~]# vi /var/lib/mysql/my.cnf #添加
[mysqld]
####################################
innodb_data_home_dir = /var/lib/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /var/lib/mysql
innodb_buffer_pool_size = 384M
innodb_buffer_pool_instances = 4
default_storage_engine = innodb
innodb_log_file_size = 100M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_file_per_table = 1
innodb_flush_method = O_DIRECT
#transaction-isolation =
READ-UNCOMMITTED
log_bin_trust_function_creators = 1
wait_timeout=20
[root@web01 mysql]# pwd
/var/lib/mysql
[root@web01 mysql]# mv ib_logfile* ../ #必须得把这个数据文件移走才能启动成功
[root@web01 installer]#
/usr/local/mysql/bin/mysqld_safe --defaults-file=/var/lib/mysql/my.cnf &
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword /mysql_backup/ #全备完毕
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf --user=root
--password=testpassword --stream=tar /mysql_backup/
1>/mysql_backup/20160614_all.tar.gz | gzip /mysql_backup/20160614_all.tar.gz
2>/mysql_backup/innobackupex.log #加上tar流的备份
#给数据库添加一些数据
[root@web01 ~]# mysql -uroot -ptestpassword
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| mywebsite |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.05 sec)
mysql> use test;
Database changed
mysql> create table mytest(id int);
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| mytest |
+----------------+
1 row in set (0.00 sec)
mysql> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
|
1465872984 |
+-----------------------+
1 row in set (0.02 sec)
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> insert into mytest values(unix_timestamp(now()));
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> insert into mytest
values(unix_timestamp(now()));
mysql> select * from mytest ;
+------------+
| id
|
+------------+
|
1 |
| 1465873375 |
| 1465873378 |
| 1465873378 |
| 1465873379 |
| 1465873380 |
| 1465873427 |
| 1465873428 |
| 1465873429 |
| 1465873430 |
| 1465873431 |
| 1465873431 |
+------------+
mysql> insert into mytest select * from
mytest; #大批量插入
mysql> insert into mytest select * from
mytest;
mysql> insert into mytest select * from
mytest;
mysql> insert into mytest select * from
mytest;
mysql> insert into mytest select * from
mytest;
#下面小段是为了修复数据库,无此问题的可以忽略。
[root@web01 mysql]# mysqladmin -uroot -ptestpassword shutdown
[root@web01 mysql]# cd ..
[root@web01 lib]# ls ib*
ib_logfile0
ib_logfile1
[root@web01 lib]# rm -rf ib*
[root@web01 lib]# cd mysql/
[root@web01 mysql]# find . -name
"*.ibd"
./test/mytest.ibd
[root@web01 mysql]# find . -name
"*.ibd" | sed ‘s/^/mv /g‘ | sed ‘s/$/ ..\/mybackup\//g‘
[root@web01 mysql]# vi aa.txt
mv ./test/mytest.ibd ../mybackup/
-----------------------------------------------------
:%s/ibd/frm/g
#替换掉
[root@web01 mysql]# cat aa.txt | bash
[root@web01 mysql]# mysqld_safe
--defaults-file=/var/lib/mysql/my.cnf &
#备份
[root@web01 mysql_backup]# mkdir -p
/mysql_backup/xtrabackup/base
[root@web01 mysql_backup]# mkdir -p
/mysql_backup/xtrabackup/detla
[root@web01 xtrabackup]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword /mysql_backup/xtrabackup/base/ #第一次全量备份
mysql> insert into mytest select * from
mytest; #全备完之后对表插入一些数据
mysql> insert into mytest select * from
mytest;
mysql> insert into mytest select * from
mytest;
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --incremental
--incremental-basedir=/mysql_backup/xtrabackup/base/2016-06-14_11-56-26/ /mysql_backup/xtrabackup/detla/ #增量备份
[root@web01 xtrabackup]# cat
base/2016-06-14_11-56-26/xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 61510896
last_lsn = 61510896
compact = 0
recover_binlog_info = 0
[root@web01 xtrabackup]#
[root@web01 xtrabackup]#
[root@web01 xtrabackup]#
[root@web01 xtrabackup]# cat
detla/2016-06-14_12-50-38/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 61510896
to_lsn = 241364779
last_lsn = 241364779
compact = 0
recover_binlog_info = 0
[root@web01 xtrabackup]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword /mysql_backup/xtrabackup/base/ #第二次全量备份
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --apply-log --redo-only
/mysql_backup/xtrabackup/base/2016-06-14_12-57-21/ #恢复到第二次的全量备份
[root@web01 mysql]# chown -R mysql:mysql
/var/lib/mysql #可以不执行,但是建议执行此命令。
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --apply-log --redo-only
/mysql_backup/xtrabackup/base/2016-06-14_11-56-26/ #先执行恢复第一全量备份的prepare
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --apply-log --redo-only
/mysql_backup/xtrabackup/base/2016-06-14_11-56-26/
--incremental-dir=/mysql_backup/xtrabackup/detla/2016-06-14_12-50-38/ #恢复第一次全量备份+增量备份
#已经恢复完了,现在在插入一些数据。
mysql> insert into mytest select * from
mytest;
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --incremental
--incremental-basedir=/mysql_backup/xtrabackup/base/2016-06-14_12-57-21/
/mysql_backup/xtrabackup/detla/ #基于第二次全量的增量备份
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --apply-log --redo-only
/mysql_backup/xtrabackup/base/2016-06-14_12-57-21/ #先执行恢复第二次全量备份的prepare
[root@web01 mysql]# innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --apply-log --redo-only
/mysql_backup/xtrabackup/base/2016-06-14_12-57-21/
--incremental-dir=/mysql_backup/xtrabackup/detla/2016-06-14_13-15-21/ #恢复第二次全量备份+增量备份
#常用的xtrabackup备份脚本
[root@web01 xtrabackup]# ls
01_full_backup.sh 03_restore_full_backup.sh 05_copy_back.sh detla
02_incremental_backup.sh 04_restore_incremental_backup.sh base
[root@web01 xtrabackup]# cat
01_full_backup.sh
#!/bin/bash
innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword /mysql_backup/xtrabackup/base/
[root@web01 xtrabackup]#
[root@web01 xtrabackup]# cat 02_incremental_backup.sh
#!/bin/bash
filename=$1
if [ -z $filename ];then
exit 0
fi
innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --incremental
--incremental-basedir=/mysql_backup/xtrabackup/base/$filename/
/mysql_backup/xtrabackup/detla/
[root@web01 xtrabackup]#
[root@web01 xtrabackup]# cat 03_restore_full_backup.sh
#!/bin/bash
filename=$1
if [ -z $filename ];then
exit 0
fi
innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --apply-log --redo-only
/mysql_backup/xtrabackup/base/$filename
[root@web01 xtrabackup]#
[root@web01 xtrabackup]#
[root@web01 xtrabackup]# cat
04_restore_incremental_backup.sh
#!/bin/bash
innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --apply-log --redo-only
/mysql_backup/xtrabackup/base/XXXXXXXXXX/
--incremental-dir=/mysql_backup/xtrabackup/detla/XXXXXXXXXXX
[root@web01 xtrabackup]#
[root@web01 xtrabackup]#
[root@web01 xtrabackup]# cat
05_copy_back.sh
#!/bin/bash
innobackupex
--defaults-file=/var/lib/mysql/my.cnf
--user=root --password=testpassword --copy-back
/mysql_backup/xtrabackup/base/XXXXXXXX
[root@web01 xtrabackup]#
[root@web01 xtrabackup]#
#模拟数据库备份恢复
[root@web01 lib]# pwd
/var/lib
[root@web01 lib]# mysqladmin -uroot -ptestpassword
shutdown
[root@web01 lib]# tar zcf
mysql_20160614.tar.gz mysql/
[root@web01 mysql]# mysqld_safe
--defaults-file=/var/lib/mysql/my.cnf &
#模拟在线业务环境
mysql> show global variables like
‘%binlog_format‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set global
binlog_format=statement;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> show global variables like
‘%binlog_format‘;
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)
[root@web01 lib]# vi /var/lib/mysql/my.cnf
#####################
#binlog_format=mixed
binlog_format=statement
[root@web01 lib]# mysqladmin -uroot -ptestpassword
shutdown
[root@web01 mysql]# mysqld_safe
--defaults-file=/var/lib/mysql/my.cnf &
mysql> use test;
mysql> create table t5(id int);
insert into t5 values(111111);
insert into t5 values(222222);
insert into t5 values(333333);
insert into t5 values(444444);
mysql>
create table t6(id int);
insert into t6 values(111111);
insert into t6 values(2222222);
insert into t6 values(333333);
insert into t6 values(444444);
mysql> drop table t5; #突然出现了一张表被删掉的认为误操作
mysql>
insert into t6 values(555555);
insert into t6 values(666666);
insert into t6 values(777777);
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000021 | 126 |
| mysql-bin.000022 | 37754479 |
| mysql-bin.000023 | 1932 |
| mysql-bin.000024 | 298 |
| mysql-bin.000025 | 1779 |
| mysql-bin.000026 |
4778 | #查一下最新的binlog点
+------------------+-----------+
mysql> show global variables like
‘%binlog_format‘;
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)
[root@web01 mysql]# mysqlbinlog
mysql-bin.000027 |less #确定删表操作的大概时间点,进入binlog进行查看。
#搜索DROP 找到那个删除的点
[root@web01 lib]# mysqladmin -uroot
-ptestpassword shutdown
[root@web01 lib]# mv mysql mysql_temp #临时备份一下
[root@web01 lib]# tar zxf
mysql_20160614.tar.gz #恢复之前的备份
[root@web01 lib]# mysqld_safe
--defaults-file=/var/lib/mysql/my.cnf &
[root@web01 mysql_temp]# pwd
/var/lib/mysql_temp
[root@web01 mysql_temp]# mysqlbinlog
--start-position=3005 --stop-position=4647 mysql-bin.000026 >my.sql #3005是全备恢复的点,4647是DROP之的前一个点
[root@web01 mysql_temp]# mysqlbinlog
--start-position=4778 --stop-position=5339 mysql-bin.000026 >my1.sql #4778是DROP之后的一个点,5339是最后的一个点
[root@web01 mysql_temp]# mysql -uroot
-ptestpassword < my.sql
mysql> select * from t5; #验证
+--------+
| id
|
+--------+
| 111111 |
| 222222 |
| 333333 |
| 444444 |
+--------+
4 rows in set (0.00 sec)
mysql> select * from t6;
+---------+
| id
|
+---------+
|
111111 |
| 2222222 |
|
333333 |
+---------+
3 rows in set (0.00 sec)
[root@web01 mysql_temp]# mysql -uroot
-ptestpassword < my1.sql #到此可以对外提供服务了
mysql> select * from t6; #验证
+---------+
| id
|
+---------+
|
111111 |
| 2222222 |
|
333333 |
| 555555 |
| 666666 |
| 777777 |
+---------+
6 rows in set (0.00 sec)
[root@web01 mysql]# mysqlbinlog
--base64-output=‘DECODE-ROWS‘ --verbose mysql-bin.000026
备份恢复步骤:
1.做了一个全量的备份,有一个postion
mysql-bin文件名,大小。
例如:mysql-bin.000042 2068
2.找到drop table t5的语句的前后的postion
drop table t5;前面一个位置:3178
后面开的是位置:3282
3.停止线上服务,用1的备份替代上线的数据。
4.利用mysqlbinlog --start-postion=2068
--stop-postion=3178 导出一个my.sql
5.利用mysqlbinlog --start-postion=3282
导出一个my2.sql文件。
6.把my.sql和my2.sql按次序导入恢复的全量mysql中。
7.对外提供服务。
L008-oldboy-mysql-dba-lesson08
标签:
原文地址:http://www.cnblogs.com/bolinzhihua/p/5650810.html