解析XtraBackup備份MySQL的原理和過程

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

原文:http://ourlinux.blog.51cto.com/274624/844859

      XtraBackup是percona公司提供的開源工具,以熱備Innodb表著稱而被廣泛採用。

      XtraBackup對Innodb的備份之所以是熱備,無需鎖表,是基於Innodb自身的崩潰恢複機制,它首先複製所有的Innodb資料檔案,這樣

複製出來的檔案肯定是不一致的,然後對每個檔案進行崩潰恢複處理,最終達到一致。就和MySQL在啟動Innodb的時候一樣,會通過比較資料

檔案頭和redo log檔案頭資訊來檢查資料是否是一致的,如果不一致就嘗試通過前滾(把redo log中所有提交的事務寫入資料檔案)和復原(從數

據檔案中撤銷所有redo log中未提交的事務引起的修改)來使資料達到最終一致。

      XtraBackup在啟動的時候會記錄一個LSN(log sequence number),然後就把所有的Innodb資料檔案複製出來,這樣複製出來的資料文

件是不一致的,但是XtraBackup會在後台運行一個進程把所有 對redo log file的修改記錄下來,只要有了這個資料,就能進行崩潰恢複。只所

以要額外記錄下來,是因為MySQL自身的redo log file是可重用的。

      以上的操作是由xtrabackup二進位程式(比如xtrabackup_55)完成的,如果使用 innobackupex 指令碼,剛才的步驟完成以後,innobackupex

就會去備份MyISAM表和.frm檔案,這時要保證資料的一致性就會先鎖表了,通過FLUSH TABLES WITH READ LOCK命令鎖表然後把檔案複

制出來,再釋放掉這個鎖。

      在恢複資料的時候,要經過 prepare(recovery)和restore兩個步驟。在prepare結束以後,Innodb的表恢複到了複製Innodb檔案結束的

時間點, 這個時間點也就是鎖表複製MyISAM表的起點,所以最終資料是一致的。一般我們在恢複的時候執行兩次prepare,是因為第二次prepare

會協助 我們產生redo log檔案,從而加快MySQL資料庫啟動的速度。

  我們再來看一下實際備份的日誌來理解這個過程:
......110701 03:29:13  innobackupex: Starting ibbackup with command: xtrabackup_55  --defaults-file="/home/mysql/3306/my.cnf" --backup --suspend-at-end --log-stream --target-dir=./innobackupex: Waiting for ibbackup (pid=22334) to suspendinnobackupex: Suspend file ‘/home/mysql/3306/data/xtrabackup_suspended‘xtrabackup: suspend-at-end is enabled.xtrabackup: uses posix_fadvise().xtrabackup: cd to /home/mysql/3306/dataxtrabackup: Target instance is assumed as followings.xtrabackup:   innodb_data_home_dir = /home/mysql/3306/dataxtrabackup:   innodb_data_file_path = ibdata1:512M:autoextendxtrabackup:   innodb_log_group_home_dir = /home/mysql/3306/redologxtrabackup:   innodb_log_files_in_group = 3xtrabackup:   innodb_log_file_size = 134217728110701  3:29:13 InnoDB: Using Linux native AIO110701  3:29:13  InnoDB: Warning: allocated tablespace 268, old maximum was 0xtrabackup: Stream mode.>> log scanned up to (2371741708)110701 03:29:15  innobackupex: Continuing after ibbackup has suspendedinnobackupex: Starting to backup InnoDB tables and indexesinnobackupex: from original InnoDB data directory ‘/home/mysql/3306/data‘innobackupex: Backing up as tar stream ‘ibdata1‘>> log scanned up to (2371741708)>> log scanned up to (2371742105)>> log scanned up to (2371742105)innobackupex: Backing up file ‘/home/mysql/3306/data/test/t.ibd‘>> log scanned up to (2371742115)innobackupex: Backing up files ‘/home/mysql/3306/data/banping/*.ibd‘ (16 files)......110701 03:29:35  innobackupex: Connected to database with mysql child process (pid=22630)>> log scanned up to (2371742526)110701 03:29:39  innobackupex: Starting to lock all tables...>> log scanned up to (2371742526)>> log scanned up to (2371742526)110701 03:29:51  innobackupex: All tables locked and flushed to disk110701 03:29:51  innobackupex: Starting to backup .frm, .MRG, .MYD, .MYI,innobackupex: .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files ininnobackupex: subdirectories of ‘/home/mysql/3306/data‘innobackupex: Backing up files ‘/home/mysql/3306/data/banping/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (17 files)innobackupex: Backing up file ‘/home/mysql/3306/data/test/t.frm‘......110701 03:29:53  innobackupex: Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt filesinnobackupex: Resuming ibbackupxtrabackup: The latest check point (for incremental): ‘2371742526‘>> log scanned up to (2371742526)xtrabackup: Transaction log of lsn (2371741708) to (2371742526) was copied.110701 03:29:55  innobackupex: All tables unlocked110701 03:29:55  innobackupex: Connection to database server closedinnobackupex: Backup created in directory ‘/home/mysql/backup/data/3306‘innobackupex: MySQL binlog position: filename ‘bin.000014‘, position 309836330 mysql,information_schema,performance_schemainnobackupex: MySQL slave binlog position: master host ‘‘, filename ‘‘, positioninnobackupex: You must use -i (--ignore-zeros) option for extraction of the tar stream.110701 03:29:55  innobackupex: completed OK!

 

解析XtraBackup備份MySQL的原理和過程(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.