View previous topic :: View next topic |
Author |
Message |
FlyingFoX n00b
Joined: 02 Dec 2008 Posts: 8
|
Posted: Tue Dec 02, 2008 1:50 am Post subject: Automatically update files upon mount |
|
|
I want to do the following:
My partition /dev/sdb3 is usually mounted to /usr/portage/distfiles. But as sdb is an external hard drive it is not always connected to my computer and therefore /usr/portage/distfiles is sometimes empty. When /usr/portage/distfiles is empty i want to be able to put files there and when i connect /dev/sdb3 to my computer again i want the files from /usr/portage/distfiles to be copied to /dev/sda3 then have /dev/sda3 mounted again.
Do you have any suggestions how I can accomplish this behaviour automatically without copying those files manually at every new mount of /dev/sda3?
I already thought about writing a script that syncs /usr/portage/distfiles before mounting /dev/sda3 again, but don't really know how to invoke this automatically before every remount of /dev/sda3.
I could as well mount /dev/sda3 at a totally different position (let's say /mnt/sda3) and run my script after the actual mount process. Doing a sync between /usr/portage/distfiles and /mnt/sda3, deleting the content of /usr/portage/distfiles and then making /usr/portage/distfiles a symlink to /mnt/sda3. But there again I don't know how to invoke my script automatically after /dev/sda3 was mounted.
I wonder if there is something like an "overlay" of the file system, that behaves as desired, but i haven't heard of anything like that yet and don't even know how to search for that.
Any ideas and hints are welcome. |
|
Back to top |
|
|
richard.scott Veteran
Joined: 19 May 2003 Posts: 1497 Location: Oxfordshire, UK
|
Posted: Wed Dec 03, 2008 4:18 pm Post subject: |
|
|
with udev you can get it to run a command i.e. a script each time a device is found.
you could use that to copy files over each time you plug the drive in? |
|
Back to top |
|
|
FlyingFoX n00b
Joined: 02 Dec 2008 Posts: 8
|
Posted: Thu Dec 04, 2008 12:19 am Post subject: |
|
|
udev looks like a good idea. I will read try if i can get it to work with udev. |
|
Back to top |
|
|
richard.scott Veteran
Joined: 19 May 2003 Posts: 1497 Location: Oxfordshire, UK
|
|
Back to top |
|
|
FlyingFoX n00b
Joined: 02 Dec 2008 Posts: 8
|
Posted: Sun Jan 11, 2009 12:25 pm Post subject: |
|
|
Thanks richard.scott! Udev nearly solved my problem, but unfortunately not completely.
After i spent some time on this topic and created an udev script that updates my files when plugging in the device I got another design problem here. When i (manually) unmount the partition the udev script is of course not triggered because the device is still plugged in, but just not mounted any more. So my files won't be updated when I unmount and then remount the partition some time later.
If I understood udev correctly this behaviour cannot be achieved with udev scripts.
So I am thinking of how else to do this.
I thought about moving "mount" and "umount" to some other place and replace them with a script that takes care of my special partition, but this is maybe not a so good idea. Does someone have another idea of how to trigger a script on every mount/umount of a partition or achieve my goal in another way? |
|
Back to top |
|
|
richard.scott Veteran
Joined: 19 May 2003 Posts: 1497 Location: Oxfordshire, UK
|
Posted: Mon Jan 12, 2009 10:43 am Post subject: |
|
|
You could run a cron script like this one:
Code: | #!/bin/bash
# MP = the mountpoint for your usb drive
MP="/mnt/usb"
# If .timestamp is there then USB drive must be mounted
if [ -f ${MP}/.timestamp ]; then
# Check if the .timestamp File's data was last modified more than 120 minutes ago
if [ $(find ${MP} -name .timestamp -mmin +120 | wc -l) -eq 1 ]; then
# ...
# Update some files
# ...
# Update the data in the .timestamp file
echo "$(date)" > ${MP}/.timestamp
fi
fi |
It checks for a .timestamp file to exist in the mounted directory of your usb device and would only run your update commands if 120min has passed since the last update.
You'll need to first mount your device and then create a .timestamp file in its root and then add this script to root's crontab to run each min. Obviously you can tweak the 120 setting in the find command to reduce how often a gap there is between updates?
Hope this helps.
Rich |
|
Back to top |
|
|
FlyingFoX n00b
Joined: 02 Dec 2008 Posts: 8
|
Posted: Tue Jan 13, 2009 5:19 pm Post subject: |
|
|
This seems a good solution for me. I'll try that out.
Thanks for the help! |
|
Back to top |
|
|
|