CentOS 恢复 rm -rf * 误删数据

一、 将磁盘分区挂载为只读

这一步很重要,并且在误删除文件后应尽快将磁盘挂载为只读。越早进行,恢复的成功机率就越大。

 

1.  查看被删除文件位于哪个分区

[root@localhost  ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4(rw)
/dev/mapper/VolGroup-lv_home on /home type ext4(rw)

2.  尝试将对应目录重新挂载为只读

[root@localhost  ~]#  mount -r -n -o remount /home
mount: /home is busy

 

3.  如果显示 xxx is busy

[root@localhost  ~ ]fuser -v -m /data

找出相关进程,kill.

 

4.  成功将目录挂载为只读

[root@localhost  ~ ] #  mount -r -n -o remount /home

此时在/home目录 touch文件时,会报错:

[root@localhost  ~ ] touch txt
touch: cannot touch `txt’: Read-only file system

 

二、 使用数据恢复工具 extundelete

之前尝试了debugfs + dd,未果。

后来安装 extundelete-0.2.4 ,:

1.  下载

(1) 因为sourceforge被墙,服务器上直接wget不成功,所以只能在本地翻墙下载,链接如下:

      http://superb-dca2.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2 

(2) 把下载的文件放到服务器

      启动本地的nginx,然后在服务器上wget(当然通过其它方法也可以,只要能传到服务器):

wget http://本机IP/extundelete-0.2.4.tar.bz2  

(3) 解压

tar jxf extundelete - 0 . 2 . 4 .tar.bz2

 

2.  编译

(1) configure

[root@localhost  extundelete-0.2.4]./configure

configure时报错,看了下config.log,确定是本机没编译环境 。

yum -y install gcc+ gcc-c++

等待,有一点慢。

安装完成后,再次config,依然报错

Configuring extundelete 0.2.4
configure: error: Can’t find ext2fs library

这是因为extundelete依赖e2fsprogs。

安装e2fsprogs后再次configure,成功。

[root@localhost  extundelete-0.2.4]yum install e2fsprogs-devel
[root@localhost  extundelete-0.2.4]# ./configure
Configuring extundelete 0.2.4
Writing generated files to disk

 

(2) make & make install

[root@localhost  extundelete-0.2.4]#make & make install

 如果没有异常信息,基本说明安装成功.

 

(3) 可以到src目录验证下.

[root@localhost  extundelete-0.2.4]# cd src
[root@localhost  src]./extundelete
No action specified; implying --superblock.
./extundelete: Missing device name.
Usage: ./extundelete [options] [--] device-file
.............

 

[root@localhost  src]# ./extundelete -v

extundelete version 0.2.4

libext2fs version 1.41.12

Processor is little endian.

如上信息,证明安装成功。

下面才真正开始数据恢复。

 

三、 挂载新硬盘

(如果原服务器磁盘空间够大,可以跳过这一步。)

因为被误删的数据很大(约200G),原服务器所在的物理机上也没有磁盘空间了。因些需要到远程挂载另一台服务器B上的磁盘,B是xen虚出的机器,空间也不够,但所在的物理机上还有磁盘空间,这时需要从宿主机上分空间给B。

1  在xen上挂载一块磁盘给B

因为是图形操作,就不再细说。只需分配足够大的空间就可以了,我当时选的是300G。

2  登录服务器B, 准备挂载新磁盘。

(1) 查看新磁盘是否已挂载

[nmen@dev -ubuntu-server] ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda5  /dev/sda6  /dev/sda7  /dev/sda8  /dev/sdb

/dev/sdb确实已挂载。

此时新盘是未分区,也未格式化,因此需要先进行这两件事。

 

(2)  分区

下图是hdb的硬盘,sdb的盘也一样的操作。


(本图来自:http://www.shyw.net/bbs/yxt443333-1-1.html)

 

(3)  格式化

[nmen@dev -ubuntu-server]:~sudo mkfs -t ext3 /dev/sdb1
mke2fs 1.41.11 (14-Mar-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
22937600 inodes, 91749215 blocks
4587460 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    327689830416384022937629491281920088473616056322654208,
    409600079626241123942420480000238878727166361678675968
 
Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -or -i to override.

显示以上信息说明已成功格式化。

 

(4)  设置卷标

sudo e2label /dev/sdb1 /restore

 

(5)  挂载

[nmen@dev -ubuntu-server]:~$ mkdir /restore
[nmen@dev  - ubuntu - server] : ~ $ mount -vl -t ext3 /dev/sdb1 /restore

至此服务器B上挂载新硬盘结束,现在有足够空间来做存放要恢复的数据了。

 

四、通过NFS远程挂载

通过网络, 将远程主机B共享的文件系统,挂载到需要做数据恢复的机器A。

1. 服务器B上安装NFS

(1) 安装

B是ubuntu系统,默认没安装nfs.

#  sudo apt-get install nfs-kernel-server

 

(2) 配置

修改/etc/exports , 添加如下语句。


# /etc/exports: the access control list for filesystems which may be exported
#               to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#
/restore *(rw,sync,no_root_squash,no_subtree_check)

其中 :

  /restore                                                                  -- 需要与客户机共享的目录;

    *                                                                            -- 表示任何主机均可访问本目录,也可指定IP;

  (w,sync,no_root_squash,no_subtree_check)      -- 配置客户机的权限;

因为是临时使用,并且是服务器位于内网,所以设置相对随意。

 

(3) 使配置生效

#  exportfs –rv

#  /etc/init.d/nfs-kernel-server restart

 

(4) 验证是否配置成功

显示NFS服务器输出目录列表:

nmen@dev -ubuntu-server: showmount -e
Export list for chinahrd-ubuntu-dev:
/restore *

 

(5) 防火墙

因为时间紧急,并且是内网,所以临时关闭了B上的防火墙:

sudo ufw disable

可通过以下命令启用防火墙:

sudo ufw enable

2.  服务器A上挂载远程目录

将/restore目录从服务器 B 挂载到 /mnt 上。

mount -t nfs [B的IP]:/restore  /mnt

命令详解如下:

# mount -t nfs [-o mount-options] server:/directory /mount-point
 
-o mount-options
     指定可以用来挂载 NFS 文件系统的挂载选项。
server:/directory
     指定包含共享资源的服务器主机名,以及要挂载的文件或目录的路径。
/mount-point
     指定要挂载文件系统的目录。

 

五、数据恢复

1.   得到删除的大概时间

这一步不是必须,但这个有助于更快的回复想要的数据。

date -"Fri Apr 15:40:00 2014" +%s
1397202000

1397202000这个时间值,我们后期会用到。

2.  查看被删除文件

# extundelete /dev/sdb1 --inode 2

File name                                       | Inode number | Deleted status

.                                                 9

..                                                11

lost+found                                        24             Deleted

data                                              82             Deleted

一个分区挂载到一个目录下时,”根”目录的inode值为一般是2。

状态为deleted的是被删除的文件。

3.  数据恢复

进入刚mount的远程目录/restore;

指定--after "1397202000", 表示恢复这个时间点之后的文件;

文件默认会被恢复到当前目录下的RECOVERED_FILES目录中。

cd /restore
[root@localhost  restore]#[extundelete的安装路径]./extundelete --restore-all --after "1397202000"/dev/mapper/VolGroup-lv_home

Only show and process deleted entries if they are deleted on or after 1397202000 and before 9223372036854775807.

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 6924 groups loaded.

Loading journal descriptors ... 27149 descriptors loaded.

Searching for recoverable inodes in directory / ...

696 recoverable inodes found.

Looking through the directory structure for deleted files ...

Unable to restore inode 27394319 (VMware/9.50_ps/9.55locate.vmx.lck): Space has been reallocated.

Unable to restore inode 27402241 (VMware/9.35win7/9.35win7.vmx.lck): Space has been reallocated.

Unable to restore inode 27396032 (VMware/9.35win7/9.35win7-Snapshot1.vmsn): No undeleted copies found in the journal.

Unable to restore inode 27394051 (VMware/9.36win2008/9.36win2008R2.vmx.lck/E00633.lck): Space has been reallocated.

Unable to restore inode 27394603 (lost+found/E09292.lck): Space has been reallocated.

8 recoverable inodes still lost.

 

一般来说,要等很久。。。

cd restore/RECOVERED_FILES$
ls
110_open_dns  111_open_dns_node1  112_DNS_node2  116_svn

删除的文件回来了,至此松一口气。

六、收尾工作

(1) 重新挂载A上的磁盘为可读写:

[root@localhost  src]# mount -o remount, rw /home/

卸载服务器B上的目录。

 

(2) 开启B的防火墙。

sudo ufw enable

 

(3) 在A上对rm命令启用别名,防止沉默式删除。

vi /etc/bashrc

source /etc/bashrc

 

# do not delete / or prompt if deleting more than 3 files at a time #
alias rm='rm -I --preserve-root'  
 
# confirmation #
alias mv='mv -i'
alias cp='cp -i' alias ln='ln -i'  
 
# Parenting changing perms on / #
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'

(4) 在B上使用Rsync,定期备份A上数据。

参考:http://abloz.com/2013/09/12/linux-rm-rf-file-recovery-record.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CentOS系统下,如果你误删了文件,可以通过以下步骤来恢复文件。首先,进入被删除文件所在的目录,例如执行命令"cd /gitea"进入服务器根目录gitea。然后,创建一个新的文件夹,可以执行命令"mkdir asd"。接下来,删除这个新创建的文件夹,可以执行命令"rm -rf asd"。此时,使用"ll"命令查看该文件夹已经不存在了。接下来,使用系统自带的恢复工具debugfs来修复文件。同时,打开一个新的终端窗口,执行命令"df /gitea/"来查看被删除文件所在的分区,例如/dev/vda1。然后,执行命令"open /dev/vda1"来查看目录分区。接着,执行命令"ls -d /www/wwwroot/gitea"来显示被删除文件所在的目录。继续执行命令"logdump -i <1452682>"来查找文件所在的区块号。记住这个区块号,并执行命令"quit"退出debugfs。最后,执行命令"dd if=/dev/vda1 of=/www/wwwroot/gitea/asd bs=1152 count=1 skip=5767796"来恢复文件操作。其中,bs的值与区块号后面的数值一致,skip的值与区块号一致。执行完毕后,可以使用"ls"命令来确认文件已经成功恢复了。\[2\]另外,你也可以使用extundelete工具来恢复误删的文件。首先,在/usr/local/software目录下解压安装包,然后进入解压后的安装包目录,执行命令"yum -y install gcc"和"yum -y install gcc-c++"安装必要的编译工具。接着,执行"./configure"、"make"和"make install"来安装extundelete工具。最后,使用"which extundelete"命令来确认工具是否安装成功。\[3\] #### 引用[.reference_title] - *1* *3* [Linux系统:第十三章:centos误删文件如何恢复文件数据](https://blog.csdn.net/java_wxid/article/details/120795537)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [centos误删恢复](https://blog.csdn.net/cheers_bin/article/details/112380310)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值