欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Linux利用lsof/extundelete工具恢復(fù)誤刪除的文件或目錄

 更新時間:2020年08月23日 16:42:08   作者:21運維  
這篇文章主要給大家介紹了關(guān)于Linux利用lsof/extundelete工具恢復(fù)誤刪除的文件或目錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

Linux不像windows有那么顯眼的回收站,不是簡單的還原就可以了。

linux刪除文件還原可以分為兩種情況,一種是刪除以后在進程存在刪除信息,一種是刪除以后進程都找不到,只有借助于工具還原。這里分別檢查介紹下

一,誤刪除文件進程還在的情況。

這種一般是有活動的進程存在持續(xù)標(biāo)準(zhǔn)輸入或輸出,到時文件被刪除后,進程PID還是存在。這也就是有些服務(wù)器刪除一些文件但是磁盤不釋放的原因。比如當(dāng)前舉例說明:

通過一個shell終端對一個測試文件做cat追加操作:

[root@21yunwei_backup ~]# echo "hello py" > testdelete.py
[root@21yunwei_backup ~]# cat >> testdelete.py 
hello delete

另外一個終端查看這個文件可以清楚看到內(nèi)容:

[root@21yunwei_backup ~]# cat testdelete.py 
hello py
hello delete

此時,在當(dāng)前服務(wù)器刪除文件rm -f ./testdelete.py

命令查看這個目錄,文件已經(jīng)不存在了,那么現(xiàn)在我們將其恢復(fù)出來。

1,lsof查看刪除的文件進程是否還存在。這里用到一個命令lsof,如沒有安裝請自行yum或者apt-get。類似這種情況,我們可以先lsof查看刪除的文件 是否還在:

[root@21yunwei_backup ~]# lsof | grep deleted
mysqld  1512 mysql 5u  REG    252,3   0 6312397 /tmp/ibzW3Lot (deleted)
cat  20464 root 1w  REG    252,3   23 1310722 /root/testdelete.py (deleted)

幸運的是這種情況進程還存在 ,那么開始進行恢復(fù) 操作。

2,恢復(fù)。

恢復(fù)命令:

cp /proc/pid/fd/1 /指定目錄/文件名

進入 進程目錄,一般是進入/proc/pid/fd/,針對當(dāng)前情況:

[root@21yunwei_backup ~]# cd /proc/20464/fd
[root@21yunwei_backup fd]# ll
total 0
lrwx------ 1 root root 64 Nov 15 18:12 0 > /dev/pts/1
l-wx------ 1 root root 64 Nov 15 18:12 1 > /root/testdelete.py (deleted)
lrwx------ 1 root root 64 Nov 15 18:12 2 > /dev/pts/1

恢復(fù)操作:

cp 1 /tmp/testdelete.py

查看文件:

[root@21yunwei_backup fd]# cat /tmp/testdelete.py
hello py
hello delete

恢復(fù)完成。

二,誤刪除的文件進程已經(jīng)不存在,借助于工具還原。

創(chuàng)建準(zhǔn)備刪除的目錄并echo一個 帶有內(nèi)容的文件:

[root@21yunwei_backup 21yunwei]# tree
.
├── deletetest
│ └── mail
│  └── test.py
├── lost+found
└── passwd
 
3 directories, 2 files
[root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py 
hello Dj
[root@21yunwei_backup 21yunwei]# tail -2 passwd 
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

執(zhí)行刪除操作:

[root@21yunwei_backup 21yunwei]# rm -rf ./*
[root@21yunwei_backup 21yunwei]# ll
total 0

現(xiàn)在開始進行誤刪除文件的恢復(fù)。這種情況一般是沒有守護進行或者后臺進程對其持續(xù)輸入,所以刪除就刪除 了,lsof也看不到。就要借助于工具。這里我們采用的工具是extundelete第三方工具。恢復(fù)步驟如下:

1,停止對當(dāng)前分區(qū)做任何操作,防止inode被覆蓋。inode被覆蓋基本就告別自行車了。比如停止所在分區(qū)的服務(wù),卸載目錄所在的設(shè)備,有必要的情況下都可以斷網(wǎng)。

2,通過dd命令對 當(dāng)前分區(qū)進行備份,防止第三方軟件恢復(fù)失敗導(dǎo)致數(shù)據(jù)丟失。適合數(shù)據(jù)非常重要的情況,這里測試,就沒有備份,如備份可以考慮如下方式:

dd if=/path/filename of=/dev/vdc1

3,通過umount命令,對當(dāng)前設(shè)備分區(qū)卸載。或者fuser 命令。

umount /dev/vdb1 或者 umount /21yunwei

如果提示設(shè)備busy,可以用fuser命令強制卸載:fuser -m -v -i -k /21yunwei

4,下載第三方工具extundelete安裝,搜索誤刪除的文件進行還原。

wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
tar jxvf extundelete-0.2.4.tar.bz2
cd extundelete-0.2.4
./configure 
make
make install

掃描誤刪除的文件:

[root@21yunwei_backup extundelete-0.2.4]# extundelete --inode 2 /dev/vdb1
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Group: 0
Contents of inode 2:
 
.
.省略N行
 
File name          | Inode number | Deleted status
.             2
..            2
lost+found          11    Deleted
deletetest          12    Deleted
passwd           14    Deleted
 

通過掃描發(fā)現(xiàn)了我們刪除的文件夾,現(xiàn)在執(zhí)行恢復(fù)操作。

(1)恢復(fù)單一文件passwd

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-file passwd  
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Successfully restored file passwd
 

恢復(fù)文件是放到了當(dāng)前目錄RECOVERED_FILES。

查看恢復(fù)的文件:

[root@21yunwei_backup /]# tail -5 RECOVERED_FILES/passwd 
mysql:x:497:500::/home/mysql:/bin/false
nginx:x:496:501::/home/nginx:/sbin/nologin
zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

(2)恢復(fù)目錄deletetest

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-directory deletetest 
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Searching for recoverable inodes in directory deletetest ... 
5 recoverable inodes found.
Looking through the directory structure for deleted files ... 
[root@21yunwei_backup /]# cat RECOVERED_FILES/deletetest/mail/test.py 
hello Dj

(3)恢復(fù)所有

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Searching for recoverable inodes in directory / ... 
5 recoverable inodes found.
Looking through the directory structure for deleted files ... 
0 recoverable inodes still lost. 
[root@21yunwei_backup /]# cd RECOVERED_FILES/
[root@21yunwei_backup RECOVERED_FILES]# tree
.
├── deletetest
│  └── mail
│    └── test.py
└── passwd
 
2 directories, 2 files

(4),恢復(fù)指定inode。

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
[root@21yunwei_backup /]# tail -5  /RECOVERED_FILES/file.14 
mysql:x:497:500::/home/mysql:/bin/false
nginx:x:496:501::/home/nginx:/sbin/nologin
zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

注意恢復(fù)inode的時候,恢復(fù) 出來的文件名和之前不一樣,需要單獨進行改名。內(nèi)容是沒問題的。

更多的extundelete用法請參考extundelete –help選項參數(shù)說明,當(dāng)前恢復(fù)所有的操作完成。

總結(jié)

到此這篇關(guān)于Linux利用lsof/extundelete工具恢復(fù)誤刪除的文件或目錄的文章就介紹到這了,更多相關(guān)Linux恢復(fù)誤刪除的文件或目錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • CentOS7搭建gerrit 代碼審查服務(wù)方法

    CentOS7搭建gerrit 代碼審查服務(wù)方法

    下面小編就為大家分享一篇CentOS7搭建gerrit 代碼審查服務(wù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • centos7下如何安裝ftp服務(wù)

    centos7下如何安裝ftp服務(wù)

    這篇文章主要介紹了centos7下如何安裝ftp服務(wù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • linux 磁盤轉(zhuǎn)移空間的方法

    linux 磁盤轉(zhuǎn)移空間的方法

    本篇文章主要介紹了linux 磁盤轉(zhuǎn)移空間的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 干貨 | Linux新手入門好書推薦

    干貨 | Linux新手入門好書推薦

    今天在知乎上看到了這樣一個問答:學(xué)習(xí)操作系統(tǒng)的知識,看哪本書好?讀完之后,我決定理一下操作系統(tǒng)方面的好書推薦給需要學(xué)習(xí)這個方向知識的人。下面這篇文章主要給Linux新手們推薦了一些入門的好書,需要的朋友可以參考下。
    2017-10-10
  • apache后綴名支持 讓apache支持apk ipk下載的方法

    apache后綴名支持 讓apache支持apk ipk下載的方法

    一般都在mime.types文件中添加相應(yīng)的后綴,重啟apache后即可
    2012-04-04
  • linux中高并發(fā)socket最大連接數(shù)的優(yōu)化詳解

    linux中高并發(fā)socket最大連接數(shù)的優(yōu)化詳解

    這篇文章主要給大家介紹了關(guān)于linux中高并發(fā)socket最大連接數(shù)優(yōu)化的相關(guān)資料,文中介紹的很詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-02-02
  • Apache 的 httpd.conf 中文詳解

    Apache 的 httpd.conf 中文詳解

    Apache 的 httpd.conf 中文詳解,需要的朋友可以參考下。
    2009-11-11
  • Linux修改主機名命令詳解

    Linux修改主機名命令詳解

    在本篇文章里小編給大家整理的關(guān)于Linux修改主機名命令的知識點詳解,有興趣的朋友可以參考學(xué)習(xí)下。
    2020-02-02
  • 在Ubuntu Linux上安裝和使用Git和GitHub

    在Ubuntu Linux上安裝和使用Git和GitHub

    今天小編就為大家分享一篇關(guān)于在Ubuntu Linux上安裝和使用Git和GitHub的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-09-09
  • LNMP系列教程之 設(shè)置301重定向的方法

    LNMP系列教程之 設(shè)置301重定向的方法

    因為LNMP系統(tǒng)環(huán)境采用的是Nginx而不是Apache,所以在設(shè)置301重定向的時候,不是簡單的在根目錄.htaccess文件添加代碼就可以實現(xiàn),因為他需要對單獨的網(wǎng)站conf文件進行修改配置
    2012-09-09

最新評論