Automate rsync w/ authentication

Bryan bryanvick at gmail.com
Fri Jul 10 22:16:02 CEST 2009


On Jul 10, 12:43 pm, Piet van Oostrum <p... at cs.uu.nl> wrote:
> >>>>> Chris Rebert <c... at rebertia.com> (CR) wrote:
> >CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan<bryanv... 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"
> >>> r... 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 = 'r... 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 <p... at cs.uu.nl>
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p... at vanoostrum.org

I tried removing the internal quotes, and splitting '-e' from the
other arguments.  I still get the same ssh -h output however:

rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key'
args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source,
dest]
p = Popen(args, stdout=PIPE, stderr=PIPE)




More information about the Python-list mailing list