Skip to content

Instantly share code, notes, and snippets.

@jimmygle
Last active April 28, 2021 20:08
Show Gist options
  • Save jimmygle/641a57bdde5401101f49b552ed78388d to your computer and use it in GitHub Desktop.
Save jimmygle/641a57bdde5401101f49b552ed78388d to your computer and use it in GitHub Desktop.
Bash script for managing the mount state of external drives.
#!/bin/bash
# Helps mount/unmount disks
#
# Author: Jimmy Gleason <github.com/jimmygle
#
# The goal of this script is to provide a helper for faciliating the un/mounting
# of drives. It's useful for mounting external drives for doing automated backups,
# or when frequently transferring data via external drives to/from a NAS.
#
# NOTE: Only tested on Debian based distros.
#
# TODO: External config file that can be managed by script (for dynamic adding of new drives)
#
# Recognizeable drives/partitions
# Get the value of each array element from `ls -la /dev/disk/by-id/` when drive is attached.
declare -A drives
drives[musty]="usb-WD..."
drives[krusty]="usb-ST..."
drives[rusty]="usb-Seag..."
drives[speedy]="ata-San..."
# Find a drive by its key
get_drive_id_by_key() {
if [[ -v "drives[$1]" ]]; then
printf "${drives[$1]}"
fi
}
# Gets mount status of drive key
mount_status() {
local drive_id=`get_drive_id_by_key $1`
if mount | grep "/media/$1" > /dev/null; then
printf "mounted"
elif ls /dev/disk/by-id/ | grep "$drive_id" > /dev/null; then
printf "not mounted, but attached"
else
printf "not attached"
fi
}
# Echoes the configured drive list
print_drive_list() {
printf "Configured Drives/Partitions:\n"
for key in "${!drives[@]}"; do
local status=`mount_status $key`
printf " - key: $key\n"
printf " id: ${drives[$key]}\n"
printf " status: $status\n"
done
}
# Mounts an exfat drive/partition by its ID
mount_exfat_by_id() {
/usr/bin/mount -t exfat /dev/disk/by-id/$1 $2
}
# Umounts an exfat drive/partition by its ID
unmount_exfat_by_id() {
/usr/bin/umount /dev/disk/by-id/$1
}
# Runs mounting operations
mount_runner() {
local drive_id=`get_drive_id_by_key $1`
local mount_path="/media/$1"
local mount_state=`mount_status $1`
if [ -z "$drive_id" ]; then
printf "\nERROR: Drive with key '$1' not found.\n\n"
print_drive_list
exit 1
fi
case "$mount_state" in
"mounted")
printf "Drive '$1' is already mounted (probably at $mount_path)\n"
;;
"not attached")
printf "Drive '$1' is not plugged in (ID $drive_id)\n"
;;
"not mounted, but attached")
printf "Attempting to mount $1 to $mount_path\n"
mkdir -p $mount_path
mount_exfat_by_id $drive_id $mount_path
;;
*)
printf "\nERROR: Unable to determine status for '$1' (ID $drive_id)\n\n"
exit 1
;;
esac
}
# Runs unmounting operations
unmount_runner() {
local drive_id=`get_drive_id_by_key $1`
local mount_path="/media/$1"
local mount_state=`mount_status $1`
if [ -z "$drive_id" ]; then
printf "\nERROR: Drive with key '$1' not found\n\n"
print_drive_list
exit 1
fi
case "$mount_state" in
"mounted")
printf "Attempting to unmount $mount_path\n"
unmount_exfat_by_id $drive_id
;;
"not attached")
printf "Drive '$1' is not plugged in (ID '$drive_id')\n"
;;
"not mounted, but attached")
printf "Drive '$1' is not mounted\n"
;;
*)
printf "\nERROR: Unable to determine status for '$1' (ID $drive_id)\n\n"
exit 1
;;
esac
}
# Runner - Handles drive selection/input
case "$1" in
# Mount
"m")
mount_runner $2
;;
# Mount all
"ma")
printf "Attempting to mount all drives\n"
for key in "${!drives[@]}"; do
mount_runner $key
done
;;
# Unmount
"u")
unmount_runner $2
;;
# Unmount all
"ua")
printf "Attempting to unmount all drives\n"
for key in "${!drives[@]}"; do
unmount_runner $key
done
;;
*)
printf "\njimmys's drive mounting helper\n"
printf "\nPurpose: Help faciliate the quick mounting/unmounting of commonly used media drives.\n\n"
printf "Usage:\n"
printf " jt-mounter m <drive_key> Mounts drive of given key\n"
printf " jt-mounter ma Mounts all unmounted and plugged in drives\n"
printf " jt-mounter u <drive_key> Unmounts drive of given key\n"
printf " jt-mounter ua Unmounts all drives that are currently mounted\n\n"
printf "Example:\n"
printf " jt-mounter m krusty Mounts drive /dev/disk/by-id/usb-ST1000... to /media/krusty\n\n"
print_drive_list
printf "\n"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment