恢復 rm -rf * 誤刪數據

摘要 1. xen server上磁盤分區、格式化、掛載; 2. NFS server搭建、掛載遠程目錄; 3. 使用extundelete恢復數據;

一、 將磁盤分區掛載爲只讀

這一步很重要,並且在誤刪除文件後應儘快將磁盤掛載爲只讀。越早進行,恢復的成功機率就越大。

 

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:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968
 
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 -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-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 -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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章