定時帰る術-Linux xfsのバックアップとリストア（xfsdump、xfsrestoreコマンド）

xfsdump、xfsrestoreはよくOSバックアップに使われてる。
やり方は簡単（**適当**）に説明する。
①xfsdumpでバックアップをとる
②必要に応じて、増分バックアップも忘れず
③DVDでレスキューモードでサーバを起動する
④バックアップが格納されるメディアをマウントして、リストアする。
⑤grubを再設定
（やるのはもう何年前で、確かに必要な気がする）
（今回も検証してみようと思ったら、面倒くさいからやりたくない。。。興味がある方はgoogle先生に聞いてみればと思う）
(**m面倒くさいことは、一切やりません**)

xfsdumpは、レベル(0から9まで)が指定できる。（オプション：-l）
0：フルバックアップ
1：0の増分バックアップ
2：1の増分バックアップ

0→1→2→...みたい考え方
0→2はできない
↓で同じレベルが複数回のバックアップができる
0→1
 →1

### 20GBテスト用sdbを追加
```
[root@test ~]# fdisk -l /dev/sdb
ディスク /dev/sdb: 20 GiB, 21474836480 バイト, 41943040 セクタ
単位: セクタ (1 * 512 = 512 バイト)
セクタサイズ (論理 / 物理): 512 バイト / 512 バイト
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
```
### ラベル付きて、ファイルシステム作成
```
[root@test ~]# mkfs.xfs -L "snow-test" /dev/sdb
meta-data=/dev/sdb               isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
```
### マウントする
```
[root@test ~]# mkdir /var/tmp/snow
[root@test ~]# mount /dev/sdb /var/tmp/snow
[root@test ~]# df -h
ファイルシス          サイズ  使用  残り 使用% マウント位置
devtmpfs                1.9G     0  1.9G    0% /dev
tmpfs                   2.0G     0  2.0G    0% /dev/shm
tmpfs                   2.0G  8.5M  2.0G    1% /run
tmpfs                   2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/mapper/rhel-root    26G  5.6G   21G   22% /
/dev/sda1              1014M  184M  831M   19% /boot
tmpfs                   393M     0  393M    0% /run/user/0
/dev/sdb                 20G  175M   20G    1% /var/tmp/snow
```
### テスト用ファイルを作る
```
[root@test ~]# touch /var/tmp/snow/test1
[root@test ~]# ll /var/tmp/snow/test1
-rw-r--r-- 1 root root 0 10月 27 10:15 /var/tmp/snow/test1
```
### レベル0のxfsdumpファイルを作成
```
[root@test ~]# xfsdump -l 0 -L "sdb_bk_20231027" -M sdb_bk -f /var/tmp/snow.xfsdump /var/tmp/snow
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.8 (dump format 3.0) - type ^C for status and control
xfsdump: level 0 dump of test:/var/tmp/snow
xfsdump: dump date: Fri Oct 27 10:27:24 2023
xfsdump: session id: 3b1cc689-b8fc-4692-b99c-0e8cca00303a
xfsdump: session label: "sdb_bk_20231027"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 21120 bytes
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 21328 bytes
xfsdump: dump size (non-dir files) : 0 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /var/tmp/snow.xfsdump OK (success)
xfsdump: Dump Status: SUCCESS
```

### .xfsdumpファイルができた
```
[root@test ~]# ll /var/tmp/snow.xfsdump
-rw-r--r-- 1 root root 21328 10月 27 10:27 /var/tmp/snow.xfsdump
```
### 増分テストファイルを作る
```
[root@test ~]# touch /var/tmp/snow/test2
[root@test ~]# ll /var/tmp/snow/test2
-rw-r--r-- 1 root root 0 10月 27 10:32 /var/tmp/snow/test2
```
### レベル1まだやってないのに、レベル2をやるとエラーが出る
```
[root@test ~]# xfsdump -l 2 -L "sdb_bk_20231027_1" -M sdb_bk -f /var/tmp/snow.xfsdump /var/tmp/snow
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.8 (dump format 3.0) - type ^C for status and control
xfsdump: WARNING: most recent base for incremental dump was interrupted (level 1): must resume or redump at or below level 2
xfsdump: Dump Status: ERROR
```

### レベル0の増分バックアップをやる
```
[root@test ~]# xfsdump -l 1 -L "sdb_bk_20231027_1" -M sdb_bk -f /var/tmp/snow2.xfsdump /var/tmp/snow
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.8 (dump format 3.0) - type ^C for status and control
xfsdump: WARNING: most recent level 1 dump was interrupted, but not resuming that dump since resume (-R) option not specified
xfsdump: level 1 incremental dump of test:/var/tmp/snow based on level 0 dump begun Fri Oct 27 10:27:24 2023
xfsdump: dump date: Fri Oct 27 10:38:00 2023
xfsdump: session id: 96142f1e-cd1c-4561-94ef-67df8c2d6232
xfsdump: session label: "sdb_bk_20231027_1"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 21120 bytes
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 21352 bytes
xfsdump: dump size (non-dir files) : 0 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /var/tmp/snow2.xfsdump OK (success)
xfsdump: Dump Status: SUCCESS
```
### レベル1の.xfsdumpファイルができた
```
[root@test ~]# ll /var/tmp/*.xfsdump
-rw-r--r-- 1 root root 21328 10月 27 10:27 /var/tmp/snow.xfsdump
-rw-r--r-- 1 root root 21352 10月 27 10:38 /var/tmp/snow2.xfsdump
```


### 増分テストファイルを削除
```
[root@test ~]# rm -rf /var/tmp/snow/test2
[root@test ~]# ll /var/tmp/snow
合計 0
-rw-r--r-- 1 root root 0 10月 27 10:15 test1
```
### レベル1の.xfsdumpファイルからリストア
```
[root@test ~]# xfsrestore -f /var/tmp/snow2.xfsdump /var/tmp/snow
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.8 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: test
xfsrestore: mount point: /var/tmp/snow
xfsrestore: volume: /dev/sdb
xfsrestore: session time: Fri Oct 27 10:38:00 2023
xfsrestore: level: 1
xfsrestore: session label: "sdb_bk_20231027_1"
xfsrestore: media label: "sdb_bk"
xfsrestore: file system id: 2899d991-e28c-4356-8442-a58b8fe75cca
xfsrestore: session id: 96142f1e-cd1c-4561-94ef-67df8c2d6232
xfsrestore: media id: 80ae3ef9-6684-453a-a7e4-ed6deae24311
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 1 directories and 2 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /var/tmp/snow2.xfsdump OK (success)
xfsrestore: Restore Status: SUCCESS
```
### 結果確認
test2が戻ってきた
```
[root@test ~]# ll /var/tmp/snow
合計 0
-rw-r--r-- 1 root root 0 10月 27 10:15 test1
-rw-r--r-- 1 root root 0 10月 27 10:32 test2
```

### test1、test2を削除する。
```
[root@test ~]# rm -rf /var/tmp/snow/test*
[root@test ~]# ll /var/tmp/snow
合計 0
```
### もう一回レベル1の.xfsdumpファイルからリストア
```
[root@test ~]# xfsrestore -f /var/tmp/snow2.xfsdump /var/tmp/snow
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.8 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: test
xfsrestore: mount point: /var/tmp/snow
xfsrestore: volume: /dev/sdb
xfsrestore: session time: Fri Oct 27 10:38:00 2023
xfsrestore: level: 1
xfsrestore: session label: "sdb_bk_20231027_1"
xfsrestore: media label: "sdb_bk"
xfsrestore: file system id: 2899d991-e28c-4356-8442-a58b8fe75cca
xfsrestore: session id: 96142f1e-cd1c-4561-94ef-67df8c2d6232
xfsrestore: media id: 80ae3ef9-6684-453a-a7e4-ed6deae24311
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 1 directories and 2 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /var/tmp/snow2.xfsdump OK (success)
xfsrestore: Restore Status: SUCCESS
```
### 結果確認
test2が戻ってきたが、test1がなくなった
```
[root@test ~]#  ll /var/tmp/snow
合計 0
-rw-r--r-- 1 root root 0 10月 27 10:32 test2
```

### test2を削除
```
[root@test ~]# rm -rf /var/tmp/snow/test2
[root@test ~]# ll /var/tmp/snow
合計 0
```
### レベル0の.xfsdumpファイルからリストア
```
[root@test ~]# xfsrestore -f /var/tmp/snow.xfsdump /var/tmp/snow
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.8 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: test
xfsrestore: mount point: /var/tmp/snow
xfsrestore: volume: /dev/sdb
xfsrestore: session time: Fri Oct 27 10:27:24 2023
xfsrestore: level: 0
xfsrestore: session label: "sdb_bk_20231027"
xfsrestore: media label: "sdb_bk"
xfsrestore: file system id: 2899d991-e28c-4356-8442-a58b8fe75cca
xfsrestore: session id: 3b1cc689-b8fc-4692-b99c-0e8cca00303a
xfsrestore: media id: 7ea04ae9-8313-4b87-94a5-a4043e57d0d8
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 1 directories and 1 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /var/tmp/snow.xfsdump OK (success)
xfsrestore: Restore Status: SUCCESS
```
### 結果確認
test1が戻ってきた
```
[root@test ~]#  ll /var/tmp/snow
合計 0
-rw-r--r-- 1 root root 0 10月 27 10:15 test1
```