Sunday, October 3, 2010

Python enhancement to scp

I was on windows a while ago and found out that WinSCP has this cool feature to keep a remote directory in sync. I didn't really find anything similar on linux. There's rsync, but it's not the same thing.

I wrote a little python script that does just that. You can use it to keep multiple local directories or files in sync with several remote directories. The syntax is just like scp

scpy path/to/directory username@remotehost:/path/to/
This will sync the local directory "directory" to the remote host under the path /path/to/directory. Then, subsequent modifications to any files within the local directory will propagate to the remote one. This fully works with nested folders.

What you can also do is sync multiple local directories

scpy path/to/directory path/to/otherdirectory username@remotehost:/path/to

Now this will sync both directory and otherdirectory into the /path/to remote directory. You can add as many local paths as you want, and you can add as many remote hosts as you want.

There are a couple of options with this. If you want to propagate modifications in a directory but you DON'T want to initially sync the hole directory, run scpy with the -s flag.

scpy -s path/to/directory username@remotehost:/path/to

This will watch the directory, and propagate any file modifications to the remote directory. For now, if you modify a file in a watched directory, and that directory doesn't exist on the remote host, it won't work.

There are more options in the --help section, but they're not implemented yet. Also be aware that this is beta software. I've tested it and I use it myself at work. But with remote file transfers, you should always be careful. I would suggest against using it to send to the root user on a remote host until you've tried it under your work conditions.

Since it uses inotify to watch for directory modifications, it needs linux kernel 2.6.13. I wrote this in python 2.6.5, but it will probably work in any version that supports the following modules:

There's no installation to this, just put the script wherever you want and run it. I suggest putting it in /usr/local/bin and renaming it to scpy (remote the .py extension). Also, don't forget to chmod it to executable (chmod +x scpy)

Download here

No comments: