Python implements an SVN catalog periodic backup instance

Source: Internet
Author: User
This article describes the Python implementation of the SVN directory periodic backup method. Share to everyone for your reference. Specific as follows:

Cause: Today with SVN, accidentally deleted the directory on the remote SVN server, and then in the local and hand to restore the project (right-click Project team in Eclipse Restore), resulting in the writing of the most days of the code is lost, with a variety of data recovery software recovery has no effect. Rage wrote this directory periodic backup gadget that backs up all files in the source directory every 5 seconds to the target directory (retaining structure), ensuring that the files in the target directory are only increased. and copy only the changed files at a time (compare the MD5 values of two files).

Think: Although SVN is also a version management software, the use of the process always feel that it is a lot of awkward operation, compared to git more powerful and flexible. My own ideal version control software should at least take into account some of the user-generated use scenarios (perhaps most of the situation is due to user misuse, but if the misuse of the situation can also be taken into account, in order to more reflect the ultimate humane care of the software), such as the removal of files on the remote server, I think it should be designed like this: even if the user deletes the file, the file should remain in a buffer for 1 days (or a few hours, configurable), and then a scheduled task periodically deletes the buffer for longer than 1 days. Instead of physically deleting it immediately.

Todo:
Add config file to command line program

md5_caculate.py:

#-*-Coding:utf-8-*-#!/usr/bin/pythonfrom hashlib import md5import os def calMD5 (str): M = MD5 () m.update (str) retur     N M.hexdigest () def calmd5forfile (file): Statinfo = os.stat (file) if int (statinfo.st_size)/(1024*1024) >= 1000:# Print ("File size > $, move to Big File ...") return Calmd5forbigfile (file) m = MD5 () f = open (file, ' RB ') m.up Date (F.read ()) F.close () return m.hexdigest () def calmd5forfolder (dir, md5file): outfile = open (Md5file, ' W ') for Root, Subdirs, files in Os.walk (dir): for file in Files:filefullpath = Os.path.join (root, file) "" "Print FILEFULLP Ath "" "Filerelpath = Os.path.relpath (Filefullpath, dir) md5 = Calmd5forfile (filefullpath) Outfile.write (fil erelpath+ ' +md5+ "\ n") outfile.close () def calmd5forbigfile (file): M = MD5 () f = open (file, ' RB ') buffer = 8192 # why is 8192 | 8192 is fast than 2048 and 1:chunk = f.read (buffer) if not chunk:break m.update (chunk) F.close () return m . Hexdigest () if __name__== "__main__": Print (Calmd5forfile ("E:/test/target/a/b/rabbit.txt")) 

file_util.py:

#-*-Coding:utf-8-*-#!/usr/bin/pythonimport os,shutilfrom md5_caculate import calmd5forfile# Copy source directory to target directory def copyDir ( Srcdir, Dstdir): If Srcdir in Dstdir: # The source directory is contained in the destination directory, then returns the return if not Os.path.isdir (srcdir): Print (Srcdir, "path specified Source directory does not exist!  ") return; If not os.path.exists (Dstdir): # The destination directory does not exist when you create Os.makedirs (Dstdir) for fileordirname in Os.listdir (srcdir): # All files under the source directory ( Include files and directories) TODO BUG: What if Srcdir is an empty directory? # Fileordirpath = Srcdir + "/" + fileordirname Fileordirpath = Os.path.join (Srcdir, fileordirname) if os.path.is File (Fileordirpath): # If it is currently a sub-file, copy the files directly copyFile (Fileordirpath, Dstdir) if Os.path.isdir (Fileordirpath): # If the current is  A subdirectory, recursively replicating the directory copydir (Fileordirpath, Os.path.join (Dstdir, Fileordirname)) # Copy the source file to the target directory def copyFile (Srcfile, Dstdir): If not Os.path.isfile (srcfile): Print (srcfile, "path specified by the source file does not exist!") ") Return fileName = Os.path.basename (srcfile) dstfile = Os.path.join (Dstdir, FileName) if Os.path.isfile (dstfile): # There is a target file with the same name, check if the MD5 value is the same, copy only if different    If Calmd5forfile (srcfile)! = Calmd5forfile (dstfile): Try:shutil.copy (Srcfile, dstdir) except Permiss   Ionerror:print ("Permissionerror occurs:", Srcfile) else:shutil.copy (srcfile, Dstdir) if __name__== "__main__": Copydir ("E:/test/src", "E:/test/target")

backuper.py:

#-*-coding:utf-8-*-#!/usr/bin/pythonimport OS, timefrom file_util import copydir#---------------------------------- ---------------------------# Scheduled backup of source directory to target directory # version = 1.0# Author = will#--------------------------------------------- ----------------# Scheduled backup of source directory to target directory, sleepintervalseconds for backup interval seconds def backupdir (Srcdir, Dstdir, sleepintervalseconds):  if not Os.path.isdir (srcdir):    print ("Please specify that you want to back up the source directory and make sure the directory exists!") ")    return;  While True:    print ("Backup:", Srcdir, ", To:", Dstdir)    copydir (Srcdir, Dstdir)    print ("Start hibernation", Sleepintervalseconds, "seconds ...")    time.sleep (sleepintervalseconds) if __name__== "__main__":  backupdir ("d:/ Documents/workspace/workspace/griddle "," E:/backup/griddle ", 20)

Hopefully this article will help you with Python programming.

  • 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.