views:

1908

answers:

5

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.

+2  A: 

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.

Peter Rowell
I've definitely considered using a key pair for doing this, but I wanted to see if it was possible to have one program control another since it seems like a skill that could come in handy for situations that require interaction and don't have an easy solution to disregard it. But I may end up doing a keypair anyways...
Cheesemold
+1  A: 

If you need to programatically control a sub-process, you should look into using expect.

R Samuel Klatchko
You should be clear that that's actually pexpect.
Dennis Williamson
+6  A: 

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.

Ryan Bright
A: 

I don't think it supports rsync out of the box, but paramiko might have some components you could recycle?

Mark Streatfield
A: 

If you just need to enter the password, you can try populating the RSYNC_PASSWORD environmental variable or using the --password-file option.

cozzyd