Chinaunix首页 | 论坛 | 博客
  • 博客访问: 616744
  • 博文数量: 151
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 1664
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-13 16:37
文章分类

全部博文(151)

文章存档

2010年(16)

2009年(135)

我的朋友

分类: LINUX

2009-11-19 20:35:44

数据恢复-抢救已经删除的档案   当不小心删除了重要的档案,且系统又没有资源回收站的机制时,心中除了懊恼外,想必会希望有个方法可以将这些资料抢救回来。
       目前linux常见的系统中,ext2已经有非常方便的结构程序可以抢劫已经删除的档案,不过可惜的是,除了ext2外,其他档案系统目前仍缺乏可靠的方法。虽然如此,本节将提供一个可行的方法,让你在其他档案系统下也能够尝试复原档案。
抢救已删除档案的第一步    一 将分割区卸载
------------------------------------------------------------
       我们之所以可以抢救系统删除的档案,是因为系统删除档案时,并不是真的将档案所在的资料区块清除,而是仅删除档案系统中关于该档案的资讯记录,然后将档案之前占用的资料区块表示为可用,所以只要没有新的档案写入这些资料区块,原先档案的内容实际上仍然存在与硬盘中。
       所以档发现误删了重要文档时,首先第一步表是将档案存在的分割区块卸载,或是重新挂载成只读的分割区.这个第一步骤实行的时间点,将是能否成功挽救被误删档案的重要关键。如果档案误删后,经过很久的时间才发现,很可能该档案的资料区块早已被其他档案写入,此时不论是多么厉害的专家,都无能为力。
       因此抢救已删除的档案的第一步,绝对不是找资料或是问专家,务必在第一时间将档案所在的分割区卸载,或是重新挂载成只读的分区。
如何将档案系统由ext3转换成ext2
---------------------------------------------------------------------
虽然ext3为日志式档案系统(JFS),提供了相当良好的安全性,可大幅减少不正常开机后需花费的系统修复时间,让资料的使用更有效率。不过如过反删除功能对你而言非常重要,那么可以将分区转换为ext2档案系统。
由于ext3和ext2档案系统的架构安全相同,唯一的相异处在于ext3多出了一个日志档案,以记录磁盘状态,所以两者间的转换非常容易,也不会影响分区上的资料。
假设现在要将/dev/sda1的档案系统有ext3转换为ext2,如下操作:
[root@smart402 ~]# umount -t ext3 /dev/sda1
[root@smart402 ~]# tune2fs -o ^has_journal /dev/sda1 【移除日志记录的功能】
tune2fs 1.35 (28-Feb-2004)
Invalid mount option set: ^has_journal
 [root@smart402 ~]# e2fsck -y /dev/sda1 【检查并修正分割区的错误】
e2fsck 1.35 (28-Feb-2004)
/boot: clean, 38/26104 files, 15244/104388 blocks
 [root@smart402 ~]# mkdir /mnt/tmp             【为挂载做准备】
[root@smart402 ~]# mount -t ext2 /dev/sda1 /mnt/tmp   【将/dev/sda1挂载到/mnt/tmp】
[root@smart402 ~]# cd /mnt/tmp/
[root@smart402 tmp]# ls
config-2.6.9-55.EL     grub                    initrd-2.6.9-55.ELsmp.img  message     System.map-2.6.9-55.EL     vmlinuz-2.6.9-55.EL
config-2.6.9-55.ELsmp  initrd-2.6.9-55.EL.img  lost+found                 message.ja  System.map-2.6.9-55.ELsmp  vmlinuz-2.6.9-55.ELsmp
[root@smart402 tmp]# rm -f .journal        【移除日志档案,有些系统可能没有日志档案】
[root@smart402 tmp]# vi /etc/fstab
# This file is edited by fstab-sync - see 'man fstab-sync' for details
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext2    defaults        1 2
none                    /dev/pts                devpts  gid=5,mode=620  0 0
none                    /dev/shm                tmpfs   defaults        0 0
LABEL=/opt              /opt                    ext3    defaults        1 2
………
【将/boot分区的系统格式改为ext2即可】
修改好后reboot!
在ext2档案系统下的抢救
-------------------------------------------------------------
[root@smart402 ~]# mount
/dev/sda2 on / type ext3 (rw)
…..
/dev/sda1 on /boot type ext2 (rw)             【就来测试这个ext2系统格式下的文件】
[root@smart402 ~]# ls /boot/
config-2.6.9-55.ELsmp   initrd-2.6.9-55.ELsmp.img  message.ja                 test
grub                    lost+found                 System.map-2.6.9-55.EL     vmlinuz-2.6.9-55.EL
initrd-2.6.9-55.EL.img  message                    System.map-2.6.9-55.ELsmp  vmlinuz-2.6.9-55.ELsmp
我们来测试test这个文件
[root@smart402 ~]# ls -l /boot/test
-rw-r--r--  1 root root 40  7月 28 06:30 /boot/test       【该文件建档者root,大小40bytes,时间】
[root@smart402 ~]# more /boot/test
test:red hat linux ext2 test
a
b
c
d
e
[root@smart402 ~]#rm –f /boot/test
[root@smart402 ~]#umount /dev/sda1 【立刻卸载该档案所在的分区,如果该分区挂载于根目录,则请使用开机关盘或软盘重启电脑】
【若分区有重要用途,目前无法卸载的话,可以使用下面指令将其重新挂载成只读的分区:
[root@smart402 ~]# mount -o ro,remount /dev/sda1     ro参数为readonly的意思】
[root@smart402 ~]# debugfs /dev/sda1
debugfs 1.35 (28-Feb-2004)
debugfs:  lsdel     【输入lsdel列出所删除的档案清单】
 Inode  Owner  Mode    Size    Blocks   Time deleted
    15      0 100600   4096    4/   4 Mon Jul 28 06:30:37 2008
    22      0 100644     40    1/   1 Mon Jul 28 06:34:07 2008  【这个大小为40的很相似】
2 deleted inodes found.
debugfs:  stat <22>     【输入stat <22>显示其信息,注意之间有空格】
Inode: 22   Type: regular    Mode:  0644   Flags: 0x0   Generation: 3426109884
User:     0   Group:     0   Size: 40
File ACL: 5435    Directory ACL: 0
Links: 0   Blockcount: 4
Fragment:  Address: 0    Number: 0    Size: 0
ctime: 0x488cf7df -- Mon Jul 28 06:34:07 2008
atime: 0x488cf70d -- Mon Jul 28 06:30:37 2008
mtime: 0x488cf70d -- Mon Jul 28 06:30:37 2008
dtime: 0x488cf7df -- Mon Jul 28 06:34:07 2008
BLOCKS:
(0):5444
TOTAL: 1
debugfs:  dump <22> /root/testfile.sav    【将该inode所指的档案,存储为root目录下的testfile.sav文件】
debugfs:  quit
[root@smart402 ~]# ls -l /root/testfile.sav
-rw-r--r--  1 root root 40  7月 28 06:41 /root/testfile.sav
[root@smart402 ~]# more /root/testfile.sav
test:red hat linux ext2 test
a
b
c
d
e
此时便可以开启root目录下的testfile.sav,检查看看内容是否正确。如果内容之前不相同,表示该档案的资料区块已经被其他档案使用了,再也无法还原。
【如同一部主机上还安装了windows系统,可以到,下载并安装了R-Linux这套软件,便可以在windowns下抢救ext2分割区内的档案】
在非ext2的档案系统下抢救已经删除的档案
------------------------------------------------------------------------------------
如果误删档案所在的分割区为ext3,reiserfs等其他非ext2的档案系统,目前仍未有完美的方法复原档案。如果该档案真的非常重要,只好使用关键字,针对整个分割区进行扫描,找出档案可能所在的区块。
[root@smart402 ~]# mount -t ext3 /dev/sda1 /boot/
[root@smart402 ~]# mount
/dev/sda2 on / type ext3 (rw)
……
/dev/sda1 on /boot type ext3 (rw)
【重新挂载/dev/sda1为ext3分区】
[root@smart402 ~]# more /boot/grub/grub.conf  【这个是grub.conf的内容】
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux AS (2.6.9-55.ELsmp)
        root (hd0,0)
        kernel /vmlinuz-2.6.9-55.ELsmp ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.9-55.ELsmp.img
title Red Hat Enterprise Linux AS-up (2.6.9-55.EL)
        root (hd0,0)
        kernel /vmlinuz-2.6.9-55.EL ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.9-55.EL.img
[root@smart402 ~]# ls -l /boot/grub/grub.conf
-rw-------  1 root root 747  7月  2 14:59 /boot/grub/grub.conf
[root@smart402 ~]# head -4 /boot/grub/grub.conf 【显示该文件的前两行】
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
[root@smart402 ~]# wc -l /boot/grub/grub.conf      【计算文件的行数】
21 /boot/grub/grub.conf                                           【有21行】
[root@smart402 ~]# cp /boot/grub/grub.conf /root/   
[root@smart402 ~]# rm -f /boot/grub/grub.conf     【删除该档案】
[root@smart402 ~]# umount /dev/sda1                   【立刻卸载该档案所在的分区,如果该分区挂载于根目录,则请使用开机关盘或软盘重启电脑】
【若目前系统内有程序正在使用该分区,导致无法卸载,可执行#fuser –v –m /boot,来列出占有该分区的程序,要是这些进程不重要的话,使用kill指令将其删除】
【若分区有重要用途,目前无法卸载的话,可以使用下面指令将其重新挂载成只读的分区:
[root@smart402 ~]# mount -o ro,remount /dev/sda1】
[root@smart402 ~]# grep -a -B4 -A5 'You have a /boot partition' /dev/sda1 > /root/grub.sav
【-a是将分割区视为文字档扫描;B4是找到关键字后,一并显示关键字前面4行的资料;A5是找到关键字后,显示关键字后5行的资料;You have a /boot partition是关键字;/root/grub.sav存储为root目录下的grub.sav文件】
【扫描所需的时间视该分区的大小,可能要数分钟到数十分钟不等】
[root@smart402 ~]# more /root/grub.sav
iKA瑚A?瘫○鱔瑩?壤撦  悭鐲鸪瀷(
3掙冑?gQ呼?                    oKQ:袅寖u=I兠R#cy饢
             &纊釧雱52啫?>x6?-[玻?}?貅牓F起?"0蔟D}?鬗gCu?隽訯匦5矤E?t?械"绲BD躯丵閮斦f琵?軼?簍憮叓˙??唣(
并j0糊强?!豉?瓥滟莤i欄]醢?额欯Y销`?}?硜趵嶸Р?3釆*仑縌擋羴鄁/}?mp?3秴0U4噌`柅琤絧#
依zm|三j+?{P嘏概尺翮癸碥盢n|毀柦攆梅讌e5蓝&躣契`+
                                                   唵?2痴蓷?w??碖q\澑
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-version.img
#boot=/dev/sda
【上面的信息是关键字和其后5行的信息】
       此方法利用关键字扫描整个分区,找到关键字所在处前后的资料,因此必须事先得知或自己假设该档案大约的总行数。此外若档案所在的区块不连续,则只能找到片段的资料,必须再利用不同的关键字搜寻,尝试寻找起到失落的段落。
       虽然这个方法可能无法完全复原已删除的档案,但如果误删除档案真的非常重要,便可以藉由此法尽量复原其中的资料。
阅读(1223) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~