您好,登錄后才能下訂單哦!
數據恢復篇
XtraBackup工具介紹:
現在我們給MySQL做備份的時候經常會考慮到數據量的增長,數據量較小的時候用mysqldump,隨著數據量越來越大mysqldump也就不太合適了.第一不支持增量備份,第二恢復的時候也較慢.這里推薦使用Percona公司的XtraBackup.
簡單介紹一下熱備份工具XtraBackup,它是Percona公司推出的一款熱備份工具,備份的時候不影響數據讀寫操作,是商業工具HotBackup的一個替代(現在應該叫MySQL Enterprise Backup這個工具不止可以備份InnoDB還可以備份MyISAM等等)
XtraBackup有兩個工具:xtrabackup和innobackupex.
xtrabackup本身只能備份InnoDB和XtraDB,不能備份MyISAM.
innobackupex本身是Hot Backup腳本修改而來,同時可以備份MyISAM和InnoDB,但是備份MyISAM需要加讀鎖.
#安裝percona-xtrabackup rpm軟件包。
[root@localhost data]# rpm -ivh percona-xtrabackup-2.3.2-1.el6.x86_64.rpm warning: percona-xtrabackup-2.3.2-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY error: Failed dependencies: libev.so.4()(64bit) is needed by percona-xtrabackup-2.3.2-1.el6.x86_64 perl(DBD::mysql) is needed by percona-xtrabackup-2.3.2-1.el6.x86_64
#安裝依賴:
yum install perl-DBD-MySQL
#安裝libev包
到http://rpmfind.net/linux/rpm2html/search.php?query=libev.so.4%28%29%2864bit%29&submit=Search+...&system=&arch= 下載操作系統對應的libev包,注意系統號與版本位數
[root@master ~]# wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libev-4.15-1.el6.rf.x86_64.rpm --2016-05-17 12:54:43-- ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libev-4.15-1.el6.rf.x86_64.rpm => “libev-4.15-1.el6.rf.x86_64.rpm” Resolving rpmfind.net... 195.220.108.108 Connecting to rpmfind.net|195.220.108.108|:21... connected. Logging in as anonymous ... Logged in! ==> SYST ... done. ==> PWD ... done. ==> TYPE I ... done. ==> CWD (1) /linux/dag/redhat/el6/en/x86_64/dag/RPMS ... done. ==> SIZE libev-4.15-1.el6.rf.x86_64.rpm ... 74692 ==> PASV ... done. ==> RETR libev-4.15-1.el6.rf.x86_64.rpm ... done. Length: 74692 (73K) (unauthoritative) 100%[===============================================================================================================>] 74,692 187K/s in 0.4s 2016-05-17 12:54:46 (187 KB/s) - “libev-4.15-1.el6.rf.x86_64.rpm” saved [74692]
安裝RHEL6對應的libev-4.15-1.el6.rf.x86_64.rpm包
[root@master ~]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm warning: libev-4.15-1.el6.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY Preparing... ########################################### [100%] 1:libev ########################################### [100%]
#安裝percona-xtrabackup軟件包:
[root@localhost data]# rpm -ivh percona-xtrabackup-2.3.2-1.el6.x86_64.rpm warning: percona-xtrabackup-2.3.2-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY Preparing... ########################################### [100%] percona-xtrabackup ########################################### [100%]
操作過程如圖:
#備份恢復時數據庫必須停止的。
[root@localhost ~]# /etc/init.d/mysqld stop Shutting down MySQL.. [ OK ]
#上篇fpm封裝好的二進制mysql,純凈。
注意:備份時候MySQL必須是運行狀態,而在做數據恢復時候服務必須是處于停滯狀態。
重要參數:
--defaults-file=/etc/my.cnf:備份時,指定配置文件
--socket=/tmp/mysql.sock:備份時,連接到備份的mysql服務器所使用的 套接字文件
--user=root:連接備份mysqld所使用的用戶
--password=hello:連接備份mysqld所使用的用戶密碼
--no-timestamp:默認情況下,備份目錄會使用以當前時間為名稱創建一個備份目錄,加上此參數則不會做這個動作
--apply-log:創建ib_logfile文件,并恢復備份期間產生的redo日志到ib_logfile和表空間中。注意:若備份期間產生了提交的事務日志,則apply-log操作會將未提交的事務回滾
--copy-back:執行恢復數據到目標mysqld,但是目標mysqld的數據目錄不 能用—datadir指定,這一點不同與meb,而是需要使用—defaults-file指 定一個配置文件,在配置文件中指定恢復的datadir。與meb不同,恢復前必 須清空目錄數據目錄
--use-memory=1G:備份時xtrabackup可用的內存大小,取決于操作系統可用的內存量,默認為100M,使用大的內存可加速備份的速度
--incremental:表示為增量備份,增量備份只對innodb表有效,對于non-innodb表,例如myisam則無效,因為每次增量備份都會拷貝myisam相關的所有文件,不管上次備份到這次備份有沒有發生更改;而對于innodb表,增量備份則只會備份上次備份到這次備份所更改的表
--incremental-basedir=/lxw/full-back/:基于上一次的備份目錄做增量 備份
--incremental-dir=/lxw/incr-backup/:用于將增量備份恢復到全備時指定增量備份的目錄
--redo-only:增量備份恢復到之前的備份時,除了最后一個增量備份外,其他的增量備份最好指定該參數
--databases=’dba.t1 mysql performance_schema lxw.t1’:用于部分備份,可以用空格分開,指定備份哪些庫的所有表,或者哪些庫的某些表
--export:在執行apply-log操作時,將應用信息保存到文件中,在恢復部分備份時有用
--compact:采用壓縮備份
--rebuild-indexes:解壓縮
----記錄備份信息(如備份命令、備份開始和完成時間、binlog position、備份開始和完成的lsn等
注意:備份使用的innobackupex是2.3.1 恢復可以使用innobackupex2.3.x等小版本,但是不能夸大版本,否則會有問題。
新建一個數據目錄,和原數據目錄同樣的名字:
mkdir /data/mysqldata
#為了練習使用percona-xtrabackup全量備份了線上的mysql,scp到新機器上恢復練習。
備份完備份的時間目錄下都有xtrabackup_info 文件,記錄了你備份時的動作。
[root@localhost 2017-03-27_04-00-01]# cat xtrabackup_info uuid = c4c25074-1260-11e7-9a6a-069e3400093a name = tool_name = innobackupex tool_command = --defaults-file=/etc/my.cnf --socket=/usr/local/mysql56/data/mysqld.sock --host=localhost --user=backuser --password=... /usr/local/mysql56/mysql_data_backup/o2o/full_backup/ tool_version = 2.3.3#工具的版本 ibbackup_version = 2.3.3 server_version = 5.6.25-log start_time = 2017-03-27 04:00:02 end_time = 2017-03-27 04:14:08 lock_time = 0 binlog_pos = filename 'mysql-bin.000285', position '764217961' innodb_from_lsn = 0 innodb_to_lsn = 393358640191 partial = N incremental = N format = file compact = N compressed = N encrypted = N
#首先恢復事務,也可以加--user=:
[root@localhost data]# innobackupex --apply-log --use-memory=10M 2017-03-27_04-00-01/ 170331 01:28:54 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!". innobackupex version 2.3.2 based on MySQL server 5.6.24 Linux (x86_64) (revision id: 306a2e0) xtrabackup: cd to /data/2017-03-27_04-00-01/ xtrabackup: This target seems to be not prepared yet. xtrabackup: xtrabackup_logfile detected: size=26689536, start_lsn=(393334934174) xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 26689536 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 26689536 xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 10485760 bytes for buffer pool (set by --use-memory parameter) InnoDB: Using atomics to ref count buffer pool pages InnoDB: The InnoDB memory heap is disabled InnoDB: Mutexes and rw_locks use GCC atomic builtins InnoDB: Memory barrier is not used InnoDB: Compressed tables use zlib 1.2.3 InnoDB: Using CPU crc32 instructions InnoDB: Initializing buffer pool, size = 10.0M InnoDB: Completed initialization of buffer pool InnoDB: Highest supported file format is Barracuda. InnoDB: Log scan progressed past the checkpoint lsn 393334934174 InnoDB: Database was not shutdown normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages InnoDB: from the doublewrite buffer... InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Doing recovery: scanned up to log sequence number 393340176896 (22%) InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Doing recovery: scanned up to log sequence number 393345419776 (44%) InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Doing recovery: scanned up to log sequence number 393350662656 (66%) InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Doing recovery: scanned up to log sequence number 393355905536 (88%) InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: Doing recovery: scanned up to log sequence number 393358640191 (99%) InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed InnoDB: 128 rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.6.24 started; log sequence number 393358640191 xtrabackup: Last MySQL binlog file position 764217961, file name mysql-bin.000285 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 393358641472 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 50331648 InnoDB: Using atomics to ref count buffer pool pages InnoDB: The InnoDB memory heap is disabled InnoDB: Mutexes and rw_locks use GCC atomic builtins InnoDB: Memory barrier is not used InnoDB: Compressed tables use zlib 1.2.3 InnoDB: Using CPU crc32 instructions InnoDB: Initializing buffer pool, size = 10.0M InnoDB: Completed initialization of buffer pool InnoDB: Setting log file ./ib_logfile101 size to 48 MB InnoDB: Setting log file ./ib_logfile1 size to 48 MB InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 InnoDB: New log files created, LSN=393358641472 InnoDB: Highest supported file format is Barracuda. InnoDB: 128 rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.6.24 started; log sequence number 393358641676 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 393358644153 170331 01:29:33 completed OK!
#恢復數據,也可以加--defaults-file=/etc/my.cnf --rsync 也可以去掉
innobackupex --copy-back --rsync 2017-03-27_04-00-01/ ........ ......... 170331 03:03:04 [01] ...done 170331 03:03:04 [01] Copying ./ecos_notice/tbl_notice_file.ibd to /data/mysqldata/ecos_notice/tbl_notice_file.ibd 170331 03:03:04 [01] ...done 170331 03:03:04 [01] Copying ./stest/tbl_test_user_info.ibd to /data/mysqldata/stest/tbl_test_user_info.ibd 170331 03:03:04 [01] ...done 170331 03:03:04 [01] Copying ./stest/tbl_test_user_info.frm to /data/mysqldata/stest/tbl_test_user_info.frm 170331 03:03:04 [01] ...done 170331 03:03:04 [01] Copying ./stest/db.opt to /data/mysqldata/stest/db.opt 170331 03:03:04 [01] ...done 170331 03:03:04 completed OK!
授權恢復數據的目錄為mysql用戶和mysql組:
[root@localhost data]# ll total 5624 drwx------ 14 root root 4096 Mar 31 02:33 2017-03-27_04-00-01 -rw-r--r-- 1 root root 74692 Mar 31 01:10 libev-4.15-1.el6.rf.x86_64.rpm drwxr-xr-x 13 mysql mysql 4096 Mar 31 02:42 mysql drwxr-xr-x 14 root root 4096 Mar 31 03:03 mysqldata drwxr-xr-x 2 mysql mysql 4096 Mar 31 02:47 mysqldata20170330 -rw-r--r-- 1 root root 5664452 Mar 31 01:09 percona-xtrabackup-2.3.2-1.el6.x86_64.rpm
#授權mysql用戶和mysql組
[root@localhost data]# chown mysql:mysql mysqldata -R #授權mysql用戶和mysql組
#mysql啟動跳過授權用戶表
[root@localhost data]# cat /etc/my.cnf [mysqld] basedir=/data/mysql datadir=/data/mysqldata port=3306 socket=/tmp/mysql.sock user=mysql innodb_file_per_table=on skip_name_resolve=on symbolic-links=0 init-connect='SET NAMES utf8' character-set-server = utf8 max_connections = 5000 max_connect_errors = 100000 skip-grant-tables #添加啟動mysql跳過用戶授權表 [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid、
#啟動mysql服務:
[root@localhost data]# /etc/init.d/mysqld start Starting MySQL... [ OK ] [root@localhost data]# cd mysqldata [root@localhost mysqldata]# ls 2017-03-31_02-33-37 ecos ecoslog ibdata1 localhost.localdomain.pid stest xtrabackup_info appversion ecosantispam ecos_notice ib_logfile0 mysql test auto.cnf ecos_delivery ecoswallet ib_logfile1 performance_schema xtrabackup_binlog_pos_innodb
#如果忘記了登錄mysql的用戶密碼可以跳過用戶認證表,當然也可以加在my.cnf文件中:
/etc/rc.d/init.d/mysqld start --skip-grant-tables; #跳過權限表啟動mysql不用密碼;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。