網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
對(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命令需要訪(fǎng)問(wèn)核心內(nèi)存和各種文件,所以需要root用戶(hù)執(zhí)行。
在linux環(huán)境下,任何事物都以文件的形式存在,通過(guò)文件不僅僅可以訪(fǎng)問(wèn)常規(guī)數(shù)據(jù),還可以訪(fǎng)問(wèn)網(wǎng)絡(luò)連接和硬件。所以如傳輸控制協(xié)議 (TCP) 和用戶(hù)數(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)程在訪(fǎng)問(wè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)程的名稱(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_bac shell> lsof |grep /var/log/message rsyslogd 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/messages rsyslogd 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> 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對(duì)應(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
對(duì)比備份文件:
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
對(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.bz2 tar (child): bzip2:無(wú)法 exec: 沒(méi)有那個(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
來(lái)解決。#Ubuntu的解決辦法為sudo apt-get install e2fslibs-dev e2fslibs-dev
不出意外的話(huà)到這里應(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)換為較窄的類(lèi)型‘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我們的掛載盤(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/taroballs taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1 taroballs@taroballs-PC:~$ #記得刪除一定要后umount哦,不然二次寫(xiě)入誰(shuí)也幫不了你呢。
通過(guò)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
通過(guò)掃描發(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ù) 出來(lái)的文件名和之前不一樣,需要單獨(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.
原文鏈接:https://www.cnblogs.com/cs-markdown10086/archive/2022/11/30/16938664.html
相關(guān)推薦
- 2022-10-16 docker保存鏡像到本地并加載本地鏡像文件詳解_docker
- 2022-09-17 c語(yǔ)言實(shí)現(xiàn)從源文件從文本到可執(zhí)行文件經(jīng)歷的過(guò)程_C 語(yǔ)言
- 2022-07-09 python沒(méi)有g(shù)pu,如何改用cpu跑代碼_python
- 2023-12-11 Mybatis對(duì)于多對(duì)一和一對(duì)多的處理
- 2023-11-15 linux查看目錄的大小,指定目錄查看所占的空間大小
- 2023-05-16 Golang的鎖機(jī)制使用及說(shuō)明_Golang
- 2022-09-21 Android?Flutter繪制有趣的?loading加載動(dòng)畫(huà)_Android
- 2022-08-28 flask?route對(duì)協(xié)議作用及設(shè)計(jì)思路_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支