Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling rsync with Python?

I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.

At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.

I want to be able to use python to do this for me. I know very vaguely how to use subprocess.popen, but I have no clue how to get python to interact with rsync directly, like for entering my password for me. Can python do that?

Something like:

if theProccess.proccessResponse == "Password:" :
    theProccess.respond(string)

Or is the best that I can do is just have it, or even a bash script, just run the rsyncs in sequence and have to type my password in over and over again?

Thanks in advance.

like image 257
Cheesemold Avatar asked Nov 01 '09 16:11

Cheesemold


2 Answers

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.

like image 93
Ryan Bright Avatar answered Sep 30 '22 10:09

Ryan Bright


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.

like image 45
radicalbiscuit Avatar answered Sep 30 '22 11:09

radicalbiscuit