udev auto mount disk by partition UUID

created by RTWM

If you have several disks that you will use in a regular rotation for offsite backup you can use udev rules to automatically mount these known disks into your Linux server that handles the backup jobs.

still needs work… but will successfully mount partition based on UUID small excerpt taken from /etc/udev/rules.d/50-udev.rules

this add line seems to break things, maybe it’s the position it’s in?
ACTION==“add”, GOTO=“offsite_plugin”

ACTION=="remove", GOTO="offsite-plugout"

SUBSYSTEM!="block", GOTO="offsite_plugin"
# tell the kernel to listen for events like disks being plugged in
###KERNEL=="*[!0-9]“, SYSFS{removable}=="0", GOTO="offsite_plugin"
# we are assuming that the offsite backup disk will be sdd or higher. (lots of room for improvement here)
# we know that the offsite backup disk will only have one partition, so we have 1 hard coded in here.
KERNEL=="sd[d-z]1", SYSFS{removable}=="0"

# use the vol_id or blkid command to create a temporary node id of the disk
ACTION=="add", IMPORT{program}="/lib/udev/vol_id –export $tempnode"
# we can probably use the blkid command here instead of vol_id command if we wanted. syntax to get UUID is different, but result is same.
#IMPORT{program}="/sbin/blkid -o value -s UUID $tempnode"

# This step is more or less extra / optional but I wanted to do it.
# create a symlink in /dev called offsite, and put the exact UUID of the file system in there, so we will get: /dev/offsite/xxxx-xxxx-xxxx-xxxx
# downside here is that **any** new disk connected will get a symlink created in /dev/offsite. See next section below.
ENV{ID_FS_UUID}=="?*", SYMLINK+="offsite/$env{ID_FS_UUID}"

# NOTE – the RUN statement below is where we specify if we want to mount any disk connected or just ones with specific UUID’s.

# if we want to mount any new disk connected by UUID just do this:
#RUN+="/bin/mount -o noatime,nodiratime /dev/offsite/$env{ID_FS_UUID} /offsite-backup/"

# if we want to just mount specific disks that we know about do this, with a new line and UUID for each disk:
# this is the method we are using because we know the UUID of the offsite backup disks and we only want those mounted at
# our special purpose mount point called /offsite-backup

Daily / Weekly / Monthly Offsite Disks

RUN+="/bin/mount -o noatime,nodiratime /dev/offsite/18251612-3a33-4873-b97b-5e31e010c55e /offsite-backup/"
RUN+="/bin/mount -o noatime,nodiratime /dev/offsite/b696f3ac-3457-4d41-b211-3d1d46baa83a /offsite-backup/"
RUN+="/bin/mount -o noatime,nodiratime /dev/offsite/xxxx2 /offsite-backup/"

June monthly offsite disk

RUN+="/bin/mount -o noatime,nodiratime /dev/offsite/c48a0646-5b3a-4cd0-abc9-c89380ffa776 /offsite-backup/"

RUN+="/bin/mount -o noatime,nodiratime /dev/offsite/xxxx4 /offsite-backup/"

test lines below


ACTION=="remove", ENV{DEVLINKS}=="/dev/offsite/*", RUN+="/bin/umount /offsite-backup"

#ACTION=="remove", PROGRAM="/bin/ls /dev/offsite", RESULT=="ls: /dev/offsite: No such file or directory", RUN+="/bin/umount /offsite-backup"
#ACTION=="remove", PROGRAM="/lib/udev/vol_id –export $tempnode", RUN+="/bin/umount /offsite-backup"
#ACTION=="remove", $env{ID_FS_UUID}="", RUN+="/bin/umount /offsite-backup"