linux 基本。。

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

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

 

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不成功,所以只能在本地FQ下载,链接如下:

      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 typeLinux
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:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968
 
Writing inode tablesdone                           
Creating journal (32768 blocks)done
Writing superblocks and filesystem accounting informationdone
 
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c 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-servershowmount -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 -d "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

原文地址:https://www.cnblogs.com/ldms/p/6769122.html