File size: 1,864 Bytes
1d6e453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then
    echo "Missing 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 repo..."
git clone "$REPO_URL" ./data/github_data || {
    echo "Clone failed"
    exit 1
}

if [ -f ./data/github_data/webui.db ]; then
    cp ./data/github_data/webui.db ./data/webui.db
    echo "Pulled webui.db from GitHub"
fi

do_sync() {
    CURRENT_TIME=$(TZ=Asia/Ho_Chi_Minh date '+%Y-%m-%d %H:%M:%S')
    echo "Running sync at $CURRENT_TIME"

    cd ./data/github_data || exit 1
    git config user.name "AutoSync Bot"
    git config user.email "[email protected]"
    git checkout main 2>/dev/null || git checkout master

    if [ -f "../webui.db" ]; then
        cp ../webui.db ./webui.db
    fi

    if [[ -n $(git status -s) ]]; then
        git add webui.db
        git commit -m "Auto sync $(TZ=Asia/Ho_Chi_Minh date '+%Y-%m-%d %H:%M:%S')"
        git push origin HEAD || {
            echo "GitHub push failed, retrying..."
            sleep 10
            git push origin HEAD || echo "Push failed again"
        }
    else
        echo "No changes to commit"
    fi

    cd ../..

    if [ -n "$WEBDAV_URL" ] && [ -n "$WEBDAV_USERNAME" ] && [ -n "$WEBDAV_PASSWORD" ]; then
        FILENAME="webui_$(TZ=Asia/Ho_Chi_Minh date +'%m_%d').db"
        if [ -f ./data/webui.db ]; then
            curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" || {
                echo "WebDAV upload failed, retrying..."
                sleep 10
                curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" || echo "Upload failed again"
            }
        fi
    fi
}

while true; do
    do_sync
    sleep 60
done