對(duì)于rm,很多人都有慘痛的教訓(xùn)。我也遇到一次,一下午寫(xiě)的程序就被rm掉了,幸好只是一個(gè)文件,第二天很快又重新寫(xiě)了一遍。但是很多人可能就不像我這么幸運(yùn)了。本文收集了一些在linux下恢復(fù)rm刪除的文件的方法,給大家作為參考。
1.幾點(diǎn)建議避免誤刪
首先,最好的方法是避免這個(gè)問(wèn)題,以下是幾點(diǎn)建議:
1、rm -rf誤操作的后果是可怕的,rm -f也要三思而行,不能輕易使用。
2、做好數(shù)據(jù)備份。
3、用一些策略避免出錯(cuò):
提倡在shell下用 TAB 補(bǔ)全,用腳本執(zhí)行任務(wù),減少出錯(cuò)的機(jī)會(huì)。或者編寫(xiě)一個(gè)腳本,起名rm,在腳本里將真實(shí)的rm改為mv ,將刪除的都mv到一個(gè)指定的目錄里面,定期清理。
那么rm刪除的文件還能恢復(fù)嗎?
rm的man里面有如下說(shuō)法:
請(qǐng)注意,如果使用 rm 來(lái)刪除文件,通常仍可以將該文件恢復(fù)原狀。如果想保證該文件的內(nèi)容無(wú)法還原,請(qǐng)考慮使用 shred。
所以理論上rm刪除的文件是還能恢復(fù)的。刪掉文件其實(shí)只是將指向數(shù)據(jù)塊的索引點(diǎn)(information nodes)釋放,只要不被覆蓋,數(shù)據(jù)其實(shí)還在硬盤(pán)上,關(guān)鍵在于找出索引點(diǎn),然后將其所指數(shù)據(jù)塊內(nèi)的數(shù)據(jù)抓出,再保存到另外的分區(qū)。在用rm誤刪除文件后,我們要做的第一件事就是保證不再向誤刪文件的分區(qū)寫(xiě)數(shù)據(jù)。
2.使用lsof命令恢復(fù)
lsof命令用于查看你進(jìn)程開(kāi)打的文件,打開(kāi)文件的進(jìn)程,進(jìn)程打開(kāi)的端口(TCP、UDP)。找回/恢復(fù)刪除的文件。是十分方便的系統(tǒng)監(jiān)視工具,因?yàn)閘sof命令需要訪問(wèn)核心內(nèi)存和各種文件,所以需要root用戶執(zhí)行。
在linux環(huán)境下,任何事物都以文件的形式存在,通過(guò)文件不僅僅可以訪問(wèn)常規(guī)數(shù)據(jù),還可以訪問(wèn)網(wǎng)絡(luò)連接和硬件。所以如傳輸控制協(xié)議 (TCP) 和用戶數(shù)據(jù)報(bào)協(xié)議 (UDP) 套接字等,系統(tǒng)在后臺(tái)都為該應(yīng)用程序分配了一個(gè)文件描述符,無(wú)論這個(gè)文件的本質(zhì)如何,該文件描述符為應(yīng)用程序與基礎(chǔ)操作系統(tǒng)之間的交互提供了通用接口。因?yàn)閼?yīng)用程序打開(kāi)文件的描述符列表提供了大量關(guān)于這個(gè)應(yīng)用程序本身的信息,因此通過(guò)lsof工具能夠查看這個(gè)列表對(duì)系統(tǒng)監(jiān)測(cè)以及排錯(cuò)將是很有幫助的。
1.語(yǔ)法
lsof(選項(xiàng))
2.參數(shù)
-a:列出打開(kāi)文件存在的進(jìn)程;-c<進(jìn)程名>:列出指定進(jìn)程所打開(kāi)的文件;-g:列出GID號(hào)進(jìn)程詳情;-d<文件號(hào)>:列出占用該文件號(hào)的進(jìn)程;+d<目錄>:列出目錄下被打開(kāi)的文件;+D<目錄>:遞歸列出目錄下被打開(kāi)的文件;-n<目錄>:列出使用NFS的文件;-i<條件>:列出符合條件的進(jìn)程。(4、6、協(xié)議、:端口、 @ip )-p<進(jìn)程號(hào)>:列出指定進(jìn)程號(hào)所打開(kāi)的文件;-u:列出UID號(hào)進(jìn)程詳情;-h:顯示幫助信息;-v:顯示版本信息。
3.使用
lsof -i:(端口) 查看這個(gè)端口有那些進(jìn)程在訪問(wèn),比如22端口
shell> lsof -i:22COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEsshd 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)程的名稱(chēng)
- PID:進(jìn)程標(biāo)識(shí)符
- USER:進(jìn)程所有者
- FD:文件描述符,應(yīng)用程序通過(guò)文件描述符識(shí)別該文件。如cwd、txt等
- TYPE:文件類(lèi)型,如DIR、REG等
- DEVICE:指定磁盤(pán)的名稱(chēng)
- SIZE:文件的大小
- NODE:索引節(jié)點(diǎn)(文件在磁盤(pán)上的標(biāo)識(shí))
- NAME:打開(kāi)文件的確切名稱(chēng)
恢復(fù)文件
利用lsof可以恢復(fù)一些系統(tǒng)日志,前提是這個(gè)進(jìn)程必須存在。這里就拿最常用的/var/log/messages來(lái)舉例說(shuō)明,大家在做測(cè)試的時(shí)候最好先備份一下。
#備份shell> cp /var/log/message /var/log/message_bacshell> lsof |grep /var/log/messagersyslogd 1737 root 1w REG 8,2 5716123 652638 /var/log/messages
進(jìn)程在運(yùn)行中,接下來(lái)我就把/var/log/messages這個(gè)文件刪掉
shell> rm /var/log/messages
刪掉之后,我再來(lái)看看這個(gè)進(jìn)程的變化
shell> lsof |grep /var/log/messagesrsyslogd 1737 root 1w REG 8,2 5716123 652638 /var/log/messages (deleted)
大家看到有變化了吧, 對(duì)比兩個(gè)之后發(fā)現(xiàn)多了(deleted)。要找到這個(gè)文件在哪還要看看這個(gè)
PID:1737 FD:1 那我們有直接進(jìn)入/proc/1737/FD/1用ll查看一下
shell> cd /proc/1737/fd/shell> lltotal 0lrwx------ 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/securelr-x------ 1 root root 64 Dec 23 13:00 3 -> /proc/kmsgl-wx------ 1 root root 64 Dec 23 13:00 4 -> /var/log/maillog
看到了1對(duì)應(yīng)/var/log/messages (deleted),看看文件是不是我們要的文件:
shell> head -5 1Nov 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"] startNov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpusetNov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpuNov 14 03:11:11 localhost kernel: Linux version 2.6.32-431.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013
對(duì)比備份文件:
shell> head -5 /var/log/message_bacNov 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"] startNov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpusetNov 14 03:11:11 localhost kernel: Initializing cgroup subsys cpuNov 14 03:11:11 localhost kernel: Linux version 2.6.32-431.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013
對(duì)比發(fā)現(xiàn)數(shù)據(jù)是一樣的,恢復(fù)
shell> cat 1 > /var/log/messages
再次提醒,恢復(fù)前提是這個(gè)進(jìn)程必須存在。
3.使用extundelete工具
extundelete工具安裝
- extundelete下載地址:http://extundelete.sourceforge.NET/
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.bz2tar (child): bzip2:無(wú)法 exec: 沒(méi)有那個(gè)文件或目錄tar (child): Error is not recoverable: exiting nowtar: Child returned status 2tar: Error is not recoverable: exiting now
則使用yum -y install bzip2進(jìn)行解決
[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2extundelete-0.2.4/extundelete-0.2.4/acinclude.m4extundelete-0.2.4/missingextundelete-0.2.4/autogen.shextundelete-0.2.4/aclocal.m4extundelete-0.2.4/configureextundelete-0.2.4/LICENSEextundelete-0.2.4/README
cd extundelete-0.2.4./configure
若這步驟報(bào)錯(cuò)
[root@docking extundelete-0.2.4]# ./configureConfiguring extundelete 0.2.4configure: error: in `/root/extundelete-0.2.4':configure: error: C++ compiler cannot create executablesSee `config.log' for more details
則使用yum -y install gcc-c++解決.
若執(zhí)行上一步仍然報(bào)錯(cuò),
[root@docking extundelete-0.2.4]# ./configureConfiguring extundelete 0.2.4configure: error: Can't find ext2fs library
則使用yum -y install e2fsprogs e2fsprogs-devel來(lái)解決。
#Ubuntu的解決辦法為sudo apt-get install e2fslibs-dev e2fslibs-dev
不出意外的話到這里應(yīng)該configure能夠順利完成.
[root@docking extundelete-0.2.4]# ./configureConfiguring extundelete 0.2.4Writing generated files to disk[root@docking extundelete-0.2.4]#
最后make然后 make install
[root@docking extundelete-0.2.4]# makemake -s all-recursiveMaking all in srcextundelete.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)換為較窄的類(lèi)型‘ext2_ino_t {aka unsigned int}’ [-Wnarrowing]buf, match_name2, priv, 0};[root@docking extundelete-0.2.4]# make installMaking 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% /devtmpfs 387M 1.8M 385M 1% /run/dev/sda2 92G 61G 26G 71% /tmpfs 1.9G 49M 1.9G 3% /dev/shmtmpfs 5.0M 4.0K 5.0M 1% /run/locktmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup/dev/sda3 104G 56G 44G 57% /hometmpfs 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-00taroballs@taroballs-PC:~$ cd /media/taroballs/taroballs/taroballs@taroballs-PC:/media/taroballs/taroballs$
可以看到,我們的目錄/media/taroballs/taroballs
掛載到/dev/sdb1 這個(gè)文件系統(tǒng)中.
umount我們的掛載盤(pán)
比如:
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/taroballstaroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1taroballs@taroballs-PC:~$#記得刪除一定要后umount哦,不然二次寫(xiě)入誰(shuí)也幫不了你呢。
通過(guò)inode節(jié)點(diǎn)恢復(fù)
taroballs@taroballs-PC:~$ mkdir recovertesttaroballs@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 2NOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Group: 0Contents of inode 2:.省略N行File name | Inode number | Deleted status. 2.. 2deletetest 12 Deletedtmppasswd 14 Deleted
通過(guò)掃描發(fā)現(xiàn)了我們刪除的文件夾,現(xiàn)在執(zhí)行恢復(fù)操作。
(1)恢復(fù)單一文件tmppasswd
taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-file passwdNOTICE: 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 tmppasswdtcpdump:x:172:72::/:/sbin/nologin
(2)恢復(fù)目錄deletetest
extundelete /dev/sdb1 --restore-directory deletetestNOTICE: 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-allNOTICE: 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$ treebackuptest/├── deletetest│ └── innerfolder│ └── deletefile.txt└── tmppasswd2 directories, 2 files
(4)恢復(fù)指定inode
taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-inode 14NOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Loading journal descriptors ... 46 descriptors loaded.taroballs@taroballs-PC:~/recovertest$ cat file.14tcpdump:x:172:72::/:/sbin/nologin#注意恢復(fù)inode的時(shí)候,恢復(fù) 出來(lái)的文件名和之前不一樣,需要單獨(dú)進(jìn)行改名。
最后附上extundelete的用法:
$ extundelete --helpUsage: extundelete [options] [--] device-fileOptions:--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_FILESwith their inode number as extension (ie, file.12345).--restore-file 'path' Will restore file 'path'. 'path' is relative to rootof the partition and does not start with a '/'The restored file is created in the currentdirectory 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 optionto --restore-file, and there should be one per line.--restore-directory 'path'Will restore directory 'path'. 'path' is relative to theroot directory of the file system. The restoreddirectory 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 openingthe file system.-B blocksize Uses blocksize as the block size when opening the filesystem. 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-separatedExamples 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 specifiedlevel will be turned off. If the parameter is'=filename', messages with that level will be writtento filename.-o directory Save the recovered files to the named directory.The restored files are created in a directorynamed 'RECOVERED_FILES/' by default.