准备一个干净的备份目录;

[root@mysql~]$ll/backups/total0MariaDB[(none)]>showbinarylogs;+------------------+-----------+|Log_name|File_size|+------------------+-----------+|mysql-bin.000001|30331||mysql-bin.000002|1038814||mysql-bin.000003|7698||mysql-bin.000004|442||mysql-bin.000005|423|+------------------+-----------+5rowsinset(0.00sec)


数据备份:

默认就是以root用户的身份进行的备份;[root@mysql~]$innobackupex--user=root/backups/[root@mysql~]$ll/backups/total0drwxr-x---6rootroot217Feb2514:142018-02-25_14-14-07[root@mysql~]$ll/backups/2018-02-25_14-14-07/total18460-rw-r-----1rootroot417Feb2514:14backup-my.cnfdrwxr-x---2rootroot272Feb2514:14hellodb-rw-r-----1rootroot18874368Feb2514:14ibdata1drwxr-x---2rootroot4096Feb2514:14mysqldrwxr-x---2rootroot4096Feb2514:14performance_schemadrwxr-x---2rootroot20Feb2514:14test-rw-r-----1rootroot21Feb2514:14xtrabackup_binlog_info-rw-r-----1rootroot113Feb2514:14xtrabackup_checkpoints-rw-r-----1rootroot454Feb2514:14xtrabackup_info-rw-r-----1rootroot2560Feb2514:14xtrabackup_logfile[root@mysql~]$cd/backups/2018-02-25_14-14-07/[root@mysql2018-02-25_14-14-07]$lsbackup-my.cnfibdata1performance_schemaxtrabackup_binlog_infoxtrabackup_infohellodbmysqltestxtrabackup_checkpointsxtrabackup_logfile[root@mysql2018-02-25_14-14-07]$#lsn是日志序列号,在磁盘上保存了数据库的所有数据;文件很大,分成很多的小块存储在了磁盘上;每个数据块的小块都有所谓的lsn号;如从100-200,如果将100这个数据块中的数据做了修改,那么他的LSN会加1,即变为了201;所以根据LSN的大小,可以判断数据块中的数据是否备份过;这里是全备份,所以就是将所有的LSN对应的数据块的数据都进行了备份。[root@mysql2018-02-25_14-14-07]$lessxtrabackup_checkpointsbackup_type=full-backupedfrom_lsn=0to_lsn=1640915last_lsn=1640915compact=0recover_binlog_info=0尽管表面来看是拷贝的文件,但是底层实际拷贝的是数据块,所有效率很高。这个文件中显示的是全备份备份到二进制文件的哪个位置;[root@mysql2018-02-25_14-14-07]$catxtrabackup_binlog_infomysql-bin.000005423[root@mysql2018-02-25_14-14-07]$filextrabackup_logfilextrabackup_logfile:data




还原数据到一个新的MySQL主机上

找一个干净的主机,将在27.7主机上备份的数据在27.17上实现还原;[root@mysql17~]$vim/etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock#Disablingsymbolic-linksisrecommendedtopreventassortedsecurityriskssymbolic-links=0log_bininnodb_file_per_table[root@mysql17~]$yuminstallpercona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm[root@mysqlbackups]$scp-rp/backups/2018-02-25_14-14-07/192.168.27.17:/app/[root@mysql17~]$ls/app/2018-02-25_14-14-07




数据库的整理操作:
包括将不完整的事务进行回滚;因为备份的时间点极有可能被一个事务横跨;

[root@mysql17~]$innobackupex--apply-log/app/2018-02-25_14-14-07/18022501:53:05completedOK!

确保要恢复的数据库的数据目录是空的;[root@mysql17~]$ll/var/lib/mysql/total0将整理过数据复制到数据库的数据目录;这个过程就是复制数据,对于innodb引擎,他是基于块的方式实现的;对于myISAM引擎,那么就是单个文件的复制;[root@mysql17~]$innobackupex--copy-back/app/2018-02-25_14-14-07/...18022501:56:10completedOK![root@mysql17~]$ll/var/lib/mysql/total40976drwxr-x---2rootroot272Feb2501:56hellodb-rw-r-----1rootroot18874368Feb2501:56ibdata1-rw-r-----1rootroot5242880Feb2501:56ib_logfile0-rw-r-----1rootroot5242880Feb2501:56ib_logfile1-rw-r-----1rootroot12582912Feb2501:56ibtmp1drwxr-x---2rootroot4096Feb2501:56mysqldrwxr-x---2rootroot4096Feb2501:56performance_schemadrwxr-x---2rootroot20Feb2501:56test-rw-r-----1rootroot35Feb2501:56xtrabackup_binlog_pos_innodb-rw-r-----1rootroot454Feb2501:56xtrabackup_info[root@mysql17~]$chown-Rmysql.mysql/var/lib/mysql/[root@mysql17~]$ll/var/lib/mysql/total40976drwxr-x---2mysqlmysql272Feb2501:56hellodb-rw-r-----1mysqlmysql18874368Feb2501:56ibdata1-rw-r-----1mysqlmysql5242880Feb2501:56ib_logfile0-rw-r-----1mysqlmysql5242880Feb2501:56ib_logfile1-rw-r-----1mysqlmysql12582912Feb2501:56ibtmp1drwxr-x---2mysqlmysql4096Feb2501:56mysqldrwxr-x---2mysqlmysql4096Feb2501:56performance_schemadrwxr-x---2mysqlmysql20Feb2501:56test-rw-r-----1mysqlmysql35Feb2501:56xtrabackup_binlog_pos_innodb-rw-r-----1mysqlmysql454Feb2501:56xtrabackup_info[root@mysql17~]$ll/var/lib/mysql/-ddrwxr-xr-x6mysqlmysql198Feb2501:56/var/lib/mysql/

[root@mysql17~]$systemctlstartmariadbMariaDB[(none)]>showdatabases;+--------------------+|Database|+--------------------+|information_schema||hellodb||mysql||performance_schema||test|+--------------------+5rowsinset(0.00sec)MariaDB[(none)]>MariaDB[(none)]>showmasterlogs;+--------------------+-----------+|Log_name|File_size|+--------------------+-----------+|mariadb-bin.000001|245|+--------------------+-----------+1rowinset(0.00sec)MariaDB[(none)]>select*fromhellodb.students;Emptyset(0.00sec)以上就是使用xtrabackup实现将一个主机的数据全备份后还原到一个远程的新的主机的过程。