MySQL——全量、増分バックアップと回復(理論+実操)


データバックアップの重要性
1、生産環境において、データの安全性は極めて重要であり、いかなるデータの損失も重大な結果をもたらす可能性がある.
2、データ損失の原因
  • プログラムエラー
  • 人為エラー
  • コンピュータ障害
  • ディスク障害
  • 災害と窃盗
  • データベース・バックアップの分類
    物理と論理の観点から、バックアップは次のように分けられます.
    1、物理バックアップ:データベースオペレーティングシステムの物理ファイル(例えばデータファイル、ログファイルなど)のバックアップ
    物理バックアップは、オフラインバックアップ(コールドバックアップ)とオンラインバックアップ(ホットバックアップ)に分けられます.
    コールド・バックアップ:データベースを閉じるときのホット・バックアップです.データベースは実行中です.このバックアップ・メソッドは、データベースのログ・ファイルに依存します.
    2、論理バックアップ:データベースの論理コンポーネント(例えば表などのデータベースオブジェクト)のバックアップ
    データベースのバックアップポリシーの観点から、バックアップは次のように分けられます.
    ≪フル・バックアップ|Full Backup|oem_src≫:データをフル・バックアップするたびに
    差分バックアップ:前回の完全バックアップ以降に変更されたファイルをバックアップします.
    インクリメンタルバックアップ:前回のフルバックアップまたはインクリメンタルバックアップ後に変更されたファイルはバックアップされます.
    注意:差異は増分と相補的である
    MySQLフルバックアップ
    1、完全バックアップはデータベース全体のバックアップ、データベース構造とファイル構造のバックアップである2、完全バックアップはバックアップ完了時刻のデータベースである3、完全バックアップはインクリメンタルバックアップの基礎である
    (1)完全バックアップのメリット
    ●バックアップとリカバリ操作が簡単で便利
    (2)完全バックアップの欠点
    ●データ重複が多い●バックアップスペースが多くかかる●バックアップとリカバリ時間が長い
    mysqldumpバックアップライブラリ
    1、MySQL               
    
    ●          , /usr/local/mysql/data
    ●        mysqldump
    
    2、mysqldump  
    
    ●MySQL       ,     MySQL    
    ●              、         SQL  ,             
    
    3、mysqldump            
    
    mysqldump -u   -p [  ] [  ] [    ] > /    /     
    
          
    mysqldump -u root -p auth > /backup/auth.sql
    mysqldump -u root -p mysql > /bakcup/mysql.sql
    
    4、mysqldump            
    
    mysqldump -u     -p [  ] [  ] --databases   1 [  2]... >/    /     
    
          
    mysqldump -u root -p --databases autth mysql > /backup/databases-auth-mysql.sql
    
    5、          
    
    mysqldump -u   -p [  ] [  ] --all-databases > /    /     
    
           
    mysqldump -u root -p --opt --all-databases > /backup/all-data.sql

    mysqldumpバックアップテーブル
    1、        ,             ,  mysqldump        
    
    2、  mysqldump      
    mysqldump -u     -p [  ] [  ]         > /    /     
    
          
    mysqldump -u root -p mysql user > /backup/mysql-user.sql
    
    3、  mysqldump        
    mysqldump -u     -p [  ] [  ] -d         > /    /     
    
            
    mysqldump -u root -p mysql -d user > /backup/mysql-user.sql

    データベースのリカバリ
    1、  mysqldump     SQL    ,                 
    ●source                    
    ●mysq|             Linux     
    
    2、  source        
    ●   MySQL   
    ●  source  sq|     (    )
    
    source     
    MySQL [(none)] > source /backup/all-data.sql
    
    3、  mysq|      
    mysql -u   -p [  ] 

    リカバリ・テーブルの操作
    1、          source  mysql    
    
    2、source               
    
    3、             ,           ,      ,        
    mysql -u   -p [  ] <         
    mysql -u root -p mysql < /backup/mysql-user.sql
    
    4、      ,    shell          

    MySQLバックアップの考え方
    1、定期的にバックアップを実施し、バックアップ計画或いは策略を制定し、厳格に守る
    2、完全バックアップのほか、MySQLサーバーのログ機能をオンにすることが重要
    ●完全バックアップとログによるMySQLの最大リストア
    MySQL-bin:MySQLデータベースのバイナリ・ログ.ユーザーがデータベースに対して操作するSQL文(データ問合せ文を除く)の情報を記録します.mysqlbinコマンドを使用してバイナリ・ログの内容を表示できます.
    3、統一的で分かりやすいバックアップファイル名を使用する
    ●backup 1、backup 2といった意味のない名前は使わない●ライブラリ名やテーブル名に時間を加えたネーミングルールがおすすめ
    MySQLインクリメンタルバックアップ
    1、mysqldumpによる完全バックアップの問題
    ●バックアップデータに重複データがある●バックアップ時間とリカバリ時間が長い
    2、インクリメンタルバックアップとは、前回のバックアップ以降増加または変化したファイルまたは内容をバックアップすることである
    3、増分バックアップの特徴
    ●重複データがなく、バックアップ量が少なく、時間が短い●リカバリが面倒:前回の完全バックアップと完全バックアップ後のすべての増分バックアップが必要で、しかもすべての増分バックアップに対して逐次バックアップを行う
    4、MySQLは直接の増分バックアップ方法を提供していない
    5、MySQLが提供するバイナリログ(binary logs)で間接的に増分バックアップを実現できる
    6、MySQLバイナリログのバックアップに対する意義
    ●バイナリ・ログには、すべての更新またはデータベースの更新が可能なアクションが保存されています●バイナリ・ログは、MySQLサーバの起動後に記録を開始し、ファイルがmax_に達したときに記録を開始します.binlog_sizeが設定したサイズまたはflush logsコマンドを受信してから新しいログファイルを再作成●flush logsメソッドをタイミングよく実行して新しいログを再作成し、バイナリファイルシーケンスを生成し、古いログを安全な場所に保存するだけで1つの時間帯のインクリメンタルバックアップを完了
    7、一般的なリカバリ追加データ——完全バックアップを行う——新しいデータを入力する——増分バックアップを行う——模擬故障——リカバリ操作
    8、位置に基づく返信とは、ある開始時間のバイナリログをデータベースにインポートし、エラーが発生した時点をスキップしてデータの回復を実現することである.
    9、ポイント・ベースのリカバリポイント・ベースのリカバリを使用すると、1つのポイントに正しい操作と誤った操作が同時に存在する可能性があります.そのため、より正確なリカバリ方法が必要です.
    10、増分回復の方法
    (1)一般復旧
    mysqlbinlog[--no-defaults]インクリメンタルバックアップファイル|mysql-uユーザー名-p(2)位置に基づくリカバリ●指定位置mysqlbinlog--stop-position=操作'id'1進ログ|mysql-uユーザー名-pパスワード●指定された位置からデータをリカバリmysqlbinlog--start-position=操作'id'バイナリログ|mysql-uユーザー名-pパスワード
    (3)時点ベースのリカバリ
    ●ログの先頭からある時点までのリカバリmysqlbinlog[--no-defaults]--stop-datetime='年-月-日時間:分:秒'バイナリログ|mysql-uユーザー名-pパスワード●ある時点からログの末尾までのリカバリmysqlbinlog[--no defaults]--start-datetime='年-月-日時間:分:秒'バイナリログ|mysql-uユーザー名-pパスワード●ある時点からある時点までの回復mysqlbinlog[--no defaults]--start-datetime='年-月-日時間:分:秒'--stop-datetime='年-月-日時間:分:秒'バイナリログ|mysql-uユーザー名-pパスワード
    バイナリログファイル(復号)mysqlbinlog--no-defaults--base 64-output=decode-rows-V mysql-binを表示します.000002 >/opt/bak. txt
    フル・バックアップ/リカバリ
    1、データベースに入り、テーブルを作成し、テーブルデータを挿入する
    [root@master2 ~]# mysql -uroot -p   ##     
    Enter password: 
    
    mysql> create database school;    ##     
    Query OK, 1 row affected (0.01 sec)
    
    mysql> use school;   ##     
    Database changed
    mysql> create table info(    ##   
            -> id int(3) not null primary key auto_increment,
            -> name varchar(10) not null,
            -> score decimal(4,1) not null);
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> desc info;   ##     
    +-------+--------------+------+-----+---------+----------------+
    | Field | Type         | Null | Key | Default | Extra          |
    +-------+--------------+------+-----+---------+----------------+
    | id    | int(3)       | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(10)  | NO   |     | NULL    |                |
    | score | decimal(4,1) | NO   |     | NULL    |                |
    +-------+--------------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)
    
    mysql> insert into info (name,score) values ('stu01',88),('stu02',77);   ##     
    Query OK, 2 rows affected (0.02 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> select * from info;   ##     
    +----+-------+-------+
    | id | name  | score |
    +----+-------+-------+
    |  1 | stu01 |  88.0 |
    |  2 | stu02 |  77.0 |
    +----+-------+-------+
    2 rows in set (0.01 sec)
    
    mysql> select * from info limit 1;   ##       1 
    +----+-------+-------+
    | id | name  | score |
    +----+-------+-------+
    |  1 | stu01 |  88.0 |
    +----+-------+-------+
    1 row in set (0.00 sec)

    2.データベースの物理的完全バックアップ
    [root@master2 ~]# cd /usr/local/mysql/data/   ##            
    [root@master2 data]# ls
    auto.cnf        ibdata1      ib_logfile1  mysql               school  test
    ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  sys
    [root@master2 data]# cd school/
    [root@master2 school]# ls   ##      
    db.opt  info.frm  info.ibd
    [root@master2 school]# cd ..
    [root@master2 data]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/ ## xz    
    [root@master2 data]# cd /opt/
    [root@master2 opt]# ls
    mysql-2019-11-26.tar.xz  mysql-5.7.20  rh

    3.単一データベースの論理的バックアップ
    [root@master2 opt]# mysqldump -uroot -p school > /opt/school.sql  ##         
    Enter password: 
    [root@master2 opt]# ls
    mysql-2019-11-26.tar.xz  mysql-5.7.20  rh  school.sql
    [root@master2 opt]# vim school.sql  ##         
    
    ...
    CREATE TABLE `info` (
        `id` int(3) NOT NULL AUTO_INCREMENT,
        `name` varchar(10) NOT NULL,
        `score` decimal(4,1) NOT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    ...
    LOCK TABLES `info` WRITE;
    /*!40000 ALTER TABLE `info` DISABLE KEYS */;
    INSERT INTO `info` VALUES (1,'stu01',88.0),(2,'stu02',77.0);

    4.複数のデータベースのバックアップ
    [root@master2 opt]# mysqldump -uroot -p --databases school mysql > /opt/db_school_mysql.sql
    ##       
    Enter password: 
    [root@master2 opt]# ls
    db_school_mysql.sql  mysql-2019-11-26.tar.xz  mysql-5.7.20  rh  school.sql

    5、データベースの完全バックアップ
    [root@master2 opt]# mysqldump -uroot -p --opt --all-databases > /opt/all.sql ##    
    Enter password: 
    [root@master2 opt]# ls
    all.sql              mysql-2019-11-26.tar.xz  rh
    db_school_mysql.sql  mysql-5.7.20             school.sql

    6データベース内のテーブルのバックアップ
    [root@master2 opt]# mysqldump -uroot -p school info > /opt/school_info.sql ##           
    Enter password: 
    [root@master2 opt]# ls
    all.sql              mysql-2019-11-26.tar.xz  rh               school.sql
    db_school_mysql.sql  mysql-5.7.20             school_info.sql

    7.データベース内のテーブル構造のバックアップ
    [root@master2 opt]# mysqldump -uroot -p -d school info > /opt/school_info_desc.sql  
    ##        
    Enter password: 
    [root@master2 opt]# ls
    all.sql                  mysql-5.7.20          school_info.sql
    db_school_mysql.sql      rh                    school.sql
    mysql-2019-11-26.tar.xz  school_info_desc.sql

    8、スクリプト・ベースのデータベースのリカバリ
    [root@master2 opt]# mysql -uroot -p     ##     
    Enter password: 
    
    mysql> show databases;   ##     
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | school             |
    | sys                |
    | test               |
    +--------------------+
    6 rows in set (0.00 sec)
    
    mysql> use school;    ##     
    Database changed
    
    mysql> show tables;   ##   
    +------------------+
    | Tables_in_school |
    +------------------+
    | info             |
    +------------------+
    1 row in set (0.00 sec)
    
    mysql> drop table info;    ##   
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> show tables;        ###   
    Empty set (0.00 sec)
    
    mysql> source /opt/school.sql  ##         
    
    mysql> show tables;    ##   
    +------------------+
    | Tables_in_school |
    +------------------+
    | info             |
    +------------------+
    1 row in set (0.00 sec)

    9外部MySQLコマンドに基づいてデータベースをリカバリ
    mysql> drop table info;   ##   
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> show tables;   ##   
    Empty set (0.00 sec)
    
    mysql> quit   ##  
    Bye
    [root@master2 opt]# mysql -uroot -p123123 school < /opt/school.sql   ##  mysql      
    mysql: [Warning] Using a password on the command line interface can be insecure.
    [root@master2 opt]# mysql -uroot -p123123    ##     
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> use school;    ##     
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> show tables;   ##   
    +------------------+
    | Tables_in_school |
    +------------------+
    | info             |
    +------------------+
    1 row in set (0.00 sec)

    MySQLインクリメンタルバックアップおよびリカバリ
    1、バイナリログファイルを開く
    [root@master2 opt]# vim /etc/my.cnf  ##         
    [mysqld]
    user = mysql
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    port = 3306
    character_set_server=utf8
    pid-file = /usr/local/mysql/mysql.pid
    socket = /usr/local/mysql/mysql.sock
    log-bin=mysql-bin   ##         
    server-id = 1
    [root@master2 opt]# systemctl restart mysqld.service   ##  mysql  
    [root@master2 opt]# cd /usr/local/mysql/data/   ##   mysql  
    [root@master2 data]# ls     ##         
    auto.cnf        ib_logfile0  mysql             performance_schema  test
    ib_buffer_pool  ib_logfile1  mysql-bin.000001  school
    ibdata1         ibtmp1       mysql-bin.index   sys

    2、フルバックアップ
    [root@master2 data]# mysqldump -uroot -p123123 school > /opt/school.sql ##      
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    [root@master2 data]# ls
    auto.cnf        ib_logfile0  mysql             performance_schema  test
    ib_buffer_pool  ib_logfile1  mysql-bin.000001  school
    ibdata1         ibtmp1       mysql-bin.index   sys
    [root@master2 data]# mysqladmin -uroot -p123123 flush-logs  ##         
    mysqladmin: [Warning] Using a password on the command line interface can be insecure.
    [root@master2 data]# ls     ##           ,          mysql-bin.000002 
    auto.cnf        ib_logfile0  mysql             mysql-bin.index     sys
    ib_buffer_pool  ib_logfile1  mysql-bin.000001  performance_schema  test
    ibdata1         ibtmp1       mysql-bin.000002  school

    3、データベースに入り、誤操作を模擬する
    [root@master2 data]# mysql -uroot -p123123  ##     
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> use school;    ##     
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> select * from info;  ##   
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    +----+------+-------+
    2 rows in set (0.00 sec)
    
    mysql> insert into info (name,score) values ('by01',66);   ##    
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from info;
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    |  3 | by01 |  66.0 |
    +----+------+-------+
    3 rows in set (0.00 sec)
    
    mysql> delete from info where name='st01';   ##    
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into info (name,score) values ('by02',99); ##    
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from info;
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  2 | st02 |  77.0 |
    |  3 | by01 |  66.0 |
    |  4 | by02 |  99.0 |
    +----+------+-------+
    3 rows in set (0.00 sec)
    
    [root@master2 data]# mysqladmin -uroot -p123123 flush-logs    ##                 
    mysqladmin: [Warning] Using a password on the command line interface can be insecure.
    [root@master2 data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 > /opt/bak.txt
    ## 64             ,       
    [root@master2 data]# cd /opt/
    [root@master2 opt]# ls
    bak.txt  mysql-5.7.20  rh  school.sql
    [root@master2 opt]# vim bak.txt    ##         
    
    # at 1084
    #191127 20:14:01 server id 1  end_log_pos 1132 CRC32 0xdcc90eb5         Write_rows: table id 221 flags: STMT_END_F
    ### INSERT INTO `school`.`info`   ##             
    ### SET
    ###   @1=3
    ###   @2='by01'
    ###   @3=66.0
    ...
    # at 1302   ##     
    #191127 20:14:46 server id 1  end_log_pos 1357 CRC32 0x6648509a         Table_map: `school`.`info` mapped to number 221
    # at 1357
    #191127 20:14:46 server id 1  end_log_pos 1405 CRC32 0x1eeb752b         Delete_rows: table id 221 flags: STMT_END_F
    ### DELETE FROM `school`.`info`   ##               191127 20:14:46
    ### WHERE
    ###   @1=1
    ###   @2='st01'
    ###   @3=88.0
    # at 1405  ##     
    #191127 20:14:46 server id 1  end_log_pos 1436 CRC32 0xf1c8d903         Xid = 54
    ...
    # at 1630
    #191127 20:15:16 server id 1  end_log_pos 1678 CRC32 0x08d9b0f4         Write_rows: table id 221 flags: STMT_END_F
    ### INSERT INTO `school`.`info`   ##             191127 20:15:16
    ### SET
    ###   @1=4
    ###   @2='by02'
    ###   @3=99.0

    4、時点によるブレークポイント復旧
    [root@master2 opt]# mysql -uroot -p123123   ##     
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> use school;  ##     
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> drop table info;   ##     
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> select * from info;   ##   
    ERROR 1146 (42S02): Table 'school.info' doesn't exist
    mysql> source /opt/school.sql   ##           
    ...
    mysql> show tables;   ##   
    +------------------+
    | Tables_in_school |
    +------------------+
    | info             |
    +------------------+
    1 row in set (0.00 sec)
    
    mysql> select * from info;  ##     
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    +----+------+-------+
    2 rows in set (0.00 sec)
    
    [root@master2 opt]# mysqlbinlog --no-defaults --stop-datetime='2019-11-27 20:14:46' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123
    ##  bin.000002           (             )
    mysql: [Warning] Using a password on the command line interface can be insecure.
    [root@master2 opt]# mysql -uroot -p123123   ##     
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> use school;  ##     
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> select * from info;   ##     ,          
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    |  3 | by01 |  66.0 |
    +----+------+-------+
    3 rows in set (0.00 sec)
    
    [root@master2 opt]# mysqlbinlog --no-defaults --start-datetime='2019-11-27 20:15:16' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123   
    ##      ,           (               )
    mysql: [Warning] Using a password on the command line interface can be insecure.
    [root@master2 opt]# mysql -uroot -p123123  ##     
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> use school;   ##     
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> select * from info;    ##     ,          ,        
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    |  3 | by01 |  66.0 |
    |  4 | by02 |  99.0 |
    +----+------+-------+
    4 rows in set (0.00 sec)

    5、位置点によるブレークポイント回復
    mysql> delete from info where name='by01';    ##         
    Query OK, 1 row affected (0.01 sec)
    
    mysql> delete from info where name='by02';    ##  
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from info;  ##         
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    +----+------+-------+
    2 rows in set (0.00 sec)
    
    mysql> quit
    Bye
    
    [root@master2 opt]# mysqlbinlog --no-defaults --stop-position='1302' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123                           
    ##                   
    [root@master2 opt]# mysql -uroot -p123123   ##     
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> use school;    ##     
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> select * from info;   ##     ,           
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    |  3 | by01 |  66.0 |
    +----+------+-------+
    3 rows in set (0.00 sec)
    
    mysql> quit
    Bye
    
    [root@master2 opt]# mysqlbinlog --no-defaults --start-position='1405' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123   
    ##              ,          
    [root@master2 opt]# mysql -uroot -p123123   ##     
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> use school;   ##     
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> select * from info;    ##     ,      ,           
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | st01 |  88.0 |
    |  2 | st02 |  77.0 |
    |  3 | by01 |  66.0 |
    |  4 | by02 |  99.0 |
    +----+------+-------+
    4 rows in set (0.00 sec)

    6:インクリメンタルバックアップのすべてのリカバリ
    [root@master2 opt]# mysqlbinlog --no-defaults  /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123
    ##