Automate rsync w/ authentication

Chris Rebert clp2 at
Fri Jul 10 21:47:59 CEST 2009

On Fri, Jul 10, 2009 at 12:43 PM, Piet van Oostrum<piet at> wrote:
>>>>>> Chris Rebert <clp2 at> (CR) wrote:
>>CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan<bryanvick at> wrote:
>>>> I am trying to automate rsync to backup server A from server B.  I
>>>> have set up a private/public key between the two servers so I don't
>>>> have to enter a password when using rsync.  Running rsync manually
>>>> with the following command works fine:
>>>> rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"
>>>> root at /home/bry/tmp
>>>> But when I try to do it with python, the subprocess simply returns the
>>>> ssh -h output on stderr like I am passing some invalid syntax.  What
>>>> is wrong in my translation of rsync's -e command from shell to
>>>> pythyon?
>>>> #! /usr/bin/python
>>>> from subprocess import Popen, PIPE
>>>> rsyncExec = '/usr/bin/ssh'
>>>> source = 'root at'
>>>> dest = '/home/bry/tmp'
>>>> rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"'
>>>> args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest]
>>CR> Like many problems involving the subprocess module, I think you've
>>CR> tokenized the arguments incorrectly. Try:
>>CR> rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"'
>>CR> args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest]
>>CR> Note that the -e switch and its operand are separate arguments for the
>>CR> purposes of POSIX shell tokenization.
> I think you should have only one kind of quotes in rshArg:
> rshArg = "/usr/bin/ssh -i /home/bry/keys/brybackup.key"
> I haven't tried it, however, but this is just how Unix works.

Ah, indeed, I think you're probably right. Just goes to show it's not
always easy to get exactly right.


More information about the Python-list mailing list