Skip to content

Instantly share code, notes, and snippets.

@wteuber
Last active January 24, 2026 00:29
Show Gist options
  • Select an option

  • Save wteuber/64ae630a100ceb55da84f9a747398fcc to your computer and use it in GitHub Desktop.

Select an option

Save wteuber/64ae630a100ceb55da84f9a747398fcc to your computer and use it in GitHub Desktop.
Backup Google AI Studio Chats as markdown file
#!/bin/bash
# This script backs up Google AI Studio chat conversations
# by converting them from JSON format to Markdown files.
# It requires 'jq' to be installed for JSON parsing.
# --- CONFIGURATION ---
# Set the Google AI Studio directory path where chat JSON files are stored.
# Set the backup directory path where Markdown files will be saved.
# Set environment variables, e.g. in your .zshrc:
# export GOOGLE_AI_STUDIO_DIR="$HOME/Library/CloudStorage/[email protected]/My Drive/Google AI Studio"
# export BACKUP_DIR="$HOME/path/to/backups/Google AI Studio Chats"
GOOGLE_AI_STUDIO_DIR=$(echo $GOOGLE_AI_STUDIO_DIR || "$HOME/Library/CloudStorage/[email protected]/My Drive/Google AI Studio")
BACKUP_DIR=$(echo $BACKUP_DIR || "$HOME/path/to/backups/Google AI Studio Chats")
# Check if an argument was provided, if not back up all chats
if [ -z "$1" ]; then
echo "Usage: $0 <Chat Title>"
echo "No chat title provided, backing up all chats..."
pushd "$GOOGLE_AI_STUDIO_DIR"
for file in *; do
CHAT_TITLE="$file"
CHAT_TITLES+=("$CHAT_TITLE")
done
popd
else
CHAT_TITLES=("$1")
fi
# Check if GOOGLE_AI_STUDIO_DIR exists
if [ ! -d "$GOOGLE_AI_STUDIO_DIR" ]; then
echo "Error: Google AI Studio directory not found at '$GOOGLE_AI_STUDIO_DIR'."
exit 1
fi
for CHAT_TITLE in "${CHAT_TITLES[@]}"
do
INPUT_FILE="${GOOGLE_AI_STUDIO_DIR}/${CHAT_TITLE}"
OUTPUT_FILENAME="${CHAT_TITLE}.md"
OUTPUT_PATH="${BACKUP_DIR}/${OUTPUT_FILENAME}"
# Check if Input File exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File '$INPUT_FILE' not found in '$GOOGLE_AI_STUDIO_DIR'."
exit 1
fi
# Create Backup Directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
echo "Creating directory: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
fi
# Ensure jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: 'jq' is not installed. Please install it to proceed."
exit 1
fi
# --- EXECUTION ---
echo "Processing '$INPUT_FILE'..."
# Run the jq command
jq -r '.chunkedPrompt.chunks[] | "### " + (.role | ascii_upcase) + "\n\n" + .text + "\n\n---"' "$INPUT_FILE" > "$OUTPUT_PATH"
# --- COMPLETION ---
if [ $? -eq 0 ]; then
echo "✅ Success! Backup saved to:"
echo "$OUTPUT_PATH"
else
echo "❌ Error: Failed to parse JSON."
rm "$OUTPUT_PATH"
fi
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment