逻辑备份工具
逻辑备份三种工具:
MySQLdump
mydumper
PHPmyadmin
schema和数据存储在一起、巨大的SQL语句、单个巨大的备份文件;
MySQLdump:客户端命令,通过MySQL协议连接至mysqld服务器;
mysqldump [options] [db_name [tbl_name ...]]
备份的数据集:
-A,--all-databases
-B db_name,....
--databases db_name,...
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
备份单个数据库
[root@node7 /data/mysql]#mysqldump -uroot -p123456 --databases hellodb > hellodb.sql
备份两个数据库:
[root@node7 /data/mysql]#mysqldump -uroot -p123456 --databases hellodb mysql > /root/hellodb_mysql.sql
MyISAM:支持锁定备份库,而后启动备份操作:
锁定方法:
--lock-all-tables:锁定所有库的所有表;
--lock-tables:对于每个单独的数据库,在启动备份之前锁定其所有表;
对innodb表一样生效,实现温备;
innodb;支持热备;
--single-transaction
其它选项:
-E:--events:备份指定数据库相关的所有event scheduler
-R:--routines;备份指定数据库相关的所有存储过程和存储函数;
--triggers:备份表相关的触发器;
--master-data[=#]
1、记录为CHANGE MASTER TO语句,此语句不被注释;
2、记录为注释的CHANGE MASTER TO语句;
--flush-logs;
锁定表完成后,执行flush logs命令做日志滚动;
注意:二进制日志文件不应该与数据文件放在同一磁盘里:
备份还原示例
模拟场景,在周日刚做完完全备份之后,周一早上数据库服务器崩掉了
1、正常用MySQLdump做完全备份
[root@node7 ~]# mysqldump -uroot -p123456 --all-databases --lock-all-tables --master-data=2 > /root/all.sql
2、正常使用SQL语句插入数据
MariaDB [hellodb]> insert into students (name,age,gender,classid,teacherid)values('li',43,'M',3,7);
MariaDB [hellodb]> delete from students where stuid=3;
3、模拟数据库服务器down机,这里就把数据库服务给停掉就行
[root@node7 ~]#systemctl stop mysqld
4、找台已经装好MySQL数据库备用服务器,然后把之前备份的数据库拷贝到这台备用机上做还原,建议把二进制日志临时关闭,这里修改的是会话级别的,关闭之后不要退出当前会话,导入完之后把二进制开启(注意;数据库的版本最好要一致,否则导入数据时会报各种错误)
MariaDB [hellodb]> set sql_log_bin=off;
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> source /root/all.sql;
MariaDB [hellodb]> set sql_log_bin=on;
Query OK, 0 rows affected (0.00 sec)
5、登录数据库查看数据库是否完整,这里看到的数据还是周日做的完全备份之前的数据,之后新添加的数据没有记录,这时候就需要去之前那台服务器通过二进制日志文件把后续新加的给导出来
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 21 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
6、把后续新添加的数据从二进制日志文件里面导出来然后再导入到新服务器上去,这时候就需要查看之前做完全备份时里面的内容,从CHANGE MASTER TO MASTER_LOG_FILE=看出最后更新到这个点上后续我们只需要从这个时间点往后的所有内容导出即可,再传给新服务器做导入。
[root@node6 ~]#less all.sql
-- MySQL dump 10.16 Distrib 10.2.15-MariaDB, for Linux (x86_64)
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 10.2.15-MariaDB-log
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=604;
[root@node7 ~]#mysqlbinlog --start-position=604 mysql-bin.000002 > /root/incre.sql
[root@node6 ~]#scp incre.sql [email protected]:/root/
7、再次把后续新增加的内容导入到数据库中,查看数据是否完整
[root@node6 ~]#mysql -uroot -p123456 < incre.sql
[root@node6 ~]#mysql -uroot -p123456
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 21 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 28 | li | 43 | M | 3 | 7 |
+-------+---------------+-----+--------+---------+-----------+
基于lvm2的备份
1、请求锁定所有表;
mysql> flush tables with read lock;
2、记录二进制日志文件及事件位置;
mysql> flush logs;
mysql> show master status;
# mysql -e 'show master status' > /root/log.txt
3、创建快照:
lvcreate -L # -s -p r -n NAME /DEV/VG_NAME/LV_NAME
4、释放锁
mysql> unlock tables;
5、挂载快照卷,执行数据备份;
6、备份完成后,删除快照卷;
7、制定好策略,通过原卷备份二进制日志,最好滚动二进制日志
Xtrabackup备份和恢复方式
xtrabackup
percona公司研发
官网:
www.percona.com
percona-server
innodb --> xtradb
xtrabackup
备份出来的数据文件,是不能直接拿来去还原的,要还原之前需要把事务日志应用到数据文件上,提交的事务给它提交,未提交的事务给它做回滚,这数据文件才算完整。接着这个数据文件才能拿来做还原
如果说只做一次完全备份,这个备份的结果中除了数据之外还有事务日志,事务日志中以提交事务应该都给它同步合并到数据文件中去在还原之前,而未提交事务都应该回滚
xtrabackup完全备份示例:
1、创建备份目录
[root@node7 ~]#mkdir /backups
[root@node7 ~]#innobackupex --user=root --password=123456 /backups/
2、备份生成的文件:
[root@node7 ~]#ls /backups/2018-10-15_19-54-48/
backup-my.cnf ibdata1 test xtrabackup_info
hellodb mysql xtrabackup_binlog_info xtrabackup_logfile
ib_buffer_pool performance_schema xtrabackup_checkpoints
在另外一台机器上实现还原,要还原的这台机器事先不要启动
3、还原之前先做整理
[root@node6 ~]#innobackupex --apply-log /backups/2018-10-15_19-54-48/
4、把MySQL服务停掉
[root@node6 ~]#service mysqld stop
Stopping mysqld (via systemctl): [ OK ]
5、还原数据库
[root@node6 ~]#rm -rf /data/mysql/*
[root@node6 ~]#innobackupex --copy-back /backups/2018-10-15_19-54-48
6、最后修改权限
[root@node6 /data/mysql]#chown -R mysql.mysql /data/mysql/
增量备份示例:
1、增量备份前先做完全备份
[root@node6 ~]#innobackupex --user=root --password=123456 /backups/
2、做完完全备份之后连接到数据库里面新增几条新数据
MariaDB [hellodb]> insert into teachers values (6,'b',32,'F'),(7,'c',40,'M');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
3、做增量备份
[root@node6 ~]#innobackupex --user=root --password=123456 --incremental /backups/ --incremtal-basedir=/backups/2018-10-15_20-34-39/
4、查看增量备份目录下的文件
[root@node6 /backups/2018-10-15_20-41-47]#cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 33003889
to_lsn = 33006670
last_lsn = 33006679
compact = 0
recover_binlog_info = 0
5、开始还原,先整理完全备份
[root@node6 ~]#innobackupex --apply-log --redo-only /backups/2018-10-15_20-34-39/
6、接着整理增量备份
[root@node6 ~]#innobackupex --apply-log --redo-only /backups/2018-10-15_20-34-39/ --incrementadir=/backups/2018-10-15_20-41-47/
7、查看完全备份里面的信息
[root@node6 ~]#less /backups/2018-10-15_20-34-39/xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 33006670
last_lsn = 33006679
compact = 0
recover_binlog_info = 0
8、模拟数据库服务故障,先停止服务、删除数据文件
[root@node6 ~]#service mysqld stop
[root@node6 ~]#rm -rf /data/mysql/*
9、开始还原
[root@node6 /data/mysql]#innobackupex --user=root --password=123456 --copy-back /backups/28-10-15_20-34-39/
10、查看数据库的数据目录
[root@node6 /data/mysql]#ll -h
total 77M
drwxr-x--- 2 root root 272 Oct 15 20:53 hellodb
-rw-r----- 1 root root 4.3K Oct 15 20:53 ib_buffer_pool
-rw-r----- 1 root root 76M Oct 15 20:53 ibdata1
drwxr-x--- 2 root root 4.0K Oct 15 20:53 mysql
drwxr-x--- 2 root root 20 Oct 15 20:53 performance_schema
drwxr-x--- 2 root root 102 Oct 15 20:53 test
-rw-r----- 1 root root 36 Oct 15 20:53 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root 584 Oct 15 20:53 xtrabackup_info
-rw-r----- 1 root root 1 Oct 15 20:53 xtrabackup_master_key_id
11、最后修改数据目录的所属主和所属组
[root@node6 /data/mysql]#chown -R mysql.mysql ./*
12、启动数据库
[root@node6 /data/mysql]#service mysqld start
Starting mysqld (via systemctl): [ OK ]
13、连接服务器查看数据是否完整,之前添加的两条数据都在
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | a | 34 | M |
| 6 | b | 32 | F |
| 7 | c | 40 | M |
+-----+---------------+-----+--------+
新版xtrabackup用法,旧用法也支持
完全备份、还原示例:
新版的话需要手动创建备份目录不会像旧版一样自动生成带时间目录,
[root@node6 ~]# mkdir -p /backups/{base,incre}
[root@node6 ~]# xtrabackup --user=root --password=123456 --backup --target-dir=/backups/base
模拟数据库故障,停止数据库,及删除数据文件
[root@node6 ~]#service mysqld stop
[root@node6 ~]#rm -rf /data/mysql/*
恢复数据
1、先做整理,确保数据完整
[root@node6 ~]# xtrabackup --prepare --target-dir=/backups/base
2、开始还原数据
[root@node6 ~]# xtrabackup --copy-back --target-dir=/backups/base
3、修改数据目录的所属组
[root@node6 ~]# chown -R mysql.mysql /data/mysql
4、启动数据库
[root@node6 ~]# service mysqld start
完全+增量备份、还原示例:
1、做完全备份
[root@node6 ~]# xtrabackup --user=root --password=123456 --backup --target-dir=/backups/base
2、往数据库里面插入新数据记录
MariaDB [(none)]> use hellodb
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> insert into teachers values (5,'cobbler',28,'M');
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | cobbler | 28 | M |
+-----+---------------+-----+--------+
5 rows in set (0.00 sec)
3、第一次增量备份
xtrabackup --user=root --password=123456 --backup --target-dir=/backups/incre1 --incremental-basedir=/backups/base
4、第二次往数据库里面新增加数据记录
MariaDB [(none)]> use hellodb
Database changed
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | cobbler | 28 | M |
+-----+---------------+-----+--------+
5 rows in set (0.00 sec)
MariaDB [hellodb]> insert into teachers values (6,'wang',23,'M'),(7,'mage',32,'M');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | cobbler | 28 | M |
| 6 | wang | 23 | M |
| 7 | mage | 32 | M |
+-----+---------------+-----+--------+
7 rows in set (0.00 sec)
5、第二次增量备份
xtrabackup --user=root --password=123456 --backup --target-dir=/backups/incre1 --incremental-basedir=/backups/incre2
6、模拟数据库崩溃,还是先停止数据库,后删除/data/mysql下所有文件
[root@node6 ~]# service mysqld stop
[root@node6 ~]# rm -rf /data/mysql/*
7、整理备份文件,先从完全备份开始
[root@node6 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backups/base
8、整理第一次增量备份文件
[root@node6 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backups/base --incremental-dir=/backups/incre1
9、整理第二次增量备份文件,也是最后一次,这次不需要加--apply-log-only
[root@node6 ~]# xtrabackup --prepare --target-dir=/backups/base --incremental-dir=/backups/incre2
10、做完所有整理之后开始还原
[root@node6 ~]# xtrabackup --copy-back --target-dir=/backups/base
11、修改数据存放目录的属性
[root@node6 ~]# chown -R mysql.mysql /data/mysql
12、启动数据库,并进入数据库查看数据是否完整
[root@node6 ~]# service mysqld start
[root@node6 ~]# mysql -uroot -p123456
MariaDB [(none)]> use hellodb
Database changed
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | cobbler | 28 | M |
| 6 | wang | 23 | M |
| 7 | mage | 32 | M |
+-----+---------------+-----+--------+
7 rows in set (0.00 sec)