体会
当误删文件时,一定不要在发生错误操作的机器上尝试恢复。可以在其它环境相似的机器上模拟找回操作,尽量不要因为找回的过程中的破坏导致最终无法找回。
[参考]
前题
- Nginx的进程在Nginx配置删除后没有重启,也没有通过
nginx -s reload方式加载配置文件
使用gdb dump出内存中的配置
- 使用到软件
https://github.com/ccieliu/dump.sh.git - gdb, gcc的debug工具
操作步骤
- 获取nginx
root@chen:~/gitstore# ps -ef |grep nginx
root 3918 1 0 15:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 3919 3918 0 15:37 ? 00:00:00 nginx: worker process
www-data 3920 3918 0 15:37 ? 00:00:00 nginx: worker process
www-data 3921 3918 0 15:37 ? 00:00:00 nginx: worker process
www-data 3922 3918 0 15:37 ? 00:00:00 nginx: worker process
root 13271 3417 0 15:56 pts/1 00:00:00 grep --color=auto nginx
root@chen:~/gitstore#
- 利用dump.sh项目,dump出nginx内存镜像,
注意: 此处的pid为nginx的master_process id
生成 gdb-commands 脚本如下
# Set pid of nginx master process here
pid=3918
# generate gdb commands from the process's memory mappings using awk
cat /proc/$pid/maps | awk '$6 !~ "^/" {split ($1,addrs,"-"); print "dump memory mem_" addrs[1] " 0x" addrs[1] " 0x" addrs[2] ;}END{print "quit"}' > gdb-commands
- 将内存中的数据保存到文件
gdb -p $pid -x gdb-commands - 获取包含
worker_connections和server_name的文件名
root@chen:~/gitstore# grep worker_connection mem_*
Binary file mem_558991213000 matches
root@chen:~/gitstore# grep server_name mem_*
Binary file mem_558991213000 matches
- 将
worker_connection、mem_558991213000文件取回,并使用nginx打开,找到文件中的worker_connections和server_name内容
为了防止nginx配置文件的丢失,借助linux 定时任务定期保存nginx配置文件
- crontab -e 添加定时任务,
* 1 * * *每天1点执行脚本