New User? Register here - Existing Users: Username: Password: [Advanced Login]

 

 

Current Poll

Your preferred Interactive shell?









( 1337 votes ~ 14 comments )

 

Weblog entry #45 for Utumno

Incron
Posted by Utumno on Thu 30 Oct 2008 at 09:48
Tags:

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

Posted by Anonymous (221.161.xx.xx) on Thu 30 Oct 2008 at 11:30
I would have used a hard link.

[ Parent | Reply to this comment ]

Posted by daemon (146.231.xx.xx) on Thu 30 Oct 2008 at 20:47
[ Send Message | View Weblogs ]

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 ]

Posted by Anonymous (216.244.xx.xx) on Fri 31 Oct 2008 at 13:05
if you use hard links, the file wont be removed until you remove both hard links (the one in dirA and the one in dirB). Hard links is the way to go, if both directories are in the same particion (editing file in dirA will modified file in dirB).

[ Parent | Reply to this comment ]

Posted by Utumno (118.168.xx.xx) on Fri 31 Oct 2008 at 17:01
[ 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 ]

Posted by simonw (84.45.xx.xx) on Fri 31 Oct 2008 at 21:05
[ Send Message | View Weblogs ]
No he means the file should be a hard link, so it doesn't take twice the disk space, and it can be done more efficiently (no need to write the data twice).

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 ]

Posted by Utumno (118.168.xx.xx) on Sat 1 Nov 2008 at 06:35
[ 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 ]

Posted by Anonymous (216.244.xx.xx) on Mon 3 Nov 2008 at 18:07
<cuote>
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 ]

 

 

Flattr