Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

There is a directory A whose contents are changed frequently by other people.

I have made a personal directory B where I keep all the files that have ever been in A.

Currently I just occasionally run rsync to get the files to be backed up from A to B. However I fear the possibility that some files will get added in A, and then removed from A before I get the chance to copy them over to B.

What is the best way to prevent this from occurring? Ideally i'd like to have my current backup script run every time the contents of A get changed.

share|improve this question

3 Answers 3

up vote 15 down vote accepted

If you have inotify-tools installed you can use inotifywait to trigger an action if a file or directory is written to:

#!/bin/sh
dir1=/path/to/A/
while inotifywait -qqre modify "$dir1"; do
    /run/backup/to/B 
done

Where the -qq switch is completely silent, -r is recursive (if needed) and -e is the event to monitor, in this case modify. From man inotifywait:

modify
A watched file or a file within a watched directory was written to.
share|improve this answer
3  
incrond may also be of interest. –  Shawn J. Goff Nov 6 '11 at 13:50
    
Cool. Would I just put the above script in my bashrc so that it run's when i log in? Is there another way I can have it running all the time? –  oadams Nov 7 '11 at 11:53
    
For login, yes /etc/profile for system-wide or .bash_profile for just your user. To run it after boot, it depends on your flavour of Unix/Linux; /etc/rc.local,/etc/rc.d/ or /etc/init.d/ –  jasonwryan Nov 7 '11 at 17:05
    
For larger directories you might want to consider using the --monitor switch (and pipe the output to your loop instead), otherwise there is a lot of overhead when inotifywait is started over and over again –  Tobias Kienzler Jan 10 '13 at 17:10

Strictly speaking, if someone drops a file and very quickly removes it, you might miss it. The use of inotify (under Linux, or a similar feature under other unices) makes the window of risk small.

If you can mount the filesystem of your choice on that directory (I realize this may not be an option), you can put one that records all file versions, for example copyfs.

share|improve this answer

The program you're looking for is inotify.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.