用Python控制rsync?
我一直想编写一个 python 脚本,该脚本将按顺序运行多个 rsync 实例,以便将数据备份到另一台计算机。
目前,我只有这个文本文件,其中包含我使用的命令,我刚刚将它们复制粘贴到终端中,这看起来有点愚蠢。
我希望能够使用 python 为我做到这一点。我非常模糊地知道如何使用 subprocess.popen,但我不知道如何让 python 直接与 rsync 交互,比如为我输入密码。蟒蛇能做到吗?
比如:
if theProccess.proccessResponse == "Password:" :
theProccess.respond(string)
或者我能做的最好的事情就是拥有它,甚至是 bash 脚本,只需按顺序运行 rsync,并且必须一遍又一遍地输入我的密码?
提前致谢。
I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.
At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.
I want to be able to use python to do this for me. I know very vaguely how to use subprocess.popen, but I have no clue how to get python to interact with rsync directly, like for entering my password for me. Can python do that?
Something like:
if theProccess.proccessResponse == "Password:" :
theProccess.respond(string)
Or is the best that I can do is just have it, or even a bash script, just run the rsyncs in sequence and have to type my password in over and over again?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您想与子流程进行交互,可以使用 pexpect,如其他地方所述。但对于您的特定情况,假设您的 rsync 通过 ssh (默认)运行,那么您可能需要考虑设置 两台主机之间的无密码 ssh 连接,这样就无需输入密码了。这是一个基于密钥的解决方案,比将密码存储在源代码中要安全得多。
这是一位博主,他讨论了您的确切问题并决定使用无密码 ssh。
If you'd like to interact with a subprocess in general, you can use pexpect as mentioned elsewhere. But for your particular case, assuming your rsync is running over ssh (the default), then you may want to consider setting up a passwordless ssh connection between the two hosts, which will eliminate the need to enter the password. This is a key-based solution and will be much more secure than storing your password in your source code.
Here's a blogger who discusses your exact problem and decides to go with passwordless ssh.
Colin Stewart 编写了一个很棒的 Python 模块,称为 RSyncBackup,它鲜为人知,也很少有文档记录,但非常有用。
默认情况下,它不包含任何在 rsync 命令中包含密码的方法,因此我修改了该模块并在此博客文章中讨论了它: http://technofart.blogspot.com/2012/02/rsync-control-by-python.html
我修改的模块的链接可以在我帖子的下载部分。
基于密钥的解决方案也是一个好主意。此外,许多 rsync 实现都会查找您可以设置的 RSYNC_PASSWORD 环境变量。如果您的环境变量对其他用户可见,请务必小心。
There's a great Python module written by Colin Stewart called RSyncBackup that's little known and little documented, but very useful.
By default, it doesn't contain any methods for including a password in your rsync commands, so I modified the module and talked about it in this blog post: http://technofart.blogspot.com/2012/02/rsync-controlled-by-python.html
A link to my modified module can be found in the Download section of my post.
A key-based solution is also a great idea. Also, many rsync implementation will look for the RSYNC_PASSWORD environment variable, which you can set. Just be careful if your environment variables are visible to other users.
我使用 rsync 来备份我客户的所有网站。脚本由 cron 触发,并且由于每个客户端的不同需求而使用 Makefile。
不要执行输入密码的操作,而是使用 ssh-keygen 创建公钥/私钥对并将公钥放在远程计算机上。这为您提供安全、无密码的连接。这也意味着您不必向外界公开 rsync 端口。当你克服了这个学习曲线(而且不是很陡峭)之后,ssh 绝对是你的朋友。
I use rsync to back up all of my clients' web sites. A script is triggered by cron and it uses Makefiles for each client because of their different needs.
Rather than do something that enters the password, use ssh-keygen to create a public/private key pair and put your public key on the remote machine. This gives you secure, no-password connections. This also means you don't have to expose the rsync port to the world. After you get past the learning curve on this (and it's not very steep) ssh is most definitely your friend.
我很抱歉这么晚才回答,但我觉得其他人的回答都是错误的。他们确实回答了你的问题,但没有像他们本可以的那样直接回答。
更重要的是,您问过如何以交互方式获取密码。为此,我建议使用内置的 getpass。简而言之,您没有与 rsync 交互。您在执行 rsync 之前从用户处获取密码,并将其传递给 rsync。另一个选择是允许用户将其作为选项传递,我的大多数命令行脚本都使用 optparse
继续,我偶然发现了你的问题,因为我正在寻找类似的东西。仅供其他人参考,我最终引用了其中两个堆栈溢出链接:
从 python subprocess.call 调用 rsync
和
来自线程的 Python Subprocess.Popen
I feel bad, for answering this late, but I feel like everyone else's answer was wrong. They did KINDA answer your question, but not directly as they could have.
More to the point, you had asked how to grab the password interactively. To do so I would suggest the built-in getpass. In short, you are not interacting with rsync. You are grabbing the password from the user RIGHT before you execute rsync, and passing that into rsync. Another option is to allow the user to pass it in as an option, most of my command line scripts use optparse
To continue, I stumbled upon your question because I was looking for something similar. Just an FYI to anyone else out there, I ended up referencing the two of these stack overflow links:
calling rsync from python subprocess.call
and
Python Subprocess.Popen from a thread
我不认为它支持开箱即用的 rsync,但是 paramiko 可能有一些你可以使用的组件回收?
I don't think it supports rsync out of the box, but paramiko might have some components you could recycle?
如果您需要以编程方式控制子流程,则应该考虑使用 pexpect。
If you need to programatically control a sub-process, you should look into using pexpect.
如果您只需要输入密码,可以尝试填充
RSYNC_PASSWORD
环境变量或使用--password-file
选项。If you just need to enter the password, you can try populating the
RSYNC_PASSWORD
environmental variable or using the--password-file
option.我制作了一个名为parallel_sync 的python 包,它可以并行执行rsync 命令。
您可以在结构内部或外部使用它来同时在多个主机上执行操作。
它是开源的。 GNU 许可证。
https://github.com/kouroshparsa/parallel_sync
欢迎提出改进或做出贡献。
I've made a python package called parallel_sync that does rsync commands in parallel.
You can use it within or without fabric to perform the operation on multiple hosts at the same time.
It's open source. GNU license.
https://github.com/kouroshparsa/parallel_sync
Feel free to ask for improvements or contribute.
您也许可以使用 asyncssh SSHClientConnection.run()。 Asyncssh 将用于连接到本地 ssh 守护进程,然后从那里调用 rsync。我还没有尝试过这个,但它在我的内部 DevOps 工具的优先列表中。
You may be able to use asyncssh SSHClientConnection.run(). Asyncssh would be used to connect to your local ssh daemon and then invoke rsync from there. I have not yet tried this but its on my priority list for my internal devops tools.