rm -rf / #此方法删除不了/目錄;
rm -rf /* #此方法可以删除/目錄下的所有内容,禁止使用;
40.1、前言:
作為一個多使用者、多任務的作業系統,Linux下的檔案一旦被删除,是難以恢複的。盡管删除指令隻是在檔案節點中作删除标記,
并不真正清除檔案内容,但是其他使用者和一些有寫盤動作的程序會很快覆寫這些資料。不過,對于家庭單機使用的Linux,或者誤删
檔案後及時補救,還是可以恢複的。
40.2、檔案誤删除情況一:
目前系統有多使用者,其中一個使用者對檔案進行修改,另一個使用者對檔案進行删除操作,此時該檔案
的程序id還是存在的通過以下指令進行恢複;
1、視窗一:
[root@nfs01 ~]# cd /tmp
[root@nfs01 tmp]# echo 'test' >test.txt
[root@nfs01 tmp]# cat test.txt
test
[root@nfs01 tmp]# cat >>test.txt
1
2
3
2、視窗二:
[root@nfs01 ~]# rm -vf /tmp/test.txt
已删除"/tmp/test.txt"
[root@nfs01 ~]# ls /tmp
[root@nfs01 ~]#
[root@nfs01 ~]# lsof | grep -i "delete"
4715 root 1w
正在使用的指令 pid程序号 root使用者 打開的檔案句柄 /tmp/test.txt檔案是delete狀态
4715/fd
1
[root@nfs01 ~]# cp -v /proc/4715/fd/1 /tmp/test.txt
"/proc/4715/fd/1" -> "/tmp/test.txt"
[root@nfs01 ~]# ls /tmp
test.txt
[root@nfs01 ~]# cat /tmp/test.txt
test
1
2
3
3、對于一個正在使用的vim編輯的檔案的使用者,檔案被其他使用者删除後,編輯的使用者儲存檔案後,檔案依舊存在;
40.3、檔案誤删除情況二:
1、目前系統隻有一個使用者在登入,對檔案進行了誤删除操作(如果有多個使用者進入單使用者模式);
2、目前的測試環境:
(1)使用fdfisk /dev/sdb指令建立一個/dev/sdb1主分區并挂載到 /test/目錄下;
[root@nfs01 ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 57G 5.4G 49G 10% /
tmpfs tmpfs 297M 0 297M 0% /dev/shm
/dev/sda1 ext4 190M 36M 145M 20% /boot
/dev/sdb1 ext4 2.0G 3.0M 1.9G 1% /test
(2)生成測試檔案和目錄:
[root@nfs01 ~]# cd /test/
[root@nfs01 test]# echo 'lc_2018/9/14' >lc_test.txt
[root@nfs01 test]# mkdir test
[root@nfs01 test]# echo 'mkdir test' >test/dir.txt
[root@nfs01 test]# tree
.
├── lc_test.txt
├── lost+found
└── test
└── dir.txt
2 directories, 2 files
(3)删除/test/目錄中的内容:
[root@nfs01 test]# rm -rvf test/ lc_test.txt
已删除"test/dir.txt"
已删除目錄:"test"
已删除"lc_test.txt"
[root@nfs01 test]# ls
lost+found
3、注意事項及解決思路:
(1)停止對目前分區進行任何操作,防止inode值被覆寫;
(2)通過dd指令對目前分區進行備份,防止通過第三方軟體恢複失敗,造成資料丢失;
[root@nfs01 test]# dd if=/dev/sdb1 of=/tmp/sdb1_test.bak
記錄了4194303+0 的讀入
記錄了4194303+0 的寫出
2147483136位元組(2.1 GB)已複制,71.7973 秒,29.9 MB/秒
(3)對目前的分區進行解除安裝;
[root@nfs01 ~]# umount /dev/sdb1
[root@nfs01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 57G 7.4G 47G 14% /
tmpfs 297M 0 297M 0% /dev/shm
/dev/sda1 190M 36M 145M 20% /boot
(4)下載下傳并安裝第三方開源工具;
下載下傳位址:http://zy-res.oss-cn-hangzhou.aliyuncs.com/server/extundelete-0.2.4.tar.bz2
[root@nfs01 tools]# rz -y
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring extundelete-0.2.4.tar.bz2...
100% 105 KB 105 KB/sec 00:00:01 0 Errors
[root@nfs01 tools]# tar -xjf extundelete-0.2.4.tar.bz2
[root@nfs01 tools]# cd extundelete-0.2.4
[root@nfs01 extundelete-0.2.4]# ./configure --prefix=/application/extundelete0.2.4
Configuring extundelete 0.2.4
configure: error: Can't find ext2fs library
[root@nfs01 extundelete-0.2.4]# yum install e2fsprogs-devel e2fsprogs -y
[root@nfs01 extundelete-0.2.4]# rpm -qa e2fsprogs-devel e2fsprogs
e2fsprogs-1.41.12-24.el6.x86_64
e2fsprogs-devel-1.41.12-24.el6.x86_64
[root@nfs01 extundelete-0.2.4]# ./configure --prefix=/application/extundelete0.2.4 #參數配置;
Configuring extundelete 0.2.4
Writing generated files to disk
[root@nfs01 extundelete-0.2.4]# make #編譯;
make -s all-recursive
Making all in src
extundelete.cc:571: 警告:未使用的參數‘flags’ #忽略此錯誤
[root@nfs01 extundelete-0.2.4]# echo $?
[root@nfs01 extundelete-0.2.4]# make install #安裝;
Making install in src
/usr/bin/install -c extundelete '/application/extundelete0.2.4/bin'
(5)資料恢複
[root@nfs01 extundelete-0.2.4]# cd /application/extundelete0.2.4/bin/
1)查詢/dev/sdb1分區可恢複的資料資訊,标記為deleted狀态的是已經删除的檔案或目錄;
[root@nfs01 bin]# ./extundelete /dev/sdb1 --inode 2
........................................................................
File name | Inode number | Deleted status
. 2
.. 2
lost+found 11
lc_test.txt 12 Deleted
test 8193 Deleted
檔案名 inode值 檔案的狀态
2)恢複單個檔案:
[root@nfs01 bin]# ./extundelete /dev/sdb1 --restore-file lc_test.txt
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 16 groups loaded.
Loading journal descriptors ... 47 descriptors loaded.
Successfully restored file lc_test.txt
#恢複的檔案在目前目錄下的“RECOVERED_FILES”目錄中;
[root@nfs01 bin]# ls RECOVERED_FILES/
lc_test.txt
3)恢複單個目錄:
[root@nfs01 bin]# ./extundelete /dev/sdb1 --restore-directory test
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 16 groups loaded.
Loading journal descriptors ... 47 descriptors loaded.
Searching for recoverable inodes in directory test ...
3 recoverable inodes found.
Looking through the directory structure for deleted files ...
2 recoverable inodes still lost.
[root@nfs01 bin]# ls RECOVERED_FILES/
lc_test.txt test
[root@nfs01 bin]# ls RECOVERED_FILES/test/
dir.txt
4)恢複所有的檔案和目錄:
[root@nfs01 bin]# ./extundelete /dev/sdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 16 groups loaded.
Loading journal descriptors ... 47 descriptors loaded.
Searching for recoverable inodes in directory / ...
3 recoverable inodes found.
Looking through the directory structure for deleted files ...
0 recoverable inodes still lost.
如果重複恢複的話,不會覆寫之前恢複目錄中的檔案,會生成filename_v1....的形式進行
(6)挂載磁盤并拷貝資料:
[root@nfs01 bin]# mount /dev/sdb1 /test
[root@nfs01 bin]# mount
/dev/sdb1 on /test type ext4 (rw)
[root@nfs01 bin]# cp -av RECOVERED_FILES/* /test/
"RECOVERED_FILES/lc_test.txt" -> "/test/lc_test.txt"
"RECOVERED_FILES/test" -> "/test/test"
"RECOVERED_FILES/test/dir.txt" -> "/test/test/dir.txt"
[root@nfs01 bin]# ls /test
lc_test.txt lost+found test
40.4、内容補充:
1、lost+found:
幾乎每個被格式化過的Linux分區都會有,意外後(非正常關機等)找回的檔案一般在這裡面。這個目錄
是儲存發生意外後丢失的檔案的,隻有root使用者才能打開。如果删除了該檔案夾,生成的指令是mklost+found;
2、fsck分區修複:
修複分區之前需要解除安裝分區;
(1)如果Linux分區有損壞情況,啟動有問題,不能正常進入文本或圖形界面。那麼一般會出現提示,需要輸入Root密碼登入後采用
fsck -t ext4 /usr/local可以修複;
(3)fsck -t ext4 /dev/sdb 修複系統分區無法挂載的情況;
報錯:mount: wrong fs type, bad option, bad superblock on /dev/mapper/oraclevg-oraclelv,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
(3)fsck指令詳解:
-t : 給定檔案系統的型式,若在 /etc/fstab 中已有定義或 kernel 本身已支援的則不需加上此參數
-s : 依序一個一個地執行 fsck 的指令來檢查
-A : 對/etc/fstab 中所有列出來的 partition 做檢查
-C : 顯示完整的檢查進度
-d : 列印 e2fsck 的 debug 結果
-p : 同時有 -A 條件時,同時有多個 fsck 的檢查一起執行
-R : 同時有 -A 條件時,省略 / 不檢查
-V : 詳細顯示模式