文件被进程占用,但文件被rm ,找回文件

1、用httpd的日志做例子

 # rm -rf /var/log/httpd/access_log

2、利用lsof找到文件被哪个进程号占用  (不能关闭或重启进程)

# lsof |grep  access_log

httpd 2384 apache 7w REG 8,3 4 101112345 /var/log/httpd/access_log (deleted)

3、恢复文件

# cd /proc/2384/fd

#ls -lsh 

total 0
0 lrwx------ 1 root root 64 May 19 13:59 8 -> anon_inode:[eventpoll]
0 l-wx------ 1 root root 64 May 19 13:59 7 -> /var/log/httpd/access_log (deleted)
0 l-wx------ 1 root root 64 May 19 13:59 6 -> pipe:[24238]
0 lr-x------ 1 root root 64 May 19 13:59 5 -> pipe:[24238]
0 lrwx------ 1 root root 64 May 19 13:59 4 -> socket:[24223]
0 lrwx------ 1 root root 64 May 19 13:59 3 -> socket:[24222]
0 l-wx------ 1 root root 64 May 19 13:59 2 -> /var/log/httpd/error_log
0 lrwx------ 1 root root 64 May 19 13:59 1 -> socket:[17261]
0 lr-x------ 1 root root 64 May 19 13:59 0 -> /dev/null

# cp 7 /var/log/httpd/access_log_new

access_log_new即是恢复后的文件,此时可关闭进程,重命名文件,然后启动httpd进程

 

posted @ 2021-05-19 14:47  xiao_pai_pai  阅读(105)  评论(0编辑  收藏  举报