## モジュール 1: Backup & Restore の展開およびデータ準備

### 手順 1. 仮想マシンにログイン

+ ここをクリック - [ターミナル 1](ssh://monitor1-ssh#1) で、ハンズオン演習用に割り当てられた仮想マシンに接続された新しいターミナルを開きます。

### 手順 2. `Backup & Restore` をインストール

  ```bash
  source .bash_profile
  ```

  ```bash
  tiup install br:v7.5.1
  ```

### 手順 3. データを抽出

  ```bash
  unzip -u stage/tidb-admin-dataset-v2.zip -d stage/
  ```

  ```output
  Archive:  stage/tidb-admin-dataset-v2.zip
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP} unzip -u stage/tidb-admin-dataset-v2.zip -d stage/
  ```

### 手順 4. ファイル `solution-lightning-init.toml` を読む

- `solution-lightning-init.toml` ファイルは、TiDB Lightning のデータインポート設定を行います。主な内容は以下の通りです:
  - ログレベルとログファイルの保存場所
  - TiKV Importer の設定（インクリメンタルインポート、バックエンド種別、ソート済みKVディレクトリなど）
  - Mydumper の設定（データソースディレクトリや、システムスキーマを除外するテーブルフィルタなど）
  - `[tidb]` セクションには、タスクの接続情報（ホスト、ポート、インポートタスク用ユーザー、パスワード、ステータスポート、PDアドレスなど）が含まれています。

  ```bash
  cat solution-lightning-init.toml
  ```

  ```output
  [lightning]
  level = "info"
  file = "tidb-lightning.log"

  [tikv-importer]
  incremental-import = true
  backend = "local"
  sorted-kv-dir = "/tmp/sorted-kv-dir"

  [mydumper]
  data-source-dir = "/home/ec2-user/stage/TiDB-Administration-exercise-data-v2/exercise-br"
  filter = ['*.*', '!mysql.*', '!sys.*', '!INFORMATION_SCHEMA.*', '!PERFORMANCE_SCHEMA.*', '!METRICS_SCHEMA.*', '!INSPECTION_SCHEMA.*']

  [tidb]
  host = "10.90.2.124"
  port = 4000
  user = "ltask"
  password = "q1w2e3R4_"
  status-port = 10080
  pd-addr = "10.90.2.84:2379"
  ```

### 手順 5. TiDB に接続し、`emp` データベースを削除

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root
  ```

  ```sql
  DROP DATABASE IF EXISTS emp;
  ```

  ```output
  mysql> DROP DATABASE IF EXISTS emp;
  Query OK, 0 rows affected (0.51 sec)
  ```

### 手順 6. 必要に応じて lightning タスク用のユーザーを作成

+ インポートタスク用ユーザー設定のため、`ltask@'%'` ユーザーが存在するか確認します。

```sql
SELECT user FROM mysql.user;
```

```output
+-------+
| user  |
+-------+
| ltask |
| root  |
+-------+
2 rows in set (0.00 sec)
```

+ `ltask@'%'` が存在しない場合、`ltask@'%'` を作成し、すべての権限を付与します。

```sql
CREATE USER ltask@'%' IDENTIFIED BY 'q1w2e3R4_';
```

```output
mysql> CREATE USER ltask@'%' IDENTIFIED BY 'q1w2e3R4_';
Query OK, 0 rows affected (0.05 sec)
```

```sql
GRANT ALL PRIVILEGES ON *.* TO ltask@'%';
```

```output
mysql> GRANT ALL PRIVILEGES ON *.* TO ltask@'%';
Query OK, 0 rows affected (0.05 sec)
```

```sql
EXIT;
```

```output
mysql> EXIT;
```

### 手順 7. TiDB にデータをインポート

  ```bash
  nohup tiup tidb-lightning:v7.5.1 -config solution-lightning-init.toml > nohup.out &
  ```

  ```output
  ...
  [1]+  Done                    nohup tiup tidb-lightning:v7.5.1 -config solution-lightning-init.toml > nohup.out
  ```

### 手順 8. `nohup.out` ファイルの内容を表示

  ```bash
  cat nohup.out
  ```

  ```output
  Verbose debug logs will be written to tidb-lightning.log

  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  # | CHECK ITEM                                                                                                             | TYPE        | PASSED |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  1 | Source csv files size is proper                                                                                        | performance | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  2 | checkpoints are valid                                                                                                  | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  3 | table schemas are valid                                                                                                | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  4 | Cluster is available                                                                                                   | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  5 | Lightning has the correct storage permission                                                                           | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  6 | sorted-kv-dir:/tmp/sorted-kv-dir and data-source-dir:/root/sample/init are in the same disk, may slow down performance | performance | false  |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  7 | local disk resources are rich, estimate sorted data size 343.6MiB, local available is 6.459GiB                         | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  8 | Cluster available is rich, available is 55.03GiB, we need 1.007GiB                                                     | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  9 | Cluster doesn't have too many empty regions                                                                            | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  | 10 | Cluster region distribution is balanced                                                                                | critical    | true   |
  +----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+

  tidb lightning exit successfully
  ```
