Weblog entry #45 for Utumno
Yesterday in my job I faced a standard assignment in system administration which essentially boiled down to the following:
Create two directories, say, /var/www/dirA and var/www/dirB with the following properties:
1) whenever anything is written to dirA, it has to immediately appear in dirB
2) whenever something is deleted from dirA, it has to stay in dirB.
I came up with this, I wonder if anyone has a better solution:
1) install incron:
aptitude install incron
It is an "inotify cron" system. It works like the regular cron but is driven by filesystem events instead of time events.
2) configure it to watch /var/www/dirA and whenever anything changes, call script /usr/local/bin/incron_script with the first argument being the watched directory and the second - file or directory created there:
incrontab -e /var/www/dirA IN_CLOSE_WRITE /usr/local/bin/incron_script $@ $#
3) write a simple /usr/local/bin/incron_script:
#!/bin/sh cp -a $@/$# /var/www/dirB/
Done!
***************************************************************************
Incron suffers from one trap, though: the 'mask' ( IN_CLOSE_WRITE in the above example ) and 'command' ( /usr/local/bin/incron_script $@ $#) stanzas in its configuration file have to be separated with exactly ONE SPACE. Two spaces or one tab make it fail silently (the second space becomes the first character of the command to be executed and the command fails). This is documented in bug 456821
Comments on this Entry
[ Parent | Reply to this comment ]
But that wouldn't help where a file delete in dirA should not remove the file in dirB. Other than that, a hard-/sym-link would have been the way to go.
Cheers.:wq
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Utumno's Scratchpad | View Weblogs ]
You mean dirB should be a hardlink to dirA?
leszek@leszek-desktop:~$ mkdir dirA
leszek@leszek-desktop:~$ ln dirA dirB
ln: `dirA': hard link not allowed for directory
[ Parent | Reply to this comment ]
Good idea, but we don't know the directories are on the same partition (it is so hard to know these days). I thought of incron when I was reading the problem, not sure there is a better solution, what would folks do before the inotify and dnotify interfaces?
[ Parent | Reply to this comment ]
[ Send Message | View Utumno's Scratchpad | View Weblogs ]
the file should be a hard link? Two questions:
1) how do you make this hard link immediately appear in dirB? Seems to me you still have to use incron to do that.
2) what if the file in dirA is, in fact, a directory?
[ Parent | Reply to this comment ]
1) how do you make this hard link immediately appear in dirB? Seems to me you still have to use incron to do that.
</cuote>
Yes, you have to use incron, but if you are soure that both dirA and dirB are in the same partion, you use half the space
<cuote>
2) what if the file in dirA is, in fact, a directory?
</cuote>
you will have to handle that case in your script
[ Parent | Reply to this comment ]