elmerzole commited on
Commit
f5da15c
·
verified ·
1 Parent(s): d3c27ad

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +14 -40
sync_data.sh CHANGED
@@ -1,19 +1,18 @@
1
- #!/bin/bash
2
  set -e
3
 
4
- # Vars
5
  REPO_URL="https://x-access-token:${G_TOKEN}@github.com/${G_NAME}.git"
6
- DB_FILE="/app/backend/data/webui.db"
7
  ENCRYPTED_DB="webui.db.gpg"
8
  TEMP_REPO="/app/db/temp_repo"
9
 
10
  echo "Configuring for HuggingFace Space deployment"
11
 
12
- # Ensure dirs
13
  mkdir -p /app/backend/data
14
- mkdir -p /app/db
15
 
16
- # Clone or init repo
17
  if [ ! -d "$TEMP_REPO/.git" ]; then
18
  echo "Cloning repo ${G_NAME}..."
19
  git clone "$REPO_URL" "$TEMP_REPO"
@@ -22,43 +21,18 @@ else
22
  cd "$TEMP_REPO" && git fetch --all && git reset --hard origin/main && cd -
23
  fi
24
 
25
- # Configure git identity
26
- cd "$TEMP_REPO"
27
- git config user.email "${G_EMAIL:-huggingface-bot@users.noreply.github.com}"
28
- git config user.name "${G_USER:-HuggingFace Bot}"
29
- cd -
30
-
31
- cd /app/backend
32
-
33
- # If encrypted DB exists in repo → decrypt into working DB
34
  if [ -f "$TEMP_REPO/$ENCRYPTED_DB" ]; then
35
  echo "Found encrypted DB in repo, decrypting..."
36
  gpg --batch --yes --passphrase "$GPG_PASSPHRASE" \
37
- -o "$DB_FILE" -d "$TEMP_REPO/$ENCRYPTED_DB"
 
38
  else
39
- echo "No DB found in repo, creating new empty DB"
40
- sqlite3 "$DB_FILE" "VACUUM;"
 
41
  fi
42
 
43
- # --- Delay first sync so alembic migrations complete ---
44
- echo "Waiting 3 mins for migrations to finish..."
45
- sleep 180
46
-
47
- # Periodic sync loop
48
- while true; do
49
- echo "[$(date)] Starting sync..."
50
-
51
- cp "$DB_FILE" "$TEMP_REPO/webui.db"
52
- gpg --batch --yes --symmetric --cipher-algo AES256 \
53
- --passphrase "$GPG_PASSPHRASE" \
54
- -o "$TEMP_REPO/$ENCRYPTED_DB" "$TEMP_REPO/webui.db"
55
-
56
- cd "$TEMP_REPO"
57
- git add "$ENCRYPTED_DB"
58
- git commit -m "DB sync: $(date)" || true
59
- git push --force "$REPO_URL" HEAD:main || echo "Force push failed, will retry..."
60
- cd /app/backend
61
-
62
- echo "[$(date)] Sync done, sleeping 5 mins..."
63
- sleep 300
64
- done
 
1
+ #!/bin/sh
2
  set -e
3
 
 
4
  REPO_URL="https://x-access-token:${G_TOKEN}@github.com/${G_NAME}.git"
5
+ DB_PATH="/app/backend/data/webui.db"
6
  ENCRYPTED_DB="webui.db.gpg"
7
  TEMP_REPO="/app/db/temp_repo"
8
 
9
  echo "Configuring for HuggingFace Space deployment"
10
 
11
+ # Ensure backend/data exists
12
  mkdir -p /app/backend/data
13
+ chown -R 1000:1000 /app/backend/data
14
 
15
+ # Clone repo
16
  if [ ! -d "$TEMP_REPO/.git" ]; then
17
  echo "Cloning repo ${G_NAME}..."
18
  git clone "$REPO_URL" "$TEMP_REPO"
 
21
  cd "$TEMP_REPO" && git fetch --all && git reset --hard origin/main && cd -
22
  fi
23
 
24
+ # Restore DB if available
 
 
 
 
 
 
 
 
25
  if [ -f "$TEMP_REPO/$ENCRYPTED_DB" ]; then
26
  echo "Found encrypted DB in repo, decrypting..."
27
  gpg --batch --yes --passphrase "$GPG_PASSPHRASE" \
28
+ -o "$DB_PATH" -d "$TEMP_REPO/$ENCRYPTED_DB"
29
+ chown 1000:1000 "$DB_PATH"
30
  else
31
+ echo "No DB found in repo, creating new empty DB..."
32
+ sqlite3 "$DB_PATH" "VACUUM;"
33
+ chown 1000:1000 "$DB_PATH"
34
  fi
35
 
36
+ # Delay before first sync
37
+ echo "Waiting 60s before first push..."
38
+ sleep 60