Python Incremental Backup implementation tips

Source: Internet
Author: User

After learning about the Python programming language, you will find that it is very easy to use in some specific environments, it can also help developers to meet functional requirements in these environments. Here we will take a look at the operations related to Python Incremental backup.

Python Incremental Backup code example:

 
 
  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. # If the file path is a directory
  17. If OS. path. isdir (file_path ):
  18. # Create a folder if it does not exist in the Backup Directory
  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. # If the file already exists, compare the file modification time
  30. If OS. path. isfile (file_path_bak ):
  31. Stat_bak = OS. stat (file_path_bak)
  32. Stat_source = OS. stat (file_path)
  33. # Determining the file modification time
  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. # File directory to be backed up
  44. Path = '/home/zyf/appspot/auto_bak/'
  45. # Backup file directory
  46. Path_bak = '/home/zyf/appspot/auto_bak/Bak'
  47. # Start backup
  48. Bak_file (path, path_bak)

The above is an introduction to the operations related to Python Incremental backup.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.