Controlling rsync with Python? Controlling rsync with Python? python python

Controlling rsync with Python?


If you'd like to interact with a subprocess in general, you can use pexpect as 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 ssh connection between the two hosts, which will eliminate the need to enter the password. This is a key-based solution and will be much more secure than storing your password in your source code.

Here's a blogger who discusses your exact problem and decides to go with passwordless ssh.


There's a great Python module written by Colin Stewart called RSyncBackup that's little known and little documented, but very useful.

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

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.


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.

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 definitely your friend.