在本地缓存和sftp服务器之间同步文件
sftpsyncer的Python项目详细描述
在本地缓存和远程缓存之间逐段同步文件 SFTP服务器。
用例
您的客户机有时需要将文件同步到和 从SFTP服务器。
- open()一个文件,不管它是远程的还是本地的。 *如果文件在本地可用,请使用本地副本。 *如果本地文件不存在,则下载它,然后使用本地副本。
- 将()文件添加到上载包,然后push()上载所有本地排队的文件 文件夹。
这个模型很简单,如果 存在同名的文件。如果你想避免,你可能会更好 不用git之类的。
使用方法:
import sftpsyncer from sftpsyncer import syncagent sagent = syncagent(hostname="remote_sagenth.example.com", host_port="1222", username="theuser", key_path="/home/theuser/.sagenth/key", local_root="/tmp/cache", remote_root="/upload_target") # upload local files sagent.upload_queue.add("myfile.txt") sagent.upload_queue.add("docs/readme.txt") sagent.push() # upload whether or not remote files exist # auto-get remote files, reading from cache when available fh1 = sagent.open("myfile.txt") # file is already local, return local fh fh2 = sagent.open("remote.txt") # file is not local, transfer to local cache, return local fh # exceptions try: fh3 = sagent.open("not_anywhere.txt") except sftpsyncer.exceptions.FileNotFound as e: print "File does not exist on local or remote" try: fh5 = sagent.open("ssh_server_unavailable.txt") except sftpsyncer.exceptions.ConnectionError as e: print "Remote SFTP service appears to be unavailable"