Automate rsync w/ authentication

Piet van Oostrum piet at cs.uu.nl
Fri Jul 10 15:43:21 EDT 2009


>>>>> Chris Rebert <clp2 at rebertia.com> (CR) wrote:

>CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan<bryanvick at gmail.com> 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 10.0.45.67:/home/bry/jquery.lookup /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 10.0.45.67:/home/bry/jquery.lookup'
>>> 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.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list