Skip to main content
1 of 4

Run backup script after systemd automounts specific USB device

I need to run a script after a specific USB stick is mounted, particulary, to perform a backup.

I've seen many (and old) posts that mount devices inside udev rules, however modern OSes already have systemd preconfigured to automount flash drives.

I have this udev rule,

ACTION=="add", ATTRS{idVendor}=="2367", ATTRS{idProduct}=="000c", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", RUN+="/home/me/scripts/backup-x.sh" 

as that's the only Linux kernel hook I know where I can specifically run something after device is connected.

However, as device gets automounted at ie. /media/me/0021-0000 right after it's connected, I guess shouldn't mount it before (and even perform a long operation, like a backup) inside a udev RUN script.

What's the simplest and correct approach to achieve it?

PD: running Pop!_OS 22.04LTS