openwebui / data-sync.sh
owlninjam's picture
Upload 4 files
7c1a9d1 verified
#!/bin/bash
# Note: Ensure that date, git, curl and other tools are installed in the system when the script is executed, and set the TZ timezone environment (can be temporarily specified in each date command)
# 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
# Define sync function, according to Beijing time 08:00~24:00 (including hourly sync) requirements
sync_data() {
while true; do
# Use Asia/Shanghai timezone to get current time and its components
CURRENT_TS=$(TZ=Asia/Shanghai date +%s)
CURRENT_DATE=$(TZ=Asia/Shanghai date '+%Y-%m-%d')
CURRENT_HOUR=$(TZ=Asia/Shanghai date +%H) # 00~23
CURRENT_MIN=$(TZ=Asia/Shanghai date +%M)
CURRENT_SEC=$(TZ=Asia/Shanghai date +%S)
# Calculate next sync target timestamp (Beijing time)
# If current time is before 08:00, target is today 08:00
if [ "$CURRENT_HOUR" -lt 8 ]; then
TARGET_TS=$(TZ=Asia/Shanghai date -d "${CURRENT_DATE} 08:00:00" +%s)
# If between 08:00 to 22:59, next hour is on the same day
elif [ "$CURRENT_HOUR" -ge 8 ] && [ "$CURRENT_HOUR" -lt 23 ]; then
# If exactly at the hour (seconds and minutes are 0) then sync immediately
if [ "$CURRENT_MIN" -eq 0 ] && [ "$CURRENT_SEC" -eq 0 ]; then
TARGET_TS=$CURRENT_TS
else
NEXT_HOUR=$((10#$CURRENT_HOUR + 1))
TARGET_TS=$(TZ=Asia/Shanghai date -d "${CURRENT_DATE} ${NEXT_HOUR}:00:00" +%s)
fi
# If current time is between 23:00~23:59, next target is next day 00:00 (which is 24:00 sync)
else # CURRENT_HOUR == 23
if [ "$CURRENT_MIN" -eq 0 ] && [ "$CURRENT_SEC" -eq 0 ]; then
TARGET_TS=$CURRENT_TS
else
TOMORROW=$(TZ=Asia/Shanghai date -d "tomorrow" '+%Y-%m-%d')
TARGET_TS=$(TZ=Asia/Shanghai date -d "${TOMORROW} 00:00:00" +%s)
fi
fi
# Calculate wait time (if exactly at sync time then sleep_time is 0)
SLEEP_TIME=$(( TARGET_TS - CURRENT_TS ))
if [ "$SLEEP_TIME" -gt 0 ]; then
echo "Time until next sync: ${SLEEP_TIME} seconds (Beijing time next sync: $(TZ=Asia/Shanghai date -d "@$TARGET_TS" '+%Y-%m-%d %H:%M:%S'))"
sleep "$SLEEP_TIME"
fi
# Output current Beijing time during sync
CURRENT_TIME=$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')
echo "Current time $CURRENT_TIME"
# ---- Start sync process ----
# 1. Sync to GitHub
echo "Starting GitHub sync..."
cd ./data/github_data || { echo "Failed to change directory"; exit 1; }
git config user.name "AutoSync Bot"
git config user.email "autosync@bot.com"
# Ensure on main branch, if switch fails try master branch
git checkout main 2>/dev/null || git checkout master
# Copy latest database file to repository directory
if [ -f "../webui.db" ]; then
cp ../webui.db ./webui.db
else
echo "Database not yet initialized"
fi
# Check if there are changes
if [[ -n $(git status -s) ]]; then
git add webui.db
git commit -m "Auto sync webui.db $(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')"
git push origin HEAD && {
echo "GitHub push successful"
} || {
echo "Push failed, waiting for retry..."
sleep 10
git push origin HEAD || {
echo "Retry failed, abandoning GitHub push."
}
}
else
echo "GitHub: No database changes detected"
fi
# Return to main directory
cd ../..
# 2. Sync to WebDAV (if environment variables are complete)
if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
echo "WebDAV environment variables missing, skipping WebDAV sync."
else
echo "Starting WebDAV sync..."
FILENAME="webui_$(TZ=Asia/Shanghai 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 successful"
} || {
echo "WebDAV upload failed, waiting for retry..."
sleep 10
curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" || {
echo "Retry failed, abandoning WebDAV upload."
}
}
else
echo "webui.db file not found, skipping WebDAV sync."
fi
fi
# ---- Sync process complete, next loop will automatically calculate wait time based on current Beijing time ----
done
}
# Start sync process in background
sync_data &