# **练习-1 : 部署备份恢复工具 `BR` 并初始化数据**

## 概述
+ 在这个练习中,我们将部署备份恢复工具 BR 并初始化数据,以为后面的 3 个练习使用｡

## 实验环境要求
+ 1. 注意，您的实验环境（包括IP，端口号，用户名，密码和目录）可能和手册不同。
+ 2. 可以连接到实验用虚拟机。
+ 3. TiDB 数据库提前部署完毕。
+ 4. 清理掉之前的实验数据。


## 步骤

-----------------------------------------------
#### 1. BR 工具集成在 tidb-toolkit 中,先进行下载安装:
+ **请从网址：https://pingcap.com/zh/product-community/ 中下载最新的 TiDB-community-toolkit 软件包，并上传到练习环境中。**  

+ **解压下载的软件包**：
    ```
     # tar xvf tidb-community-toolkit-v6.1.0-linux-amd64.tar
    ```

+ **解压　BR　工具**
    ```
     # cd tidb-community-toolkit-v6.1.0-linux-amd64
     # tar xvf br-v6.1.0-linux-amd64.tar.gz
    ```
    ```
     br
    ```

+ **确认安装完毕:**
     ```
     # ls br*
    ``` 
    ```
     br  br-v6.1.0-linux-amd64.tar.gz
    ``` 

+ **也可以将目录加入环境变量,方便后续执行:** 
    ```
     # cd
     # vi .bash_profile
    ``` 
    ```
     PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/root/tidb-community-toolkit-v6.1.0
    ``` 
-----------------------------------------------
#### 2. TiDB Lightning 工具集成在 tidb-toolkit 中，进行安装准备导入数据:

+ **解压 TiDB Lightning 工具**
    ```
     # cd tidb-community-toolkit-v6.1.0-linux-amd64
     # tar xvf tidb-lightning-v6.1.0-linux-amd64.tar.gz
    ```
    ```
     tidb-lightning
    ```

+ **确认安装完毕:**
     ```
     # ls tidb-lightning*
    ``` 
    ```
     tidb-lightning  tidb-lightning-ctl  tidb-lightning-v6.1.0-linux-amd64.tar.gz
    ``` 
-----------------------------------------------
#### 3. 编辑 TiDB Lightning 工具的配置文件:
+ **编辑配置文件 tidb-lightning-init.toml，如下:**
    ```
    # vi tidb-lightning-init.toml
    ```
-----------------------------------------------
#### 4. 连接到目标 `TiDB` ，准备并检查导入数据前的 `TiDB` 数据库:(请根据自己实验环境输入密码)
+ **连接到目标 `TiDB` 数据库，并清理旧的练习数据，如下:**
    ```sql
    # mysql -h127.0.0.1 -P4000 -uroot -ptidb
    mysql> drop database emp;
    ```
-----------------------------------------------
#### 5. 使用 TiDB Lightning 工具进行数据导入:
+ **启动 TiDB Lightning，如下:**
    ```
      # ! /bin/bash
      # nohup tidb-lightning -config tidb-lightning-init.toml > nohup.out &
    ```

+ **TiDB Lightning 导入完毕后，会出现如下字样:**
    ```
      [1]+  Done                    nohup tidb-lightning -config tidb-lightning-init.toml > nohup.out
    ```
-----------------------------------------------
#### 6. 检查数据导入结果:
+ **检查nohup日志，确认 TiDB Lightning 正常退出，如下:**
    ```
    # vi nohup.out
    ```
+ **连接到目标 `TiDB` 数据库，查看 emp 库中的数据，如下:**
    ```sql
    # mysql -h127.0.0.1 -P4000 -uroot -ptidb
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | INFORMATION_SCHEMA |
    | METRICS_SCHEMA     |
    | PERFORMANCE_SCHEMA |
    | emp                |
    | mysql              |
    | test               |
    | universe           |
    +--------------------+
    7 rows in set (0.01 sec)
    ```
-----------------------------------------------

## 输出样例

-----------------------------------------------
#### 步骤1-解压下载的软件包输出参考：
```
    # tar xvf tidb-community-toolkit-v6.1.0-linux-amd64.tar
```
```
tidb-community-toolkit-v6.1.0-linux-amd64/
tidb-community-toolkit-v6.1.0-linux-amd64/reparo
tidb-community-toolkit-v6.1.0-linux-amd64/2225.cdc.json
tidb-community-toolkit-v6.1.0-linux-amd64/9.errdoc.json
tidb-community-toolkit-v6.1.0-linux-amd64/805.dm-master.json
tidb-community-toolkit-v6.1.0-linux-amd64/2.spark.json
tidb-community-toolkit-v6.1.0-linux-amd64/1.root.json
tidb-community-toolkit-v6.1.0-linux-amd64/213.tiup.json
...
tidb-community-toolkit-v6.1.0-linux-amd64/dmctl-v6.1.0-linux-amd64.tar.gz
tidb-community-toolkit-v6.1.0-linux-amd64/root.json
```

-----------------------------------------------
#### 步骤3-编辑 TiDB Lightning 工具的配置文件输出参考：
```
# vi tidb-lightning-init.toml
```
```
[lightning]

# 转换数据的并发数，默认为逻辑 CPU 数量，不需要配置。
# 混合部署的情况下可以配置为逻辑 CPU 的 75% 大小。
# region-concurrency = 1

# 日志
level = "info"
file = "tidb-lightning.log"

[tikv-importer]
# 是否允许向已存在数据的表导入数据。默认值为 false。
# 当使用并行导入模式时，由于多个 TiDB Lightning 实例同时导入一张表，因此此开关必须设置为 true。
incremental-import = true
# backend 设置为 local 模式
backend = "local"
# 设置本地临时存储路径
sorted-kv-dir = "/tmp/sorted-kv-dir"

[mydumper]
# 源数据目录。
data-source-dir = "/root/sample/init"
# 配置通配符规则，默认规则会过滤 mysql、sys、INFORMATION_SCHEMA、PERFORMANCE_SCHEMA、METRICS_SCHEMA、INSPECTION_SCHEMA 系>统数据库下的所有表
# 若不配置该项，导入系统表时会出现“找不到 schema”的异常
filter = ['*.*', '!mysql.*', '!sys.*', '!INFORMATION_SCHEMA.*', '!PERFORMANCE_SCHEMA.*', '!METRICS_SCHEMA.*', '!INSPECTION_SCHEMA.*']

[tidb]
# 目标集群的信息。tidb-server 的监听地址，填一个即可。
host = "127.0.0.1"
port = 4000
user = "root"
password = "tidb"
# 表架构信息在从 TiDB 的“状态端口”获取。
status-port = 10080
# pd-server 的地址，填一个即可
pd-addr = "172.31.0.141:2379"
```

-----------------------------------------------
#### 步骤6-检查数据导入结果输出参考:

```
# vi nohup.out
```
```
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   |
+----+------------------------------------------------------------------------------------------------------------------------+-------------+--------+
|^[[33m  6 ^[[0m|^[[33m sorted-kv-dir:/tmp/sorted-kv-dir and data-source-dir:/root/sample/init are in the same disk, may slow down performance ^[[0m|^[[33m performance ^[[0m|^[[33m false  ^[[0m|
+^[[33m----^[[0m+^[[33m------------------------------------------------------------------------------------------------------------------------^[[0m+^[[33m-------------^[[0m+^[[33m--------^[[0m+
|  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
```
-----------------------------------------------