脚本简介:

1)备份源目录的文件

2)目标文件以tar 和bzip2的方式压缩之后放在当前日期文件夹下

4)备份文件以时间注释和执行脚本的用户命名

3)主要用到了时间模块,系统模块,和getpass模块

4)source 可以修改为想备份的目录,因为备份目录一般不经常变动,所以这里写死了

 
  


 
  
  1. #!/bin/env python  
  2. #告诉解释器查找pyton解释器并且使用它 
  3. #_*_encoding:utf8_*_  
  4. #指定编码为utf8编码 
  5. import os 
  6. #导入系统模块  
  7. import time 
  8. #导入时间模块  
  9. import getpass 
  10. #导入获取用户模块  
  11. source = ['/oracle/''/oradata/oraInventory']  
  12. #定义备份源目录 
  13. target_dir = '/oradata/backup/' 
  14. #定义备份目标目录  
  15. user = getpass.getuser() 
  16. #定义使用备份脚本的用户  
  17. today = target_dir + time.strftime('%Y%m%d'
  18. #定义今日的日期 
  19. now = time.strftime('%H%M%S'
  20. #定义现在的时间  
  21. comment = raw_input('请输入一个注释 --> ')  
  22. #定义注释为输入的字符串 
  23. if len(comment) == 0
  24. #检查输入的注释是否为空  
  25.     target = today + now + '_' + user + '_' + 'tar.bz2'  
  26. #如果注释为空,备份文件的文件名为日期时间运行脚本用户  
  27. else:  
  28.     target = today +  now + '_' + comment + '_' + user + '_' + 'tar.bz2'  
  29. #如果非空,则使用日期时间注释用户为文件名 
  30. if not os.path.exists(today):  
  31. #检查备份目录下的时间目录是否不存在 
  32.  
  33.     os.mkdir(today)  
  34. #如果不存在创建文件夹 
  35.     print '成功创建文件夹', today  
  36. else:  
  37. #存在,则打印 
  38.     print today,'文件夹已经存在'  
  39. time.sleep(5
  40. #暂停五秒  
  41. zip_command = "tar -cjPf '%s' %s" % (target, ' '.join(source))  
  42. #定义本备份命令 
  43.  
  44. if os.system(zip_command) == 0:  
  45. #使用系统环境(相当于shell执行备份命令,如果成功返回0) 
  46.     print ' 成功备份为:', target  
  47. else:  
  48.     print '备份失败' ,target