CentOS 恢複 rm -rf * 誤刪資料

來源:互聯網
上載者:User

標籤:ref   .sh   c++   磁碟分割   orm   主機名稱   磁碟   status   arch   

一、 將磁碟分割掛載為唯讀

這一步很重要,並且在誤刪除檔案後應儘快將磁碟掛載為唯讀。越早進行,恢複的成功機率就越大。

 

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,依然報錯

Configuringextundelete 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 filesto 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 littleendian.

如上資訊,證明安裝成功。

下面才真正開始資料恢複。

 

三、 掛載新硬碟

(如果原伺服器磁碟空間夠大,可以跳過這一步。)

因為被誤刪的資料很大(約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 superuser
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 storedon 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 beautomatically checked every 33 mounts or
180 days, whichever comes first.  Usetune2fs -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-getinstall nfs-kernel-server

 

(2) 配置

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


# /etc/exports: the accesscontrol list for filesystems which may be exported
#               toNFS clients.  See exports(5).
#
# Example for NFSv2 andNFSv3:
# /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-serverrestart

 

(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 [-omount-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

Filename                                      | 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 processdeleted entries if they are deleted on or after 1397202000 and before9223372036854775807.

NOTICE: Extendedattributes are not restored.

Loading filesystemmetadata ... 6924 groups loaded.

Loading journaldescriptors ... 27149 descriptors loaded.

Searching for recoverableinodes in directory / ...

696 recoverable inodesfound.

Looking through thedirectory structure for deleted files ...

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

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

Unable to restore inode27396032 (VMware/9.35win7/9.35win7-Snapshot1.vmsn): No undeleted copies foundin the journal.

Unable to restore inode27394051 (VMware/9.36win2008/9.36win2008R2.vmx.lck/E00633.lck): Space has beenreallocated.

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

8 recoverable inodesstill 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 promptif 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 permson / #
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

 

CentOS 恢複 rm -rf * 誤刪資料(轉)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.