You are here:

Ubuntu and Associated Distributions/Use inotify and rsync to sync files to hosting server on the fly



What I'm trying to accomplish is to have a folder monitored for any folder/files created, modified, moved or deleted on the local folder and sync them with the hosting server.

I've blown a whole day Googling and trying this and that to make this go.  No luck! I monitors and see a changed file but never sends anything to the hosting server.

This is the bash script I came up with:

RSYNC_OPTIONS="-avrz --exclude-from .rsync-exclude-list 'ssh -p 2222'"
while true
   inotifywait -r $LOCAL
   rsync $RSYNC_OPTIONS --progress "$LOCAL" "$DEST"

Now I know the rsync part does work because if I do it that from the command line like this:

rsync -avrz --progress --rsh='ssh -p 2222' /home/dave/WWW/ftsc-v3.0/ :/home4/mrbiggz6/public_html/ftscv3/

That works.

Now if I execute the script I get this:

dave@linuxmint ~ $ ./auto-sync
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/home/dave/WWW/ftsc-v3.0/iface/ MODIFY ptest.php
ssh: connect to host port 22: Connection timed out
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

I did modify that file ptest.php and the rsync is trying to connect on port 22 when that should be 2222.

I got spoiled having an application like WinSCP on my windows partition.  I'm now on day 7 of being off of windows and with the exception of this it's going ok.

So you know on using Linuxmint 17

A little bit about me, I'm an IT person.  I work primarily with mainframe systems.  So I'm not stranger to this world and I've diddled with Linux here and there so I'm not quite a novice and at best a shaky intermediate level skill set.

I hope you can help me with my little plight!

Thanks so much for the help!



ANSWER: Not sure what the bash script has to do with the one executed, that has a php script in it.

ssh *is* on 22 by default.  I can't tell from the notes if the reason for the ssh timeout is the port number.  What makes you say the correct port is 2222?

What is the php script doing?

Is the ssh connection being called from the php script?  Guess this isn't important. Please use -vv when calling ssh, then re-post the output.

[an error occurred while processing this directive]---------- FOLLOW-UP ----------

QUESTION: Sorry for the confusion.

I know that SSH is usually on port 22.  HostGator for reseller and shared hosts uses port 2222.

The php script isn't anything I only modified that file to try and use the bash script to sync up with.  It would be as if I where working on a website with php documents on my local machine when when I create, modify, move or delete them they get synced on the fly with the host.  This has nothing to do with the actual watching and rsync issue.

I think I solved some of my problem!

I changed the script to this:

RSYNC_OPTIONS="-avrz --exclude-from .rsync-exclude-list"
while true
   inotifywait -r $LOCAL
   rsync $RSYNC_OPTIONS --progress --rsh='ssh -p 2222' "$LOCAL" "$DEST"

I guess it didn't like the port in the options declaration!

So when I start the script it does this:

dave@linuxmint ~ $ sh ./auto-sync
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/home/dave/WWW/ftsc-v3.0/iface/ MODIFY ptest.php
's password:
sending incremental file list
         7,655 100%  973.63kB/s    0:00:00 (xfr#1, to-chk=519/696)
         8,496 100%    8.10MB/s    0:00:00 (xfr#2, to-chk=518/696)
         436 100%  425.78kB/s    0:00:00 (xfr#3, to-chk=505/696)
         3,649 100%    3.48MB/s    0:00:00 (xfr#4, to-chk=487/696)
         1,073 100%   17.46kB/s    0:00:00 (xfr#5, to-chk=243/696)
         2,286 100%   36.60kB/s    0:00:00 (xfr#6, to-chk=242/696)
        14,854 100%  213.32kB/s    0:00:00 (xfr#7, to-chk=202/696)

sent 21,435 bytes  received 1,003 bytes  1,795.04 bytes/sec
total size is 26,943,830  speedup is 1,200.81
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

Those where a bunch of other files that I had edited prior to doing this.  As you can see ptest.php was sync'd.

I did try and deleting a file which didn't work.  Rsync did not delete the file.  

So I'm left with these two issues:

1) The biggest is I have to put my password in every time rsync runs! =( Kind of takes away from the automated aspect

2) Issue of deleting file locally and rsync deleting them on the remote server.  Which I'm assuming that move wouldn't work either since that is basically creating a file in another place and deleting the source.

Hope you can help me on my last two issues!

An ssh key and passphrase should care of the ssh password issue if I understood the problem correctly.

Deleting a file might require checking for its existence and executing code based on the check result.  ssh, in the mode where it executes on the remote machine should work.

ssh <host> "scriptpath/name"

where the script is located on the remote host.

Does this help?

Ubuntu and Associated Distributions

All Answers

Answers by Expert:

Ask Experts


John Crout


The command line, configuration files, recompiling the kernel, maintaining a system, hardening a system, package managers, browsers. Questions about Debian, Ubuntu, KUbuntu, MINT, Knoppix, CentOS, Red Hat Enterprise, Linux Wifi, embedded Linux, Live CD, forensics tools, vulnerability and pen testing tools, virtual installations.


LiveCDs, Debian, Red Hat, CentOS, Running on VMware, MINT, Ubuntu Studio, NIST Working Groups,

Association for Computing Machinery Information Systems Security Association (ISSA), United States Coast Guard Auxiliary, ISC2, ACM

RTCA/DO-178B, "Software Considerations in Airborne Systems and Equipment Certification", (member of SC-167), RTCA/DO-160C, "Environmental Conditions and Test Procedures for Airborne Equipment", (member of SC-135) Student Doctor Magazine EE Times Network World blog Navy CIO blog Medforum CGAUX groups and CGAUX National Staff groups Analytic Bridge LinkedIn

Master of Public Health (Epidemiology and Biostatistics) Bachelor of Science in Electrical Engineering (Computer Engineering Option) Digital Forensics Basics, Texas Engineering Extension Secure Software, Texas Engineering Extension Network Security, Texas Engineering Extension Infosec Basics, Texas Engineering Extension Information Assurance Awareness, Department of Defense Information Systems Security, United States Coast Guard Online safety for parents and children, Fort Collins Police Department Spear Phishing, Defense Information Security Agency, DISA Spear Phishing, United States Coast Guard, USCG Meeting the Media, Testifying, Presentations, Arch Lustberg FEMA (ICS-100, -200, -210, -700, 800) United States Coast Guard Defense Information Systems Agency United States Coast Guard Auxiliary Crosby Quality Workgroup Facilitator Trainer Crosby Quality Education System Wisconsin University, ISSA training workshops

©2017 All rights reserved.

[an error occurred while processing this directive]