Upload 2 files
Browse files- Dockerfile +6 -0
- data-sync.sh +92 -0
Dockerfile
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM ghcr.io/open-webui/open-webui:latest
|
| 2 |
+
|
| 3 |
+
COPY data-sync.sh data-sync.sh
|
| 4 |
+
|
| 5 |
+
RUN chmod -R 777 ./data && \
|
| 6 |
+
sed -i "1r data-sync.sh" ./start.sh
|
data-sync.sh
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Note: Ensure that date, git, and other tools are installed in the system when the script is executed.
|
| 3 |
+
|
| 4 |
+
# Check necessary 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 |
+
# Build GitHub repository clone URL with token
|
| 11 |
+
REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
|
| 12 |
+
REPO_DIR="./data/github_data"
|
| 13 |
+
mkdir -p "$REPO_DIR"
|
| 14 |
+
|
| 15 |
+
# Clone repository
|
| 16 |
+
echo "Cloning repository..."
|
| 17 |
+
git clone --depth 1 "$REPO_URL" "$REPO_DIR" || {
|
| 18 |
+
echo "Clone failed, please check if G_NAME and G_TOKEN are correct."
|
| 19 |
+
exit 1
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# Check if the initial webui.db exists and copy it
|
| 23 |
+
if [ -f "$REPO_DIR/webui.db" ]; then
|
| 24 |
+
cp "$REPO_DIR/webui.db" ./data/webui.db
|
| 25 |
+
echo "Successfully pulled webui.db from GitHub repository"
|
| 26 |
+
else
|
| 27 |
+
echo "webui.db not found in GitHub repository, will push during sync"
|
| 28 |
+
fi
|
| 29 |
+
|
| 30 |
+
# Define sync function, performs a sync every 12 hours.
|
| 31 |
+
sync_data() {
|
| 32 |
+
while true; do
|
| 33 |
+
# Output current London time during sync
|
| 34 |
+
CURRENT_TIME=$(TZ=Europe/London date '+%Y-%m-%d %H:%M:%S')
|
| 35 |
+
echo "Current time $CURRENT_TIME"
|
| 36 |
+
|
| 37 |
+
# ---- Start sync process ----
|
| 38 |
+
echo "Starting GitHub sync..."
|
| 39 |
+
cd "$REPO_DIR" || { echo "Failed to change directory to $REPO_DIR"; exit 1; }
|
| 40 |
+
|
| 41 |
+
git config user.name "AutoSync Bot"
|
| 42 |
+
git config user.email "autosync@bot.com"
|
| 43 |
+
|
| 44 |
+
if [ -z "$(git branch --list main)" ]; then
|
| 45 |
+
echo "Branch 'main' not found. Creating it for the initial commit."
|
| 46 |
+
git checkout -b main
|
| 47 |
+
else
|
| 48 |
+
git switch main
|
| 49 |
+
fi
|
| 50 |
+
|
| 51 |
+
# Copy latest database file to repository directory
|
| 52 |
+
if [ -f "../webui.db" ]; then
|
| 53 |
+
cp ../webui.db ./webui.db
|
| 54 |
+
else
|
| 55 |
+
echo "Database file ../webui.db not yet initialized by the application."
|
| 56 |
+
if [ ! -f "./webui.db" ] && [ -z "$(git ls-files)" ]; then
|
| 57 |
+
echo "Creating .gitkeep to initialize the repository."
|
| 58 |
+
touch .gitkeep
|
| 59 |
+
git add .gitkeep
|
| 60 |
+
fi
|
| 61 |
+
fi
|
| 62 |
+
|
| 63 |
+
# Check if there are changes
|
| 64 |
+
if [[ -n $(git status --porcelain) ]]; then
|
| 65 |
+
git add webui.db .gitkeep
|
| 66 |
+
git commit -m "Auto sync webui.db: $(TZ=Europe/London date '+%Y-%m-%d %H:%M:%S')"
|
| 67 |
+
|
| 68 |
+
echo "Pushing changes to GitHub..."
|
| 69 |
+
git push --set-upstream origin main && {
|
| 70 |
+
echo "GitHub push successful"
|
| 71 |
+
} || {
|
| 72 |
+
echo "Push failed, will retry once..."
|
| 73 |
+
sleep 10
|
| 74 |
+
git push origin main || {
|
| 75 |
+
echo "Retry failed, abandoning GitHub push for this cycle."
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
else
|
| 79 |
+
echo "GitHub: No database changes detected"
|
| 80 |
+
fi
|
| 81 |
+
# Return to the original directory
|
| 82 |
+
cd ../..
|
| 83 |
+
|
| 84 |
+
# ---- Sync process complete ----
|
| 85 |
+
echo "Sync complete. Waiting 12 hours for the next sync."
|
| 86 |
+
# sleep 43200
|
| 87 |
+
sleep 300
|
| 88 |
+
done
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
# Start sync process in background
|
| 92 |
+
sync_data &
|