python实现文件夹增量同步

工作中,经常要把windows的文件夹同步到linux上。xftp也可以,sublime也有远程上传的插件,但没找到支持增量的。。。大量时间花在找插件,装环境。。。然后一怒之下,自己东拼西凑了一下。

支持:上传文件夹,和删除远程文件。

增量是用当前时间和上次上传时间对比实现的。

cache.dat记录每个文件的上传时间,必要的时候可以删除重新上传

环境。python2.7

#coding=utf-8  
#!/usr/bin/python
import os
import os.path
import shutil
import sys
import string  
import fnmatch
import pickle
import time
from ftplib import FTP  
import paramiko
import socket
from stat import S_ISDIR

import _cffi_backend
import logging

logging.basicConfig()

cachefilename="cache.dat"


class SSHSession(object):

    def __init__(self,hostname,port,username='root',password=None,key_file=None):
        #
        #  Accepts a file-like object (anything with a readlines() function)  
        #  in either dss_key or rsa_key with a private key.  Since I don't 
        #  ever intend to leave a server open to a password auth.
        #
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((hostname,port))
        print("connect ", hostname, port)

        self.t = paramiko.Transport(self.sock)
        self.t.start_client()
        # supposed to check for key in keys, but I don't much care right now to find the right notation
        if password is not None:
            self.t.auth_password(username,password,fallback=False)
        else: raise Exception('Must supply either key_file or password')
        self.sftp=paramiko.SFTPClient.from_transport(self.t)

        try:
            l = pickle.load(open(cachefilename))
        except IOError:
            l = []
        self.db = dict(l)


    def put(self,localfile,remotefile):
        #  Copy localfile to remotefile, overwriting or creating as needed.
        remotefile = remotefile.replace('\\', '/')
        print("upload %s %s"%(localfile, remotefile))
        try:
            self.sftp.put(localfile,remotefile)
        except Exception as e:
            print "put ", e

    def put_all(self,localpath,remotepath,ignore_list):
        self.mkdir_ifnotexists(remotepath)
        if os.path.exists(localpath):
            for root, dirs, files, in os.walk(localpath):
                for file in files:
                    filename = os.path.join(root, file)
                    if any(fnmatch.fnmatch(filename, pattern) for pattern in ignore_list):
                        print 'Ignore', filename
                        continue

                    mtime = time.ctime(os.path.getmtime(filename))
                    if self.db.get(filename, None) != mtime:
                        remotefile=os.path.join(remotepath,filename)
                        self.mkdir_ifnotexists(os.path.dirname(remotefile))

                        self.put(filename, remotefile)
                        self.db[filename] = mtime

        # delete files
        for filename, value in self.db.items():
            if os.path.exists(filename) == False:
                remotefile=os.path.join(remotepath,filename)
                try:
                   self.sftp.remove(remotefile)
                except Exception as e:
                    print 'Delete',filename, e

        pickle.dump(self.db.items(), open(cachefilename, "w"))
        print("ok!!!")

    def mkdir_ifnotexists(self, remotedirectory):
        remotedirectory = remotedirectory.replace('\\', '/')
        print "mkdir_ifnotexists", remotedirectory
        try:
            self.sftp.chdir(remotedirectory)  # Test if remote_path exists
        except IOError:
            self.mkdir_p(remotedirectory)

    def mkdir_p(self, remotedirectory):
        """Change to this directory, recursively making new folders if needed.
        Returns True if any folders were created."""
        if remotedirectory == '/':
            # absolute path so change directory to root
            self.sftp.chdir('/')
            return
        if remotedirectory == '':
            # top-level relative directory must exist
            return
        try:
            self.sftp.chdir(remotedirectory) # sub-directory exists
        except IOError:
            dirname, basename = os.path.split(remotedirectory.rstrip('/'))
            self.mkdir_p(dirname) # make parent directories
            self.sftp.mkdir(basename) # sub-directory missing, so created it
            self.sftp.chdir(basename)
            print "mkdir", basename
            return True

def iter_find_files(path, fnexp):
    for root, dirs, files, in os.walk(path):
        for filename in fnmatch.filter(files, fnexp):
            yield os.path.join(root, filename)

def main():
    host="10.10.XX.XX"
    user="root"
    password="123"
    port=22
    localpath="./"
    remotepath="/data/server/remotefolder"
    ignore_list=['*svn/*','*.py']

    xfer = SSHSession(host,port,user,password)
    xfer.put_all(localpath, remotepath, ignore_list)

if __name__ == '__main__':
    main()
    os.system("pause")

使用时填写下(远程地址,端口,用户,密码,本地路径,远程路径,以及过滤文件)

posted @ 2018-03-16 00:39  light_world  阅读(4397)  评论(0编辑  收藏  举报