简介:
Ext3grep 是一款很强大的恢复工具,我在它的基础上编写了一个应用脚本,实现自动下载安装配置及突破传统的命令操作让它变的更加傻瓜化, 脚本代码在附件和文章的底部

Step 1: 建立测试区

[root@manage shell]# mkdir /data  
[root@manage shell]# cd /data/
[root@manage data]# dd if=/dev/zero of=disk count=2048000
[root@manage data]# mkfs.ext3 disk
[root@manage data]# mount -o loop /data/disk /mnt
[root@manage data]# cd /mnt/
[root@manage mnt]# mkdir dir
[root@manage mnt]# touch file1
[root@manage mnt]# touch file2
[root@manage mnt]# touch file3
[root@manage mnt]# mkdir dir/file1
[root@manage mnt]# mkdir dir/file2
[root@manage mnt]# mkdir dir/file3
[root@manage mnt]# mkdir dir/file4
[root@manage mnt]# rm -rf *  

Step 2:脚本使用教程
注:不需要下载ext3grep的安装包以及安装,脚本会自动实现
1、[root@manage shell]# chmod 700 recovery.py #设置可执行权限
2、[root@manage shell]# ./recovery.py #执行脚本

执行成功以后会出现如下界面:


  • A键:卸载一个磁盘分区
  • B键:卸载所有磁盘分区
  • C键:直接恢复目录和文件
  • Q键:退出
1、我们这里刚才创建一个测试分区,用选项A来恢复操作:


2、如上图所示,测试分区磁盘路经为/data/disk,那么我们直接输入这个路经地址


注: 如果出现此问题“ init_directories.cc:534: void init_directories(): Assertion `lost_plus_found_directory_iter != all_directories.end()' failed.Aborted” 这是由于自定义分区造成,重新格式化下就可以解决”
3、扫描分区它会有一段时间,扫描结果显示如下:
  • D:表示删除的目录和文件
  • d:表示目录
  • r:表示文件

4、如下图所示共有三个选项,我这里选择1文件恢复
  • 1:表示恢复文件
  • 2:表示恢复全部目录和文件
  • B:表示反回主菜单


5、要求你想恢复的文件,我这里键入“file1”


6、恢复文件成功,如果你想继续恢复其它文件,继续如上步骤,不想恢复了按“B键“退出



7、如下示所示,如果你想恢复dir目录里面的文件怎么办?没关系跟据蓝色部分的字数,把它输入进去,就可以进入目录恢复


蓝色数字为:16001,你直接如下图输入进去


8、如下图所示,已经进入到dir目录,恢复文件和刚才操作一样就可以,输入你想恢复的文件名


9、我们现在文件恢复完成,选B直接反回主菜单,然后按Q退出脚本,直接查看RESTORED_FILES目录恢复的文件,是成功




操作就是这些,大家自己动手操作下就知道了,以下是代码及附件

 
  
  1. #!/usr/bin/env python  
  2. #Recover Deletion of Data!!!!!  
  3. import os,re,urllib,sys  
  4. if sys.platform != 'linux2':  
  5.     sys.exit  
  6. else:  
  7.     recovery = 'ext3grep-0.10.2.tar.gz' #Recovered Toos  
  8.  
  9. if not os.path.isfile(recovery):  
  10.     website = 'http://ext3grep.googlecode.com/files' + os.sep + recovery  
  11.     print 'Downloading .............."%s"' % recovery  
  12.     loading = urllib.urlopen(website)  
  13.     localfile = open(recovery,'wb').write(loading.read())  
  14.     loading.close  
  15.  
  16. #Install ext3grep  
  17. tar_command = 'tar -zxf "%s"'% recovery  
  18. checkdir = re.match (recovery[:-7],recovery)  
  19. make = './configure;make;make install' 
  20.  
  21. if not os.path.isdir(checkdir.group()):  
  22.     os.system(tar_command)  
  23.     os.chdir(checkdir.group())  
  24.     os.system(make)  
  25.  
  26. #Umount disk partitions  
  27. def main():  
  28.     if os.system('umount -l %s' % opt) != 0:  
  29.         raw_input ('Your disk partition are errors in the input')  
  30. def res():  
  31.     os.system("ext3grep %s --restore-file '%s'"%(opt,ans))  
  32. class disk:  
  33.     def pt(self):  
  34.         global opt   
  35.         if part.lower() == 'q':  
  36.             sys.exit()  
  37.         elif part.lower() == 'b':  
  38.             os.system('fdisk -l')   
  39.             opt = raw_input('Please,Enter the disk partition that you want to umount:')  
  40.             return opt  
  41.             main()  
  42.         elif part.lower() == 'c':  
  43.             os.system('fdisk -l')   
  44.             opt = raw_input('Please,Enter the disk partition that you want to recovery:')  
  45.             return opt  
  46.             #main()  
  47.         elif part.lower() == 'a':  
  48.             os.system('df -Tlh')  
  49.             opt = raw_input('Please,Enter the disk partition that you want to umount:')  
  50.             main()  
  51.             return opt  
  52.         else:  
  53.             opt = 'err' 
  54. #Create Scan and recovery  
  55.     def sf(self):  
  56.         global age  
  57.         if opt != 'err':  
  58.             scan = 'ext3grep %s --ls --inode 2'#Scan files  
  59.             if os.system(scan%opt) == 0:  
  60.                 age='--' * 40  
  61.                 print age  
  62.  
  63.     def info(self):  
  64.         global info  
  65.         info = raw_input('1:Recovery files\n2:Reovery anythings\nB:Back Meu\nDo you want to recover files or directorys:')
  66.     if info.lower() == 'b':
  67. info = 'stop'
  68.         return True  
  69.     def exc(self):  
  70.             z = info   
  71.             while z != 'stop':  
  72.  
  73.                 if z == '1':  
  74.                     global ans   
  75.                     ans = raw_input('\n********************\nB:Stop and Back Meu\n********************\nPlase, you need to recovery file name:')  
  76.                     if ans.lower() == 'b':  
  77.                         z = 'stop' 
  78.                     try:  
  79.                         if type(int(ans)) is int:  
  80.                             os.system('ext3grep  %s --ls --inode %d'%(opt,int(ans)))   
  81.                     except:  
  82.                             res()   
  83.                 elif z == '2':  
  84.                     os.system("ext3grep %s --restore-all "%opt)  
  85.                     ans = raw_input('\n********************\nData recovery success cases\n********************\nPlease,input B key back Meu:')  
  86.                     if ans.lower() == 'b':  
  87.                         z = 'stop' 
  88.  
  89.  
  90.                 else:  
  91.                     print '************your input error*************'  
  92.                     d.info()  
  93.                     z = info 
  94.  
  95.                 #    res()%(opt,ans)  
  96.               
  97. d = disk()  
  98. while 0 < 1: 
  99.     os.system('clear')  
  100.     print '--' * 30  
  101.     print '\t\tA:Unmount a disk partition\n\n'  
  102.     print '\t\tB:Unmount all disk partitions\n\n'  
  103.     print '\t\tC:Recover files and directorys\n\n'  
  104.     print '\t\tQ:Quit'  
  105.     print '--' * 30  
  106.     part = raw_input('\t\tPlase,you choose to option :')  
  107.     d.pt()  
  108.     d.sf()  
  109.     if ('age' in dir())is True:  
  110.         d.info()  
  111.         d.exc()  
  112.         del age  
  113.     del opt  
  114.     del part  
  115.