Espressomatic Posted September 16, 2024 Posted September 16, 2024 (edited) Keep your UD shares up all the time. The script will mount any unmounted shares that have been set to "Automount" by Unassigned Devices. Add it to User Scripts and set a custom schedule to run it every n minutes. Remote shares set up in Unassigned Devices as Automount are only brought online when the system is first started. If the shares become unmounted for any reason, such as rebooting their server(s), they remain unavailble, potentially causing problems for apps/containers where they're referenced, such as Plex/Jellyfin, etc. This script checks which shares are currently unmounted, verifies the server is available, then uses Unassigned Devices own mount function to reactivate them. EDITED: version 1.1 (Sep 19 2024) #!/bin/bash #================================================================================================ ## ## Unassigned Devices Share Re-Mounter - Espressomatic @UnraidForums - 15 Sep 2024 ## - inspired by script from Marc Gutt @UnraidForums - 20 Nov 2020 ## version 1.1 ## ## Keep Unassigned Devices' "Automount" remote shares mounted ## ##================================================================================================ # # Info # ---- # # Tested on Unraid versions 6.12 and 7.0 betas # # Remote shares set up in Unassigned Devices as Automount are only brought online when the system # is first started. If the shares become unmounted for any reason, such as rebooting their # server(s), they remain unavailble, potentially causing problems for apps/containers where # they're referenced, such as Plex/Jellyfin, etc. # # This script checks which shares are currently unmounted, verifies the server is available, # then uses Unassigned Devices own mount function to reactivate them. # # When scheduled as instructed below, the script will run every 5 minutues. # # # Installation # ------------ # # - Install "User Scripts" plugin on each Unraid Server that will run this script # # - Create a new script named "Re-Mount Unassigned Devices Remote Shares" # # - Click the gear icon next to the script, click Edit Description, select all the text and replace with # "Mount unmounted shares if they're set to Automount" - click the green check-mark icon # # - Click the gear icon, click Edit Script and Copy and Paste this script # into the editor - Click the Save Changes button # # - There is no need to edit nor make changes to the script # # - Click the Run Script button to test - shares are checked and mounted as needed # # - Click the Schedule button for the script and select a custom schedule # # - Paste "*/5 * * * *" into the text box to the right, excluding quotes # # - Click the Apply button at the bottom # ##================================================================================================ ##================================================================================================ ##------------------------------------------------------------------------------------------------ ## The Business - you shouldn't need to make edits below for typical use ##------------------------------------------------------------------------------------------------ ##================================================================================================ ## Make script race condition safe # if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT; ## Read Unassigned Devices mount list for remote shares (/tmp/unassigned.devices/config/samba_mount.cfg) # printf "\n\nStarting...\n\n" cat "/tmp/unassigned.devices/config/samba_mount.cfg" | while read line do # Process a server entry when found ( lines starting with "[" ) # if [[ "$line" == [* ]];then check_server=$line read line # move to the next line - share protocol # Get the share type - SMB or NFS (needed later for differences in mounting) # if [[ $line == "protocol"* ]]; then share_type=$(cut -d'"' -f\2 <<<"$line") if [[ $share_type != "SMB" ]] && [[ $share_type != "NFS" ]]; then printf "Not NFS or SMB ... skipping\n\n" continue fi fi # Get the FQDN/ip of the server to later check if it's online # server_ip="" read line if [[ $line == "ip"* ]]; then server_ip=$(cut -d'"' -f\2 <<<"$line") else printf "The server IP/FQDN wasn't read where expected ... skipping\n\n" continue fi # Check Unassigned Devices Automount setting for each share # while read line do if [[ $line == "automount"* ]]; then break fi done # If this is an Automount share, check its type and whether it needs to be re-mounted # if [[ $line == "automount"*"yes"* ]]; then if [[ $share_type == "SMB" ]]; then #--------------------------------------------------------------------------------------------- # SMB | Mount the share if it's not currently mounted & the server is online # # [//SERVER/sharename] server_name=${check_server##*//} server_name=${server_name%/*} share_name=${check_server##*/} share_name=${share_name%%]*} mount_name="${server_name}_${share_name}" if mount -t cifs | grep -q "$mount_name"; then printf "SMB share $mount_name is already mounted\n\n" check_server="" else ping -c 1 -W 1 $server_ip > /dev/null 2>&1 if [[ $? -eq 0 ]]; then printf "Mounting SMB share //${server_name}/${share_name} as ${mount_name} ... " /usr/bin/nice /var/local/overlay/usr/local/sbin/rc.unassigned mount "//${server_name}/${share_name}" printf "\n" fi fi #--------------------------------------------------------------------------------------------- else #--------------------------------------------------------------------------------------------- # NFS | Mount the share if it's not currently mounted & the server is online # # [SERVER:/mnt/some_pool/sharename] server_name=${check_server##*[} server_name=${server_name%%:*} share_path=${check_server#*:} share_path=${share_path%]*} share_name=${check_server##*/} share_name=${share_name%%]*} mount_name="${server_name}_${share_name}" if mount -t nfs4 | grep -q "$mount_name"; then printf "NFS share $mount_name is already mounted\n\n" check_server="" else ping -c 1 -W 1 $server_ip > /dev/null 2>&1 if [[ $? -eq 0 ]]; then printf "Mounting NFS share ${server_name}:${share_path} as ${mount_name} ... " /usr/bin/nice /var/local/overlay/usr/local/sbin/rc.unassigned mount "${server_name}:${share_path}" printf "\n" fi fi #--------------------------------------------------------------------------------------------- fi fi fi done printf "\n...Finished\n\n" ##================================================================================================ Edited September 19, 2024 by Espressomatic 1 Quote
Espressomatic Posted September 19, 2024 Author Posted September 19, 2024 (edited) Updated to version 1.1 - now using "ip" (FQDN) of the server instead of derived "short name" to check if it's online - in case MDNS doesn't resolve the server name without domain.tld Edited September 19, 2024 by Espressomatic Quote
YrDaDy Posted September 26, 2024 Posted September 26, 2024 Almost perfect timing, been looking for a way to do this for a bit now since my backup machine is on a power schedule. Thanks a bunch! Quote
flybrys Posted January 14 Posted January 14 This is perfect timing for me also. Just found my off-site backup wasn't working for weeks as the remote server network went down for a few hours and it never remounted. I notice it attempts to mount regardless if the share is already mounted or not. Would it be possible to do some checks to see if it is already mounted before sending the mount command again? The reason for this is my syslog now has Remote Share xxx is already mounted every 5 minutes Thanks again for your work! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.