File size: 2,294 Bytes
7c1a9d1
 
 
 
 
 
 
 
 
 
 
 
 
b191695
7c1a9d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b191695
7c1a9d1
 
b191695
 
7c1a9d1
 
974d846
00e355f
 
974d846
7c1a9d1
 
b191695
 
 
7c1a9d1
b191695
7c1a9d1
974d846
 
 
 
 
7c1a9d1
 
b191695
974d846
 
 
 
 
b191695
7c1a9d1
974d846
7c1a9d1
 
974d846
 
b191695
 
495930f
7c1a9d1
 
 
 
974d846
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash

# Check necessary environment variables
if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then
    echo "Missing required environment variables G_NAME or G_TOKEN"
    exit 1
fi

# Parse repository name and username
IFS='/' read -r GITHUB_USER GITHUB_REPO <<< "$G_NAME"

# Build GitHub repository clone URL with token
REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
mkdir -p ./data/github_data

# Clone repository
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
}

if [ -f ./data/github_data/webui.db ]; then
    cp ./data/github_data/webui.db ./data/webui.db
    echo "Successfully pulled from GitHub repository"
else
    echo "webui.db not found in GitHub repository, will push during sync"
fi

# Sync GitHub every 2 minutes using America/Detroit time
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 "../webui.db" ]; then
            cp ../webui.db ./webui.db
        else
            echo "Database not yet initialized"
        fi

        # -----------------------------
        # FIX YOU REQUESTED (APPLIED)
        # -----------------------------

        # Stage and commit if changes exist
        if [[ -n $(git status -s) ]]; then
            git add webui.db
            git commit -m "Auto sync webui.db $(TZ=America/Detroit date '+%Y-%m-%d %H:%M:%S')"
        fi

        # ALWAYS try to push if commits exist
        if git status | grep -q "ahead of"; then
            echo "Local commits ahead — pushing..."
            git push 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  # Wait 2 minutes
    done
}

# Start sync process in background
sync_data &