一、备份说明
https://www.percona.com/downloads/
[root@www ~]#man innobackupex
INNOBACKUPEX(1) Percona XtraBackup INNOBACKUPEX(1)
$ innobackupex--user=DBUSER --password=SECRET /path/to/backup/dir
$ innobackupex--user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/
$ innobackupex--user=LUKE --password=US3TH3F0RC3--stream=tar ./ | bzip2 -
$ xtrabackup--user=DVADER --password=14MY0URF4TH3R --backup --target-dir=/data/bkps/
$ innobackupex--incremental /data/backups --incremental-basedir=BASEDIR
如果只做了一次完全备份,备份的结果中,除了数据还有事务日志,事务日志中,已提交的事务应该同步合并mysql数据中去,未提交的事务都应该回滚,实现数据还原。
如果是完全备份加增量备份,数据还原前应该先把所有已提交事务都合并到数据文件中,但是未提交事务不能回滚,因为接下来还要把增量备份合并到数据文件中,合并过程中可能第一次尚未完成的事务日志已然完成,就能完成对应数据事务文件的提交过程。最后再借助二进制日志文件做时间点(master)还原。
因此 开启二进制日志并且不要把二进制日志文件和数据文件放在同一个磁盘并且还有要有冗余是二进制日志基本要求。
MyISAM不支持热备并且不支持增量
InnoDB支持热备和增量备份
二、备份的实现
1、完全备份
# innobackupex --user=DBUSER --password=SECRET/path/to/backup/dir
Permissions and Privileges Needed 需要赋予相应权限
如果要使用一个最小权限的用户进行备份,则可基于如下命令创建此类用户;
mysql> CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY's3cret';
mysql> REVOKE ALL PRIVILEGES,GRANT OPTION FROM 'bkpuser';
mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON*.* TO 'bkpuser'@'localhost';
mysql> FLUSH PRIVILEGES;
使用innobackupex备份时,其会调用xtrabackup备份所有的innodb表,复制所有关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE/CSV和ARCHIVE表的相关文件,同时还会备份触发器和数据库配置信息相关的文件,这些文件会保存至一个以时间命名的目录中。
在备份的同时,innobackupex还会在备份目录中创建如下文件:
(1)、xtrabackup_checkpoints--备份类型(如完全或增量)、备份状态(如是否已经为prepared)状态和LSN(日志序列号)范围信息:
每个InnoDB页(通常为16k大小)
都会包含一个日志序列号,即LSN。LSN是整个整个数据库系统版本号,每个页面相关的LSN能够表明此页面最近是如何发生改变的。
(2)、xtrabackup_binlog_info--mysql服务器当前正在使用的二进制日志文件至备份这一刻为止二进制日志事件的位置。
(3)、xtrabackup_binlog_pos_innodb--二进制日志文件及用于InnoDB或xtraDB表的二进制日志文件的当前position。
(4)、xtrabackup_binary--备份中用到的xtrabackup的可执行文件;
(5)、backup-my.cnf --备份命令用到的配置选项信息;
2、准备(prepare)一个完全备份
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。
因此,此时数据文件仍处理不一致状态,“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
innobackupex命令的--apply-log选项可用于实现上述功能,如下面的命令:
#innobackupex --apply-log /path/to/BACKUP-DIR/
如果执行正确,器最后输出的几行信息通常如下:
xtrabackup:starting shutdown with innodb_fast_shutdown = 1
171125 0:11:01 InnoDB: Starting shutdown...
171125 0:11:06 InnoDB: Shutdown completed; log sequence number 0 63500
17112500:11:07 innobackupex: completed OK!
在实现准备的过程中,innobackupex通常还可以使用--use-memory选项来指定其可以使用的内存的大小,默认通常为100M。
如果有足够的内存可用,可以多划分一些内存给prepare的过程,以提高其完成速度。
3、从一个完全备份中恢复数据
注意:恢复不用启动mysql
innobackupex命令的 --copy-back选项用于执行恢复操作,其通过复制所有数据相关的文件至mysql服务器DATADIR目录中来执行恢复过程;
innobackupex通过backup-my.cnf来获取datadir目录的相关信息。
#innobackupex --copy-back /path/to/BACKUP-DIR
如果执行正确,其最后输出的几行信息通常如下:
innobackupex:Starting to copy InnoDB log files
innobackupex: in'/backups/2017-11-24_16-27-01'
innobackupex:back to original InnoDB log directory '/var/lib/mysql'
innobackupex:Finished copying back files.
17112500:23:26 innobackupex: completed OK!
必须确保如上信息的最后一行出现"innobackupex: completed OK!"
将数据恢复至datadir目录以后,还需要确保所有数据文件的属主和属组均为正确的用户,如mysql,否则,在启动mysqld之前还需要修改数据文件的属主和属组
[root@test mysql]# chown -R mysql.mysql /var/lib/mysql/*
4、使用innobackupex进行增量备份
每个innodb的页面都会包含一个LSN信息,每当相关的数据发生改变,相关的页面的LSN就会自动增长,这正是InnoDB表可以进行增量备份的基础,即innobackupex通过备份上次完全备份之后发生改变的页面来实现。
5、安装xtrabackup
[root@www ~]# yum installpercona-xtrabackup-2.3.2-1.el6.x86_64.rpm
--> FinishedDependency Resolution
Error: Package: percona-xtrabackup-2.3.2-1.el6.x86_64(/percona-xtrabackup-2.3.2-1.el6.x86_64)
Requires:libev.so.4()(64bit)
报错,提示需要libev.so.4()(64bit)
[root@www ~]# yum installlibev-4.15-1.el6.rf.x86_64.rpm
Installed:
libev.x86_64 0:4.15-1.el6.rf
Complete!
[root@www ~]#yum install percona-xtrabackup-2.3.2-1.el6.x86_64.rpm
再次安装,成功。
[root@www ~]#rpm -ql percona-xtrabackup
/usr/bin/innobackupex
/usr/bin/xbcloud
/usr/bin/xbcloud_osenv
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup
/usr/share/doc/percona-xtrabackup-2.3.2
/usr/share/doc/percona-xtrabackup-2.3.2/COPYING
/usr/share/man/man1/innobackupex.1.gz
/usr/share/man/man1/xbcrypt.1.gz
/usr/share/man/man1/xbstream.1.gz
/usr/share/man/man1/xtrabackup.1.gz
[root@www ~]#man percona-xtrabackup
[root@wwwmysql]# innobackupex --user=root /backups/
171124 16:05:15innobackupex: Starting the backup operation
IMPORTANT:Please check that the backup run completes successfully.
At the end of a successful backuprun innobackupex
prints "completed OK!".
17112416:05:16 version_check Connecting toMySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;mysql_socket=/var/lib/mysql/mysql.sock'as 'root' (using password: NO).
17112416:05:16 version_check Connected toMySQL server
17112416:05:16 version_check Executing aversion check against the server...
17112416:05:16 version_check Done.
171124 16:05:16Connecting to MySQL server host: localhost, user: root, password: not set,port: 0, socket: /var/lib/mysql/mysql.sock
Error: Built-inInnoDB in MySQL 5.1 is not supported in this release. You can either usePercona XtraBackup 2.0, or upgrade to InnoDB plugin.
笔者安装centos6自带的MySQL 5.1版本太低,提示XtraBackup 2.0, or upgrade to InnoDB plugin.
卸载percona-xtrabackup-2.3.2,安装percona-xtrabackup-2.0.0-
[root@www~]# rpm -qa | grep percona-xtrabackup
percona-xtrabackup-2.3.2-1.el6.x86_64
[root@www~]# rpm -e percona-xtrabackup
[root@www~]# rpm -qa | grep percona-xtrabackup
[root@www~]# rpm -qa | grep percona-xtrabackup
[root@www ~]# yum installpercona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm
[root@www ~]#rpm -ql percona-xtrabackup
三、案例演示
案例1、完全备份示例(远程备份到备用机)192.168.88.131上备份:
[root@wwwmysql]# mkdir /backups
[root@wwwmysql]# cd
[root@www /]# cd-
/var/lib/mysql
[root@www ~]#mysql
mysql> showbinary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
|mysql-bin.000001 | 601 |
|mysql-bin.000002 | 125 |
|mysql-bin.000003 | 106 |
+------------------+-----------+
3 rows in set(0.03 sec)
mysql> exit
Bye
对数据做完全备份,将数据备份到/backus/目录下
[root@www ~]#innobackupex --user=root /backups/
InnoDB BackupUtility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona Inc2009-2012. All Rights Reserved.
……
innobackupex:Backup created in directory '/backups/2017-11-24_16-27-01'
innobackupex:MySQL binlog position: filename 'mysql-bin.000003', position 106
17112416:27:21 innobackupex: completed OK!
[root@www ~]# cd/backups/2017-11-24_16-27-01
查看是否备份完成:
[root@www2017-11-24_16-27-01]# ls
backup-my.cnf ibdata1 test xtrabackup_binary xtrabackup_checkpoints
hellodb mysql testdb xtrabackup_binlog_info xtrabackup_logfile
[root@www2017-11-24_16-27-01]# cd ..
[root@wwwbackups]# cd
将备份的数据scp到备用机上
[root@www ~]#scp -r /backups/2017-11-24_16-27-01/ 192.168.88.130:/root/
备用机上还原数据:(192.168.88.130)
注意,备用机上也要安装percona-xtrabackup
[root@test ~]#ls
all.sql install.log.syslog
apache-tomcat-8.0.47.tar.gz percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm
备用机安装percona-xtrabackup
[root@test ~]#yum install percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm
[root@test ~]#vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disablingsymbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#必须启动下面的项
innodb_file_per_table=ON
skip-name-resolve
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@test ~]#service mysqld start
正在启动 mysqld: [确定]
[root@test ~]#cd /var/lib/mysql
[root@testmysql]# ls
ibdata1 ib_logfile0 ib_logfile1 mysql mysql.sock test
[root@testmysql]# ss -tnl
Recv-QSend-Q Local Address:Port Peer Address:Port
0 50 *:3306 *:*
0 128 *:33549 *:*
0 128 :::36462 :::*
0 128 :::111 :::*
0 128 *:111 *:*
0 128 :::22 :::*
0 128 *:22 *:*
0 128 127.0.0.1:631 *:*
0 128 ::1:631 :::*
0 100 ::1:25 :::*
0 100 127.0.0.1:25 *:*
[root@testmysql]# mysql
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
| test |
+--------------------+
3 rows in set(0.09 sec)
mysql> exit
Bye
[root@testmysql]# cd
[root@test ~]#mkdir /backups
[root@test ~]#mv 2017-11-24_16-27-01/ /backups/
准备(prepare)一个完全备份
“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
[root@test ~]#innobackupex --apply-log /backups/2017-11-24_16-27-01/
…….
17112500:11:07 innobackupex: completed OK!
[root@test ~]#cd /backups
[root@testbackups]# ls
2017-11-24_16-27-01
注意:恢复不用启动mysql
[root@testbackups]# service mysqld stop
停止 mysqld: [确定]
[root@test backups]#cd /var/lib/mysql
[root@testmysql]# ls
ibdata1 ib_logfile0 ib_logfile1 mysql test
作误操作,删除mysql数据目录下的文件,尝试还原数据
[root@testmysql]# rm -rf *
[root@testmysql]# ls
从一个完全备份中恢复数据
innobackupex命令的 --copy-back选项用于执行恢复操作,其通过复制所有数据相关的文件至mysql服务器DATADIR目录中来执行恢复过程;
innobackupex通过backup-my.cnf来获取datadir目录的相关信息。
[root@testmysql]# innobackupex --copy-back /backups/2017-11-24_16-27-01/
….
17112500:23:26 innobackupex: completed OK!
[root@testmysql]# ls
hellodb ib_logfile1 testdb
ibdata1 mysql xtrabackup_binlog_pos_innodb
ib_logfile0 test xtrabackup_checkpoints
[root@testmysql]# ll
总用量 20544
drwxr-xr-x. 2root root 4096 11月 25 00:23 hellodb
-rw-r-----. 1root root 10485760 11月 25 00:11 ibdata1
-rw-r--r--. 1root root 5242880 11月 25 00:23 ib_logfile0
-rw-r--r--. 1root root 5242880 11月 25 00:23 ib_logfile1
drwxr-xr-x. 2root root 4096 11月 25 00:23 mysql
drwxr-xr-x. 2root root 4096 11月 25 00:23 test
drwxr-xr-x. 2root root 4096 11月 25 00:23 testdb
-rw-r--r--. 1 rootroot 23 11月 25 00:23 xtrabackup_binlog_pos_innodb
-rw-r--r--. 1root root 79 11月 25 00:23 xtrabackup_checkpoints
将数据恢复至datadir目录以后,还需要确保所有数据文件的属主和属组均为正确的用户,如mysql,否则,在启动mysqld之前还需要修改数据文件的属主和属组
[root@testmysql]# chown -R mysql.mysql ./*
[root@testmysql]# ll
总用量 20544
drwxr-xr-x. 2mysql mysql 4096 11月 25 00:23 hellodb
-rw-r-----. 1mysql mysql 10485760 11月 25 00:11 ibdata1
-rw-r--r--. 1mysql mysql 5242880 11月 25 00:23 ib_logfile0
-rw-r--r--. 1mysql mysql 5242880 11月 25 00:23 ib_logfile1
drwxr-xr-x. 2mysql mysql 4096 11月 25 00:23 mysql
drwxr-xr-x. 2mysql mysql 4096 11月 25 00:23 test
drwxr-xr-x. 2mysql mysql 4096 11月 25 00:23 testdb
-rw-r--r--. 1mysql mysql 23 11月 25 00:23 xtrabackup_binlog_pos_innodb
-rw-r--r--. 1mysql mysql 79 11月 25 00:23 xtrabackup_checkpoints
[root@testmysql]# service mysqld start
正在启动 mysqld: [确定]
[root@testmysql]# less /var/log/mysqld.log
171121 17:59:29mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@testmysql]# mysql
mysql> usehellodb
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
mysql> showtables;
+-------------------+
| Tables_in_hellodb|
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set(0.00 sec)
mysql> select* from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID |Name | Age | Gender | ClassID |TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22| M | 2 | 3 |
| 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 | 17| 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 |
| 26 | Cao Cao | 15 | M | 3 | 7 |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set(0.00 sec)
mysql> quit
Bye
[root@testmysql]#
案例2、完全备份+增量备份
注意:MyISAM不支持热备和增量备份
[root@www ~]#mysql
mysql> usehellodb
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
mysql> CREATEtable testdb (id int);
Query OK, 0 rowsaffected (0.21 sec)
mysql> showtables;
+-------------------+
|Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| testdb |
| toc |
+-------------------+
8 rows in set(0.00 sec)
mysql> insertinto testdb values(1),(2);
Query OK, 2 rowsaffected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insertinto testdb values(88),(9920),(78);
Query OK, 3 rowsaffected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select* from testdb;
+------+
| id |
+------+
| 1 |
| 2 |
| 88 |
| 9920 |
| 78 |
+------+
5 rows in set(0.00 sec)
mysql> quit
Bye
[root@www ~]# rm-rf /backups/*
[root@www ~]#innobackupex /backups/
......
innobackupex:Backup created in directory '/backups/2017-11-25_01-09-29'
innobackupex:MySQL binlog position: filename 'mysql-bin.000003', position 405
17112501:09:50 innobackupex: completed OK!
[root@www ~]# ls/backups/
2017-11-25_01-09-29
[root@www ~]#less /backups/2017-11-25_01-09-29
总用量 10300
drwxr-xr-x. 6root root 4096 11月 25 01:09 ./
drwxr-xr-x. 3root root 4096 11月 25 01:09 ../
-rw-r--r--. 1root root 230 11月 25 01:09 backup-my.cnf
drwxr-xr-x. 2root root 4096 11月 25 01:09 hellodb/
-rw-r-----. 1root root 10485760 11月 25 01:09 ibdata1
drwxr-xr-x. 2root root 4096 11月 25 01:09 mysql/
drwxr-xr-x. 2root root 4096 11月 25 01:09 test/
drwxr-xr-x. 2root root 4096 11月 25 01:09 testdb/
-rw-r--r--. 1root root 13 11月 25 01:09 xtrabackup_binary
-rw-r--r--. 1root root 23 11月 25 01:09 xtrabackup_binlog_info
-rw-r-----. 1root root 79 11月 25 01:09 xtrabackup_checkpoints
-rw-r-----. 1root root 2560 11月 25 01:09 xtrabackup_logfile
[root@www ~]#less /backups/2017-11-25_01-09-29/xtrabackup_checkpoints
backup_type = full-backuped
#备份类型为完全备份
from_lsn = 0:0
to_lsn = 0:63322
last_lsn =0:63322
~
[root@www ~]#mysql
mysql> usehellodb
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
mysql> showtables
-> ;
+-------------------+
|Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| testdb |
| toc |
+-------------------+
8 rows in set(0.00 sec)
mysql> DROPTABLE coc;
Query OK, 0 rowsaffected (0.19 sec)
mysql> showtables;
+-------------------+
|Tables_in_hellodb |
+-------------------+
| classes |
| courses |
| scores |
| students |
| teachers |
| testdb |
| toc |
+-------------------+
7 rows in set(0.00 sec)
mysql> insertinto testdb values (45),(88);
Query OK, 2 rowsaffected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> exit
Bye
使用innobackupex基于完全备份再做增量备份
[root@wwwbackups]# innobackupex --incremental /backups/ --incremental-basedir=/backups/2017-11-25_01-09-29
[root@www backups]# ls
2017-11-25_01-09-29 2017-11-25_01-23-17
[root@wwwbackups]# less 2017-11-25_01-23-17
总用量 420
drwxr-xr-x. 6root root 4096 11月 25 01:23 ./
drwxr-xr-x. 4root root 4096 11月 25 01:23 ../
-rw-r--r--. 1root root 230 11月 25 01:23 backup-my.cnf
drwxr-xr-x. 2root root 4096 11月 25 01:23 hellodb/
-rw-r-----. 1root root 376832 11月 25 01:23 ibdata1.delta
-rw-r-----. 1root root 18 11月 25 01:23 ibdata1.meta
drwxr-xr-x. 2root root 4096 11月 25 01:23 mysql/
drwxr-xr-x. 2root root 4096 11月 25 01:23 test/
drwxr-xr-x. 2root root 4096 11月 25 01:23 testdb/
-rw-r--r--. 1root root 13 11月 25 01:23 xtrabackup_binary
-rw-r--r--. 1root root 23 11月 25 01:23 xtrabackup_binlog_info
-rw-r-----. 1root root 81 11月 25 01:23 xtrabackup_checkpoints
-rw-r-----. 1root root 2560 11月 25 01:23 xtrabackup_logfile
[root@www backups]# less2017-11-25_01-23-17/xtrabackup_checkpoints
backup_type = incremental
#备份类型为增量备份
from_lsn =0:63322
to_lsn = 0:64059
last_lsn =0:64059
[root@wwwbackups]# service mysqld stop
停止 mysqld: [确定]
[root@wwwbackups]# ls
2017-11-25_01-09-29 2017-11-25_01-23-17
整理完全备份
[root@wwwbackups]# innobackupex --apply-log --redo-only /backups/2017-11-25_01-09-29/
将第一个增量备份合并到完全备份,如果有第二个增量备份也要合并到完全备份上;以此类推到最后一个
[root@wwwbackups]# innobackupex --apply-log --redo-only /backups/2017-11-25_01-09-29/--incremental-dir=/backups/2017-11-25_01-23-17/
[root@www backups]# ls
2017-11-25_01-09-29 2017-11-25_01-09-29test 2017-11-25_01-23-17
2017-11-25_01-09-29hellodb 2017-11-25_01-09-29testdb
2017-11-25_01-09-29mysql 2017-11-25_01-09-29xtrabackup_binlog_info
[root@wwwbackups]# ls 2017-11-25_01-09-29
backup-my.cnf mysql xtrabackup_binary xtrabackup_checkpoints
hellodb test xtrabackup_binlog_info xtrabackup_logfile
ibdata1 testdb xtrabackup_binlog_pos_innodb
[root@wwwbackups]# less 2017-11-25_01-09-29/xtrabackup_checkpoints
backup_type = full-prepared
#备份类型为整理好的完全备份
from_lsn = 0:0
to_lsn = 0:64059
last_lsn =0:64059
[root@wwwbackups]# cd
删除mysql数据目录下的文件,尝试还原数据
[root@www ~]# ls/var/lib/mysql/*
[root@www ~]# rm-rf /var/lib/mysql/*
[root@www ~]#innobackupex --copy-back /backups/2017-11-25_01-09-29/
[root@www ~]# cd /var/lib/mysql
[root@wwwmysql]# ll -lh
总用量 11M
drwxr-xr-x. 2root root 4.0K 11月 25 01:36 hellodb
-rw-r-----. 1 rootroot 10M 11月 25 01:31 ibdata1
drwxr-xr-x. 2root root 4.0K 11月 25 01:36 mysql
drwxr-xr-x. 2root root 4.0K 11月 25 01:36 test
drwxr-xr-x. 2root root 4.0K 11月 25 01:36 testdb
-rw-r--r--. 1root root 23 11月 25 01:36 xtrabackup_binlog_pos_innodb
-rw-r--r--. 1root root 79 11月 25 01:36 xtrabackup_checkpoints
将数据恢复至datadir目录以后,还需要确保所有数据文件的属主和属组均为正确的用户,如mysql,否则,在启动mysqld之前还需要修改数据文件的属主和属组
[root@wwwmysql]# chown -R mysql.mysql ./*
[root@wwwmysql]# ll -lh
总用量 11M
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 01:36 hellodb
-rw-r-----. 1mysql mysql 10M 11月 25 01:31 ibdata1
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 01:36 mysql
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 01:36 test
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 01:36 testdb
-rw-r--r--. 1mysql mysql 23 11月 25 01:36 xtrabackup_binlog_pos_innodb
-rw-r--r--. 1mysql mysql 79 11月 25 01:36 xtrabackup_checkpoints
[root@wwwmysql]# service mysqld start
正在启动 mysqld: [确定]
[root@wwwmysql]# mysql
mysql> usehellodb;
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
mysql> select* from testdb;
+------+
| id |
+------+
| 1 |
| 2 |
| 88 |
| 9920 |
| 78 |
+------+
5 rows in set(0.02 sec)
我们可以发现,此处只还原了完全备份,增量备份所做备份并未还原回去;
究其原因,笔者安装的mysql默认引擎为MyISAM,建testdb表时为指明engine=innodb,无法做增量备份;
mysql> showengines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints|
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM |YES | Collection of identical MyISAMtables | NO | NO | NO |
| CSV | YES | CSV storage engine |NO | NO | NO |
| MyISAM | DEFAULT | Default engine as of MySQL3.23 with great performance | NO | NO | NO |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set(0.00 sec)
mysql> showcreate table testdb;
+--------+------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------------------------------------------+
| testdb |CREATE TABLE `testdb` (
`id` int(11) DEFAULT NULL
) ENGINE=MyISAMDEFAULT CHARSET=utf8 |
+--------+------------------------------------------------------------------------------------------+
1 row in set(0.00 sec)
mysql> quit
Bye
[root@wwwmysql]#
编辑mysql配置文件/etc/my.cnf修改默认engine为innodb
default-storage-engine=InnoDB
[root@www ~]#vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disablingsymbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log_bin=mysql-bin
innodb_file_per_table=ON
default-storage-engine=InnoDB
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@www ~]#service mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
[root@www ~]#mysql
mysql> usehellodb;
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
mysql> showtables;
+-------------------+
|Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| testdb |
| toc |
+-------------------+
8 rows in set(0.00 sec)
可以看到,重启后DEFAULT engine=InnoDB ;
mysql> showengines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints|
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM |YES | Collection of identical MyISAMtables | NO | NO | NO |
| CSV | YES | CSV storage engine |NO | NO | NO |
| MyISAM | YES | Default engine as of MySQL 3.23 with great performance | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions,row-level locking, and foreign keys | YES | YES | YES |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set(0.00 sec)
mysql> select* from testdb;
+------+
| id |
+------+
| 1 |
| 2 |
| 88 |
| 9920 |
| 78 |
| 45 |
| 88 |
+------+
7 rows in set(0.00 sec)
便于演示,新建id表重新做增量备份
mysql> CREATETABLE id(id int);
Query OK, 0 rowsaffected (0.00 sec)
mysql> insertinto id values (666),(1321);
Query OK, 2 rowsaffected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select* from id;
+------+
| id |
+------+
| 666 |
| 1321 |
+------+
2 rows in set(0.00 sec)
mysql> showtables;
+-------------------+
| Tables_in_hellodb|
+-------------------+
| classes |
| coc |
| courses |
| id |
| scores |
| students |
| teachers |
| testdb |
| toc |
+-------------------+
9 rows in set(0.00 sec)
确认id表引擎是否为InnoDB
mysql> showcreate table id;
+-------+--------------------------------------------------------------------------------------+
| Table | CreateTable |
+-------+--------------------------------------------------------------------------------------+
| id | CREATE TABLE `id` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDBDEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------+
1 row in set(0.02 sec)
mysql> quit;
Bye
案例3、本机上做完全备份+增量备份
删除备份目录下的文件
[root@www ~]# rm-rf /backups/*
将数据备份到/backus/目录下,注意--user=root可以不指定
[root@www ~]# innobackupex /backups/
[root@www ~]# cd /backups/
[root@wwwbackups]# ls
2017-11-25_16-55-09
[root@wwwbackups]# less 2017-11-25_16-55-09
总用量 10300
drwxr-xr-x. 6root root 4096 11月 25 16:55 ./
drwxr-xr-x. 3root root 4096 11月 25 16:55 ../
-rw-r--r--. 1root root 230 11月 25 16:55 backup-my.cnf
drwxr-xr-x. 2root root 4096 11月 25 16:55 hellodb/
-rw-r-----. 1root root 10485760 11月 25 16:55 ibdata1
drwxr-xr-x. 2root root 4096 11月 25 16:55 mysql/
drwxr-xr-x. 2root root 4096 11月 25 16:55 test/
drwxr-xr-x. 2root root 4096 11月 25 16:55 testdb/
-rw-r--r--. 1root root 13 11月 25 16:55 xtrabackup_binary
-rw-r--r--. 1root root 23 11月 25 16:55 xtrabackup_binlog_info
-rw-r-----. 1root root 79 11月 25 16:55 xtrabackup_checkpoints
-rw-r-----. 1root root 2560 11月 25 16:55 xtrabackup_logfile
[root@wwwbackups]# less 2017-11-25_16-55-09/xtrabackup_checkpoints
backup_type = full-backuped
#备份类型为完全备份
from_lsn = 0:0
to_lsn = 0:71490
last_lsn =0:71490
[root@wwwbackups]# mysql
mysql> usehellodb
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
有意对数据进行修改,以便进行增量备份
mysql> insertinto testdb VALUES (4454),(1111);
Query OK, 2 rowsaffected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select* from testdb;
+------+
| id |
+------+
| 1 |
| 2 |
| 88 |
| 9920 |
| 78 |
| 45 |
| 88 |
| 4454 |
| 1111 |
+------+
9 rows in set(0.00 sec)
mysql> insertinto id values (555),(888);
Query OK, 2 rowsaffected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select* from id;
+------+
| id |
+------+
| 666 |
| 1321 |
| 555 |
| 888 |
+------+
4 rows in set(0.00 sec)
mysql> quit
Bye
[root@wwwbackups]# ls
2017-11-25_16-55-09
使用innobackupex基于完全备份再做增量备份
[root@www backups]# innobackupex --incremental /backups/ --incremental-basedir=/backups/2017-11-25_16-55-09
[root@www backups]# ls
2017-11-25_16-55-09 2017-11-25_16-59-02
[root@wwwbackups]# less 2017-11-25_16-59-02/xtrabackup_checkpoints
backup_type = incremental
#备份类型为增量备份
from_lsn =0:71490
to_lsn = 0:72131
last_lsn =0:72131
[root@wwwbackups]# service mysqld stop
停止 mysqld: [确定]
做完全备份准备
[root@www backups]# innobackupex--apply-log --redo-only /backups/2017-11-25_16-55-09/
将第一个增量备份合并到完全备份,如果有第二个增量备份也要合并到完全备份上;以此类推到最后一个
[root@www backups]# innobackupex--apply-log --redo-only /backups/2017-11-25_16-55-09/--incremental-dir=/backups/2017-11-25_16-59-02/
[root@www backups]# ls
2017-11-25_16-55-09 2017-11-25_16-55-09test 2017-11-25_16-59-02
2017-11-25_16-55-09hellodb 2017-11-25_16-55-09testdb
2017-11-25_16-55-09mysql 2017-11-25_16-55-09xtrabackup_binlog_info
[root@wwwbackups]# less 2017-11-25_16-55-09/xtrabackup_checkpoints
backup_type = full-prepared
#备份类型为整理好的完全备份
from_lsn = 0:0
to_lsn = 0:72131
last_lsn =0:72131
[root@wwwbackups]# ls /var/lib/mysql/
hellodb ib_logfile1 mysql-bin.index xtrabackup_binlog_pos_innodb
ibdata1 mysql test xtrabackup_checkpoints
ib_logfile0 mysql-bin.000001 testdb
删除mysql数据目录下的文件,尝试还原数据
[root@wwwbackups]# rm -rf /var/lib/mysql/*
[root@www backups]# innobackupex --copy-back /backups/2017-11-25_16-55-09/
[root@www backups]# cd /var/lib/mysql
[root@wwwmysql]# ll -lh
总用量 11M
drwxr-xr-x. 2root root 4.0K 11月 25 17:03 hellodb
-rw-r-----. 1root root 10M 11月 25 17:01 ibdata1
drwxr-xr-x. 2root root 4.0K 11月 25 17:03 mysql
drwxr-xr-x. 2root root 4.0K 11月 25 17:03 test
drwxr-xr-x. 2root root 4.0K 11月 25 17:03 testdb
-rw-r--r--. 1root root 23 11月 25 17:03 xtrabackup_binlog_pos_innodb
-rw-r--r--. 1root root 79 11月 25 17:03 xtrabackup_checkpoints
将数据恢复至datadir目录以后,还需要确保所有数据文件的属主和属组均为正确的用户,如mysql,否则,在启动mysqld之前还需要修改数据文件的属主和属组
[root@www mysql]# chown -Rmysql.mysql ./*
[root@wwwmysql]# ll -lh
总用量 11M
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 17:03 hellodb
-rw-r-----. 1mysql mysql 10M 11月 25 17:01 ibdata1
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 17:03 mysql
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 17:03 test
drwxr-xr-x. 2mysql mysql 4.0K 11月 25 17:03 testdb
-rw-r--r--. 1mysql mysql 23 11月 25 17:03 xtrabackup_binlog_pos_innodb
-rw-r--r--. 1mysql mysql 79 11月 25 17:03 xtrabackup_checkpoints
[root@wwwmysql]# service mysqld start
正在启动 mysqld: [确定]
[root@wwwmysql]# mysql
mysql> use hellodb;
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
mysql> showtables;
+-------------------+
|Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| id |
| scores |
| students |
| teachers |
| testdb |
| toc |
+-------------------+
9 rows in set(0.00 sec)
可以看到,修改engine=innodb后增量备份还原成功
mysql> select* from id;
+------+
| id |
+------+
| 666 |
| 1321 |
| 555 |
| 888 |
+------+
4 rows in set(0.00 sec)
mysql> select* from testdb;
+------+
| id |
+------+
| 1 |
| 2 |
| 88 |
| 9920 |
| 78 |
| 45 |
| 88 |
| 4454 |
| 1111 |
+------+
9 rows in set(0.00 sec)
mysql>