溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux系統(tǒng)中文件被刪除后的恢復(fù)方法(ext4)

發(fā)布時(shí)間:2020-08-09 11:03:01 來(lái)源:ITPUB博客 閱讀:199 作者:夢(mèng)共里醉 欄目:建站服務(wù)器
本文檔給出了恢復(fù)ext4文件系統(tǒng)被誤刪的文件的方法,需要使用的軟件是extundelete,這款軟件對(duì)ext4文件系統(tǒng)的恢復(fù)成功率比較高,值得擁有!

Linux系統(tǒng)中文件被刪除后的恢復(fù)方法(ext4)

[root@localhost ~]# rm -Rf /   #執(zhí)行不成功的!
rm: 在"/" 進(jìn)行遞歸操作十分危險(xiǎn)
rm: 使用 --no-preserve-root 選項(xiàng)跳過(guò)安全模式
[root@localhost ~]# rm -rf /*    #這個(gè)可以執(zhí)行成功! 呵呵。。。
ext4文件系統(tǒng)上刪除文件,可以恢復(fù): extundelete ,ext3恢復(fù)使用:ext3grep
windows恢復(fù)誤刪除的文件:  final data v2.0 漢化版  和  easyrecovery

擴(kuò)展:

Linux文件系統(tǒng)由三部分組成:文件名,inode,block
windows也由這三部分組成。
a.txt          -->inode              --> block
文件名       存放文件元數(shù)據(jù)信息       真正存放數(shù)據(jù)
查看文件文件名:
[root@localhost ~]# cp /etc/passwd a.txt
[root@localhost ~]# ls a.txt
a.txt
查看inode號(hào):
常識(shí): 每個(gè)文件,有一個(gè)inode號(hào)。
[root@localhost ~]# ls -i a.txt
440266 a.txt
查看inode中的文件屬性;  通過(guò)stat命令查看inode中包含的內(nèi)容
[root@localhost ~]# stat a.txt   #查看inode信息:
[root@localhost ~]# ls -l a.txt
-rw-r--r-- 1 root root 1720 Oct 25 10:21 a.txt
block塊:真正存儲(chǔ)數(shù)據(jù)的地方
邏輯刪除:假刪除
為什么刪除比復(fù)制快?

Linux系統(tǒng)中文件被刪除后的恢復(fù)方法(ext4)

誤刪除文件后,第一件事要做什么?  你不心刪除把存了幾十年的大片刪除了!
要避免誤刪除的文件內(nèi)容被覆蓋,如何避免?

卸載需要恢復(fù)文件的分區(qū)或者以只讀的方式掛載
例如:

mount -o remount,ro /mnt
實(shí)戰(zhàn):恢復(fù)ext4文件系統(tǒng)被誤刪的文件

下載extundelete
http://sourceforge.net/   開(kāi)源軟件發(fā)布中心

extundelete-0.2.4.tar.bz2
鏈接: https://pan.baidu.com/s/1n0dtGnhffcH7XrLv0TqUsw
提取碼:a5m7

準(zhǔn)備測(cè)試分區(qū):
[root@localhost ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x539f33b8.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
Command (m for help): p #查看分區(qū)表信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x539f33b8
   Device Boot      Start         End      Blocks   Id  System
Command (m for help): n #創(chuàng)建一個(gè)新分區(qū)
Command action
   e   extended
   p   primary partition (1-4)
p #創(chuàng)建一個(gè)主分區(qū)
Partition number (1-4): 1
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G #指定分區(qū)大小
Command (m for help): p #查看分區(qū)表信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x539f33b8
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
Command (m for help): w #保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb1  #獲得新分區(qū)表
或者
[root@localhost ~]# reboot

擴(kuò)展:
如果在根下刪除文件了,想恢復(fù),怎么辦?
方法1: 立即斷電,然后把磁盤(pán)以只讀方式,掛載到另一個(gè)電腦中進(jìn)行恢復(fù)。
方法2:把extundelete在虛擬機(jī)上(虛擬機(jī)系統(tǒng)要和服務(wù)器版本一樣),提前安裝好后再?gòu)?fù)制到U盤(pán)中,把U盤(pán)插入服務(wù)器,恢復(fù)時(shí),恢復(fù)的文件要保存到U盤(pán)中,(不要讓恢復(fù)的數(shù)據(jù)寫(xiě)到/下,那樣會(huì)覆蓋之前刪除的文件)。

使用新的分區(qū):
[root@localhost ~]# mkdir /tmp/sdb1 #創(chuàng)建掛載點(diǎn)
[root@localhost ~]# mkfs.ext4 /dev/sdb1 #把/dev/sdb1分區(qū)文件系統(tǒng)格式化成ext4
[root@localhost ~]# mount /dev/sdb1 /tmp/sdb1 #把/dev/sdb1分區(qū)掛到/tmp/sdb1
[root@localhost ~]# df -h 
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
tmpfs                         499M     0  499M   0% /dev/shm
/dev/sda1                     485M   33M  427M   8% /boot
/dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
/dev/sdb1                    1020M   34M  935M   4% /tmp/sdb1
復(fù)制一些測(cè)試文件,然后把這些文件再刪除,然后演示恢復(fù):
[root@localhost ~]# cp /etc/passwd /tmp/sdb1
[root@localhost ~]# cp /etc/hosts /tmp/sdb1 
[root@localhost ~]# echo aaa > a.txt
[root@localhost ~]# mkdir -p /tmp/sdb1/a/b/c
[root@localhost ~]# cp a.txt /tmp/sdb1/a
[root@localhost ~]# cp a.txt /tmp/sdb1/a/b
[root@localhost ~]# touch /tmp/sdb1/a/b/kong.txt
[root@localhost ~]# yum install -y tree
[root@localhost ~]# tree /tmp/sdb1
/tmp/sdb1
├── a
│   ├── a.txt
│   └── b
│       ├── a.txt
│       ├── c #空目錄
│       └── kong.txt #空文件
├── hosts
├── lost+found
└── passwd
4 directories, 5 files
刪除文件:
[root@localhost ~]# cd /tmp/sdb1
[root@localhost sdb1]# ls
a  hosts  lost+found  passwd
[root@localhost sdb1]# rm -rf a hosts passwd 
[root@localhost sdb1]# ls
lost+found

誤刪除文件后,第一件事要做什么???
如何避免誤刪除的文件內(nèi)容被覆蓋???
卸載需要恢復(fù)文件的分區(qū)或者以只讀的方式掛載

[root@localhost sdb1]# cd /root
[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
tmpfs                         499M     0  499M   0% /dev/shm
/dev/sda1                     485M   33M  427M   8% /boot
/dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
/dev/sdb1                    1020M   34M  935M   4% /tmp/sdb1
[root@localhost ~]# echo "/dev/sdb1 /tmp/sdb1 ext4 defaults 0 0" >> /etc/fstab 
[root@localhost ~]# mount -o remount,ro /tmp/sdb1 #以讀寫(xiě)的形式重新掛載/tmp/sdb1所在分區(qū)
[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
tmpfs                         499M     0  499M   0% /dev/shm
/dev/sda1                     485M   33M  427M   8% /boot
/dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
/dev/sdb1                    1020M   34M  935M   4% /tmp/sdb1
[root@localhost ~]# touch /tmp//sdb1/testfile
touch: cannot touch `/tmp//sdb1/testfile': Read-only file system

或者

[root@localhost ~]# umount /tmp/sdb1 #卸載/tmp/sdb1所在分區(qū)
[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
tmpfs                         499M     0  499M   0% /dev/shm
/dev/sda1                     485M   33M  427M   8% /boot
/dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
安裝extundelete工具 
上傳extundelete到Linux中:
從Windows上傳extundelete文件到Linux,安裝SecureCRT或者XShell[root@localhost ~]# yum install -y lrzsz  
# 安裝后就有了rz命令和sz命令
rz: 將Windows中的文件上傳到Linux
sz: 將Linux中的文件下載到Windows
源碼安裝extundelete
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# ls
[root@localhost src]# rz
rz waiting to receive.
 zmodem trl+C ?
  100%     105 KB  105 KB/s 00:00:01       0 Errorsbz2...
[root@localhost src]# ls
extundelete-0.2.4.tar.bz2
[root@localhost src]# tar xjvf extundelete-0.2.4.tar.bz2 
[root@localhost src]# cd extundelete-0.2.4
[root@localhost extundelete-0.2.4]# yum install -y e2fsprogs-devel gcc*
[root@localhost extundelete-0.2.4]# ./configure   #檢查系統(tǒng)安裝環(huán)境
[root@localhost extundelete-0.2.4]# make  -j 4  #編譯,把源代碼編譯成可執(zhí)行的二進(jìn)制文件。 -j 4   使用4進(jìn)程同時(shí)編譯,提升編譯速度或者使用4核CPU同時(shí)編譯。
[root@localhost extundelete-0.2.4]# make install  #編譯安裝

擴(kuò)展:

install 和cp 有什么區(qū)別? 
install 復(fù)制時(shí)可以指定權(quán)限  cp不可以
例:
[root@localhost ~]# install -m 777 /bin/find /opt/a.sh
[root@localhost ~]# ll /opt/
開(kāi)始恢復(fù):

方法一:通過(guò)inode結(jié)點(diǎn)恢復(fù)
方法二:通過(guò)文件名恢復(fù)
方法三:恢復(fù)某個(gè)目錄,如目錄a下的所有文件:
方法四:恢復(fù)所有的文件

[root@localhost extundelete-0.2.4]# mkdir /test #創(chuàng)建一個(gè)目錄使用于存放恢復(fù)的數(shù)據(jù)
[root@localhost extundelete-0.2.4]# cd /test
[root@localhost test]# 
通過(guò)inode結(jié)點(diǎn)查看被刪除的文件名字:
[root@localhost test]# extundelete /dev/sdb1 --inode 2  
File name                                       | Inode number | Deleted status
.                                                 2
..                                                2
lost+found                                        11
passwd                                            12             Deleted
hosts                                             13             Deleted
a                                                 7377           Deleted
擴(kuò)展:ext4文件系統(tǒng)的分區(qū)根目錄的inode值為2,xfs分區(qū)根目錄的inode值為64
[root@localhost test]# ls -id /boot/   #xfs文件系統(tǒng)
64 /boot/
[root@localhost test]# ls -id /tmp/sdb1
2 /tmp/sdb1
方法一:通過(guò)inode結(jié)點(diǎn)恢復(fù)
[root@localhost test]# ls
[root@localhost test]# extundelete /dev/sdb1 --restore-inode 12
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 9 groups loaded.
Loading journal descriptors ... 61 descriptors loaded.
[root@localhost test]# ls
RECOVERED_FILES
[root@localhost test]# ls RECOVERED_FILES/
file.12
[root@localhost test]# diff /etc/passwd  RECOVERED_FILES/file.12 #對(duì)比文件內(nèi)容,沒(méi)有任何輸出,說(shuō)明恢復(fù)后的文件內(nèi)容沒(méi)有變化
方法二:通過(guò)文件名恢復(fù)
[root@localhost test]# extundelete /dev/sdb1 --restore-file passwd
[root@localhost test]# diff /etc/passwd RECOVERED_FILES/passwd #對(duì)比文件內(nèi)容,沒(méi)有任何輸出,說(shuō)明恢復(fù)后的文件內(nèi)容沒(méi)有變化
方法三:恢復(fù)某個(gè)目錄,如目錄a下的所有文件:
[root@localhost test]# extundelete /dev/sdb1 --restore-directory a
[root@localhost test]#  tree RECOVERED_FILES/a/
RECOVERED_FILES/a/
├── a.txt
└── b
    └── a.txt
1 directory, 2 files
方法四:恢復(fù)所有的文件
[root@localhost test]# rm -rf RECOVERED_FILES/*
[root@localhost test]# extundelete /dev/sdb1 --restore-all
[root@localhost test]# ls RECOVERED_FILES/
a  hosts  passwd
[root@localhost test]# tree  RECOVERED_FILES/
RECOVERED_FILES/
├── a
│   ├── a.txt
│   └── b
│       └── a.txt
├── hosts
└── passwd
2 directories, 4 files
數(shù)據(jù)對(duì)比
刪除前:
[root@localhost ~]# tree /tmp/sdb1
/tmp/sdb1
├── a
│   ├── a.txt
│   └── b
│       ├── a.txt
│       ├── c #空目錄
│       └── kong.txt #空文件
├── hosts
├── lost+found
└── passwd
4 directories, 5 files
恢復(fù)后:
[root@localhost test]# tree  RECOVERED_FILES/
RECOVERED_FILES/
├── a
│   ├── a.txt
│   └── b
│       └── a.txt
├── hosts
└── passwd
2 directories, 4 files

extundelete在恢復(fù)文件的時(shí)候能不能自動(dòng)創(chuàng)建空文件和目錄?
答:不能

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI