jualhosting commited on
Commit
002a6d2
·
verified ·
1 Parent(s): ca04d92

Upload 4 files

Browse files
Files changed (1) hide show
  1. entrypoint.sh +69 -12
entrypoint.sh CHANGED
@@ -95,27 +95,84 @@ echo "Syncing files from Google Drive..."
95
  rclone copy "$REMOTE_PATH" "$DATA_DIR" --config "$CONFIG_PATH" --drive-skip-shortcuts --drive-acknowledge-abuse $SHARED_FLAG || true
96
 
97
  # 3. Start background sync loops
98
- # Loop A: Sync files in /data to Google Drive when modified
99
  (
100
  echo "Starting file sync daemon..."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  while true; do
102
- # Monitor /data for write, create, delete, or move events
103
- inotifywait -r -e modify,create,delete,move "$DATA_DIR"
104
- sleep 5
105
- echo "Changes detected in $DATA_DIR, copying to Google Drive ($REMOTE_PATH)..."
106
- rclone copy "$DATA_DIR" "$REMOTE_PATH" --config "$CONFIG_PATH" --drive-skip-shortcuts --drive-acknowledge-abuse $SHARED_FLAG
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  done
108
  ) &
109
 
110
- # Loop B: Sync database /tmp/filebrowser.db to Google Drive when modified
111
  (
112
  echo "Starting database sync daemon..."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  while true; do
114
- # Monitor SQLite database file for modification events
115
- inotifywait -e modify "$DB_PATH"
116
- sleep 2
117
- echo "Database modified, uploading to Google Drive ($REMOTE_PATH)..."
118
- rclone copyto "$DB_PATH" "$REMOTE_PATH/.filebrowser.db" --config "$CONFIG_PATH" $SHARED_FLAG
 
 
 
 
 
 
 
 
 
 
 
 
119
  done
120
  ) &
121
 
 
95
  rclone copy "$REMOTE_PATH" "$DATA_DIR" --config "$CONFIG_PATH" --drive-skip-shortcuts --drive-acknowledge-abuse $SHARED_FLAG || true
96
 
97
  # 3. Start background sync loops
98
+ # Loop A: Sync files in /data to Google Drive when modified (Debounced & Robust)
99
  (
100
  echo "Starting file sync daemon..."
101
+ LAST_EVENT_FILE="/tmp/last_event_time"
102
+ PENDING_FILE="/tmp/sync_pending"
103
+ DEBOUNCE_SECS=10
104
+
105
+ # Clear any old state
106
+ rm -f "$LAST_EVENT_FILE" "$PENDING_FILE"
107
+
108
+ # Start inotifywait in monitor mode to ensure we don't miss any events while syncing
109
+ inotifywait -r -m -e close_write,delete,move "$DATA_DIR" 2>/dev/null | while read -r _; do
110
+ date +%s > "$LAST_EVENT_FILE"
111
+ if [ ! -f "$PENDING_FILE" ]; then
112
+ touch "$PENDING_FILE"
113
+ fi
114
+ done &
115
+
116
+ # Background runner that waits for a quiet period before syncing
117
  while true; do
118
+ if [ -f "$PENDING_FILE" ]; then
119
+ last_time=$(cat "$LAST_EVENT_FILE" 2>/dev/null || echo 0)
120
+ now=$(date +%s)
121
+ elapsed=$((now - last_time))
122
+ if [ "$elapsed" -ge "$DEBOUNCE_SECS" ]; then
123
+ rm -f "$PENDING_FILE"
124
+ echo "Changes detected in $DATA_DIR, syncing to Google Drive ($REMOTE_PATH)..."
125
+ if rclone copy "$DATA_DIR" "$REMOTE_PATH" --config "$CONFIG_PATH" --drive-skip-shortcuts --drive-acknowledge-abuse $SHARED_FLAG; then
126
+ echo "File sync successful."
127
+ else
128
+ echo "File sync failed or partially completed (e.g. active uploads). Will retry..."
129
+ # Set last event to now to debounce the next retry
130
+ date +%s > "$LAST_EVENT_FILE"
131
+ touch "$PENDING_FILE"
132
+ fi
133
+ fi
134
+ fi
135
+ sleep 2
136
  done
137
  ) &
138
 
139
+ # Loop B: Sync database /tmp/filebrowser.db to Google Drive when modified (Debounced & Robust)
140
  (
141
  echo "Starting database sync daemon..."
142
+ DB_LAST_EVENT="/tmp/db_last_event"
143
+ DB_PENDING="/tmp/db_sync_pending"
144
+ DEBOUNCE_SECS=3
145
+
146
+ # Clear any old state
147
+ rm -f "$DB_LAST_EVENT" "$DB_PENDING"
148
+
149
+ # Start inotifywait in monitor mode
150
+ inotifywait -m -e modify "$DB_PATH" 2>/dev/null | while read -r _; do
151
+ date +%s > "$DB_LAST_EVENT"
152
+ if [ ! -f "$DB_PENDING" ]; then
153
+ touch "$DB_PENDING"
154
+ fi
155
+ done &
156
+
157
+ # Background runner for database sync
158
  while true; do
159
+ if [ -f "$DB_PENDING" ]; then
160
+ last_time=$(cat "$DB_LAST_EVENT" 2>/dev/null || echo 0)
161
+ now=$(date +%s)
162
+ elapsed=$((now - last_time))
163
+ if [ "$elapsed" -ge "$DEBOUNCE_SECS" ]; then
164
+ rm -f "$DB_PENDING"
165
+ echo "Database modified, uploading to Google Drive ($REMOTE_PATH)..."
166
+ if rclone copyto "$DB_PATH" "$REMOTE_PATH/.filebrowser.db" --config "$CONFIG_PATH" $SHARED_FLAG; then
167
+ echo "Database sync successful."
168
+ else
169
+ echo "Database sync failed. Will retry..."
170
+ date +%s > "$DB_LAST_EVENT"
171
+ touch "$DB_PENDING"
172
+ fi
173
+ fi
174
+ fi
175
+ sleep 1
176
  done
177
  ) &
178