openwebui / sync_data.sh
Shadman9416's picture
Upload sync_data.sh
8681d05 verified
#!/bin/bash
# Note: Ensure date, git, curl tools are installed and TZ timezone environment is set (can specify timezone temporarily in each date command)
# Check required 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 requirements (including hourly sync)
sync_data() {
while true; do
# Get current time and components using Asia/Dhaka timezone
CURRENT_TS=$(TZ=Asia/Dhaka date +%s)
CURRENT_DATE=$(TZ=Asia/Dhaka date '+%Y-%m-%d')
CURRENT_HOUR=$(TZ=Asia/Dhaka date +%H) # 00~23
CURRENT_MIN=$(TZ=Asia/Dhaka date +%M)
CURRENT_SEC=$(TZ=Asia/Dhaka 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/Dhaka date -d "${CURRENT_DATE} 08:00:00" +%s)
# If between 08:00 and 22:59, next hour is same day
elif [ "$CURRENT_HOUR" -ge 8 ] && [ "$CURRENT_HOUR" -lt 23 ]; then
# If exactly at hour (minutes and seconds both 0), 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/Dhaka date -d "${CURRENT_DATE} ${NEXT_HOUR}:00:00" +%s)
fi
# If current time is 23:00~23:59, next target is next day 00:00 (sync at 24:00)
else # CURRENT_HOUR == 23
if [ "$CURRENT_MIN" -eq 0 ] && [ "$CURRENT_SEC" -eq 0 ]; then
TARGET_TS=$CURRENT_TS
else
TOMORROW=$(TZ=Asia/Dhaka date -d "tomorrow" '+%Y-%m-%d')
TARGET_TS=$(TZ=Asia/Dhaka date -d "${TOMORROW} 00:00:00" +%s)
fi
fi
# Calculate wait time (if exactly sync time, sleep_time is 0)
SLEEP_TIME=$(( TARGET_TS - CURRENT_TS ))
if [ "$SLEEP_TIME" -gt 0 ]; then
echo "Next sync in ${SLEEP_TIME} seconds (Beijing time next sync: $(TZ=Asia/Dhaka date -d "@$TARGET_TS" '+%Y-%m-%d %H:%M:%S'))"
sleep "$SLEEP_TIME"
fi
# Output current Beijing time during sync
CURRENT_TIME=$(TZ=Asia/Dhaka 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, try master branch if switch fails
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 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/Dhaka date '+%Y-%m-%d %H:%M:%S')"
git push --force-with-lease origin HEAD && {
echo "GitHub force push successful"
} || {
echo "Force push failed, waiting to retry..."
sleep 10
git push --force-with-lease origin HEAD || {
echo "Retry failed, giving up on GitHub force push."
}
}
else
echo "GitHub: No database changes detected"
fi
# Return to main directory
cd ../..
# 2. Sync to WebDAV (if environment variables configured)
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/Dhaka 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 to retry..."
sleep 10
curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" || {
echo "Retry failed, giving up on 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 &