如何恢复 hdfs 删除的文件

how t restore a hdfs deleted file

有人问我以下问题。

面试官:如何恢复hdfs中被删除的文件。 我:从垃圾目录我们可以 copy/move 回到原来的目录。 采访者:除了垃圾回收还有别的办法吗? 我:我说没有

所以我的问题是,是否真的有任何方法可以恢复已删除的文件,或者面试官只是让我测试我的信心。

我发现了以下不同于 hdfs 的恢复方法 -cp/mv 但它也从垃圾中获取文件。

hadoop distcp -D ipc.client.fallback-to-simple-auth-allowed=true -D dfs.checksum.tpe=CRC32C -m 10 -pb -update /users/vijay/.Trash/ /application/data/vijay;

Hadoop从2.1.0版本开始提供HDFS快照(SnapShot)功能 你可以尝试使用它

首先,创建快照

hdfs dfsadmin -allowSnapshot /user/hdfs/important
hdfs dfs -createSnapshot /user/hdfs/important important-snapshot

接下来,尝试删除一个文件

hdfs dfs -rm -r /user/hdfs/important/important-file.txt

完结,还原

hdfs dfs -ls /user/hdfs/important/.snapshot/
hdfs dfs -cp /user/hdfs/important/.snapshot/important-snapshot/important-file.txt /user/hdfs/important/
hdfs dfs -cat /user/hdfs/important/important-file.txt

P.S:这种方式恢复删除的文件必须使用CP命令(不是MV命令) 因为快照中删除的文件是只读文件

希望我的回答能帮到您