HDFS 快照管理

1. HDFS快照管理

快照相当于对目录做一个备份。并不会立即复制所有文件,而是记录文件变化

1.1. 开启快照功能

开启指定目录的快照功能

1
2
$ hdfs dfsadmin -allowSnapshot /input
Allowing snaphot on /input succeeded

1.2. 创建快照

-createSnapshot <snapshotDir> [<snapshotName>] 创建一个快照

1
2
$ hdfs dfs -createSnapshot /input
Created snapshot /input/.snapshot/s20190225-152050.714

默认是看不到隐藏文件的

1
2
3
4
5
6
7
8
9
10
11
12
$ hdfs dfs -ls -R /input     # 看不到.snapshot目录,实际是存在的
-rw-r--r-- 3 root supergroup 280 2019-02-25 14:55 /input/1.txt
-rw-r--r-- 3 root supergroup 58 2019-02-25 15:17 /input/2.txt
-rw-r--r-- 3 root supergroup 4 2019-02-25 15:17 /input/3.txt
-rw-r--r-- 3 root supergroup 4 2019-02-25 15:18 /input/4.txt

$ hdfs dfs -ls -R /input/.snapshot # 直接查看
drwxr-xr-x - root supergroup 0 2019-02-25 15:20 /input/.snapshot/s20190225-152050.714
-rw-r--r-- 3 root supergroup 280 2019-02-25 14:55 /input/.snapshot/s20190225-152050.714/1.txt
-rw-r--r-- 3 root supergroup 58 2019-02-25 15:17 /input/.snapshot/s20190225-152050.714/2.txt
-rw-r--r-- 3 root supergroup 4 2019-02-25 15:17 /input/.snapshot/s20190225-152050.714/3.txt
-rw-r--r-- 3 root supergroup 4 2019-02-25 15:18 /input/.snapshot/s20190225-152050.714/4.txt

删除一个文件

1
hdfs dfs -rm -r -f /input/1.txt

再创建一个快照,可指定名称

1
2
$ hdfs dfs -createSnapshot /input snapshot2
Created snapshot /input/.snapshot/snapshot2

1.3. 列出有快照的目录

列出当前用户有快照的目录

1
2
$ hdfs lsSnapshottableDir
drwxr-xr-x 0 root supergroup 0 2019-02-25 15:20 1 65536 /input

1.4. 重命名快照

-renameSnapshot <snapshotDir> <oldName> <newName>

1
2
hdfs dfs -renameSnapshot /input snapshot2 snapshot3  # snapshot2重命名为snapshot3 
hdfs dfs -renameSnapshot /input snapshot3 snapshot2 # 恢复修改

1.5. 比较快照不同以及目录变化

snapshotDiff用法:hdfs snapshotDiff <snapshotDir> <from> <to>

  • <snapshotDir>:有快照的目录
  • <from/to>
    • 可以是 . ,表示该目录的当前状态
    • 也可以是.snapshot/snapshot_name,代表该目录的某个快照。其中.snapshot可以省略,简写为snapshot_name

比较两个快照的不同

1
2
3
4
5
6
7
8
9
$ hdfs snapshotDiff /input s20190225-152050.714 snapshot2
Difference between snapshot s20190225-152050.714 and snapshot snapshot2 under directory /input:
M . # 当前目录发生变化
- ./1.txt # 后者比前者少了1.txt

$ hdfs snapshotDiff /input snapshot2 s20190225-152050.714 # 参数的一下
Difference between snapshot snapshot2 and snapshot s20190225-152050.714 under directory /input:
M .
+ ./1.txt # 后者比前者多了1.txt

比较当前状态与快照的不同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ hdfs dfs -rm -r -f /input/2.txt
$ hdfs dfs -put input/5.txt /input/
$ hdfs dfs -rm -r -f /input/3.txt
$ hdfs dfs -put input/3.txt /input
$ hdfs dfs -appendToFile - /input/4.txt
hello world
^D # CTRL+D

$ hdfs snapshotDiff /input snapshot2 . # 后者比前者
Difference between snapshot snapshot2 and current directory under directory /input:
M .
+ ./3.txt
+ ./5.txt
- ./2.txt
- ./3.txt
M ./4.txt

$ hdfs snapshotDiff /input . snapshot2 # 后者比前者
Difference between current directory and snapshot snapshot2 under directory /input:
M .
- ./3.txt
- ./5.txt
+ ./2.txt
+ ./3.txt
M ./4.txt

1.6. 恢复快照

这些的恢复快照不能一次性回到原来的状态,只能手动拷贝。将快照目录复制出来即可

1
2
3
4
5
$ hdfs dfs -cp /input/.snapshot/snapshot2 /   # 复制snapshot2目录
$ hdfs dfs -ls -R /snapshot2
-rw-r--r-- 3 root supergroup 58 2019-02-25 16:13 /snapshot2/2.txt
-rw-r--r-- 3 root supergroup 4 2019-02-25 16:13 /snapshot2/3.txt
-rw-r--r-- 3 root supergroup 4 2019-02-25 16:13 /snapshot2/4.txt

1.7. 删除快照

-deleteSnapshot <snapshotDir> <snapshotName>

1
2
hdfs dfs -deleteSnapshot /input s20190225-152050.714
hdfs dfs -deleteSnapshot /input snapshot2

1.8. 禁用快照

hdfs dfsadmin [-disallowSnapshot <snapshotDir>]

1
2
3
4
5
6
7
8
9
10
$ hdfs dfs -createSnapshot /input snapshot2       # 先创建一个快照
Created snapshot /input/.snapshot/snapshot2

$ hdfs dfsadmin -disallowSnapshot /input # 尝试禁用,失败,如果当前目录有快照,则无法禁止
disallowSnapshot: The directory /input has snapshot(s). Please redo the operation after removing all the snapshots.

$ hdfs dfs -deleteSnapshot /input snapshot2 # 先删除该目录的所有快照

$ hdfs dfsadmin -disallowSnapshot /input # 再禁用
Disallowing snaphot on /input succeeded
panchaoxin wechat
关注我的公众号
支持一下