python實現對檔案的全量、增量備份

來源:互聯網
上載者:User

標籤:backup   __name__   .gz   ict   incr   auth   close   erro   direct   


#!/user/bin/env python# @Time     :2018/6/6 10:10# @Author   :PGIDYSQ#@File      :FileBackup2.pyimport osimport filecmpimport shutilimport sysimport time,sched‘‘‘定時任務備份,增量備份‘‘‘schedule = sched.scheduler(time.time, time.sleep)def autoBackup(scrDir,dstDir):    if((not os.path.isdir(scrDir))or(not os.path.isdir(dstDir))or           (os.path.abspath(scrDir) != scrDir)or(os.path.abspath(dstDir) != dstDir)):        usage()    for item in os.listdir(scrDir):        scrItem = os.path.join(scrDir,item)        dstItem= scrItem.replace(scrDir,dstDir)        if os.path.isdir(scrItem):            #建立新增加的檔案夾,保證目標檔案夾結構與原始檔案一致            if not os.path.exists(dstItem):                os.makedirs(dstItem)                print(‘make directory‘+dstItem)            #遞迴調用自身函數            autoBackup(scrItem,dstItem)        elif os.path.isfile(scrItem):            #只複製新增或修改的檔案            if((not os.path.exists(dstItem))or(not filecmp.cmp(scrItem,dstItem,shallow=False))):                shutil.copyfile(scrItem,dstItem)                print(‘file:‘+scrItem+‘==>‘+dstItem,os.system(‘echo %time%‘))    schedule.enter(10, 0, autoBackup, (scrDir, dstDir))def usage():    print(‘Error‘)    print(‘For example:{0}‘.format(sys.argv[0]))    sys.exit(0)if __name__ == "__main__":    # if len(sys.argv) != 3:    #     usage()    # scrDir,dstDir = sys.argv[1],sys.argv[2]    scrDir, dstDir = r‘E:\PyCharm\WorkSpace\TestPkg\base\src‘,r‘E:\PyCharm\WorkSpace\TestPkg\base\dest‘    #定時周期執行備份任務    schedule.enter(10, 0, autoBackup, (scrDir,dstDir))    schedule.run()  # 持續運行,直到計劃時間隊列變成空為止    #autoBackup(scrDir,dstDir)

 

#!/user/bin/env python# @Time     :2018/6/6 10:10# @Author   :PGIDYSQ#@File      :FileBackup.pyimport timeimport osimport tarfileimport shutilimport pickle as pimport hashlib‘‘‘實現每周一全量備份,其餘時間增量備份‘‘‘def md5check(fname):    m = hashlib.md5()    with open(fname) as fobj:        while True:            data = fobj.read(4096)            if not data:                break            m.update(data.encode())#pay    return m.hexdigest()def full_backup(src_dir: object, dst_dir, md5file):    par_dir, base_dir = os.path.split(src_dir.rstrip(‘/‘))    back_name = ‘%s_full_%s.tar.gz‘ % (base_dir, time.strftime(‘%Y%m%d‘))    full_name = os.path.join(dst_dir, back_name)    md5dict = {}    # tar = tarfile.open(full_name, ‘w:gz‘)    # tar.add(src_dir)    # tar.close()    if os.path.exists(dst_dir):        shutil.rmtree(dst_dir)    shutil.copytree(src_dir,dst_dir)#拷貝檔案    #shutil.make_archive(dst_dir,‘zip‘,src_dir)#將檔案壓縮,註:如果壓縮tar,中文檔案名稱有可能亂碼    for path, folders, files in os.walk(src_dir):        for fname in files:            full_path = os.path.join(path, fname)            md5dict[full_path] = md5check(full_path)    if os.path.exists(md5file):        with open(md5file, ‘wb‘) as f0:            p.dump(md5dict, f0)    else:        with open(md5file,‘xb‘) as f1:            p.dump(md5dict,f1)def incr_backup(src_dir, dst_dir, md5file):    par_dir, base_dir = os.path.split(src_dir.rstrip(‘/‘))    back_name = ‘%s_incr_%s.tar.gz‘ % (base_dir, time.strftime(‘%Y%m%d‘))    full_name = os.path.join(dst_dir, back_name)    md5new = {}    for path, folders, files in os.walk(src_dir):        for fname in files:            full_path = os.path.join(path, fname)            md5new[full_path] = md5check(full_path)    with open(md5file,‘rb‘) as fobj:        md5old = p.load(fobj)    with open(md5file, ‘wb‘) as fobj:        p.dump(md5new, fobj)    #tar = tarfile.open(full_name, ‘w:gz‘)    for key in md5new:        if md5old[key] != md5new[key]:            #tar.add(key)            shutil.copyfile(key,dst_dir)    #tar.close()if __name__ == ‘__main__‘:    src_dir = ‘base/src‘    dst_dir = ‘base/dest/backupfile‘    md5file = ‘base/md5.data‘    full_backup(src_dir, dst_dir, md5file)    # if time.strftime(‘%a‘) == ‘Mon‘:    #     full_backup(src_dir, dst_dir, md5file)    # else:    #     incr_backup(src_dir, dst_dir, md5file)

 

python實現對檔案的全量、增量備份

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.