## Module 4: Backup and Restore a Table

### Step 1. Backup the `emp.sal` table

+ Use the `Backup & Restore` utility to back up the `sal` table in the `emp` database.
+ The parameters are explained as follows:
  + `--pd "${HOST_PD1_PRIVATE_IP}:2379"`：The IP address of the PD node.
  + `--storage "s3://pe-class-${LAB:REGION_NAME}/${LAB:USER_UNIQUE_TAG}/empsal"`：The S3 bucket and object prefix under where the backup files are stored.
  + `--db emp --table sal`：specifies the database name and the table name.
  + `--ratelimit 128`：The maximum speed per TiKV performing backup tasks (in MiB/s).
  + `--log-file backuptable.log`：`Backup & Restore` The target file for BR logging.

  ```bash
  source ~/hosts-env.sh
  tiup br:v7.5.1 backup table \
    --pd "${HOST_PD1_PRIVATE_IP}:2379" \
    --db emp \
    --table sal \
    --storage "s3://pe-class-${LAB:REGION_NAME}/${USER_UNIQUE_TAG}/empsal"  \
    --send-credentials-to-tikv=false \
    --ratelimit 128 \
    --log-file backuptable.log
  ```

  ```output
  Detail BR log in backuptable.log
  Table Backup <----------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  Checksum <--------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  [2022/06/25 20:25:08.432 +08:00] [INFO] [collector.go:69] ["Table Backup success summary"] [total-ranges=6] [ranges-succeed=6] [ranges-failed=0] [backup-checksum=963.74443ms] [backup-fast-checksum=219.535µs] [backup-total-ranges=2] [backup-total-regions=6] [total-take=4.664522392s] [Size=58300863] [BackupTS=434152381823320066] [total-kv=5688094] [total-kv-size=301MB] [average-speed=64.54MB/s] [backup-data-size(after-compressed)=58.3MB]
  ```

### Step 2. Drop the `emp.sal` table

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

  ```sql
  SELECT COUNT(*) FROM emp.sal;
  ```

  ```output
  mysql> SELECT COUNT(*) FROM emp.sal;
  +----------+
  | COUNT(*) |
  +----------+
  |  2844047 |
  +----------+
  1 row in set (0.00 sec)
  ```

  ```sql
  DROP TABLE emp.sal;
  ```

  ```output
  mysql> DROP TABLE emp.sal;
  ```

  ```sql
  SELECT COUNT(*) FROM emp.sal;
  ```

  ```output
  mysql> SELECT COUNT(*) FROM emp.sal;
  ERROR 1146 (42S02): Table 'emp.sal' doesn't exist
  ```

  ```sql
  EXIT;
  ```

### Step 3. Restore the `emp.sal` table

+ Use the `Backup & Restore` utility to restore the `emp.sal` table.

  ```bash
  source ~/hosts-env.sh
  tiup br:v7.5.1 restore table \
    --pd "${HOST_PD1_PRIVATE_IP}:2379" \
    --db "emp" \
    --table "sal" \
    --storage "s3://pe-class-${LAB:REGION_NAME}/${USER_UNIQUE_TAG}/empsal" \
    --send-credentials-to-tikv=false \
    --log-file restoretable.log
  ```

  ```output
  Detail BR log in restoretable.log
  Table Restore <--------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  [2022/06/25 20:39:21.447 +08:00] [INFO] [collector.go:69] ["Table Restore success summary"] [total-ranges=7] [ranges-succeed=7] [ranges-failed=0] [split-region=150.684µs] [restore-ranges=6] [total-take=3.206398749s] [restore-data-size(after-compressed)=58.3MB] [Size=58300863] [BackupTS=434152605812523049] [total-kv=5688094] [total-kv-size=301MB] [average-speed=93.89MB/s]
  ```

### Step 4. Check the restored `emp.sql` table

+ Connect to the TiDB and verify that the `sal` table in the `emp` database is restored

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

  ```bash
  SELECT COUNT(*) FROM emp.sal;
  ```

  ```output
  mysql> SELECT COUNT(*) FROM emp.sal;
  +----------+
  | COUNT(*) |
  +----------+
  |  2844047 |
  +----------+
  1 row in set (0.53 sec)
  ```
