Mansoor
Mansoor

Reputation: 83

Why my Inotify script wont go further after watches established?

I have made a script for inotify merge with rsync following this tutorial https://linuxhint.com/inotofy-rsync-bash-live-backups/ but i face an issue. It took so much time and wont go further after just showing this output

Setting up watches.
Watches established.

Here is my script

!/bin/bash
while inotifywait -e modify,create,delete /home/My_home/ITB/uploads
do 
    rsync -avz /home/My_home/ITB/uploads /home/My_home/destination
done

Upvotes: 1

Views: 872

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84642

You have your while loop backwards, you just want to trigger rsync when inotify fires, in response to a file change in your directory -- not repeatedly call inotifywait. You should also add the -me (-m monitor) option for inotifywait, e.g.

inotifywait -me modify,create,delete /home/Mansoor/ITB/uploads | while read; do
    rsync -avz /home/My_home/ITB/uploads /home/My_home/destination
done

That way, each time inotifywait fires (you don't capture the output), you simply call rsync to sync the directories.

Here is an example:

Example Script

#!/bin/bash

inotifywait -me modify,create,delete ~/dev/src-c/tmp/debug/dir1 | while read; do
    rsync -avz ~/dev/src-c/tmp/debug/dir1/ ~/dev/src-c/tmp/debug/dir2
done

Create 2 Empty Dirs

$ ls -al dir1
total 52
drwxr-xr-x  2 david david  4096 May  4 01:42 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..

$ ls -al dir2
total 52
drwxr-xr-x  2 david david  4096 May  4 01:42 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..

Start inotifywait script

$ bash inw.sh &
Setting up watches.
Watches established.

Make a change

$ touch dir1/foo

(additional terminal output resulting from rsync triggered by inotifywait)

sending incremental file list
./
foo

sent 115 bytes  received 38 bytes  306.00 bytes/sec
total size is 0  speedup is 0.00

Verify it Works

$ ls -al dir2
total 52
drwxr-xr-x  2 david david  4096 May  4 01:44 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..
-rw-r--r--  1 david david     0 May  4 01:44 foo

Yes, working as advertised...

Do a couple more

$ touch dir1/bar
$ touch dir1/baz

and check

$ ls -al dir2
drwxr-xr-x  2 david david  4096 May  4 01:54 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..
-rw-r--r--  1 david david     0 May  4 01:53 bar
-rw-r--r--  1 david david     0 May  4 01:54 baz
-rw-r--r--  1 david david     0 May  4 01:44 foo

Upvotes: 3

Related Questions