Automatic syncing of modified files with remote server
Posted on 2020.09.04
Another quick one. In my setup, I'm coding on my own machine but I'm running the code on external machine. Sometimes on AWS, sometimes on my workstation. You see, that is the problem that I have to deploy my code to two places.
Up to now, I was relying on the "Automatic deployment" feature that is built into PyCharm and all the IntelliJ IDEs. Unfortunately, there is a bug that prevents me from setting two separate connection configurations. I'm not 100% sure, but it looks like this is due to a bug that is present in the issue tracker already for two years. [1]
Anyway, in order to make my life much easier, I decided to look for solution, outside of the PyCharm. The solution is inotify-tools. _[2]
On ubuntu, get it with:
sudo apt install inotify-tools
After that, the usage is pretty simple. If you want to observe only one file for modification and automatically upload it with rsync, you can use a command similar to mine:
while inotifywait -e modify /home/damian/important_project/source_filepath.py; do
rsync -avR -e ssh /home/damian/./important_project/source_filepath.py remoteserver:/destination/path/
done
This will only watch for modifications of a single file. If you want to observe and upload a full directory, use:
while inotifywait -e -r modify,create,delete,move /home/damian/important_project; do
rsync -avR -e ssh /home/damian/./important_project/ remoteserver:/destination/path/
done
This should keep your directories in sync.
[1] | https://youtrack.jetbrains.com/issue/PY-33981 |
[2] | https://github.com/inotify-tools/inotify-tools/wiki |