Python增量备份实现技巧分享


大家在了解了Python这一编程语言之后,会发现它在一些特定环境中的应用方式是非常简便的,而且能够很好的帮助开发人员完成这些环境下的功能需求。在这里我们先来一起了解一下Python增量备份的相关操作。

Python增量备份代码示例:

  1. #!/usr/bin/python  
  2. #-*-coding:utf-8-*-  
  3. #Filename: auto_bak.py  
  4. #Author: zz  
  5. import os  
  6. import sys  
  7. def get_dir(path):  
  8. print path, '\n'  
  9. return os.listdir(path)  
  10. def bak_file(path,path_bak):  
  11. list = os.listdir(path)  
  12. for l in list:  
  13. file_path = os.path.join(path, l)  
  14. file_path_bak = os.path.join(path_bak, l)  
  15. print file_path  
  16. #如果文件路径为目录  
  17. if os.path.isdir(file_path):  
  18. #如果在备份目录中文件夹不存在则创建  
  19. if not os.path.isdir(file_path_bak):  
  20. create_com = '''mkdir -p '%s' ''' \  
  21. % (file_path_bak)  
  22. if os.system(create_com) == 0:  
  23. print create_com   
  24. else:  
  25. print 'create folder failure!'  
  26. os._exit(0)   
  27. bak_file(file_path, file_path_bak)  
  28. else:  
  29. #如果文件已经存在,则比较文件修改时间  
  30. if os.path.isfile(file_path_bak):  
  31. stat_bak = os.stat(file_path_bak)  
  32. stat_source = os.stat(file_path)  
  33. #判断文件修改时间  
  34. if stat_source.st_mtime <= stat_bak.st_mtime:  
  35. continue  
  36. cp_com = '''cp '%s' '%s' ''' \  
  37. % (file_path, file_path_bak)  
  38. if os.system(cp_com) == 0:   
  39. print cp_com  
  40. else:   
  41. print 'create folder failure!'  
  42. os._exit(0)   
  43. #要备份的文件目录  
  44. path = '/home/zyf/appspot/auto_bak/a' 
  45. #备份文件目录  
  46. path_bak = '/home/zyf/appspot/auto_bak/bak' 
  47. #开始备份  
  48. bak_file(path, path_bak) 

以上就是我们对Python增量备份的相关操作方法的介绍。

相关内容

    暂无相关文章

评论关闭