Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Remote Directory Up-to-date

I absolutely love the Keep Remote Directory Up-to-date feature in Winscp. Unfortunately, I can't find anything as simple to use in OS X or Linux. I know the same thing can theoretically be accomplished using changedfiles or rsync, but I've always found the tutorials for both tools to be lacking and/or contradictory.

I basically just need a tool that works in OSX or Linux and keeps a remote directory in sync (mirrored) with a local directory while I make changes to the local directory.


Update

Looking through the solutions, I see a couple which solve the general problem of keeping a remote directory in sync with a local directory manually. I know that I can set a cron task to run rsync every minute, and this should be fairly close to real time.

This is not the exact solution I was looking for as winscp does this and more: it detects file changes in a directory (while I work on them) and then automatically pushes the changes to the remote server. I know this is not the best solution (no code repository), but it allows me to very quickly test code on a server while I develop it. Does anyone know how to combine rsync with any other commands to get this functionality?

like image 903
Jake McGraw Avatar asked Aug 12 '08 21:08

Jake McGraw


3 Answers

lsyncd seems to be the perfect solution. it combines inotify (kernel builtin function which watches for file changes in a directory trees) and rsync (cross platform file-syncing-tool).

lsyncd -rsyncssh /home remotehost.org backup-home/

Quote from github:

Lsyncd watches a local directory trees event monitor interface (inotify or fsevents). It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes. By default this is rsync. Lsyncd is thus a light-weight live mirror solution that is comparatively easy to install not requiring new filesystems or blockdevices and does not hamper local filesystem performance.

like image 181
juwens Avatar answered Oct 21 '22 03:10

juwens


How "real-time" do you want the syncing? I would still lean toward rsync since you know it is going to be fully supported on both platforms (Windows, too, with cygwin) and you can run it via a cron job. I have a super-simple bash file that I run on my system (this does not remove old files):

#!/bin/sh
rsync -avrz --progress --exclude-from .rsync_exclude_remote . remote_login@remote_computer:remote_dir    

# options
#   -a  archive
#   -v  verbose
#   -r  recursive
#   -z  compress 

Your best bet is to set it up and try it out. The -n (--dry-run) option is your friend!

Keep in mind that rsync (at least in cygwin) does not support unicode file names (as of 16 Aug 2008).

like image 19
dwj Avatar answered Oct 21 '22 03:10

dwj


What you want to do for linux remote access is use 'sshfs' - the SSH File System.

# sshfs username@host:path/to/directory local_dir

Then treat it like an network mount, which it is...

A bit more detail, like how to set it up so you can do this as a regular user, on my blog

If you want the asynchronous behavior of winSCP, you'll want to use rsync combined with something that executes it periodically. The cron solution above works, but may be overkill for the winscp use case.

The following command will execute rsync every 5 seconds to push content to the remote host. You can adjust the sleep time as needed to reduce server load.

# while true; do rsync -avrz localdir user@host:path; sleep 5; done

If you have a very large directory structure and need to reduce the overhead of the polling, you can use 'find':

# touch -d 01/01/1970 last; while true; do if [ "`find localdir -newer last -print -quit`" ]; then touch last; rsync -avrz localdir user@host:path; else echo -ne .; fi; sleep 5; done

And I said cron may be overkill? But at least this is all just done from the command line, and can be stopped via a ctrl-C.

kb

like image 9
KevinButler Avatar answered Oct 21 '22 03:10

KevinButler