elmerzole commited on
Commit
72b93a1
·
verified ·
1 Parent(s): 4c5fc93

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +45 -74
sync_data.sh CHANGED
@@ -1,86 +1,57 @@
1
  #!/bin/bash
2
- set -euo pipefail
3
 
4
- DB_PATH="/app/db/webui.db"
5
- DB_DIR="/app/db"
 
6
 
7
- # Check required environment variables
8
- if [ -z "${G_NAME:-}" ] || [ -z "${G_TOKEN:-}" ] || [ -z "${GPG_PASSPHRASE:-}" ]; then
9
- echo "ERROR: Missing required environment variables G_NAME, G_TOKEN, or GPG_PASSPHRASE"
10
- exit 1
11
- fi
12
-
13
- # Validate tool availability
14
- command -v inotifywait >/dev/null 2>&1 || { echo >&2 "inotifywait is not installed."; exit 1; }
15
- command -v gpg >/dev/null 2>&1 || { echo >&2 "gpg is not installed."; exit 1; }
16
 
17
- # Parse repository info
18
- IFS='/' read -r GITHUB_USER GITHUB_REPO <<< "$G_NAME"
19
  REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
20
 
21
- # Create db directory
22
- mkdir -p "$DB_DIR"
23
-
24
- # Initialize database
25
- if [ ! -f "$DB_PATH" ]; then
26
- echo "Initializing encrypted database..."
27
- if git clone "$REPO_URL" "$DB_DIR/temp_repo"; then
28
- if [ -f "$DB_DIR/temp_repo/webui.db.gpg" ]; then
29
- echo "$GPG_PASSPHRASE" | gpg --batch --passphrase-fd 0 -o "$DB_PATH" -d "$DB_DIR/temp_repo/webui.db.gpg"
30
- echo "Database restored from GitHub"
31
- else
32
- touch "$DB_PATH"
33
- echo "Created new empty database"
34
- fi
35
- rm -rf "$DB_DIR/temp_repo"
36
- else
37
- touch "$DB_PATH"
38
- echo "Created new empty database"
39
- fi
 
40
  fi
41
 
42
- # Configure Git
43
- git config --global user.name "Encrypted DB Sync"
44
- git config --global user.email "encrypted-sync@bot.com"
 
 
45
 
46
- # Change to db directory
47
- cd "$DB_DIR" || exit 1
 
 
48
 
49
- # Synchronization function with retry logic
50
- sync_loop() {
51
- while true; do
52
- # Monitor for database changes
53
- inotifywait -qq -e modify "$DB_PATH"
54
-
55
- # Get current time
56
- CURRENT_TIME=$(date '+%Y-%m-%d %H:%M:%S')
57
- echo "[$CURRENT_TIME] Local changes detected, starting sync..."
58
-
59
- # Pull latest changes
60
- git pull origin main 2>/dev/null || git pull origin master || true
61
-
62
- # Encrypt database
63
- echo "$GPG_PASSPHRASE" | gpg --batch --passphrase-fd 0 -o webui.db.gpg -c "$DB_PATH"
64
-
65
- # Commit changes
66
  git add webui.db.gpg
67
- git commit -m "Auto-sync: Encrypted webui.db updated at $CURRENT_TIME" || true
68
-
69
- # Push with exponential backoff
70
- MAX_RETRIES=3
71
- RETRY_DELAY=5
72
- for ((i=1; i<=MAX_RETRIES; i++)); do
73
- if git push origin HEAD; then
74
- echo "GitHub push successful"
75
- break
76
- else
77
- echo "Push attempt $i failed, retrying in $RETRY_DELAY seconds..."
78
- sleep $RETRY_DELAY
79
- RETRY_DELAY=$((RETRY_DELAY * 2)) # Exponential backoff
80
- fi
81
- done
82
- done
83
- }
84
 
85
- # Start synchronization
86
- sync_loop &
 
 
1
  #!/bin/bash
2
+ set -e
3
 
4
+ DB_PATH="/app/data/webui.db"
5
+ REPO_DIR="/app/db/temp_repo"
6
+ LOG_FILE="/app/db/sync.log"
7
 
8
+ echo "Configuring for HuggingFace Space deployment" | tee -a $LOG_FILE
 
 
 
 
 
 
 
 
9
 
10
+ # Construct repo URL using token
 
11
  REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
12
 
13
+ # Clean old repo (if container restarted)
14
+ rm -rf $REPO_DIR
15
+
16
+ # Clone fresh copy
17
+ echo "Cloning repo $G_NAME..." | tee -a $LOG_FILE
18
+ git clone "$REPO_URL" "$REPO_DIR"
19
+ cd $REPO_DIR
20
+ git config user.email "bot@huggingface.space"
21
+ git config user.name "HF Space Bot"
22
+ cd -
23
+
24
+ # If encrypted DB exists in repo, restore it
25
+ if [ -f "$REPO_DIR/webui.db.gpg" ]; then
26
+ echo "Found encrypted DB in repo, decrypting..." | tee -a $LOG_FILE
27
+ gpg --batch --yes --passphrase "$GPG_PASSPHRASE" -o "$DB_PATH" -d "$REPO_DIR/webui.db.gpg" || \
28
+ echo "Failed to decrypt DB, starting fresh" | tee -a $LOG_FILE
29
+ else
30
+ echo "No DB found in repo, starting with new empty DB" | tee -a $LOG_FILE
31
+ mkdir -p /app/data
32
+ touch "$DB_PATH"
33
  fi
34
 
35
+ # ---------------------------
36
+ # Runtime loop: sync every 5 min
37
+ # ---------------------------
38
+ while true; do
39
+ echo "[$(date)] Starting sync..." | tee -a $LOG_FILE
40
 
41
+ if [ -f "$DB_PATH" ]; then
42
+ # Encrypt current DB
43
+ gpg --batch --yes --passphrase "$GPG_PASSPHRASE" -c "$DB_PATH"
44
+ mv "$DB_PATH.gpg" "$REPO_DIR/webui.db.gpg"
45
 
46
+ cd "$REPO_DIR"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  git add webui.db.gpg
48
+ git commit -m "DB sync: $(date)" || echo "No changes to commit" | tee -a $LOG_FILE
49
+ git push origin HEAD:main || echo "Git push failed" | tee -a $LOG_FILE
50
+ cd -
51
+ else
52
+ echo "[$(date)] Warning: DB file missing, skipping sync" | tee -a $LOG_FILE
53
+ fi
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ echo "[$(date)] Sync done, sleeping 5 mins..." | tee -a $LOG_FILE
56
+ sleep 300
57
+ done