| #!/bin/bash |
|
|
| |
| if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then |
| echo "Missing required environment variables G_NAME or G_TOKEN" |
| exit 1 |
| fi |
|
|
| |
| IFS='/' read -r GITHUB_USER GITHUB_REPO <<< "$G_NAME" |
|
|
| |
| REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git" |
| mkdir -p ./data/github_data |
|
|
| |
| echo "Cloning repository..." |
| git clone "$REPO_URL" ./data/github_data || { |
| echo "Clone failed, please check if G_NAME and G_TOKEN are correct." |
| exit 1 |
| } |
|
|
| SOURCE_DB="$HOME/.omniroute/storage.sqlite" |
| mkdir -p "$HOME/.omniroute" |
|
|
| |
| if [ -f ./data/github_data/storage.sqlite ]; then |
| cp ./data/github_data/storage.sqlite "$SOURCE_DB" |
| echo "Successfully pulled from GitHub repository" |
| else |
| echo "storage.sqlite not found in GitHub repository, will push during sync" |
| fi |
|
|
| |
| sync_data() { |
| while true; do |
| CURRENT_TIME=$(TZ=America/Detroit date '+%Y-%m-%d %H:%M:%S') |
| echo "Current time $CURRENT_TIME - Starting GitHub sync..." |
|
|
| cd ./data/github_data || { echo "Failed to change directory"; exit 1; } |
|
|
| git config user.name "y4shg" |
| git config user.email "y.ghule77@gmail.com" |
|
|
| git checkout main 2>/dev/null || git checkout master |
|
|
| if [ -f "$SOURCE_DB" ]; then |
| cp "$SOURCE_DB" ./storage.sqlite |
| else |
| echo "Database not yet initialized" |
| fi |
|
|
| |
| if [[ -n $(git status -s) ]]; then |
| git add storage.sqlite |
| git commit -m "Auto sync storage.sqlite $(TZ=America/Detroit date '+%Y-%m-%d %H:%M:%S')" |
| fi |
|
|
| |
| if git status | grep -q "ahead of"; then |
| echo "Local commits ahead — pushing..." |
| git push -u origin HEAD && echo "GitHub push successful" || echo "GitHub push failed" |
| else |
| echo "GitHub: No new commits to push" |
| fi |
|
|
| cd ../.. |
| echo "Waiting 2 minutes until next sync..." |
| sleep 120 |
| done |
| } |
|
|
| |
| sync_data & |