## Module 2: Backup TiDB Cluster to TiKV Local Storage

### Step 1. Create directories on TiKV nodes

- Execute the following commands to create a folder on each TiKV node to store the backup data. Answer `yes` when prompted.

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP} mkdir /tmp/backup
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV2_PRIVATE_IP} mkdir /tmp/backup
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV3_PRIVATE_IP} mkdir /tmp/backup
  ```

### Step 2. Backup TiDB

- Execute the backup command. The parameters are explained as follows:
  - `--pd "${HOST_PD1_PRIVATE_IP}:2379"`: The IP address of the PD node.
  - `--storage "local:///tmp/backup"`: The path where the backup files are stored.
  - `--ratelimit 128`: The maximum speed per TiKV performing backup tasks (in MiB/s).
  - `--log-file backupfull.log`: The target file for BR logging.

  ```bash
  tiup br:v7.5.1 backup full --pd "${HOST_PD1_PRIVATE_IP}:2379" --storage "local:///tmp/backup" --ratelimit 128 --log-file backupfull.log
  ```

  ```output
  Detail BR log in backupfull.log
  Full Backup <----------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  Checksum <-------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  [2022/06/25 18:39:20.183 +08:00] [INFO] [collector.go:69] ["Full Backup success summary"] [total-ranges=36] [ranges-succeed=36] [ranges-failed=0] [backup-checksum=951.052235ms] [backup-fast-checksum=4.138427ms] [backup-total-ranges=73] [backup-total-regions=78] [total-take=4.187101891s] [BackupTS=434150717785899011] [total-kv=5999621] [total-kv-size=322.9MB] [average-speed=77.11MB/s] [backup-data-size(after-compressed)=65.71MB] [Size=65706363]
  ```

+ Check the task log file - `backupfull.log`.

  ```bash
  cat backupfull.log
  ```

  ```output
  [2022/06/25 18:39:15.996 +08:00] [INFO] [info.go:49] ["Welcome to Backup & Restore (BR)"] [release-version=v6.1.0] [git-hash=1a89decdb192cbdce6a7b0020d71128bc964d30f] [git-branch=heads/refs/tags/v6.1.0] [go-version=go1.18.2] [utc-build-time="2022-06-05 05:09:12"] [race-enabled=false]
  [2022/06/25 18:39:15.996 +08:00] [INFO] [common.go:692] [arguments] [__command="br backup full"]
  ...
  [2022/06/25 18:39:20.183 +08:00] [INFO] [collector.go:70] ["Full Backup success summary"] [total-ranges=36] [ranges-succeed=36] [ranges-failed=0] [backup-checksum=951.052235ms] [backup-fast-checksum=4.138427ms] [backup-total-ranges=73] [backup-total-regions=78] [total-take=4.187101891s] [BackupTS=434150717785899011] [total-kv=5999621] [total-kv-size=322.9MB] [average-speed=77.11MB/s] [backup-data-size(after-compressed)=65.71MB] [Size=65706363]
  ```

### Step 3. Check the output files

+ KV1 is used as an example here.

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP} find /tmp/backup/
  ```

  ```output
  /tmp/backup/
  /tmp/backup/1
  /tmp/backup/1/38_19_8dd75043152f9a7a07ef9bc0b24b629d464621806e3c93219cac918ec3d5b346_1670169544111_write.sst
  ...
  /tmp/backup/1/147_51_a005fa70db819be4f64dc51d7e50f9a5c5694304295cf5f41b0b7f7df19b5e0e_1670169550220_write.sst
  /tmp/backup/1/147_51_82164855cb8e661a0ef5d566634576dd53a3dd7cd3954478f03955a43c453a63_1670169550222_write.sst
  ```

### Step 4. Check the metadata of Backup & Restore

  ```bash
  ls /tmp/backup/
  ```

  ```output
  backup.lock  backupmeta  checkpoint.meta  checkpoints
  ```
