Created
August 1, 2025 15:09
-
-
Save moio/55b1d0e1012cdb5fc04fc158864c71ac to your computer and use it in GitHub Desktop.
Downloads the Vai SQLite DB from a Rancher instance pointed by KUBECONFIG
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 | |
| # This script automates copies the Vai DBs from Rancher to local machine. | |
| # NOTE THIS IS NOT SECURE FOR A PRODUCTION ENVIRONMENT, use for development/test only | |
| # Exit immediately if a command exits with a non-zero status. | |
| set -e | |
| # --- Configuration --- | |
| # Which app to profile? Supported choices: rancher, cattle-cluster-agent | |
| APP=rancher | |
| # --- Constants --- | |
| DB_PATH_IN_CONTAINER="/var/lib/rancher/informer_object_cache.db" | |
| DB_NAME=$(basename "$DB_PATH_IN_CONTAINER") | |
| VACUUMED_DB_PATH_IN_CONTAINER="/tmp/vacuumed_$DB_NAME" | |
| SQLITE_TOOLS_URL="https://www.sqlite.org/2025/sqlite-tools-linux-x64-3500400.zip" | |
| case $APP in | |
| rancher) | |
| CONTAINER=rancher | |
| NAMESPACE=cattle-system | |
| ;; | |
| cattle-cluster-agent) | |
| CONTAINER=cluster-register | |
| NAMESPACE=cattle-system | |
| ;; | |
| esac | |
| for pod in $(kubectl -n $NAMESPACE get pods -l app=${APP} --no-headers -o custom-columns=name:.metadata.name); do | |
| echo "Processing pod: $pod" | |
| # A multi-line command to be executed inside the container. | |
| # It downloads sqlite3, runs VACUUM INTO, and reports status. | |
| # Using a heredoc makes it clean and readable. | |
| REMOTE_COMMAND=$(cat <<EOF | |
| set -e | |
| echo "[Pod] Downloading SQLite tools from $SQLITE_TOOLS_URL..." | |
| curl -k -L -o "/tmp/sqlite-tools.zip" "$SQLITE_TOOLS_URL" | |
| echo "[Pod] Unpacking SQLite tools..." | |
| mkdir -p /tmp/sqlite-tools | |
| unzip -o "/tmp/sqlite-tools.zip" -d /tmp/sqlite-tools | |
| echo "[Pod] Running VACUUM on '$DB_PATH_IN_CONTAINER' into '$VACUUMED_DB_PATH_IN_CONTAINER'..." | |
| rm -f "$VACUUMED_DB_PATH_IN_CONTAINER" | |
| /tmp/sqlite-tools/sqlite3 "$DB_PATH_IN_CONTAINER" "VACUUM INTO '$VACUUMED_DB_PATH_IN_CONTAINER';" | |
| echo "[Pod] Cleaning up downloaded tools..." | |
| rm -rf /tmp/sqlite-tools | |
| EOF | |
| ) | |
| # === Step 1: Execute the remote script inside the pod === | |
| echo "➡️ Step 1: Executing remote script in pod..." | |
| kubectl exec -n "$NAMESPACE" "$pod" -c $CONTAINER -- /bin/sh -c "$REMOTE_COMMAND" | |
| # === Step 2: Copy the resulting file from the pod to the local machine === | |
| echo "➡️ Step 2: Copying file from pod to '$pod-$DB_NAME'..." | |
| kubectl cp "$NAMESPACE/$pod:$VACUUMED_DB_PATH_IN_CONTAINER" "$pod-$DB_NAME" -c $CONTAINER --retries=10 | |
| # === Step 3: Clean up the vacuumed file from the container's /tmp directory === | |
| echo "➡️ Step 3: Cleaning up the temporary file from the pod..." | |
| kubectl exec -n "$NAMESPACE" "$pod" -c $CONTAINER -- rm -f "$VACUUMED_DB_PATH_IN_CONTAINER" | |
| done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment