Linux Sync Two Directories using rsync and inotify

Linux has powerful tools that enables you to sync files or folders from one source directory to another destination directory. 
Step 1: Install inotify tools
sudo apt-get install inotify-tools
More information can be found on their wiki: https://github.com/rvoicilas/inotify-tools/wiki
Step 2: Install rsync
sudo apt-get install ssh rsync
Step 3: Create a new bash script file called sync.sh
nano sync.sh
Step 4: Paste the below code in the sync.sh file
#!/bin/sh
while inotifywait -r -e modify,create,delete,move /home/user/rsync-test/source_folder; do
    rsync -avz /home/user/rsync-test/source_folder/home/user/rsync-test/destination_folder --delete
done
You must modify the highlighted source and destination folder locations in the above shell script file to match your setup.
Step 5: Make the shell script executable
chmod +x sync.sh
Step 6: Run the shell script
./sync.sh
That's it, you are done! At this point if you make any change to the source directory, it will also apply to the destination directory.

Post a Comment

Previous Post Next Post