4

Is it possible on Linux (Ubuntu in my case) to configure the system to run a bash script just before a USB key is unmounted?

I have a portable personal wiki on a pendrive, and every time I unmount it from my machine I would like its contents to be automatically backed up to Amazon S3.

Is anything like this possible? It doesn't matter if the script runs for every single unmounting as I can easily identify in the script whether the device being unmounted is the one I want to back up.

Thanks in advance for any advice.

Rich

3
  • Of course, the script won't be able to run (or at least won't be able to access the contents of the USB drive) if the drive is forcibly pulled out without being explicitly unmounted. You're not worried about that case, are you? – David Z Sep 7 '10 at 17:34
  • Which desktop are you using? Gnome? KDE? Other? How are you currently unmounting it? By clicking something like a popup menu, an icon, or command line? It's certainly possible. – Matt Sep 7 '10 at 18:31
  • I'm using Gnome and I always unmount by clicking the unmount button in Nautilus – Rich Sep 7 '10 at 19:22
2

I don't think there's a generic way of running a script before a device is unmounted.

You could run the backup script after the device is unmounted, but it would be very fiddly — you'd have to remount the device, run the backup, unmount the device again (without retriggering your script), and finally arrange to notify the user that the device can be unplugged.

To run a script before the unmount, call your own unmount script instead of /bin/umount. If you want to handle GUIs that call /bin/umount whether you want it or not, you can divert the umount executable and put your script in its place:

dpkg-divert --local --rename --divert /bin/umount.real /bin/umount
1

You can probably use udev but you might want the process that you trigger to make the copy locally and exit then have a separate incron process do the upload to Amazon S3 since you shouldn't have long running processes triggered by udev.

0

Take a look at inotify. It should be possible to catch an unmount event with handle that with a custom script.

0

The best way I can think about it is that you make a script that makes the backup first, and then unmounts the drive. Just use it every time instead of pure unmounting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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