在Linux
中,刪除rm
命令使用需謹慎,有時候可能因爲誤操做,致使重要文件刪除了,這時不要太緊張,操做得當的話,仍是能夠恢復的。node
刪除一個文件,實際上並不清除inode
節點和block
的數據,只是在這個文件的父目錄裏面的block
中,刪除這個文件的名字。Linux
是經過Link
的數量來控制文件刪除的,只有當一個文件不存在任何Link
的時候,這個文件纔會被刪除。c++
固然,這裏所指的是完全刪除,即已經不能經過回收站
找回的狀況,好比使用rm -rf
來刪除數據。針對Linux
下的EXT
文件系統,可用的恢復工具備debugfs
、ext3grep
、extundelete
等。 其中extundelete
是一個開源的Linux
數據恢復工具,支持ext3
、ext4
文件系統。數據庫
在數據被誤刪除後,第一時間要作的就是卸載被刪除數據所在的分區,若是是根分區的數據遭到誤刪,就須要將系統進入單用戶模式,而且將根分區以只讀模式掛載。這樣作的緣由很簡單,由於將文件刪除後,僅僅是將文件的inode
節點中的扇區指針清零,實際文件還存儲在磁盤上,若是磁盤繼續以讀寫模式掛載,這些已刪除的文件的數據塊就可能被操做系統從新分配出去,在這些數據庫被新的數據覆蓋後,這些數據就真的丟失了,恢復工具也迴天無力。因此以只讀模式掛載磁盤能夠儘可能下降數據庫中數據被覆蓋的風險,以提升恢復數據成功的比例。session
在編譯安裝extundelete
以前須要先安裝兩個依賴包e2fsprogs-libs
和e2fsprogs-devel
,這兩個包在系統安裝光盤的/Package
目錄下就有,使用rpm
或yum
命令將其安裝。e2fsprogs-devel
安裝依賴於libcom_err-devel
包。dom
1.系統使用的是rhel6.5
,掛載光盤,安裝依賴包,這裏使用的是rpm
安裝方式。工具
[root@localhost ~]# mkdir /mnt/cdrom [root@localhost ~]# mount /dev/cdrom /mnt/cdrom/ mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cd /mnt/cdrom/Packages/
[root@localhost Packages]# rpm -ivh e2fsprogs-libs-1.41.12-18.el6.x86_64.rpm warning: e2fsprogs-libs-1.41.12-18.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ########################################### [100%] package e2fsprogs-libs-1.41.12-18.el6.x86_64 is already installed
[root@localhost Packages]# rpm -ivh libcom_err-devel-1.41.12-18.el6.x86_64.rpm warning: libcom_err-devel-1.41.12-18.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ########################################### [100%] 1:libcom_err-devel ########################################### [100%]
[root@localhost Packages]# rpm -ivh e2fsprogs-devel-1.41.12-18.el6.x86_64.rpm warning: e2fsprogs-devel-1.41.12-18.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ########################################### [100%] 1:e2fsprogs-devel ########################################### [100%]
2.建立本地yum
源,安裝編譯環境。post
[root@localhost ~]# yum install gcc gcc-c++ -y
3.解壓extundelete
軟件包。測試
[root@localhost ~]# tar jxvf extundelete-0.2.4.tar.bz2 -C ~ extundelete-0.2.4/ extundelete-0.2.4/acinclude.m4 extundelete-0.2.4/missing extundelete-0.2.4/autogen.sh extundelete-0.2.4/aclocal.m4 extundelete-0.2.4/configure extundelete-0.2.4/LICENSE extundelete-0.2.4/README extundelete-0.2.4/install-sh extundelete-0.2.4/config.h.in extundelete-0.2.4/src/ extundelete-0.2.4/src/extundelete.cc extundelete-0.2.4/src/block.h extundelete-0.2.4/src/kernel-jbd.h extundelete-0.2.4/src/insertionops.cc extundelete-0.2.4/src/block.c extundelete-0.2.4/src/cli.cc extundelete-0.2.4/src/extundelete-priv.h extundelete-0.2.4/src/extundelete.h extundelete-0.2.4/src/jfs_compat.h extundelete-0.2.4/src/Makefile.in extundelete-0.2.4/src/Makefile.am extundelete-0.2.4/configure.ac extundelete-0.2.4/depcomp extundelete-0.2.4/Makefile.in extundelete-0.2.4/Makefile.am
4.配置、編譯、安裝extundelete
軟件包this
[root@localhost ~]# cd extundelete-0.2.4 [root@localhost extundelete-0.2.4]# ls acinclude.m4 aclocal.m4 autogen.sh config.h.in configure configure.ac depcomp install-sh LICENSE Makefile.am Makefile.in missing README src [root@localhost extundelete-0.2.4]# ./configure Configuring extundelete 0.2.4 Writing generated files to disk [root@localhost extundelete-0.2.4]# make make -s all-recursive Making all in src extundelete.cc:571: 警告:未使用的參數‘flags’ [root@localhost extundelete-0.2.4]# make install Making install in src /usr/bin/install -c extundelete '/usr/local/bin'
5.準備好用於測試的分區,/dev/sdb1
爲ext4
格式,掛載到/mnt/ext4
目錄下。操作系統
[root@localhost ~]# mkdir /mnt/ext4 [root@localhost ~]# mount /dev/sdb1 /mnt/ext4/ [root@localhost ~]# df -hT /mnt/ext4/ Filesystem Type Size Used Avail Use% Mounted on /dev/sdb1 ext4 20G 172M 19G 1% /mnt/ext4
6.建立測試文件。
[root@localhost ~]# cd /mnt/ext4/ [root@localhost ext4]# echo 1 > a [root@localhost ext4]# echo 2 > b [root@localhost ext4]# echo 3 > c [root@localhost ext4]# ls a b c lost+found
7.刪除測試文件。
[root@localhost ext4]# rm -f a b [root@localhost ext4]# ls c lost+found
8.卸載對應的分區。
[root@localhost ext4]# cd [root@localhost ~]# umount /mnt/ext4/
9.恢復刪除的內容。
[root@localhost ~]# extundelete /dev/sdb1 --restore-all NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 160 groups loaded. Loading journal descriptors ... 24 descriptors loaded. Searching for recoverable inodes in directory / ... 2 recoverable inodes found. Looking through the directory structure for deleted files ... 0 recoverable inodes still lost.
10.恢復的文件會在在當前目錄下的RECOVERED_FILES
文件夾內。
[root@localhost ~]# ls RECOVERED_FILES/ a b
extundelete
工具僅能夠恢復EXT
類型的文件,沒法恢復CentOS 7
系統默認採用xfs
類型的文件。針對xfs
文件系統目前也沒有比較成熟的文件恢復工具,因此建議提早作好數據備份,以免數據丟失。
xfs
類型的文件可以使用xfsdump
與xfsrestore
工具進行備份恢復。若系統中未安裝xfsdump
與xfsrestore
工具,能夠經過yum install -y xfsdump
命令安裝。xfsdump
按照inode
順序備份一個xfs
文件系統。
xfsdump
的備份級別有兩種:0
表示徹底備份;1-9
表示增量備份。默認爲0
。
xfsdump -f 備份存放位置 要備份路徑或設備文件
-f
:指定備份文件目錄
-L
:指定標籤session label
-M
:指定設備標籤media label
-s
:備份單個文件,-s
後面不能直接跟路徑。
xfsdump
時,須要注意如下的幾個限制:1.xfsdump
不支持沒有掛載的文件系統備份,因此只能備份已掛載的;
2.xfsdump
必須使用root
的權限才能操做(涉及文件系統的關係);
3.xfsdump
只能備份XFS
文件系統;
4.xfsdump
備份下來的數據(檔案或儲存媒體)只能讓xfsrestore
解析;
5.xfsdump
是透過文件系統的UUID
來分辨各個備份檔的,所以不能備份兩個具備相同UUID
的文件系統。
xfsrestore -f 恢復文件的位置 存放恢復後文件的路徑
1.準備好用於測試的分區,/dev/sdb1
爲ext4
格式,掛載到/mnt/ext4
目錄下。
[root@localhost ~]# mkdir /mnt/xfs [root@localhost ~]# mount /dev/sdb1 /mnt/xfs/ [root@localhost ~]# df -hT /mnt/xfs/ Filesystem Type Size Used Avail Use% Mounted on /dev/sdb1 xfs 20G 33M 20G 1% /mnt/xfs
2.建立測試文件。
[root@localhost ~]# cd /mnt/xfs/ [root@localhost xfs]# mkdir test [root@localhost xfs]# touch a.txt [root@localhost xfs]# touch test/b.txt
3.可使用tree
查看目錄結構。
[root@localhost ~]# yum install tree -y [root@localhost ~]# tree /mnt/xfs/ /mnt/xfs/ ├── a.txt └── test └── b.txt 1 directory, 2 files
4.使用xfsdump
命令備份整個分區。
[root@localhost ~]# xfsdump -f /opt/dump_sdb1 /dev/sdb1 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.4 (dump format 3.0) - type ^C for status and control ============================= dump label dialog ============================== please enter label for this dump session (timeout in 300 sec) -> dump_sdb1 //指定備份會話標籤 session label entered: "dump_sdb1" --------------------------------- end dialog --------------------------------- xfsdump: level 0 dump of localhost.localdomain:/mnt/xfs xfsdump: dump date: Fri Sep 6 13:36:12 2019 xfsdump: session id: 74232f85-124c-4486-8d91-f35208534f74 xfsdump: session label: "dump_sdb1" xfsdump: ino map phase 1: constructing initial dump list xfsdump: ino map phase 2: skipping (no pruning necessary) xfsdump: ino map phase 3: skipping (only one dump stream) xfsdump: ino map construction complete xfsdump: estimated dump size: 21760 bytes xfsdump: /var/lib/xfsdump/inventory created ============================= media label dialog ============================= please enter label for media in drive 0 (timeout in 300 sec) -> sdb1 //指定設備標籤,就是對要備份的設備作一個描述 media label entered: "sdb1" --------------------------------- end dialog --------------------------------- xfsdump: creating dump session media file 0 (media 0, file 0) xfsdump: dumping ino map xfsdump: dumping directories xfsdump: dumping non-directory files xfsdump: ending media file xfsdump: media file size 22952 bytes xfsdump: dump size (non-dir files) : 0 bytes xfsdump: dump complete: 46 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /opt/dump_sdb1 OK (success) xfsdump: Dump Status: SUCCESS
5.查看備份信息與內容。
[root@localhost ~]# xfsdump -I file system 0: fs id: f8805a3e-089e-4875-ad54-d31e5dc98835 session 0: mount point: localhost.localdomain:/mnt/xfs device: localhost.localdomain:/dev/sdb1 time: Fri Sep 6 13:36:12 2019 session label: "dump_sdb1" session id: 74232f85-124c-4486-8d91-f35208534f74 level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /opt/dump_sdb1 start: ino 68 offset 0 end: ino 70 offset 0 interrupted: NO media files: 1 media file 0: mfile index: 0 mfile type: data mfile size: 22952 mfile start: ino 68 offset 0 mfile end: ino 70 offset 0 media label: "sdb1" media id: cc32446f-42e8-489b-867f-84a55949c1fa xfsdump: Dump Status: SUCCESS
6.刪除建立的測試文件,模擬數據丟失。
[root@localhost ~]# rm -rf /mnt/xfs/* [root@localhost ~]# tree /mnt/xfs/ /mnt/xfs/ 0 directories, 0 files
7.恢復文件丟失的文件。
[root@localhost ~]# xfsrestore -f /opt/dump_sdb1 /mnt/xfs/ xfsrestore: using file dump (drive_simple) strategy xfsrestore: version 3.1.4 (dump format 3.0) - type ^C for status and control xfsrestore: searching media for dump xfsrestore: examining media file 0 xfsrestore: dump description: xfsrestore: hostname: localhost.localdomain xfsrestore: mount point: /mnt/xfs xfsrestore: volume: /dev/sdb1 xfsrestore: session time: Fri Sep 6 13:36:12 2019 xfsrestore: level: 0 xfsrestore: session label: "dump_sdb1" xfsrestore: media label: "sdb1" xfsrestore: file system id: f8805a3e-089e-4875-ad54-d31e5dc98835 xfsrestore: session id: 74232f85-124c-4486-8d91-f35208534f74 xfsrestore: media id: cc32446f-42e8-489b-867f-84a55949c1fa xfsrestore: using online session inventory xfsrestore: searching media for directory dump xfsrestore: reading directories xfsrestore: 2 directories and 3 entries processed xfsrestore: directory post-processing xfsrestore: restoring non-directory files xfsrestore: restore complete: 0 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /opt/dump_sdb1 OK (success) xfsrestore: Restore Status: SUCCESS [root@localhost ~]# tree /mnt/xfs/ /mnt/xfs/ ├── a.txt └── test └── b.txt 1 directory, 2 files