Open
Description
Utility that allows for models to be copied to an external drive and then auto-symlinked for seamless backup / usage when the drive is present. Utility is run from the folder of the model submitter and takes the MODEL NAME as an argument. Paths are hardcoded, I use it with an alias but if there was a button that just ran the script and some boxes for variable names that would be TITS.
#!/usr/bin/env bash
# storelm.sh - Copy a local LM Studio model directory to an external drive,
# erase the local directory's contents, and symlink all files back from the external drive.
set -euo pipefail
# --- Configuration ---
LOCAL_LM_STUDIO_MODELS_BASE="/Users/USERNAME/.cache/lm-studio/models"
EXTERNAL_DRIVE_MODELS_BASE="/Volumes/EXTDRIVE/LM-Studio"
# --- Argument Check ---
if [ -z "${1-}" ]; then
echo "Usage: $(basename "$0") <directory_name>"
echo "Run this from within a creator's directory, e.g., /Users/USERNAME/.cache/lm-studio/models/SomeCreatorName/"
exit 1
fi
LOCAL_MODEL_DIR_NAME="$1"
CURRENT_WORKING_DIR="$PWD"
LOCAL_MODEL_FULL_PATH="${CURRENT_WORKING_DIR}/${LOCAL_MODEL_DIR_NAME}"
# --- Sanity Checks ---
if [[ "$CURRENT_WORKING_DIR" != "$LOCAL_LM_STUDIO_MODELS_BASE/"* ]] || [[ "$CURRENT_WORKING_DIR" == "$LOCAL_LM_STUDIO_MODELS_BASE" ]]; then
echo "Error: Run from within a creator's directory under '$LOCAL_LM_STUDIO_MODELS_BASE/'."
exit 1
fi
if [ ! -d "$LOCAL_MODEL_FULL_PATH" ]; then
echo "Error: Local model directory '$LOCAL_MODEL_FULL_PATH' not found."
exit 1
fi
CREATOR_NAME=$(basename "$CURRENT_WORKING_DIR")
EXTDRIVE_CREATOR_DIR="${EXTERNAL_DRIVE_MODELS_BASE}/${CREATOR_NAME}"
EXTDRIVE_MODEL_FULL_PATH="${EXTDRIVE_CREATOR_DIR}/${LOCAL_MODEL_DIR_NAME}"
echo "--------------------------------------------------"
echo "🚀 Backing up model:"
echo " Local Model: $LOCAL_MODEL_FULL_PATH"
echo " Creator: $CREATOR_NAME"
echo " Target EXTDRIVE Path: $EXTDRIVE_MODEL_FULL_PATH"
echo "--------------------------------------------------"
# --- Prepare EXTDRIVE Destination ---
if [ ! -d "$EXTDRIVE_CREATOR_DIR" ]; then
echo "🛠️ Creating creator directory '$EXTDRIVE_CREATOR_DIR' on EXTDRIVE drive..."
mkdir -p "$EXTDRIVE_CREATOR_DIR"
fi
if [ -e "$EXTDRIVE_MODEL_FULL_PATH" ]; then
read -rp "⚠️ '$EXTDRIVE_MODEL_FULL_PATH' already exists on EXTDRIVE. Overwrite? (y/n): " answer
if ! [[ "$answer" =~ ^[Yy]$ ]]; then
echo "↪️ Skipping copy. No changes made."
exit 0
fi
echo "🗑️ Removing existing target..."
rm -rf "$EXTDRIVE_MODEL_FULL_PATH"
fi
# --- Copy Operation ---
echo "📦 Copying '$LOCAL_MODEL_DIR_NAME' to EXTDRIVE..."
cp -Rp "$LOCAL_MODEL_FULL_PATH" "$EXTDRIVE_MODEL_FULL_PATH"
echo "✅ Copy complete."
# --- Ask to Symlink Files Back ---
read -rp "❓ Replace all files in '$LOCAL_MODEL_DIR_NAME' with symlinks to EXTDRIVE? (y/n): " confirm_replace
if [[ "$confirm_replace" =~ ^[Yy]$ ]]; then
# Extra safety: make sure we're not about to delete something dangerous
if [[ "$LOCAL_MODEL_FULL_PATH" == "$LOCAL_LM_STUDIO_MODELS_BASE"* ]] && [[ "$LOCAL_MODEL_FULL_PATH" != "$LOCAL_LM_STUDIO_MODELS_BASE" ]]; then
echo "🗑️ Removing contents of local model directory: $LOCAL_MODEL_FULL_PATH"
find "$LOCAL_MODEL_FULL_PATH" -mindepth 1 -exec rm -rf {} +
else
echo "❌ Refusing to delete: path sanity check failed."
exit 1
fi
echo "🔗 Creating symlinks for each file from EXTDRIVE to local directory..."
(
cd "$EXTDRIVE_MODEL_FULL_PATH"
find . -type f -print0 | while IFS= read -r -d '' relpath; do
srcfile="$EXTDRIVE_MODEL_FULL_PATH/${relpath#./}"
targetfile="$LOCAL_MODEL_FULL_PATH/${relpath#./}"
mkdir -p "$(dirname "$targetfile")"
[ -e "$targetfile" ] && rm -f "$targetfile"
ln -s "$srcfile" "$targetfile"
echo " Symlinked: $targetfile"
done
)
echo "✅ All files symlinked. '$LOCAL_MODEL_DIR_NAME' is now a directory of symlinks to EXTDRIVE."
else
echo "Local directory retained. Backup is at '$EXTDRIVE_MODEL_FULL_PATH'."
fi
echo "--------------------------------------------------"
echo "🎉 Operation finished."
exit 0
Metadata
Metadata
Assignees
Labels
No labels