欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 将A服务器上指定文件夹中的文件,批量同步到B服务器上

将A服务器上指定文件夹中的文件,批量同步到B服务器上

2025/9/14 19:18:12 来源:https://blog.csdn.net/qq_26870933/article/details/141189323  浏览:    关键词:将A服务器上指定文件夹中的文件,批量同步到B服务器上

需求:最近有一个需求,需要定期将A服务器上的PDF文件,同步到B服务器上,于是便写个脚本记录一下!

  • 下面是使用Python3脚本实现的方法
import os
import paramikodef copy_pdf_files(source_ip, source_user, source_password, source_path,dest_ip, dest_user, dest_password, dest_path):# 创建一个SSH客户端ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 连接到源服务器ssh.connect(source_ip, username=source_user, password=source_password)sftp = ssh.open_sftp()# 获取源目录下的所有PDF文件pdf_files = [f for f in sftp.listdir(source_path) if f.endswith('.pdf')]# 断开与源服务器的连接,重新创建SSH客户端ssh.close()ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 连接到目标服务器ssh.connect(dest_ip, username=dest_user, password=dest_password)dest_sftp = ssh.open_sftp()# 将PDF文件从源服务器复制到目标服务器for file_name in pdf_files:with sftp.file(os.path.join(source_path, file_name), 'rb') as src_file:with dest_sftp.open(os.path.join(dest_path, file_name), 'wb') as dest_file:dest_file.write(src_file.read())# 关闭SFTP和SSH连接dest_sftp.close()ssh.close()# 使用你的参数调用函数
copy_pdf_files('192.168.3.1', 'your_username', 'your_password', '/home/Files','192.168.30.10', 'your_username', 'your_password', '/home/Files')
  • 定时任务
    • 使用linux中自带的 crontab 实现 => 传送门

完毕!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词