Created
October 20, 2025 03:33
-
-
Save YuanLiou/ba8d6edaff829b89d0e94a39de8b63db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ============================================================================== | |
| # | |
| # Title: archive_to_gdrive.sh | |
| # Description: Compresses a specified folder, adds a timestamp to the filename, | |
| # and moves the resulting .zip file to a user-defined | |
| # Google Drive folder on macOS. | |
| # Author: Gemini and ray liu | |
| # | |
| # Usage: ./archive_to_gdrive.sh /path/to/your/folder | |
| # | |
| # ============================================================================== | |
| # --- Step 1: Validate Input --- | |
| # Check if a folder path was provided as an argument. | |
| if [ -z "$1" ]; then | |
| echo "β Error: No folder specified." | |
| echo "Usage: $0 /path/to/your/folder" | |
| exit 1 | |
| fi | |
| SOURCE_FOLDER="$1" | |
| # Check if the provided path is a valid directory. | |
| if [ ! -d "$SOURCE_FOLDER" ]; then | |
| echo "β Error: '$SOURCE_FOLDER' is not a valid directory." | |
| exit 1 | |
| fi | |
| # --- Step 2: Prepare Filenames and Paths --- | |
| # Remove any trailing slash from the folder path for consistency. | |
| SOURCE_FOLDER_CLEAN="${SOURCE_FOLDER%/}" | |
| # Extract just the folder name from the path. | |
| FOLDER_BASENAME=$(basename "$SOURCE_FOLDER_CLEAN") | |
| # Get the current date and time in YYYYMMDD-HHMMSS format. | |
| TIMESTAMP=$(date +"%Y%m%d-%H%M%S") | |
| # Construct the final filename for the zip archive. | |
| ZIP_FILENAME="${FOLDER_BASENAME}_${TIMESTAMP}.zip" | |
| echo "π Source folder: $SOURCE_FOLDER_CLEAN" | |
| echo "π¦ Archive name will be: $ZIP_FILENAME" | |
| echo "" | |
| # --- Step 3: Get Google Drive Folder Location from User --- | |
| # Set a default path for Google Drive. | |
| DEFAULT_GDRIVE_PATH="$HOME/Google Drive/ζηι²η«―瑬η’" | |
| GDRIVE_PATH="" | |
| # Check if the default path exists and prompt the user accordingly. | |
| if [ -d "$DEFAULT_GDRIVE_PATH" ]; then | |
| echo "β Default Google Drive folder found at: '$DEFAULT_GDRIVE_PATH'" | |
| read -p "Press ENTER to use this path, or specify a new one: " USER_GDRIVE_PATH | |
| if [ -z "$USER_GDRIVE_PATH" ]; then | |
| GDRIVE_PATH="$DEFAULT_GDRIVE_PATH" | |
| else | |
| GDRIVE_PATH="$USER_GDRIVE_PATH" | |
| fi | |
| else | |
| echo "Please specify your Google Drive folder." | |
| # Provide a helpful tip for macOS users to avoid typing long paths. | |
| echo "π‘ Tip: Drag and drop your 'My Drive' folder from Finder into this window and press Enter." | |
| read -p "Enter path to your Google Drive folder: " GDRIVE_PATH | |
| fi | |
| # Clean up the path in case it gets wrapped in quotes from drag-and-drop. | |
| GDRIVE_PATH="${GDRIVE_PATH//\'/}" | |
| GDRIVE_PATH="${GDRIVE_PATH//\"/}" | |
| # --- Step 4: Validate Google Drive Path --- | |
| if [ -z "$GDRIVE_PATH" ]; then | |
| echo "β Error: No Google Drive path was entered. Aborting." | |
| exit 1 | |
| fi | |
| if [ ! -d "$GDRIVE_PATH" ]; then | |
| echo "β Error: The specified Google Drive path is not a valid directory." | |
| echo "You entered: '$GDRIVE_PATH'" | |
| exit 1 | |
| fi | |
| echo "β Google Drive folder verified: $GDRIVE_PATH" | |
| echo "" | |
| # --- Step 5: Compress and Move --- | |
| echo "βοΈ Compressing '$FOLDER_BASENAME'..." | |
| # To handle permissions robustly, we create the zip file locally first, | |
| # in the parent directory of the source folder. This ensures a clean zip structure. | |
| SOURCE_PARENT_DIR=$(dirname "$SOURCE_FOLDER_CLEAN") | |
| ZIP_TEMP_PATH="${SOURCE_PARENT_DIR}/${ZIP_FILENAME}" | |
| # We run zip in a subshell `(...)` to avoid changing the script's working directory. | |
| # We 'cd' into the parent directory and then zip the target folder by its basename. | |
| (cd "$SOURCE_PARENT_DIR" && zip -r -q "$ZIP_FILENAME" "$(basename "$SOURCE_FOLDER_CLEAN")") | |
| # Check the exit code of the zip command to see if it succeeded. | |
| if [ $? -ne 0 ]; then | |
| echo "β Error: Compression failed. The script could not read the source folder or write the temporary archive." | |
| exit 1 | |
| fi | |
| echo "β Compression successful!" | |
| echo "π Moving '$ZIP_FILENAME' to Google Drive..." | |
| # Now, move the created zip file to its final destination. | |
| mv "$ZIP_TEMP_PATH" "$GDRIVE_PATH/" | |
| # Check the exit code of the move command. | |
| if [ $? -ne 0 ]; then | |
| echo "β Error: Failed to move the file to Google Drive. This is likely a permissions issue." | |
| echo "π‘ Please double-check that Terminal has 'Full Disk Access' in System Settings > Privacy & Security." | |
| # Clean up the temporarily created zip file on failure. | |
| rm "$ZIP_TEMP_PATH" | |
| exit 1 | |
| fi | |
| echo "β¨ All done! Your archive has been successfully moved to Google Drive." | |
| echo "π Final location: $GDRIVE_PATH/$ZIP_FILENAME" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment