Dump UUID and LABEL Info For Mounted Partitions

While a good backup plan is a must, one thing I just caught myself doing is running those backup without storing the filesystem label or UUID. Normally, this really doesn’t matter as the minor convenience of not having to change /etc/fstab wouldn’t be worth the hassle.

However, in the case of system partitions, this can save a lot of time. With the increasing preference of using UUIDs or labels in boot loader configurations (especially GRUB2), restoring the UUID and/or label to their original value means you can instantly boot into a restored system once recovery is complete.

Of course, simply putting a couple of files into the root of each drive by hand wasn’t good enough for me. I had to go and automate it. In order to avoid writing to devices that might cause a catastrophe (like the individual devices in software RAID), I queried and parsed the output from the df command. I then used /proc/mounts to find out where the device was mounted (since the output of df is dynamically spaced).

for dev in `df -h | grep ^/dev | cut -d\  -f1 | cut -d/ -f3`; do
  loc=`cat /proc/mounts | grep $dev | cut -d\  -f2`
  lsblk -fn -o UUID /dev/$dev > $loc/.uuid
  lsblk -fn -o LABEL /dev/$dev > $loc/.label