unknownfriend00007 commited on
Commit
26ad551
·
verified ·
1 Parent(s): ef22ccb

Update start.sh

Browse files
Files changed (1) hide show
  1. start.sh +9 -53
start.sh CHANGED
@@ -1,15 +1,11 @@
1
  #!/bin/bash
2
  set -e
3
 
4
- # ============================================
5
- # CONFIGURATION
6
- # ============================================
7
  DB_PATH="/data/.flowise/database.sqlite"
8
  BACKUP_INTERVAL_SECONDS=${BACKUP_INTERVAL_SECONDS:-86400}
9
  AUTO_RESTART_DAYS=${AUTO_RESTART_DAYS:-7}
10
  HEALTH_CHECK_INTERVAL=${HEALTH_CHECK_INTERVAL:-3600}
11
 
12
- # Logging function (timestamps + clean format)
13
  log() {
14
  echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
15
  }
@@ -18,18 +14,12 @@ log_error() {
18
  echo "[$(date +'%Y-%m-%d %H:%M:%S')] ❌ $1" >&2
19
  }
20
 
21
- # ============================================
22
- # STARTUP BANNER
23
- # ============================================
24
  echo ""
25
  echo "═══════════════════════════════════════"
26
  echo " 🚀 Flowise + Neon Backup (Production)"
27
  echo "═══════════════════════════════════════"
28
  echo ""
29
 
30
- # ============================================
31
- # STEP 1: RESTORE FROM NEON
32
- # ============================================
33
  restore_from_neon() {
34
  if [ -z "$NEON_PASSWORD" ] || [ -z "$NEON_HOST" ]; then
35
  log "⚠️ Neon not configured - skipping restore"
@@ -38,32 +28,27 @@ restore_from_neon() {
38
 
39
  log "📥 Restoring from Neon..."
40
 
41
- # Fetch latest backup (clean output)
42
  PGPASSWORD="$NEON_PASSWORD" psql "postgresql://${NEON_USER}@${NEON_HOST}/${NEON_DB}?sslmode=require" \
43
- -A -t -q -c "SELECT sql_content FROM flowise_backups ORDER BY backup_date DESC LIMIT 1;" \
44
- > /tmp/neon_backup.sql 2>/dev/null || true
45
 
46
- if [ ! -f /tmp/neon_backup.sql ]; then
47
  log "ℹ️ No backup found (first run)"
48
  return 0
49
  fi
50
 
51
- # Clean SQL output
52
- sed -i 's/^[[:space:]]*//;s/[[:space:]]*$//' /tmp/neon_backup.sql 2>/dev/null || true
53
- sed -i '/^$/d' /tmp/neon_backup.sql 2>/dev/null || true
54
 
55
- FILE_SIZE=$(wc -c < /tmp/neon_backup.sql 2>/dev/null || echo "0")
56
-
57
- if [ "$FILE_SIZE" -lt 1000 ]; then
58
  log "ℹ️ No valid backup found"
 
59
  return 0
60
  fi
61
 
62
- # Prepare database directory
63
  mkdir -p /data/.flowise
64
  rm -f "$DB_PATH" "${DB_PATH}-shm" "${DB_PATH}-wal" 2>/dev/null || true
65
 
66
- # Import to SQLite (silent)
67
  sqlite3 "$DB_PATH" < /tmp/neon_backup.sql 2>/dev/null || true
68
 
69
  if [ -f "$DB_PATH" ]; then
@@ -81,20 +66,15 @@ restore_from_neon() {
81
  log_error "Database restore failed"
82
  fi
83
 
84
- rm -f /tmp/neon_backup.sql 2>/dev/null || true
85
  }
86
 
87
- # ============================================
88
- # STEP 2: START FLOWISE
89
- # ============================================
90
  start_flowise() {
91
  log "🚀 Starting Flowise..."
92
 
93
- # Start Flowise (suppress startup logs, keep errors)
94
  npx flowise start --PORT=7860 2>&1 | grep -E "^\[|ERROR|error|Error" &
95
  FLOWISE_PID=$!
96
 
97
- # Wait for ready
98
  local attempts=0
99
  local max_attempts=60
100
 
@@ -117,41 +97,29 @@ start_flowise() {
117
  exit 1
118
  }
119
 
120
- # ============================================
121
- # STEP 3: HEALTH MONITOR
122
- # ============================================
123
  start_health_monitor() {
124
  (
125
- sleep 300 # Initial delay
126
-
127
  while true; do
128
- # Check process alive
129
  if ! kill -0 $FLOWISE_PID 2>/dev/null; then
130
  log_error "Health: Flowise died - triggering restart"
131
  exit 1
132
  fi
133
-
134
- # Check HTTP responsive
135
  if ! curl -sf http://localhost:7860 > /dev/null 2>&1; then
136
  log_error "Health: Flowise unresponsive - triggering restart"
137
  kill $FLOWISE_PID 2>/dev/null || true
138
  exit 1
139
  fi
140
-
141
  sleep $HEALTH_CHECK_INTERVAL
142
  done
143
  ) &
144
  HEALTH_PID=$!
145
  }
146
 
147
- # ============================================
148
- # STEP 4: AUTO-RESTART TIMER
149
- # ============================================
150
  start_restart_timer() {
151
  if [ "$AUTO_RESTART_DAYS" -le 0 ]; then
152
  return 0
153
  fi
154
-
155
  (
156
  sleep $((AUTO_RESTART_DAYS * 86400))
157
  log "♻️ Scheduled restart ($AUTO_RESTART_DAYS days)"
@@ -160,22 +128,15 @@ start_restart_timer() {
160
  RESTART_PID=$!
161
  }
162
 
163
- # ============================================
164
- # STEP 5: BACKUP SERVICE
165
- # ============================================
166
  start_backup_service() {
167
  if [ -z "$NEON_PASSWORD" ] || [ -z "$NEON_HOST" ]; then
168
  log "⚠️ Neon not configured - backups disabled"
169
  return 0
170
  fi
171
-
172
  /data/backup.sh &
173
  BACKUP_PID=$!
174
  }
175
 
176
- # ============================================
177
- # GRACEFUL SHUTDOWN
178
- # ============================================
179
  cleanup() {
180
  log "🛑 Shutting down..."
181
  kill $FLOWISE_PID 2>/dev/null || true
@@ -187,16 +148,12 @@ cleanup() {
187
 
188
  trap cleanup SIGTERM SIGINT
189
 
190
- # ============================================
191
- # MAIN EXECUTION
192
- # ============================================
193
  restore_from_neon
194
  start_flowise
195
  start_backup_service
196
  start_health_monitor
197
  start_restart_timer
198
 
199
- # Status summary
200
  INTERVAL_HOURS=$((BACKUP_INTERVAL_SECONDS / 3600))
201
  echo ""
202
  echo "═══════════════════════════════════════"
@@ -212,5 +169,4 @@ fi
212
  echo "═══════════════════════════════════════"
213
  echo ""
214
 
215
- # Keep alive
216
  wait $FLOWISE_PID
 
1
  #!/bin/bash
2
  set -e
3
 
 
 
 
4
  DB_PATH="/data/.flowise/database.sqlite"
5
  BACKUP_INTERVAL_SECONDS=${BACKUP_INTERVAL_SECONDS:-86400}
6
  AUTO_RESTART_DAYS=${AUTO_RESTART_DAYS:-7}
7
  HEALTH_CHECK_INTERVAL=${HEALTH_CHECK_INTERVAL:-3600}
8
 
 
9
  log() {
10
  echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
11
  }
 
14
  echo "[$(date +'%Y-%m-%d %H:%M:%S')] ❌ $1" >&2
15
  }
16
 
 
 
 
17
  echo ""
18
  echo "═══════════════════════════════════════"
19
  echo " 🚀 Flowise + Neon Backup (Production)"
20
  echo "═══════════════════════════════════════"
21
  echo ""
22
 
 
 
 
23
  restore_from_neon() {
24
  if [ -z "$NEON_PASSWORD" ] || [ -z "$NEON_HOST" ]; then
25
  log "⚠️ Neon not configured - skipping restore"
 
28
 
29
  log "📥 Restoring from Neon..."
30
 
 
31
  PGPASSWORD="$NEON_PASSWORD" psql "postgresql://${NEON_USER}@${NEON_HOST}/${NEON_DB}?sslmode=require" \
32
+ -A -t -q -c "SELECT sql_content_b64 FROM flowise_backups ORDER BY backup_date DESC LIMIT 1;" \
33
+ > /tmp/backup_b64.txt 2>/dev/null || true
34
 
35
+ if [ ! -f /tmp/backup_b64.txt ]; then
36
  log "ℹ️ No backup found (first run)"
37
  return 0
38
  fi
39
 
40
+ FILE_SIZE=$(wc -c < /tmp/backup_b64.txt 2>/dev/null || echo "0")
 
 
41
 
42
+ if [ "$FILE_SIZE" -lt 100 ]; then
 
 
43
  log "ℹ️ No valid backup found"
44
+ rm -f /tmp/backup_b64.txt 2>/dev/null || true
45
  return 0
46
  fi
47
 
 
48
  mkdir -p /data/.flowise
49
  rm -f "$DB_PATH" "${DB_PATH}-shm" "${DB_PATH}-wal" 2>/dev/null || true
50
 
51
+ base64 -d /tmp/backup_b64.txt > /tmp/neon_backup.sql 2>/dev/null || true
52
  sqlite3 "$DB_PATH" < /tmp/neon_backup.sql 2>/dev/null || true
53
 
54
  if [ -f "$DB_PATH" ]; then
 
66
  log_error "Database restore failed"
67
  fi
68
 
69
+ rm -f /tmp/backup_b64.txt /tmp/neon_backup.sql 2>/dev/null || true
70
  }
71
 
 
 
 
72
  start_flowise() {
73
  log "🚀 Starting Flowise..."
74
 
 
75
  npx flowise start --PORT=7860 2>&1 | grep -E "^\[|ERROR|error|Error" &
76
  FLOWISE_PID=$!
77
 
 
78
  local attempts=0
79
  local max_attempts=60
80
 
 
97
  exit 1
98
  }
99
 
 
 
 
100
  start_health_monitor() {
101
  (
102
+ sleep 300
 
103
  while true; do
 
104
  if ! kill -0 $FLOWISE_PID 2>/dev/null; then
105
  log_error "Health: Flowise died - triggering restart"
106
  exit 1
107
  fi
 
 
108
  if ! curl -sf http://localhost:7860 > /dev/null 2>&1; then
109
  log_error "Health: Flowise unresponsive - triggering restart"
110
  kill $FLOWISE_PID 2>/dev/null || true
111
  exit 1
112
  fi
 
113
  sleep $HEALTH_CHECK_INTERVAL
114
  done
115
  ) &
116
  HEALTH_PID=$!
117
  }
118
 
 
 
 
119
  start_restart_timer() {
120
  if [ "$AUTO_RESTART_DAYS" -le 0 ]; then
121
  return 0
122
  fi
 
123
  (
124
  sleep $((AUTO_RESTART_DAYS * 86400))
125
  log "♻️ Scheduled restart ($AUTO_RESTART_DAYS days)"
 
128
  RESTART_PID=$!
129
  }
130
 
 
 
 
131
  start_backup_service() {
132
  if [ -z "$NEON_PASSWORD" ] || [ -z "$NEON_HOST" ]; then
133
  log "⚠️ Neon not configured - backups disabled"
134
  return 0
135
  fi
 
136
  /data/backup.sh &
137
  BACKUP_PID=$!
138
  }
139
 
 
 
 
140
  cleanup() {
141
  log "🛑 Shutting down..."
142
  kill $FLOWISE_PID 2>/dev/null || true
 
148
 
149
  trap cleanup SIGTERM SIGINT
150
 
 
 
 
151
  restore_from_neon
152
  start_flowise
153
  start_backup_service
154
  start_health_monitor
155
  start_restart_timer
156
 
 
157
  INTERVAL_HOURS=$((BACKUP_INTERVAL_SECONDS / 3600))
158
  echo ""
159
  echo "═══════════════════════════════════════"
 
169
  echo "═══════════════════════════════════════"
170
  echo ""
171
 
 
172
  wait $FLOWISE_PID