## 模块 1：部署 Backup & Restore 工具并准备数据

### 步骤 1：登录远程 Linux

+ 点击这里 - [终端 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 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
  ```
