bash 用 Python 控制 rsync?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1657614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controlling rsync with Python?
提问by Cheesemold
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.
我一直想编写一个 python 脚本,该脚本将按顺序运行多个 rsync 实例,以便将数据备份到不同的计算机。
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?
我希望能够使用 python 为我做这件事。我非常模糊地知道如何使用 subprocess.popen,但我不知道如何让 python 直接与 rsync 交互,比如为我输入密码。蟒蛇能做到吗?
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?
或者我能做的最好的就是拥有它,甚至是一个 bash 脚本,只需按顺序运行 rsyncs 并且必须一遍又一遍地输入我的密码?
Thanks in advance.
提前致谢。
回答by Ryan Bright
If you'd like to interact with a subprocess in general, you can use pexpectas 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 sshconnection between the two hosts, which will eliminate the need to enter the password. This is a key-based solutionand will be much more secure than storing your password in your source code.
如果您想与一般的子流程进行交互,则可以使用其他地方提到的pexpect。但是对于您的特定情况,假设您的 rsync 运行在 ssh(默认)上,那么您可能需要考虑在两台主机之间设置无密码 ssh连接,这样就无需输入密码。这是一种基于密钥的解决方案,比将密码存储在源代码中要安全得多。
Here's a bloggerwho discusses your exact problem and decides to go with passwordless ssh.
这是一位博主,他讨论了您的确切问题并决定使用无密码 ssh。
回答by radicalbiscuit
There's a great Python module written by Colin Stewart called RSyncBackup that's little known and little documented, but very useful.
有一个很棒的 Python 模块,由 Colin Stewart 编写,名为 RSyncBackup,鲜为人知,文档也很少,但非常有用。
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
默认情况下,它不包含任何在 rsync 命令中包含密码的方法,因此我修改了该模块并在这篇博文中讨论了它:http: //technofart.blogspot.com/2012/02/rsync-control -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 实现会寻找 RSYNC_PASSWORD 环境变量,您可以设置它。如果您的环境变量对其他用户可见,请小心。
回答by Peter Rowell
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.
我使用 rsync 来备份我所有客户的网站。脚本由 cron 触发,它根据每个客户端的不同需求为每个客户端使用 Makefile。
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 definitelyyour friend.
与其执行输入密码的操作,不如使用 ssh-keygen 创建公钥/私钥对并将您的公钥放在远程机器上。这为您提供安全、无密码的连接。这也意味着您不必向全世界公开 rsync 端口。在您通过这方面的学习曲线(而且不是很陡峭)之后,ssh绝对是您的朋友。
回答by PyTis
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
更重要的是,您曾询问如何以交互方式获取密码。为此,我建议使用内置的 getpass。简而言之,您没有与 rsync 交互。在执行 rsync 之前,您正在从用户那里获取密码,并将其传递给 rsync。另一种选择是允许用户将其作为选项传入,我的大多数命令行脚本都使用 optparse
import getpass
password = getpass.getpass('Password for %s: ' % opts.user)
try:
#code that uses password
except Exception, e:
# test to see if str(e) is really an invalid password error, if so tell the user and return or loop, up to you
# else
raise Exception(e) # the error that was raised in the first place
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.calland Python Subprocess.Popen from a thread
继续,我偶然发现了你的问题,因为我正在寻找类似的东西。仅供参考,我最终引用了其中两个堆栈溢出链接: 从线程调用 rsync from python subprocess.call和 Python Subprocess.Popen
回答by R Samuel Klatchko
回答by Mark Streatfield
回答by cozzyd
If you just need to enter the password, you can try populating the RSYNC_PASSWORDenvironmental variable or using the --password-fileoption.
如果只需要输入密码,可以尝试填充RSYNC_PASSWORD环境变量或使用--password-file选项。
回答by max
I've made a package called paralle_rsync that does rsync commands in parallel. You can use it within fabric to perform the operation on multiple hosts at the same time.
我制作了一个名为 paralle_rsync 的包,它可以并行执行 rsync 命令。您可以在结构中使用它同时在多个主机上执行操作。

