MySQL数据库备份之xtrabackup工具使用
一、Xtrabackup备份介绍及原理
二、Xtrabackup的安装
1、在centos7上基于yum源安装percona-xtrabackup软件
[root@node7 ~]# yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
注意:centos5不支持从远程位置直接安装软件包,必须先通过wget下载下来,然后在通过rpm安装,操作如下
$ wget http://www.percona.com/downloads/percona-release/redhat/0.1-4/ \ \ Percona的释放,0.1-4.noarch.rpm $ rpm -ivH percona-release-0.1-4.noarch.rpm
[root@node7 ~]# yum -y install percona-xtrabackup-24
查看:
[root@node7 ~]# rpm -ql percona-xtrabackup-24
/usr/bin/innobackupex
/usr/bin/xbcloud
/usr/bin/xbcloud_osenv
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup
/usr/share/doc/percona-xtrabackup-24-2.4.10
/usr/share/doc/percona-xtrabackup-24-2.4.10/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
2、通过下载对应系统的rpm包来安装
官网:https://www.percona.com/downloads/XtraBackup/LATEST/
以percona-xtrabackup-2.3.8为例
[root@node7 ~]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.3.8/binary/redhat/7/x86_64/percona-xtrabackup-2.3.8-1.el7.x86_64.rpm
[root@node7 ~]# yum -y localinstall percona-xtrabackup-2.3.8-1.el7.x86_64.rpm
三)通过percona-xtrabackup备份还原数据库
案例1)完全备份及还原
数据库服务器: node8.51yuki.cn 10.2.13.167 centos7.3
第一步:创建用于备份的用户
mysql> GRANT SELECT, INSERT, CREATE, RELOAD, PROCESS, SUPER, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost' IDENTIFIED BY 'Aa123321';
Query OK, 0 rows affected (0.08 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
第二步:创建需要存放备份的目录
[root@node8 ~]#mkdir -p /opt/backup
第三步: 执行完全备份命令 (通过innobackupex命令)
完全备份数据库
[root@node8 ~]# innobackupex --user=bkpuser --password='Aa123321' /opt/backup -S /tmp/mysql.sock
180404 09:55:38 innobackupex: Starting the backup operation
IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
180404 09:55:38 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;mysql_socket=/tmp/mysql.sock' as 'bkpuser' (using password: YES).
180404 09:55:38 version_check Connected to MySQL server
180404 09:55:38 version_check Executing a version check against the server...
180404 09:55:38 version_check Done.
180404 09:55:38 Connecting to MySQL server host: localhost, user: bkpuser, password: set, port: not set, socket: /tmp/mysql.sock
Using server version 5.6.39-log
innobackupex version 2.4.10 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 3198bce)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /data/m_data/
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 1073741824
InnoDB: Number of pools: 1
180404 09:55:38 >> log scanned up to (21886050)
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID 1 for mysql/innodb_table_stats, old maximum was 0
180404 09:55:39 [01] Copying ./ibdata1 to /opt/backup/2018-04-04_09-55-38/ibdata1
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./mysql/innodb_table_stats.ibd to /opt/backup/2018-04-04_09-55-38/mysql/innodb_table_stats.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./mysql/innodb_index_stats.ibd to /opt/backup/2018-04-04_09-55-38/mysql/innodb_index_stats.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./mysql/slave_relay_log_info.ibd to /opt/backup/2018-04-04_09-55-38/mysql/slave_relay_log_info.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./mysql/slave_master_info.ibd to /opt/backup/2018-04-04_09-55-38/mysql/slave_master_info.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./mysql/slave_worker_info.ibd to /opt/backup/2018-04-04_09-55-38/mysql/slave_worker_info.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./mydb/b.ibd to /opt/backup/2018-04-04_09-55-38/mydb/b.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./wordpress/wp_commentmeta.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_commentmeta.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./wordpress/wp_comments.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_comments.ibd
180404 09:55:39 [01] ...done
180404 09:55:39 [01] Copying ./wordpress/wp_options.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_options.ibd
180404 09:55:39 >> log scanned up to (21886050)
180404 09:55:39 [01] ...done
180404 09:55:40 [01] Copying ./wordpress/wp_postmeta.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_postmeta.ibd
180404 09:55:40 [01] ...done
180404 09:55:40 [01] Copying ./wordpress/wp_posts.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_posts.ibd
180404 09:55:40 [01] ...done
180404 09:55:40 >> log scanned up to (21886050)
180404 09:55:41 [01] Copying ./wordpress/wp_term_relationships.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_term_relationships.ibd
180404 09:55:41 [01] ...done
180404 09:55:41 [01] Copying ./wordpress/wp_term_taxonomy.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_term_taxonomy.ibd
180404 09:55:41 [01] ...done
180404 09:55:41 [01] Copying ./wordpress/wp_termmeta.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_termmeta.ibd
180404 09:55:41 [01] ...done
180404 09:55:41 [01] Copying ./wordpress/wp_terms.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_terms.ibd
180404 09:55:41 [01] ...done
180404 09:55:41 [01] Copying ./wordpress/wp_usermeta.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_usermeta.ibd
180404 09:55:41 [01] ...done
180404 09:55:41 [01] Copying ./wordpress/wp_users.ibd to /opt/backup/2018-04-04_09-55-38/wordpress/wp_users.ibd
180404 09:55:41 [01] ...done
180404 09:55:41 >> log scanned up to (21886050)
180404 09:55:42 Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
180404 09:55:42 Executing FLUSH TABLES WITH READ LOCK...
180404 09:55:42 Starting to backup non-InnoDB tables and files
180404 09:55:42 [01] Copying ./mysql/db.frm to /opt/backup/2018-04-04_09-55-38/mysql/db.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/db.MYI to /opt/backup/2018-04-04_09-55-38/mysql/db.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/db.MYD to /opt/backup/2018-04-04_09-55-38/mysql/db.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/user.frm to /opt/backup/2018-04-04_09-55-38/mysql/user.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/user.MYI to /opt/backup/2018-04-04_09-55-38/mysql/user.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/user.MYD to /opt/backup/2018-04-04_09-55-38/mysql/user.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/func.frm to /opt/backup/2018-04-04_09-55-38/mysql/func.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/func.MYI to /opt/backup/2018-04-04_09-55-38/mysql/func.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/func.MYD to /opt/backup/2018-04-04_09-55-38/mysql/func.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/plugin.frm to /opt/backup/2018-04-04_09-55-38/mysql/plugin.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/plugin.MYI to /opt/backup/2018-04-04_09-55-38/mysql/plugin.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/plugin.MYD to /opt/backup/2018-04-04_09-55-38/mysql/plugin.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/servers.frm to /opt/backup/2018-04-04_09-55-38/mysql/servers.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/servers.MYI to /opt/backup/2018-04-04_09-55-38/mysql/servers.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/servers.MYD to /opt/backup/2018-04-04_09-55-38/mysql/servers.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/tables_priv.frm to /opt/backup/2018-04-04_09-55-38/mysql/tables_priv.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/tables_priv.MYI to /opt/backup/2018-04-04_09-55-38/mysql/tables_priv.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/tables_priv.MYD to /opt/backup/2018-04-04_09-55-38/mysql/tables_priv.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/columns_priv.frm to /opt/backup/2018-04-04_09-55-38/mysql/columns_priv.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/columns_priv.MYI to /opt/backup/2018-04-04_09-55-38/mysql/columns_priv.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/columns_priv.MYD to /opt/backup/2018-04-04_09-55-38/mysql/columns_priv.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_topic.frm to /opt/backup/2018-04-04_09-55-38/mysql/help_topic.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_topic.MYI to /opt/backup/2018-04-04_09-55-38/mysql/help_topic.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_topic.MYD to /opt/backup/2018-04-04_09-55-38/mysql/help_topic.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_category.frm to /opt/backup/2018-04-04_09-55-38/mysql/help_category.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_category.MYI to /opt/backup/2018-04-04_09-55-38/mysql/help_category.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_category.MYD to /opt/backup/2018-04-04_09-55-38/mysql/help_category.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_relation.frm to /opt/backup/2018-04-04_09-55-38/mysql/help_relation.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_relation.MYI to /opt/backup/2018-04-04_09-55-38/mysql/help_relation.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_relation.MYD to /opt/backup/2018-04-04_09-55-38/mysql/help_relation.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_keyword.frm to /opt/backup/2018-04-04_09-55-38/mysql/help_keyword.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_keyword.MYI to /opt/backup/2018-04-04_09-55-38/mysql/help_keyword.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/help_keyword.MYD to /opt/backup/2018-04-04_09-55-38/mysql/help_keyword.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_name.frm to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_name.MYI to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_name.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_name.MYD to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_name.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone.frm to /opt/backup/2018-04-04_09-55-38/mysql/time_zone.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone.MYI to /opt/backup/2018-04-04_09-55-38/mysql/time_zone.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone.MYD to /opt/backup/2018-04-04_09-55-38/mysql/time_zone.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_transition.frm to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_transition.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_transition.MYI to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_transition.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_transition.MYD to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_transition.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_transition_type.frm to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_transition_type.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_transition_type.MYI to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_transition_type.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_transition_type.MYD to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_transition_type.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_leap_second.frm to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_leap_second.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_leap_second.MYI to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_leap_second.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/time_zone_leap_second.MYD to /opt/backup/2018-04-04_09-55-38/mysql/time_zone_leap_second.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/proc.frm to /opt/backup/2018-04-04_09-55-38/mysql/proc.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/proc.MYI to /opt/backup/2018-04-04_09-55-38/mysql/proc.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/proc.MYD to /opt/backup/2018-04-04_09-55-38/mysql/proc.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/procs_priv.frm to /opt/backup/2018-04-04_09-55-38/mysql/procs_priv.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/procs_priv.MYI to /opt/backup/2018-04-04_09-55-38/mysql/procs_priv.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/procs_priv.MYD to /opt/backup/2018-04-04_09-55-38/mysql/procs_priv.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/general_log.frm to /opt/backup/2018-04-04_09-55-38/mysql/general_log.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/general_log.CSM to /opt/backup/2018-04-04_09-55-38/mysql/general_log.CSM
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/general_log.CSV to /opt/backup/2018-04-04_09-55-38/mysql/general_log.CSV
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/slow_log.frm to /opt/backup/2018-04-04_09-55-38/mysql/slow_log.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/slow_log.CSM to /opt/backup/2018-04-04_09-55-38/mysql/slow_log.CSM
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/slow_log.CSV to /opt/backup/2018-04-04_09-55-38/mysql/slow_log.CSV
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/event.frm to /opt/backup/2018-04-04_09-55-38/mysql/event.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/event.MYI to /opt/backup/2018-04-04_09-55-38/mysql/event.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/event.MYD to /opt/backup/2018-04-04_09-55-38/mysql/event.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/ndb_binlog_index.frm to /opt/backup/2018-04-04_09-55-38/mysql/ndb_binlog_index.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/ndb_binlog_index.MYI to /opt/backup/2018-04-04_09-55-38/mysql/ndb_binlog_index.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/ndb_binlog_index.MYD to /opt/backup/2018-04-04_09-55-38/mysql/ndb_binlog_index.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/innodb_table_stats.frm to /opt/backup/2018-04-04_09-55-38/mysql/innodb_table_stats.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/innodb_index_stats.frm to /opt/backup/2018-04-04_09-55-38/mysql/innodb_index_stats.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/slave_relay_log_info.frm to /opt/backup/2018-04-04_09-55-38/mysql/slave_relay_log_info.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/slave_master_info.frm to /opt/backup/2018-04-04_09-55-38/mysql/slave_master_info.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/slave_worker_info.frm to /opt/backup/2018-04-04_09-55-38/mysql/slave_worker_info.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/proxies_priv.frm to /opt/backup/2018-04-04_09-55-38/mysql/proxies_priv.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/proxies_priv.MYI to /opt/backup/2018-04-04_09-55-38/mysql/proxies_priv.MYI
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./mysql/proxies_priv.MYD to /opt/backup/2018-04-04_09-55-38/mysql/proxies_priv.MYD
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/db.opt to /opt/backup/2018-04-04_09-55-38/performance_schema/db.opt
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/cond_instances.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/cond_instances.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_current.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_current.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_history.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_history.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_history_long.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_history_long.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_summary_by_instance.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_summary_by_instance.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_summary_by_host_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_summary_by_host_by_event_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_summary_by_user_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_summary_by_user_by_event_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_summary_by_account_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_summary_by_account_by_event_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/events_waits_summary_by_thread_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_summary_by_thread_by_event_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 >> log scanned up to (21886050)
180404 09:55:42 [01] Copying ./performance_schema/events_waits_summary_global_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_waits_summary_global_by_event_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/file_instances.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/file_instances.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/file_summary_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/file_summary_by_event_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/file_summary_by_instance.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/file_summary_by_instance.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/socket_instances.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/socket_instances.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/socket_summary_by_instance.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/socket_summary_by_instance.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/socket_summary_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/socket_summary_by_event_name.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/host_cache.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/host_cache.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/mutex_instances.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/mutex_instances.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/objects_summary_global_by_type.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/objects_summary_global_by_type.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/performance_timers.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/performance_timers.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/rwlock_instances.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/rwlock_instances.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/setup_actors.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/setup_actors.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/setup_consumers.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/setup_consumers.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/setup_instruments.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/setup_instruments.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/setup_objects.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/setup_objects.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/setup_timers.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/setup_timers.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/table_io_waits_summary_by_index_usage.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/table_io_waits_summary_by_index_usage.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/table_io_waits_summary_by_table.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/table_io_waits_summary_by_table.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/table_lock_waits_summary_by_table.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/table_lock_waits_summary_by_table.frm
180404 09:55:42 [01] ...done
180404 09:55:42 [01] Copying ./performance_schema/threads.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/threads.frm
180404 09:55:42 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_current.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_current.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_history.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_history.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_history_long.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_history_long.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_summary_by_thread_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_summary_by_thread_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_summary_by_host_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_summary_by_host_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_summary_by_user_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_summary_by_user_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_summary_by_account_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_summary_by_account_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_stages_summary_global_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_stages_summary_global_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_current.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_current.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_history.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_history.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_history_long.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_history_long.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_summary_by_thread_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_summary_by_thread_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_summary_by_host_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_summary_by_host_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_summary_by_user_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_summary_by_user_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_summary_by_account_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_summary_by_account_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_summary_global_by_event_name.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_summary_global_by_event_name.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/hosts.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/hosts.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/users.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/users.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/accounts.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/accounts.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/events_statements_summary_by_digest.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/events_statements_summary_by_digest.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/session_connect_attrs.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/session_connect_attrs.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./performance_schema/session_account_connect_attrs.frm to /opt/backup/2018-04-04_09-55-38/performance_schema/session_account_connect_attrs.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./mydb/db.opt to /opt/backup/2018-04-04_09-55-38/mydb/db.opt
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./mydb/b.frm to /opt/backup/2018-04-04_09-55-38/mydb/b.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/db.opt to /opt/backup/2018-04-04_09-55-38/wordpress/db.opt
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_commentmeta.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_commentmeta.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_comments.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_comments.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_hermit.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_hermit.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_hermit.MYI to /opt/backup/2018-04-04_09-55-38/wordpress/wp_hermit.MYI
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_hermit.MYD to /opt/backup/2018-04-04_09-55-38/wordpress/wp_hermit.MYD
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_hermit_cat.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_hermit_cat.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_hermit_cat.MYI to /opt/backup/2018-04-04_09-55-38/wordpress/wp_hermit_cat.MYI
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_hermit_cat.MYD to /opt/backup/2018-04-04_09-55-38/wordpress/wp_hermit_cat.MYD
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_options.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_options.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_postmeta.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_postmeta.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_posts.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_posts.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_term_relationships.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_term_relationships.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_term_taxonomy.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_term_taxonomy.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_termmeta.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_termmeta.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_terms.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_terms.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_usermeta.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_usermeta.frm
180404 09:55:43 [01] ...done
180404 09:55:43 [01] Copying ./wordpress/wp_users.frm to /opt/backup/2018-04-04_09-55-38/wordpress/wp_users.frm
180404 09:55:43 [01] ...done
180404 09:55:43 Finished backing up non-InnoDB tables and files
180404 09:55:43 [00] Writing /opt/backup/2018-04-04_09-55-38/xtrabackup_binlog_info
180404 09:55:43 [00] ...done
180404 09:55:43 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '21886050'
xtrabackup: Stopping log copying thread.
.180404 09:55:43 >> log scanned up to (21886050)
180404 09:55:43 Executing UNLOCK TABLES
180404 09:55:43 All tables unlocked
180404 09:55:43 Backup created in directory '/opt/backup/2018-04-04_09-55-38/'
MySQL binlog position: filename 'bin.000016', position '470'
180404 09:55:43 [00] Writing /opt/backup/2018-04-04_09-55-38/backup-my.cnf
180404 09:55:43 [00] ...done
180404 09:55:43 [00] Writing /opt/backup/2018-04-04_09-55-38/xtrabackup_info
180404 09:55:43 [00] ...done
xtrabackup: Transaction log of lsn (21886050) to (21886050) was copied.
180404 09:55:43 completed OK!
第四步:然后把备份目录,通过rsync拷贝到备份服务器上(以达到本地存一份,远程存一份,避免本地服务器因硬件故障,导致备份丢失)
还原数据库
情景:假如有一天,数据库服务器因硬件故障,无法启动,我们在备份服务器上有数据库的备份记录,接下就演示如何完全还原数据库
第一步:先从备份服务器上,下载最新的完全备份目录到指定的服务器(node7.51yuki.cn)上
第二步:还原数据库前,需要先做一些整理(通过apply-log)
[root@node7 ~]# innobackupex --apply-log /backup/2018-04-04_09-55-38/
180404 10:16:29 innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!".
InnoDB: page_cleaner: 1000ms intended loop took 121481ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 21887016
180404 10:18:36 completed OK!
第三步:执行还原
注意:还原前不需要初始化数据库,或者如果数据目录里有文件,需要先删除即可
关闭myqld服务
[root@node7 ~]# systemctl stop mysqld
通过copy-back执行还原
[root@node7 m_data]# innobackupex --copy-back /backup/2018-04-04_09-55-38/
180404 11:56:40 innobackupex: Starting the copy-back operation
IMPORTANT: Please check that the copy-back run completes successfully.
At the end of a successful copy-back run innobackupex
prints "completed OK!".
180404 11:57:49 [01] Copying ./xtrabackup_binlog_pos_innodb to /backup/m_data/xtrabackup_binlog_pos_innodb
180404 11:57:49 [01] ...done
180404 11:57:49 completed OK!
[root@node7 ~]# chown -R mysql.mysql /backup/m_data/
然后启动mysqld,如果失败,查看日志/var/log/mysqld.log
[root@node7 ~]# systemctl start mysqld
[root@node7 ~]# ss -tunlp|grep 3306
tcp LISTEN 0 80 10.2.13.166:3306 *:* users:(("mysqld",pid=6618,fd=13))
2)如何使用innobackupex做增量备份
说明:
无论是xtrabackup和innobackupex工具都支持增量备份,还意味着它们可以只复制上次备份以来变化的数据,例如可以每周做一次完整备份和每天增量备份
增量备份的工作原理:每个innodb页面都包含日志序列号LSN,每个页面的LSN显示他最近变化的时间,增量备份复制LSN比之前增量或完全备份的LSN更新的每个页面。
增量备份只需读取页面并将其LSN与最后一个备份的LSN进行比较。但是,您仍需要完整备份才能恢复增量更改; 如果没有完整的备份作为基础,增量备份将毫无用处
注意:
- 增量备份仅仅应用于innodb或xtradb表,对MyISAM表而言,执行增量备份其实进行的是完全备份
- 如果每次的incremental-basedir都执行为上一次完全备份,其实这里做的就是差异备份
做增量备份
[root@node8 ~]# innobackupex --incremental --user=bkpuser --password='Aa123321' --socket=/tmp/mysql.sock /backup/ --incremental-basedir=/backup/2018-04-04_14-00-23
查看:
完全备份的信息:
[root@node8 2018-04-04_14-00-23]# cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 21893387
last_lsn = 21893387
增量备份的信息:
[root@node8 2018-04-04_14-03-56]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 21893387
to_lsn = 21927178
last_lsn = 21927178
还原数据库
- 整理完全备份
[root@node7 backup]# innobackupex --apply-log --redo-only /backup/2018-04-04_14-00-23/
- 整理部分备份 (把增量合并到完全备份)
[root@node7 backup]# innobackupex --apply-log --redo-only /backup/2018-04-04_14-00-23/ --incremental-dir=/backup/2018-04-04_14-03-56/
说明:如果有多个增量,如果有多个增量,那多都要把每个增量合并到完全备份上
查看:
[root@node7 2018-04-04_14-00-23]# cat xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 21927178
last_lsn = 21927178 这里发现to_lsn和上次查看增量备份的的last_lsn一致,说明已经增量备份已经合并到完全备份咯
还原数据库:
[root@node7 ~]# cd /backup/m_data/
[root@node7 m_data]# rm -rf *
[root@node7 m_data]# systemctl stop mysqld
[root@node7 ~]# innobackupex --copy-back /backup/2018-04-04_14-00-23/ 基于合并的完全备份做还原数据库
[root@node7 m_data]# chown -R mysql.mysql /backup/m_data/
[root@node7 m_data]# systemctl start mysqld
查看数据是否存在
[root@node7 m_data]# mysql -u root -p
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| wordpress |
+--------------------+
6 rows in set (0.01 sec)
mysql> select * from student;
+------+-------+
| id | name |
+------+-------+
| 1 | xiong |
| 2 | hai |
| 3 | hua |
+------+-------+
3 rows in set (0.00 sec)
3)导入或导出单张表
默认情况下,innodb不能通过直接复制表文件的方式在mysql服务器之间进行移植,即使使用innodb_file_per_table选项,使用xtrabackup工具可以实现这个功能
- 导出表
导出表示在备份的prepare阶段进行的,因此,一旦完全备份完成,就可以在prepare过程中通过--export选项将表导出
[root@node8 ~]# innobackupex --user=bkpuser --password=Aa123321 --socket=/tmp/mysql.sock /backup/ 把这个拷贝到另外一台服务器上
[root@node7 backup]# innobackupex --apply-log --export /backup/2018-04-04_14-27-45/
查看
[root@node7 hellodb]# ll
total 388
-rw-r--r-- 1 root root 432 Apr 4 14:29 course.cfg
-rw-r----- 1 root root 16384 Apr 4 14:29 course.exp
-rw-r----- 1 root root 8590 Apr 4 14:29 course.frm
-rw-r----- 1 root root 98304 Apr 4 14:29 course.ibd
-rw-r----- 1 root root 67 Apr 4 14:29 db.opt
-rw-r--r-- 1 root root 429 Apr 4 14:29 student.cfg
-rw-r----- 1 root root 16384 Apr 4 14:29 student.exp
-rw-r----- 1 root root 8586 Apr 4 14:29 student.frm
-rw-r----- 1 root root 98304 Apr 4 14:29 student.ibd
-rw-r--r-- 1 root root 374 Apr 4 14:29 testdb.cfg
-rw-r----- 1 root root 16384 Apr 4 14:29 testdb.exp
-rw-r----- 1 root root 8556 Apr 4 14:29 testdb.frm
-rw-r----- 1 root root 98304 Apr 4 14:29 testdb.ibd
(然后把exp和ibd文件复制到其他机器上,我这个就在node7上咯)
导入单张表
- 需要先创建表结构(比如和原来的一样),然后删除原来的表空间
Database changed
mysql> create table course (id int,course char(40));
Query OK, 0 rows affected (0.13 sec)
mysql> alter table course discard tablespace;
Query OK, 0 rows affected (0.02 sec)
[root@node7 ~]# cp /backup/2018-04-04_14-27-45/hellodb/course.exp /backup/m_data/hellodb/
[root@node7 ~]# cp /backup/2018-04-04_14-27-45/hellodb/course.ibd /backup/m_data/hellodb/
[root@node7 ~]# ll /backup/m_data/hellodb/course.*
-rw-r----- 1 root root 16384 Apr 4 14:45 /backup/m_data/hellodb/course.exp
-rw-rw---- 1 mysql mysql 8590 Apr 4 14:44 /backup/m_data/hellodb/course.frm
-rw-r----- 1 root root 98304 Apr 4 14:45 /backup/m_data/hellodb/course.ibd
[root@node7 ~]# chown -R mysql.mysql /backup/m_data/hellodb/
导入表空间
mysql> alter table course import tablespace;
Query OK, 0 rows affected, 1 warning (0.13 sec)
查看,看看数据有没有
mysql> select * from course;
+------+--------+
| id | course |
+------+--------+
| 100 | yuwen |
| 101 | shuxue |
| 102 | yingyu |
+------+--------+
3 rows in set (0.00 sec)