HDFS的回收站功能可将删除的文件移动至用户.Trash目录(而非立即删除),便于快速恢复。
core-site.xml配置文件,添加以下参数并重启HDFS集群:<property>
<name>fs.trash.interval</name>
<value>120</value> <!-- 回收站保留时间(分钟),设为0则禁用 -->
</property>
<property>
<name>fs.trash.checkpoint.interval</name>
<value>120</value> <!-- 回收站检查点间隔(分钟) -->
</property>
/user/username/.Trash/Current/目录下,使用以下命令恢复至目标路径:hdfs dfs -cp /user/username/.Trash/Current/deleted_file /path/to/restore
HDFS快照是文件系统在某一时刻的只读副本,适合重要目录的备份与恢复。
hdfs dfsadmin -allowSnapshot /test # 允许/test目录创建快照
hdfs dfs -put test.txt /test # 向目录添加测试文件
hdfs dfs -createSnapshot /test snapshot1 # 创建名为snapshot1的快照
hdfs dfs -cp /test/.snapshot/snapshot1/test.txt /path/to/restore
HDFS提供fsck(文件系统检查)和balancer(数据均衡)工具,用于修复数据块问题。
fsck命令查看文件系统中的损坏块:hdfs fsck / -files -blocks -locations # 检查根目录下所有文件的块状态
fsck发现损坏块,可使用recoverLease命令尝试恢复:hdfs debug recoverLease /path/to/corrupt/file -retries 3 # 重试3次恢复租约
dfs.replication参数),也可手动触发均衡:hdfs balancer # 触发数据均衡,将块复制至目标副本数
若NameNode进程异常或数据目录损坏,可通过元数据恢复(需提前备份)。
stop-dfs.sh # 停止HDFS集群
/backup/namenode/data)复制至原位置:cp -r /backup/namenode/data /path/to/namenode/data # 替换为实际路径
start-dfs.sh
hdfs dfsadmin -report # 检查集群状态
若上述方法无效,可使用针对Linux文件系统的第三方工具(如extundelete),但需注意:操作前必须停止HDFS服务,避免覆盖数据。
sudo yum install extundelete # CentOS系统
sudo umount /path/to/hdfs/directory # 卸载HDFS数据分区
sudo extundelete /path/to/hdfs/directory --restore-file path/to/deleted/file # 恢复单个文件
sudo extundelete /path/to/hdfs/directory --restore-all # 恢复全部
sudo mount /path/to/hdfs/directory
hdfs dfsadmin -saveNamespace)和DataNode数据,启用快照功能,配置高可用(HA)集群。hdfs dfs -ls和hdfs fsck检查数据完整性和可用性。