简介:
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 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目录恢复的文件,是成功
操作就是这些,大家自己动手操作下就知道了,以下是代码及附件
- #!/usr/bin/env python
- #Recover Deletion of Data!!!!!
- import os,re,urllib,sys
- if sys.platform != 'linux2':
- sys.exit
- else:
- recovery = 'ext3grep-0.10.2.tar.gz' #Recovered Toos
- if not os.path.isfile(recovery):
- website = 'http://ext3grep.googlecode.com/files' + os.sep + recovery
- print 'Downloading .............."%s"' % recovery
- loading = urllib.urlopen(website)
- localfile = open(recovery,'wb').write(loading.read())
- loading.close
- #Install ext3grep
- tar_command = 'tar -zxf "%s"'% recovery
- checkdir = re.match (recovery[:-7],recovery)
- make = './configure;make;make install'
- if not os.path.isdir(checkdir.group()):
- os.system(tar_command)
- os.chdir(checkdir.group())
- os.system(make)
- #Umount disk partitions
- def main():
- if os.system('umount -l %s' % opt) != 0:
- raw_input ('Your disk partition are errors in the input')
- def res():
- os.system("ext3grep %s --restore-file '%s'"%(opt,ans))
- class disk:
- def pt(self):
- global opt
- if part.lower() == 'q':
- sys.exit()
- elif part.lower() == 'b':
- os.system('fdisk -l')
- opt = raw_input('Please,Enter the disk partition that you want to umount:')
- return opt
- main()
- elif part.lower() == 'c':
- os.system('fdisk -l')
- opt = raw_input('Please,Enter the disk partition that you want to recovery:')
- return opt
- #main()
- elif part.lower() == 'a':
- os.system('df -Tlh')
- opt = raw_input('Please,Enter the disk partition that you want to umount:')
- main()
- return opt
- else:
- opt = 'err'
- #Create Scan and recovery
- def sf(self):
- global age
- if opt != 'err':
- scan = 'ext3grep %s --ls --inode 2'#Scan files
- if os.system(scan%opt) == 0:
- age='--' * 40
- print age
- def info(self):
- global info
- info = raw_input('1:Recovery files\n2:Reovery anythings\nB:Back Meu\nDo you want to recover files or directorys:')
- if info.lower() == 'b':
- info = 'stop'
- return True
- def exc(self):
- z = info
- while z != 'stop':
- if z == '1':
- global ans
- ans = raw_input('\n********************\nB:Stop and Back Meu\n********************\nPlase, you need to recovery file name:')
- if ans.lower() == 'b':
- z = 'stop'
- try:
- if type(int(ans)) is int:
- os.system('ext3grep %s --ls --inode %d'%(opt,int(ans)))
- except:
- res()
- elif z == '2':
- os.system("ext3grep %s --restore-all "%opt)
- ans = raw_input('\n********************\nData recovery success cases\n********************\nPlease,input B key back Meu:')
- if ans.lower() == 'b':
- z = 'stop'
- else:
- print '************your input error*************'
- d.info()
- z = info
- # res()%(opt,ans)
- d = disk()
- while 0 < 1:
- os.system('clear')
- print '--' * 30
- print '\t\tA:Unmount a disk partition\n\n'
- print '\t\tB:Unmount all disk partitions\n\n'
- print '\t\tC:Recover files and directorys\n\n'
- print '\t\tQ:Quit'
- print '--' * 30
- part = raw_input('\t\tPlase,you choose to option :')
- d.pt()
- d.sf()
- if ('age' in dir())is True:
- d.info()
- d.exc()
- del age
- del opt
- del part
转载于:https://blog.51cto.com/skyson/507474