/ bin / gitfield-sync-gdrive.sh
gitfield-sync-gdrive.sh
 1  #!/bin/bash
 2  
 3  # ──────────────────────────────────────────────────────────────
 4  # ⚙️  GitField GDrive Sync Script
 5  #    Ensures Google Drive is mounted at ~/gdrive and syncs 
 6  #    the current Git repo into ~/gdrive/gitfield/<repo_name>
 7  # ──────────────────────────────────────────────────────────────
 8  
 9  set -e
10  
11  # ⛓ Ensure rsync is installed
12  if ! command -v rsync &> /dev/null; then
13      echo "rsync not found. Attempting to install..."
14      sudo apt update && sudo apt install -y rsync
15  fi
16  
17  # ⛓ Ensure ~/gdrive exists and is mounted
18  GDRIVE_PATH="$HOME/gdrive"
19  GITFIELD_PATH="$GDRIVE_PATH/gitfield"
20  
21  if [ ! -d "$GDRIVE_PATH" ]; then
22      echo "Google Drive folder not found at $GDRIVE_PATH."
23      echo "Create it or mount your gdrive before syncing."
24      exit 1
25  fi
26  
27  mkdir -p "$GITFIELD_PATH"
28  
29  # ⛓ Ensure current directory is inside a Git repo
30  if ! git rev-parse --is-inside-work-tree &> /dev/null; then
31      echo "Not inside a Git repository. Aborting sync."
32      exit 1
33  fi
34  
35  # 🏷 Determine repo name and paths
36  REPO_ROOT=$(git rev-parse --show-toplevel)
37  REPO_NAME=$(basename "$REPO_ROOT")
38  DEST="$GITFIELD_PATH/$REPO_NAME"
39  
40  # ♻️ Perform rsync (mirror entire repo, preserve structure, show progress)
41  echo "Syncing '$REPO_NAME' to $DEST..."
42  rsync -av --delete "$REPO_ROOT/" "$DEST/"
43  
44  echo "✅ GitField sync complete: $REPO_NAME ➝ $DEST"