美文网首页程序员
居于python写的差量同步工具

居于python写的差量同步工具

作者: 数据小菜鸟 | 来源:发表于2018-12-19 10:02 被阅读0次

    经常同步外网版本 常用 Rsync,scp 都比较方便
    有事部分平台使用ftp 就比较头疼(零散文件多目录多简直就是噩梦,尤其是公司网络渣比就更凉凉)。解决方案 Beyond Compare 远程对比(非完美,但是可行)
    另外一种自己写一个python小工具根据文件创建时间和修改去找文件然后用找出的文件直接用ftp上传。自动动手丰衣足食,训练自己动手能力。
    同时推荐一个ftp工具 WinSCP

    # -*- coding: utf-8 -*-
    # xiaoyu chen
    # 358860528@qq.com
    # 按照时间差量同步工具
    # 用于ftp 等没有比较同步的服务器作为辅助
    
    import os,datetime,json,time,shutil
    
    
    path='./src'
    fixtime = 0
    outpath = './out'
    
    
    #判断是否有辅助文件
    if os.path.exists("./find.json"):
        ffo = open("./find.json","r+")
        js = json.load(ffo)
        fixtime = js["tt"]
        tft = datetime.datetime.fromtimestamp(fixtime)
        print u"上次更新时间",tft.strftime("%Y-%m-%d %H:%M:%S")
        ffo.close()
    
    #清理老的更新文件
    if os.path.exists(outpath):
        #os.getcwd() +
        #del_file(outpath)
        shutil.rmtree(outpath)
        
    os.makedirs(outpath)
    
    print "="*20
    
    for dirpath,dirnames,filenames in os.walk(path):
        for ff in filenames:
            fullpath=os.path.join(dirpath,ff)
            
            mtime = os.path.getmtime(fullpath)
            ctime = os.path.getctime(fullpath)
            if ctime > mtime:
                mtime = ctime
    
            #修改时间和创建比上次大需要更新文件
            #datetime.datetime.fromtimestamp(mtime)
            if  int(mtime) > fixtime:
                #print dirpath,ff
                print u"更新文件:",fullpath
                #print fullpath,mtime,datetime.datetime.fromtimestamp(mtime)
                #写拷贝函数即可
                newpath = fullpath.replace(path,outpath)
                newdir = dirpath.replace(path,outpath)
    
                if os.path.exists(newdir) == False:
                    #print "exists is not",newdir
                    os.makedirs(newdir)
                    
                shutil.copy(fullpath,newpath)
                
    
    
    with open("./find.json","w+") as f:
        n = int(time.time())
        fixtime = n
        json.dump({"tt":fixtime},f)
        f.close()
    
    
    image.png

    相关文章

      网友评论

        本文标题:居于python写的差量同步工具

        本文链接:https://www.haomeiwen.com/subject/kxudkqtx.html