Spaces:
Running
Running
Upload 3 files
Browse files- Dockerfile +6 -0
- README.md +6 -5
- sync_data.sh +139 -0
Dockerfile
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM ghcr.io/open-webui/open-webui:dev
|
| 2 |
+
|
| 3 |
+
COPY sync_data.sh sync_data.sh
|
| 4 |
+
|
| 5 |
+
RUN chmod -R 777 ./data && \
|
| 6 |
+
sed -i "1r sync_data.sh" ./start.sh
|
README.md
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: OpenWebUI
|
| 3 |
+
emoji: 📉
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
+
app_port: 8080
|
| 9 |
---
|
| 10 |
|
| 11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
sync_data.sh
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Note: Ensure date, git, curl tools are installed and TZ timezone environment is set (can specify timezone temporarily in each date command)
|
| 3 |
+
|
| 4 |
+
# Check required environment variables
|
| 5 |
+
if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then
|
| 6 |
+
echo "Missing required environment variables G_NAME or G_TOKEN"
|
| 7 |
+
exit 1
|
| 8 |
+
fi
|
| 9 |
+
|
| 10 |
+
# Parse repository name and username
|
| 11 |
+
IFS='/' read -r GITHUB_USER GITHUB_REPO <<< "$G_NAME"
|
| 12 |
+
|
| 13 |
+
# Build GitHub repository clone URL with token
|
| 14 |
+
REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
|
| 15 |
+
mkdir -p ./data/github_data
|
| 16 |
+
|
| 17 |
+
# Clone repository
|
| 18 |
+
echo "Cloning repository..."
|
| 19 |
+
git clone "$REPO_URL" ./data/github_data || {
|
| 20 |
+
echo "Clone failed, please check if G_NAME and G_TOKEN are correct."
|
| 21 |
+
exit 1
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
if [ -f ./data/github_data/webui.db ]; then
|
| 25 |
+
cp ./data/github_data/webui.db ./data/webui.db
|
| 26 |
+
echo "Successfully pulled from GitHub repository"
|
| 27 |
+
else
|
| 28 |
+
echo "webui.db not found in GitHub repository, will push during sync"
|
| 29 |
+
fi
|
| 30 |
+
|
| 31 |
+
# Define sync function according to Beijing time 08:00~24:00 requirements (including hourly sync)
|
| 32 |
+
sync_data() {
|
| 33 |
+
while true; do
|
| 34 |
+
# Get current time and components using Asia/Dhaka timezone
|
| 35 |
+
CURRENT_TS=$(TZ=Asia/Dhaka date +%s)
|
| 36 |
+
CURRENT_DATE=$(TZ=Asia/Dhaka date '+%Y-%m-%d')
|
| 37 |
+
CURRENT_HOUR=$(TZ=Asia/Dhaka date +%H) # 00~23
|
| 38 |
+
CURRENT_MIN=$(TZ=Asia/Dhaka date +%M)
|
| 39 |
+
CURRENT_SEC=$(TZ=Asia/Dhaka date +%S)
|
| 40 |
+
|
| 41 |
+
# Calculate next sync target timestamp (Beijing time)
|
| 42 |
+
# If current time is before 08:00, target is today 08:00
|
| 43 |
+
if [ "$CURRENT_HOUR" -lt 8 ]; then
|
| 44 |
+
TARGET_TS=$(TZ=Asia/Dhaka date -d "${CURRENT_DATE} 08:00:00" +%s)
|
| 45 |
+
# If between 08:00 and 22:59, next hour is same day
|
| 46 |
+
elif [ "$CURRENT_HOUR" -ge 8 ] && [ "$CURRENT_HOUR" -lt 23 ]; then
|
| 47 |
+
# If exactly at hour (minutes and seconds both 0), sync immediately
|
| 48 |
+
if [ "$CURRENT_MIN" -eq 0 ] && [ "$CURRENT_SEC" -eq 0 ]; then
|
| 49 |
+
TARGET_TS=$CURRENT_TS
|
| 50 |
+
else
|
| 51 |
+
NEXT_HOUR=$((10#$CURRENT_HOUR + 1))
|
| 52 |
+
TARGET_TS=$(TZ=Asia/Dhaka date -d "${CURRENT_DATE} ${NEXT_HOUR}:00:00" +%s)
|
| 53 |
+
fi
|
| 54 |
+
# If current time is 23:00~23:59, next target is next day 00:00 (sync at 24:00)
|
| 55 |
+
else # CURRENT_HOUR == 23
|
| 56 |
+
if [ "$CURRENT_MIN" -eq 0 ] && [ "$CURRENT_SEC" -eq 0 ]; then
|
| 57 |
+
TARGET_TS=$CURRENT_TS
|
| 58 |
+
else
|
| 59 |
+
TOMORROW=$(TZ=Asia/Dhaka date -d "tomorrow" '+%Y-%m-%d')
|
| 60 |
+
TARGET_TS=$(TZ=Asia/Dhaka date -d "${TOMORROW} 00:00:00" +%s)
|
| 61 |
+
fi
|
| 62 |
+
fi
|
| 63 |
+
|
| 64 |
+
# Calculate wait time (if exactly sync time, sleep_time is 0)
|
| 65 |
+
SLEEP_TIME=$(( TARGET_TS - CURRENT_TS ))
|
| 66 |
+
if [ "$SLEEP_TIME" -gt 0 ]; then
|
| 67 |
+
echo "Next sync in ${SLEEP_TIME} seconds (Beijing time next sync: $(TZ=Asia/Dhaka date -d "@$TARGET_TS" '+%Y-%m-%d %H:%M:%S'))"
|
| 68 |
+
sleep "$SLEEP_TIME"
|
| 69 |
+
fi
|
| 70 |
+
|
| 71 |
+
# Output current Beijing time during sync
|
| 72 |
+
CURRENT_TIME=$(TZ=Asia/Dhaka date '+%Y-%m-%d %H:%M:%S')
|
| 73 |
+
echo "Current time $CURRENT_TIME"
|
| 74 |
+
|
| 75 |
+
# ---- Start sync process ----
|
| 76 |
+
|
| 77 |
+
# 1. Sync to GitHub
|
| 78 |
+
echo "Starting GitHub sync..."
|
| 79 |
+
cd ./data/github_data || { echo "Failed to change directory"; exit 1; }
|
| 80 |
+
git config user.name "AutoSync Bot"
|
| 81 |
+
git config user.email "autosync@bot.com"
|
| 82 |
+
|
| 83 |
+
# Ensure on main branch, try master branch if switch fails
|
| 84 |
+
git checkout main 2>/dev/null || git checkout master
|
| 85 |
+
|
| 86 |
+
# Copy latest database file to repository directory
|
| 87 |
+
if [ -f "../webui.db" ]; then
|
| 88 |
+
cp ../webui.db ./webui.db
|
| 89 |
+
else
|
| 90 |
+
echo "Database not initialized"
|
| 91 |
+
fi
|
| 92 |
+
|
| 93 |
+
# Check if there are changes
|
| 94 |
+
if [[ -n $(git status -s) ]]; then
|
| 95 |
+
git add webui.db
|
| 96 |
+
git commit -m "Auto sync webui.db $(TZ=Asia/Dhaka date '+%Y-%m-%d %H:%M:%S')"
|
| 97 |
+
git push origin HEAD && {
|
| 98 |
+
echo "GitHub push successful"
|
| 99 |
+
} || {
|
| 100 |
+
echo "Push failed, waiting to retry..."
|
| 101 |
+
sleep 10
|
| 102 |
+
git push origin HEAD || {
|
| 103 |
+
echo "Retry failed, giving up on GitHub push."
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
else
|
| 107 |
+
echo "GitHub: No database changes detected"
|
| 108 |
+
fi
|
| 109 |
+
# Return to main directory
|
| 110 |
+
cd ../..
|
| 111 |
+
|
| 112 |
+
# 2. Sync to WebDAV (if environment variables configured)
|
| 113 |
+
if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
|
| 114 |
+
echo "WebDAV environment variables missing, skipping WebDAV sync."
|
| 115 |
+
else
|
| 116 |
+
echo "Starting WebDAV sync..."
|
| 117 |
+
FILENAME="webui_$(TZ=Asia/Dhaka date +'%m_%d').db"
|
| 118 |
+
if [ -f ./data/webui.db ]; then
|
| 119 |
+
curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" && {
|
| 120 |
+
echo "WebDAV upload successful"
|
| 121 |
+
} || {
|
| 122 |
+
echo "WebDAV upload failed, waiting to retry..."
|
| 123 |
+
sleep 10
|
| 124 |
+
curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" || {
|
| 125 |
+
echo "Retry failed, giving up on WebDAV upload."
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
else
|
| 129 |
+
echo "webui.db file not found, skipping WebDAV sync."
|
| 130 |
+
fi
|
| 131 |
+
fi
|
| 132 |
+
|
| 133 |
+
# ---- Sync process complete, next loop will automatically calculate wait time based on current Beijing time ----
|
| 134 |
+
|
| 135 |
+
done
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
# Start sync process in background
|
| 139 |
+
sync_data &
|