문서의 전체 백업 및 증가분 백업

3131 단어
요구 사항:
  • 월요일 전체 백업, 추가 시간 증가분 백업
  • 원본 파일:/tmp/demo/security
  • 대상 파일:/tmp/demo/backup
  • md5 파일:/tmp/demp/backup/md5.data
  • import hashlib                        #      ,       
    import tarfile                           #      
    from time import strftime      #      
    import os                  
    import pickle                        #pickle                 
    
    def check_md5(fname):        #    md5    
        m = hashlib.md5()             #         md5 ,      
    
        with open(fname, 'rb') as fobj:    #        ,      
            while True:
                data = fobj.read(4096)        #    4k  
                if not data:
                    break
                m.update(data)                    #       ,   md5 
    
        return m.hexdigest()                  #     ,  md5   
      
    def full_backup(src, dst, md5file):                #        
        fname = '%s_full_%s.tar.gz' % (os.path.basename(src), strftime('%Y%m%d'))              #     :xxx_full_20190710.tar.gz
    #         :/tmp/demp/backup/xxx_full_20190710.tar.gz
        fname = os.path.join(dst, fname)      
    
        #     
        tar = tarfile.open(fname, 'w:gz')        # gz    
        tar.add(src)
        tar.close()                                        #           
    
        #        md5 
        md5dict = {}                    #       md5 
    #os.walk      [  ][    ][   ]
        for path, folders, files in os.walk(src):      
            for file in files:          
    #                ,             
                key = os.path.join(path, file)      #           
                md5dict[key] = check_md5(key)    #  md5 
    
        #  md5        
    #wb    ,       ,     
        with open(md5file, 'wb') as fobj:          
            pickle.dump(md5dict, fobj)      #  pickle.dump  
    
    #        
    #  :        md5 ,       md5      ,       
    
    def incr_backup(src, dst, md5file):            
        fname = '%s_incr_%s.tar.gz' % (os.path.basename(src), strftime('%Y%m%d'))      #xxx_incr_20190710.tar.gz
    #/tmp/demo/backup/xxx_incr_20190710.tar.gz
        fname = os.path.join(dst, fname)  
    
        #           md5 
        md5dict = {}
        for path, folders, files in os.walk(src):
            for file in files:
                key = os.path.join(path, file)
                md5dict[key] = check_md5(key)
    
        #         md5 
        with open(md5file, 'rb') as fobj:
            old_md5 = pickle.load(fobj)
    
        #              ,     
        tar = tarfile.open(fname, 'w:gz')
        for key in md5dict:      #        md5 
    #     ,old_md5.get(key)  ,     ,old_md5.get(key)   md5    ,       
            if old_md5.get(key) != md5dict[key]:      
                tar.add(key)
        tar.close()
    
        #     md5       ,         
        with open(md5file, 'wb') as fobj:
            pickle.dump(md5dict, fobj)
    
    if __name__ == '__main__':  
        src = '/tmp/demo/security'          #   ,      
        dst = '/tmp/demo/backup'          #      
        md5file = '/tmp/demo/backup/md5.data'        #md5   
        if strftime('%a') == 'Mon':                #        
            full_backup(src, dst, md5file)
        else:
            incr_backup(src, dst, md5file)
    
    

    좋은 웹페이지 즐겨찾기