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:
- #! /Usr/bin/python
- #-*-Coding: UTF-8 -*-
- # Filename: auto_bak.py
- # Author: zz
- Import OS
- Import sys
- Def get_dir (path ):
- Print path, '\ N'
- Return OS. listdir (path)
- Def bak_file (path, path_bak ):
- List = OS. listdir (path)
- For l in list:
- File_path = OS. path. join (path, l)
- File_path_bak = OS. path. join (path_bak, l)
- Print file_path
- # If the file path is a directory
- If OS. path. isdir (file_path ):
- # Create a folder if it does not exist in the Backup Directory
- If not OS. path. isdir (file_path_bak ):
- Create_com = ''' mkdir-p '% s ''''\
- % (File_path_bak)
- If OS. system (create_com) = 0:
- Print create_com
- Else:
- Print 'create folder failure! '
- OS. _ exit (0)
- Bak_file (file_path, file_path_bak)
- Else:
- # If the file already exists, compare the file modification time
- If OS. path. isfile (file_path_bak ):
- Stat_bak = OS. stat (file_path_bak)
- Stat_source = OS. stat (file_path)
- # Determining the file modification time
- If stat_source.st_mtime <= stat_bak.st_mtime:
- Continue
- Cp_com = '''cp' % s' % s ''''\
- % (File_path, file_path_bak)
- If OS. system (cp_com) = 0:
- Print cp_com
- Else:
- Print 'create folder failure! '
- OS. _ exit (0)
- # File directory to be backed up
- Path = '/home/zyf/appspot/auto_bak/'
- # Backup file directory
- Path_bak = '/home/zyf/appspot/auto_bak/Bak'
- # Start backup
- Bak_file (path, path_bak)
The above is an introduction to the operations related to Python Incremental backup.