Create data-sync.sh
Browse files- data-sync.sh +72 -0
data-sync.sh
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Check necessary environment variables
|
| 4 |
+
if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then
|
| 5 |
+
echo "Missing required environment variables G_NAME or G_TOKEN"
|
| 6 |
+
exit 1
|
| 7 |
+
fi
|
| 8 |
+
|
| 9 |
+
# Parse repository name and username
|
| 10 |
+
IFS='/' read -r GITHUB_USER GITHUB_REPO <<< "$G_NAME"
|
| 11 |
+
|
| 12 |
+
# Build GitHub repository clone URL with token
|
| 13 |
+
REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
|
| 14 |
+
mkdir -p ./data/github_data
|
| 15 |
+
|
| 16 |
+
# Clone repository
|
| 17 |
+
echo "Cloning repository..."
|
| 18 |
+
git clone "$REPO_URL" ./data/github_data || {
|
| 19 |
+
echo "Clone failed, please check if G_NAME and G_TOKEN are correct."
|
| 20 |
+
exit 1
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
SOURCE_DB="$HOME/.omniroute/storage.sqlite"
|
| 24 |
+
|
| 25 |
+
if [ -f ./data/github_data/storage.sqlite ]; then
|
| 26 |
+
cp ./data/github_data/storage.sqlite "$SOURCE_DB"
|
| 27 |
+
echo "Successfully pulled from GitHub repository"
|
| 28 |
+
else
|
| 29 |
+
echo "storage.sqlite not found in GitHub repository, will push during sync"
|
| 30 |
+
fi
|
| 31 |
+
|
| 32 |
+
# Sync GitHub every 2 minutes using America/Detroit time
|
| 33 |
+
sync_data() {
|
| 34 |
+
while true; do
|
| 35 |
+
CURRENT_TIME=$(TZ=America/Detroit date '+%Y-%m-%d %H:%M:%S')
|
| 36 |
+
echo "Current time $CURRENT_TIME - Starting GitHub sync..."
|
| 37 |
+
|
| 38 |
+
cd ./data/github_data || { echo "Failed to change directory"; exit 1; }
|
| 39 |
+
|
| 40 |
+
git config user.name "y4shg"
|
| 41 |
+
git config user.email "y.ghule77@gmail.com"
|
| 42 |
+
|
| 43 |
+
git checkout main 2>/dev/null || git checkout master
|
| 44 |
+
|
| 45 |
+
if [ -f "$SOURCE_DB" ]; then
|
| 46 |
+
cp "$SOURCE_DB" ./storage.sqlite
|
| 47 |
+
else
|
| 48 |
+
echo "Database not yet initialized"
|
| 49 |
+
fi
|
| 50 |
+
|
| 51 |
+
# Stage and commit if changes exist
|
| 52 |
+
if [[ -n $(git status -s) ]]; then
|
| 53 |
+
git add storage.sqlite
|
| 54 |
+
git commit -m "Auto sync storage.sqlite $(TZ=America/Detroit date '+%Y-%m-%d %H:%M:%S')"
|
| 55 |
+
fi
|
| 56 |
+
|
| 57 |
+
# ALWAYS try to push if commits exist (with upstream tracking)
|
| 58 |
+
if git status | grep -q "ahead of"; then
|
| 59 |
+
echo "Local commits ahead — pushing..."
|
| 60 |
+
git push -u origin HEAD && echo "GitHub push successful" || echo "GitHub push failed"
|
| 61 |
+
else
|
| 62 |
+
echo "GitHub: No new commits to push"
|
| 63 |
+
fi
|
| 64 |
+
|
| 65 |
+
cd ../..
|
| 66 |
+
echo "Waiting 2 minutes until next sync..."
|
| 67 |
+
sleep 120 # Wait 2 minutes
|
| 68 |
+
done
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Start sync process in background
|
| 72 |
+
sync_data &
|