Five hours of battle, Python Implementation of Incremental Backup

Source: Internet
Author: User

I have been thinking about Python, but I have never been idle. This is not the case. On Saturday, boxing is also there. So I started from the beginning, reading the python manual, and reading a concise manual for two hours, then write down
This script implements incremental file backup ...... Looking back, it is not complicated. Except for a recursion, there is only the limit method, and the latter makes us spend a lot of time. Some operations on the server side of Python are very convenient. I feel that this is a very flattering language,CodeAs follows:

 

 

# ! /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 ):
 
# If the folder in the backup directory does not exist, create
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)
 
# Determine the file modification time
If Stat_source.st_mtime <= Stat_bak.st_mtime:
Continue
 
Cp_com =   ''' CP '% 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)

 

Related Article

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.