Automate rsync w/ authentication

Bryan bryanvick at gmail.com
Fri Jul 10 12:13:41 EDT 2009


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]
try:
	p = Popen(args, stdout=PIPE, stderr=PIPE)
	print 'rsync running with pid %s' % p.pid
	out, err = p.communicate()
	print 'Errors: %s' % err
	print 'Output: %s' % out
except Exception:
	print 'Error running rsync'




More information about the Python-list mailing list