Emy/bin/mount_google.sh
2026-05-21 11:51:56 +02:00

68 lines
2.2 KiB
Bash
Executable file

#!/bin/bash
# Mount Google Image at startup and sync existing files
IMAGE_PATH="/Volumes/nvme/Google.asif"
MOUNT_POINT="/Users/jessi/Library/Application Support/Google"
TEMP_MOUNT="/tmp/GoogleTempMount"
# Wait up to 60 seconds for the image file to become available (e.g. nvme mounting)
MAX_WAIT=60
WAIT_TIME=0
while [ ! -f "$IMAGE_PATH" ]; do
sleep 1
WAIT_TIME=$((WAIT_TIME + 1))
if [ "$WAIT_TIME" -ge "$MAX_WAIT" ]; then
echo "Image $IMAGE_PATH not found after $MAX_WAIT seconds. Exiting."
exit 1
fi
done
# Check if already mounted
if mount | grep -q "$MOUNT_POINT"; then
echo "Image is already mounted at $MOUNT_POINT"
exit 0
fi
# Ensure MOUNT_POINT exists
mkdir -p "$MOUNT_POINT"
# If the directory is not empty (e.g., Chrome started before image was mounted)
if [ "$(ls -A "$MOUNT_POINT" 2>/dev/null)" ]; then
echo "Found existing files in $MOUNT_POINT. Syncing to image..."
# Create temp mount point
mkdir -p "$TEMP_MOUNT"
# Mount image temporarily without making it visible in Finder (-nobrowse)
if diskutil image attach "$IMAGE_PATH" --mountPoint "$TEMP_MOUNT" --nobrowse >/dev/null 2>&1; then
echo "Mounted temporarily to $TEMP_MOUNT"
# Sync files from local folder to image
# -a: archive mode, -u: update (skip files that are newer on the receiver)
rsync -au "$MOUNT_POINT/" "$TEMP_MOUNT/"
# Unmount temp image
diskutil eject "$TEMP_MOUNT" >/dev/null 2>&1
echo "Detached temp image."
# Move the existing folder out of the way to avoid hiding files
# and taking up space on the system drive.
STALE_DIR="${MOUNT_POINT}_stale_$(date +%s)"
mv "$MOUNT_POINT" "$STALE_DIR"
mkdir -p "$MOUNT_POINT"
echo "Moved old local folder to $STALE_DIR"
else
echo "Failed to mount image temporarily. Proceeding to mount over existing files..."
fi
fi
# Mount the image permanently
echo "Mounting image to $MOUNT_POINT"
diskutil image attach "$IMAGE_PATH" --mountPoint "$MOUNT_POINT" --nobrowse
if [ $? -eq 0 ]; then
echo "Successfully mounted."
else
echo "Failed to mount."
exit 1
fi