Unmount WSD2L840 after sync, but only if this run mounted it

Previously the pull script mounted the Unassigned Device and left it
mounted indefinitely. Now tracks whether it was already mounted before
this run and only unmounts at the end if it wasn't - never disturbs a
mount already in place for some other reason. Handled via an EXIT trap
so it fires on any exit path, not just success.

Found and fixed a real bug testing this: the script cd's into the
mounted directory and never leaves, so the unmount was failing with
"target is busy" (the shell's own cwd still being on the device) until
the cleanup function explicitly cd's out first. Verified both
directions live: already-mounted stays mounted after; not-mounted gets
mounted, used, and cleanly unmounted again.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-26 17:33:58 +02:00
co-authored by Claude Sonnet 5
parent e9b51dbffe
commit 5b286644db
3 changed files with 33 additions and 5 deletions
@@ -16,6 +16,9 @@
# guaranteed to already be mounted - this script mounts it itself first
# and refuses to proceed if that didn't actually result in a real
# mountpoint, rather than risk silently writing backups onto the array.
# Unmounts it again once done, but only if this run is the one that
# mounted it - never unmounts a disk something else already had mounted
# for its own reasons.
set -u
KEY=/root/.ssh/gitea-backup/id_ed25519
@@ -38,6 +41,9 @@ if [ -z "$UD_DEV" ]; then
exit 1
fi
UD_ALREADY_MOUNTED=false
mountpoint -q "$UD_MOUNTPOINT" && UD_ALREADY_MOUNTED=true
/usr/local/sbin/rc.unassigned mount "$UD_DEV" >/dev/null 2>&1
if ! mountpoint -q "$UD_MOUNTPOINT"; then
@@ -45,6 +51,20 @@ if ! mountpoint -q "$UD_MOUNTPOINT"; then
exit 1
fi
GET_BATCH=$(mktemp)
DEL_BATCH=$(mktemp)
cleanup() {
rm -f "$GET_BATCH" "$DEL_BATCH"
if [ "$UD_ALREADY_MOUNTED" = false ]; then
# cd out first - if the shell's own cwd is still on the device
# (it is, after the "cd $LOCAL_DIR" below), umount fails with
# "target is busy"
cd / || true
/usr/local/sbin/rc.unassigned umount "$UD_DEV" >/dev/null 2>&1
fi
}
trap cleanup EXIT
mkdir -p "$LOCAL_DIR"
SFTP="sftp -P $REMOTE_PORT -i $KEY -o UserKnownHostsFile=$KNOWN_HOSTS -o BatchMode=yes"
@@ -56,10 +76,6 @@ if [ -z "$REMOTE_LISTING" ]; then
fi
REMOTE_ZIPS=$(echo "$REMOTE_LISTING" | grep '\.zip$')
GET_BATCH=$(mktemp)
DEL_BATCH=$(mktemp)
trap 'rm -f "$GET_BATCH" "$DEL_BATCH"' EXIT
TO_FETCH=0
while read -r fname; do
[ -z "$fname" ] && continue