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

Linux下rm誤刪除文件的三種恢復(fù)方法

 更新時(shí)間:2022年12月01日 14:23:55   作者:NEU_ShuaiCheng  
一下午寫的程序就被rm掉了,幸好只是一個(gè)文件,第二天很快又重新寫了一遍。但是很多人可能就不像我這么幸運(yùn)了,本文收集了一些在Linux下恢復(fù)rm刪除的文件的方法,需要的朋友可以參考下

對于rm,很多人都有慘痛的教訓(xùn)。我也遇到一次,一下午寫的程序就被rm掉了,幸好只是一個(gè)文件,第二天很快又重新寫了一遍。但是很多人可能就不像我這么幸運(yùn)了。本文收集了一些在Linux下恢復(fù)rm刪除的文件的方法,給大家作為參考。

1.幾點(diǎn)建議避免誤刪

 首先,最好的方法是避免這個(gè)問題,以下是幾點(diǎn)建議:

  1、rm -rf誤操作的后果是可怕的,rm -f也要三思而行,不能輕易使用。

  2、做好數(shù)據(jù)備份。

  3、用一些策略避免出錯(cuò):

  提倡在shell下用 TAB 補(bǔ)全,用腳本執(zhí)行任務(wù),減少出錯(cuò)的機(jī)會。或者編寫一個(gè)腳本,起名rm,在腳本里將真實(shí)的rm改為mv ,將刪除的都mv到一個(gè)指定的目錄里面,定期清理。

  那么rm刪除的文件還能恢復(fù)嗎?

  rm的man里面有如下說法:

  請注意,如果使用 rm 來刪除文件,通常仍可以將該文件恢復(fù)原狀。如果想保證該文件的內(nèi)容無法還原,請考慮使用 shred。

  所以理論上rm刪除的文件是還能恢復(fù)的。刪掉文件其實(shí)只是將指向數(shù)據(jù)塊的索引點(diǎn)(information nodes)釋放,只要不被覆蓋,數(shù)據(jù)其實(shí)還在硬盤上,關(guān)鍵在于找出索引點(diǎn),然后將其所指數(shù)據(jù)塊內(nèi)的數(shù)據(jù)抓出,再保存到另外的分區(qū)。在用rm誤刪除文件后,我們要做的第一件事就是保證不再向誤刪文件的分區(qū)寫數(shù)據(jù)。

2.使用lsof命令恢復(fù)

lsof命令用于查看你進(jìn)程開打的文件,打開文件的進(jìn)程,進(jìn)程打開的端口(TCP、UDP)。找回/恢復(fù)刪除的文件。是十分方便的系統(tǒng)監(jiān)視工具,因?yàn)閘sof命令需要訪問核心內(nèi)存和各種文件,所以需要root用戶執(zhí)行。

在linux環(huán)境下,任何事物都以文件的形式存在,通過文件不僅僅可以訪問常規(guī)數(shù)據(jù),還可以訪問網(wǎng)絡(luò)連接和硬件。所以如傳輸控制協(xié)議 (TCP) 和用戶數(shù)據(jù)報(bào)協(xié)議 (UDP) 套接字等,系統(tǒng)在后臺都為該應(yīng)用程序分配了一個(gè)文件描述符,無論這個(gè)文件的本質(zhì)如何,該文件描述符為應(yīng)用程序與基礎(chǔ)操作系統(tǒng)之間的交互提供了通用接口。因?yàn)閼?yīng)用程序打開文件的描述符列表提供了大量關(guān)于這個(gè)應(yīng)用程序本身的信息,因此通過lsof工具能夠查看這個(gè)列表對系統(tǒng)監(jiān)測以及排錯(cuò)將是很有幫助的。

1.語法

lsof(選項(xiàng))

2.參數(shù)

-a:列出打開文件存在的進(jìn)程; 
-c<進(jìn)程名>:列出指定進(jìn)程所打開的文件; 
-g:列出GID號進(jìn)程詳情; 
-d<文件號>:列出占用該文件號的進(jìn)程; 
+d<目錄>:列出目錄下被打開的文件; 
+D<目錄>:遞歸列出目錄下被打開的文件; 
-n<目錄>:列出使用NFS的文件; 
-i<條件>:列出符合條件的進(jìn)程。(4、6、協(xié)議、:端口、 @ip ) 
-p<進(jìn)程號>:列出指定進(jìn)程號所打開的文件; 
-u:列出UID號進(jìn)程詳情; 
-h:顯示幫助信息; 
-v:顯示版本信息。

3.使用

查看

lsof -i:(端口) 查看這個(gè)端口有那些進(jìn)程在訪問,比如22端口

shell> lsof -i:22
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     1939 root    3u  IPv4  12317      0t0  TCP *:ssh (LISTEN)
sshd     1939 root    4u  IPv6  12321      0t0  TCP *:ssh (LISTEN)
sshd     2790 root    3u  IPv4  15229      0t0  TCP 192.168.178.128:ssh->192.168.178.1:64601 (ESTABLISHED)
sshd     2824 root    3u  IPv4  15528      0t0  TCP 192.168.178.128:ssh->192.168.178.1:64673 (ESTABLISHED)
sshd     2990 root    3u  IPv4  15984      0t0  TCP 192.168.178.128:ssh->192.168.178.1:64686 (ESTABLISHED)
sshd    14695 root    3u  IPv4  39558      0t0  TCP 192.168.178.128:ssh->192.168.178.1:49662 (ESTABLISHED)

lsof輸出各列信息的意義如下:

  • COMMAND:進(jìn)程的名稱
  • PID:進(jìn)程標(biāo)識符
  • USER:進(jìn)程所有者
  • FD:文件描述符,應(yīng)用程序通過文件描述符識別該文件。如cwd、txt等
  • TYPE:文件類型,如DIR、REG等
  • DEVICE:指定磁盤的名稱
  • SIZE:文件的大小
  • NODE:索引節(jié)點(diǎn)(文件在磁盤上的標(biāo)識)
  • NAME:打開文件的確切名稱

恢復(fù)文件

利用lsof可以恢復(fù)一些系統(tǒng)日志,前提是這個(gè)進(jìn)程必須存在。這里就拿最常用的/var/log/messages來舉例說明,大家在做測試的時(shí)候最好先備份一下。

#備份
shell> cp /var/log/message /var/log/message_bac
shell> lsof |grep /var/log/message
rsyslogd   1737      root    1w      REG                8,2   5716123     652638 /var/log/messages

進(jìn)程在運(yùn)行中,接下來我就把/var/log/messages這個(gè)文件刪掉

shell> rm /var/log/messages

刪掉之后,我再來看看這個(gè)進(jìn)程的變化

shell> lsof |grep /var/log/messages
rsyslogd   1737      root    1w      REG                8,2   5716123     652638 /var/log/messages (deleted)

大家看到有變化了吧, 對比兩個(gè)之后發(fā)現(xiàn)多了(deleted)。要找到這個(gè)文件在哪還要看看這個(gè)

PID:1737 FD:1 那我們有直接進(jìn)入/proc/1737/FD/1用ll查看一下

shell> cd /proc/1737/fd/
shell> ll

total 0
lrwx------ 1 root root 64 Dec 23 13:00 0 -> socket:[11442]
l-wx------ 1 root root 64 Dec 23 13:00 1 -> /var/log/messages (deleted)
l-wx------ 1 root root 64 Dec 23 13:00 2 -> /var/log/secure
lr-x------ 1 root root 64 Dec 23 13:00 3 -> /proc/kmsg
l-wx------ 1 root root 64 Dec 23 13:00 4 -> /var/log/maillog

看到了1對應(yīng)/var/log/messages (deleted),看看文件是不是我們要的文件:

shell> head -5 1
Nov 14 03:11:11 localhost kernel: imklog 5.8.10, log source = /proc/kmsg started.
Nov 14 03:11:11 localhost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1241" x-info="http://www.rsyslog.com"] start
Nov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpuset
Nov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpu
Nov 14 03:11:11 localhost kernel: Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013

對比備份文件:

shell> head -5 /var/log/message_bac
Nov 14 03:11:11 localhost kernel: imklog 5.8.10, log source = /proc/kmsg started.
Nov 14 03:11:11 localhost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1241" x-info="http://www.rsyslog.com"] start
Nov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpuset
Nov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpu
Nov 14 03:11:11 localhost kernel: Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013

對比發(fā)現(xiàn)數(shù)據(jù)是一樣的,恢復(fù)

shell> cat 1 > /var/log/messages

再次提醒,恢復(fù)前提是這個(gè)進(jìn)程必須存在。

3.使用extundelete工具

extundelete工具安裝

wget https://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2

解壓該文件tar jxvf extundelete-0.2.4.tar.bz2

若報(bào)這種錯(cuò)誤

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 
tar (child): bzip2:無法 exec: 沒有那個(gè)文件或目錄
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

則使用yum -y install bzip2進(jìn)行解決

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 
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
...................................................
cd  extundelete-0.2.4
./configure 

若這步驟報(bào)錯(cuò)

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: in `/root/extundelete-0.2.4':
configure: error: C++ compiler cannot create executables
See `config.log' for more details

則使用yum -y install gcc-c++解決.

若執(zhí)行上一步仍然報(bào)錯(cuò),

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: Can't find ext2fs library

則使用yum -y install e2fsprogs e2fsprogs-devel來解決。
#Ubuntu的解決辦法為sudo apt-get install e2fslibs-dev e2fslibs-dev

不出意外的話到這里應(yīng)該configure能夠順利完成.

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
Writing generated files to disk
[root@docking extundelete-0.2.4]# 

最后make然后 make install

[root@docking extundelete-0.2.4]# make
make -s all-recursive
Making all in src
extundelete.cc: 在函數(shù)‘ext2_ino_t find_inode(ext2_filsys, ext2_filsys, ext2_inode*, std::string, int)'中:
extundelete.cc:1272:29: 警告:在 {} 內(nèi)將‘search_flags'從‘int'轉(zhuǎn)換為較窄的類型‘ext2_ino_t {aka unsigned int}' [-Wnarrowing]
    buf, match_name2, priv, 0};
                             ^
[root@docking extundelete-0.2.4]# make install
Making install in src
  /usr/bin/install -c extundelete '/usr/local/bin'

extundelete安裝完成.

掃描誤刪除的文件:

使用df -lh查看掛載:

taroballs@taroballs-PC:~$ df -lh
文件系統(tǒng)        容量  已用  可用 已用% 掛載點(diǎn)
udev            1.9G     0  1.9G    0% /dev
tmpfs           387M  1.8M  385M    1% /run
/dev/sda2        92G   61G   26G   71% /
tmpfs           1.9G   49M  1.9G    3% /dev/shm
tmpfs           5.0M  4.0K  5.0M    1% /run/lock
tmpfs           1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda3       104G   56G   44G   57% /home
tmpfs           387M   40K  387M    1% /run/user/1000
/dev/sda4        70G   20G   47G   30% /media/taroballs/d8423f8c-d687-4c03-a7c8-06a7fb57f96d
/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs
/dev/sr0        4.0G  4.0G     0  100% /media/taroballs/2018-01-16-12-36-00-00
taroballs@taroballs-PC:~$ cd /media/taroballs/taroballs/
taroballs@taroballs-PC:/media/taroballs/taroballs$ 

可以看到,我們的目錄/media/taroballs/taroballs
掛載到/dev/sdb1 這個(gè)文件系統(tǒng)中.

umount我們的掛載盤比如:

taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs

umount這個(gè)目錄

taroballs@taroballs-PC:~$ umount /media/taroballs/taroballs
taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
taroballs@taroballs-PC:~$ 
#記得刪除一定要后umount哦,不然二次寫入誰也幫不了你呢。

通過inode節(jié)點(diǎn)恢復(fù)

taroballs@taroballs-PC:~$ mkdir recovertest
taroballs@taroballs-PC:~$ cd recovertest/
taroballs@taroballs-PC:~/recovertest$ 

執(zhí)行恢復(fù)extundelete /dev/sdb1 --inode 2

taroballs@taroballs-PC:/media/taroballs/taroballs$ sudo extundelete /dev/sdb1 --inode 2
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
deletetest                                        12             Deleted
tmppasswd                                            14             Deleted

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

taroballs@taroballs-PC:~/recovertest$  extundelete /dev/sdb1 --restore-file passwd   
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Successfully restored file tmppasswd

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

taroballs@taroballs-PC:~/recovertest$ cat tmppasswd 
tcpdump:x:172:72::/:/sbin/nologin

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

extundelete /dev/sdb1 --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 ... 

(3)恢復(fù)所有

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --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. 
taroballs@taroballs-PC:~/recovertest$ tree 
backuptest/
├── deletetest
│   └── innerfolder
│       └── deletefile.txt
└── tmppasswd

2 directories, 2 files

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

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-inode 14
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
taroballs@taroballs-PC:~/recovertest$ cat file.14 
tcpdump:x:172:72::/:/sbin/nologin
#注意恢復(fù)inode的時(shí)候,恢復(fù) 出來的文件名和之前不一樣,需要單獨(dú)進(jìn)行改名。

最后附上extundelete的用法:

$ extundelete --help
Usage: extundelete [options] [--] device-file
Options:
  --version, -[vV]       Print version and exit successfully.
  --help,                Print this help and exit successfully.
  --superblock           Print contents of superblock in addition to the rest.
                         If no action is specified then this option is implied.
  --journal              Show content of journal.
  --after dtime          Only process entries deleted on or after 'dtime'.
  --before dtime         Only process entries deleted before 'dtime'.
Actions:
  --inode ino            Show info on inode 'ino'.
  --block blk            Show info on block 'blk'.
  --restore-inode ino[,ino,...]
                         Restore the file(s) with known inode number 'ino'.
                         The restored files are created in ./RECOVERED_FILES
                         with their inode number as extension (ie, file.12345).
  --restore-file 'path'  Will restore file 'path'. 'path' is relative to root
                         of the partition and does not start with a '/'
                         The restored file is created in the current
                         directory as 'RECOVERED_FILES/path'.
  --restore-files 'path' Will restore files which are listed in the file 'path'.
                         Each filename should be in the same format as an option
                         to --restore-file, and there should be one per line.
  --restore-directory 'path'
                         Will restore directory 'path'. 'path' is relative to the
                         root directory of the file system.  The restored
                         directory is created in the output directory as 'path'.
  --restore-all          Attempts to restore everything.
  -j journal             Reads an external journal from the named file.
  -b blocknumber         Uses the backup superblock at blocknumber when opening
                         the file system.
  -B blocksize           Uses blocksize as the block size when opening the file
                         system.  The number should be the number of bytes.
  --log 0                Make the program silent.
  --log filename         Logs all messages to filename.
--log D1=0,D2=filename   Custom control of log messages with comma-separated
   Examples below:       list of options.  Dn must be one of info, warn, or
   --log info,error      error.  Omission of the '=name' results in messages
   --log warn=0          with the specified level to be logged to the console.
   --log error=filename  If the parameter is '=0', logging for the specified
                         level will be turned off.  If the parameter is
                         '=filename', messages with that level will be written
                         to filename.
   -o directory          Save the recovered files to the named directory.
                         The restored files are created in a directory
                         named 'RECOVERED_FILES/' by default.

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

相關(guān)文章

  • Shell編程之/bin/bash和/bin/sh的區(qū)別淺析

    Shell編程之/bin/bash和/bin/sh的區(qū)別淺析

    在redhat的系統(tǒng)中,sh是一個(gè)到bash的軟鏈接,但是如果運(yùn)行如下程序會產(chǎn)生不一樣的結(jié)果,下面這篇文章主要給大家介紹了關(guān)于Shell編程之/bin/bash和/bin/sh的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Shell?echo、printf、test命令的具體使用

    Shell?echo、printf、test命令的具體使用

    本文主要介紹了Shell?echo、printf、test命令的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • Linux Shell 如何獲取參數(shù)的方法

    Linux Shell 如何獲取參數(shù)的方法

    這篇文章主要介紹了Linux Shell 如何獲取參數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • linux下2個(gè)檢查tcp連接的命令

    linux下2個(gè)檢查tcp連接的命令

    這篇文章主要介紹了linux下2個(gè)檢查tcp連接的命令,需要的朋友可以參考下
    2016-08-08
  • linux下安裝配置Memcache和PHP環(huán)境的實(shí)現(xiàn)

    linux下安裝配置Memcache和PHP環(huán)境的實(shí)現(xiàn)

    下面小編就為大家?guī)硪黄猯inux下安裝配置Memcache和PHP環(huán)境的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • 淺談CI腳本異常退出問題定位

    淺談CI腳本異常退出問題定位

    下面小編就為大家?guī)硪黄獪\談CI腳本異常退出問題定位。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • 一些Linux Shell中的權(quán)限相關(guān)知識總結(jié)

    一些Linux Shell中的權(quán)限相關(guān)知識總結(jié)

    這篇文章主要介紹了一些Linux Shell中的權(quán)限相關(guān)知識總結(jié),使Linux入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-07-07
  • 普通用戶啟動(dòng)supervisor報(bào)HTTP錯(cuò)誤(strace)解決分析

    普通用戶啟動(dòng)supervisor報(bào)HTTP錯(cuò)誤(strace)解決分析

    這篇文章主要為大家介紹了普通用戶啟動(dòng)supervisor報(bào)HTTP錯(cuò)誤strace的問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • linux定時(shí)備份MySQL數(shù)據(jù)庫并刪除以前的備份文件(推薦)

    linux定時(shí)備份MySQL數(shù)據(jù)庫并刪除以前的備份文件(推薦)

    這篇文章主要介紹了linux定時(shí)備份MySQL數(shù)據(jù)庫并刪除以前的備份文件,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • shell命令while循環(huán)中使用sleep命令代碼示例

    shell命令while循環(huán)中使用sleep命令代碼示例

    這篇文章主要介紹了shell命令while循環(huán)中使用sleep命令代碼示例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02

最新評論