-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint
More file actions
545 lines (469 loc) · 17.4 KB
/
entrypoint
File metadata and controls
545 lines (469 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/bin/bash
set -e
# Set logging level (default to INFO if not set)
LOGGING="${LOGGING:-INFO}"
# Storage backend type (default to s3 for backward compatibility)
BACKUP_STORAGE_TYPE="${BACKUP_STORAGE_TYPE:-s3}"
# Hardcoded base mount point (internal to container)
BACKUP_MOUNT="/mnt/backup"
# Construct BORG_REPO from relative path (hides /mnt/backup from user config)
# Strip leading slash from BORG_REPO_PATH to prevent absolute paths
BORG_REPO_PATH_CLEAN="${BORG_REPO_PATH:-borgbackup}"
BORG_REPO_PATH_CLEAN="${BORG_REPO_PATH_CLEAN#/}" # Remove leading slash if present
export BORG_REPO="${BACKUP_MOUNT}/${BORG_REPO_PATH_CLEAN}"
# Create mount directory
mkdir -p /mnt/backup
# Logging helper functions
log_info() {
echo "[INFO] $1"
}
log_debug() {
if [ "$LOGGING" = "DEBUG" ]; then
echo "[DEBUG] $1"
fi
}
log_error() {
echo "[ERROR] $1" >&2
}
log_success() {
echo "[SUCCESS] $1"
}
# ============================================================================
# S3 Backend Functions
# ============================================================================
setup_s3_backend() {
log_info "Setting up S3 storage backend..."
# Export AWS credentials as environment variables
export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}"
export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}"
export AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"
# Check if required environment variables are set
if [ -z "$S3_BUCKET_NAME" ]; then
log_error "S3_BUCKET_NAME environment variable is required for S3 backend"
exit 1
fi
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
log_error "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are required for S3 backend"
exit 1
fi
if [ -z "$AWS_DEFAULT_REGION" ]; then
log_error "AWS_DEFAULT_REGION environment variable is required for S3 backend"
exit 1
fi
# Check network connectivity to AWS S3
log_info "Checking network connectivity to AWS S3..."
if ! curl -s -m 10 -I "https://s3.${AWS_DEFAULT_REGION}.amazonaws.com" >/dev/null 2>&1; then
log_error "Cannot reach AWS S3 endpoint: https://s3.${AWS_DEFAULT_REGION}.amazonaws.com"
log_error "Please check your network connection and AWS region setting"
exit 1
fi
log_success "Network connectivity to AWS S3 confirmed"
# Create AWS credentials file for s3fs
echo "${S3_BUCKET_NAME}:${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}" > /etc/passwd-s3fs
chmod 600 /etc/passwd-s3fs
# Create cache directory
mkdir -p /tmp/s3fs_cache
# Configure logging based on LOGGING environment variable
if [ "$LOGGING" = "DEBUG" ]; then
echo "========================================="
echo "Starting S3 Mount with Full Debugging"
echo "========================================="
echo "S3_BUCKET_NAME: ${S3_BUCKET_NAME}"
echo "AWS_ACCESS_KEY_ID: ...${AWS_ACCESS_KEY_ID: -4}"
echo "AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}"
echo "S3 URL: https://s3-${AWS_DEFAULT_REGION}.amazonaws.com"
echo ""
echo "Checking /etc/passwd-s3fs contents:"
echo "$(cat /etc/passwd-s3fs | sed 's/:.*:/:***:/' | sed 's/:.*$/:***/') (credentials masked)"
echo ""
echo "Testing direct S3 connectivity with curl:"
curl -I "https://s3-${AWS_DEFAULT_REGION}.amazonaws.com" 2>&1 | head -5
echo ""
echo "Mounting with full debug options..."
echo "========================================="
# Mount with debug options
s3fs "${S3_BUCKET_NAME}" /mnt/backup \
-o passwd_file=/etc/passwd-s3fs \
-o url="https://s3-${AWS_DEFAULT_REGION}.amazonaws.com" \
-o endpoint="${AWS_DEFAULT_REGION}" \
-o use_path_request_style \
-o allow_other \
-o uid=0 \
-o gid=0 \
-o mp_umask=002 \
-o umask=002 \
-o kernel_cache \
-o max_stat_cache_size=100000 \
-o dbglevel=info \
-o curldbg \
-f 2>&1 | tee /tmp/s3fs_debug.log &
else
log_info "Mounting S3 bucket '${S3_BUCKET_NAME}'..."
# Mount without debug options but still in foreground for process monitoring
s3fs "${S3_BUCKET_NAME}" /mnt/backup \
-o passwd_file=/etc/passwd-s3fs \
-o url="https://s3-${AWS_DEFAULT_REGION}.amazonaws.com" \
-o endpoint="${AWS_DEFAULT_REGION}" \
-o use_path_request_style \
-o allow_other \
-o uid=0 \
-o gid=0 \
-o mp_umask=002 \
-o umask=002 \
-o kernel_cache \
-o max_stat_cache_size=100000 \
-f >/dev/null 2>&1 &
fi
S3FS_PID=$!
# Wait for s3fs to initialize (poll instead of fixed sleep)
log_debug "Waiting for s3fs to initialize..."
for i in $(seq 1 30); do
# Check if process died
if ! kill -0 $S3FS_PID 2>/dev/null; then
log_error "s3fs process died during initialization"
if [ "$LOGGING" = "DEBUG" ] && [ -f /tmp/s3fs_debug.log ]; then
echo "Last 20 lines of debug log:"
tail -20 /tmp/s3fs_debug.log
fi
exit 1
fi
# Check if mount is ready
if mountpoint -q /mnt/backup 2>/dev/null; then
log_debug "s3fs mount detected after ${i}s (PID: $S3FS_PID)"
break
fi
if [ "$i" -eq 30 ]; then
log_error "s3fs mount timeout after 30 seconds"
kill $S3FS_PID 2>/dev/null
exit 1
fi
sleep 1
done
# Verify mount
verify_s3_mount
}
verify_s3_mount() {
if [ "$LOGGING" = "DEBUG" ]; then
echo "========================================="
echo "Mount Verification"
echo "========================================="
echo "Checking mount status..."
if mount | grep -q "/mnt/backup"; then
echo "✓ Mount point found in /etc/mtab"
echo "Testing mount functionality..."
echo "Running: ls -la /mnt/backup/"
if ls -la /mnt/backup/ 2>&1; then
echo "✓ ls command succeeded"
echo "Directory contents:"
ls -la /mnt/backup/
echo ""
echo "Running: stat /mnt/backup/"
stat /mnt/backup/ 2>&1
echo ""
echo "Running: df -h /mnt/backup/"
df -h /mnt/backup/ 2>&1
echo ""
echo "Checking s3fs process details:"
ps aux | grep s3fs | grep -v grep
else
log_error "Cannot list directory contents"
echo "Error details:"
ls -la /mnt/backup/ 2>&1
echo ""
echo "Checking s3fs debug log:"
tail -50 /tmp/s3fs_debug.log
echo ""
echo "Testing direct S3 access with authentication:"
curl -H "Host: ${S3_BUCKET_NAME}.s3-${AWS_DEFAULT_REGION}.amazonaws.com" \
-I "https://s3-${AWS_DEFAULT_REGION}.amazonaws.com/" 2>&1 | head -10
fi
else
log_error "Mount point NOT found in /etc/mtab"
echo "Checking for s3fs processes..."
pgrep -f s3fs || echo "✗ No s3fs processes running"
exit 1
fi
else
if mount | grep -q "/mnt/backup"; then
if ls /mnt/backup/ >/dev/null 2>&1; then
log_success "S3 bucket mounted successfully"
else
log_error "S3 bucket mount failed - cannot access /mnt/backup"
exit 1
fi
else
log_error "S3 bucket mount failed"
exit 1
fi
fi
}
cleanup_s3_backend() {
log_info "Starting S3 bucket cleanup..."
if ! mount | grep -q "/mnt/backup"; then
log_info "S3 bucket not mounted, skipping unmount"
else
log_info "Attempting normal unmount..."
if timeout 10s umount /mnt/backup 2>/dev/null; then
log_info "Normal unmount successful"
else
log_info "Normal unmount timed out or failed, trying lazy unmount..."
if umount -l /mnt/backup 2>/dev/null; then
log_info "Lazy unmount initiated"
sleep 2
if mount | grep -q "/mnt/backup"; then
log_info "Lazy unmount didn't complete, trying force unmount..."
umount -f /mnt/backup 2>/dev/null || log_info "Force unmount failed"
fi
else
log_info "Lazy unmount failed, trying force unmount..."
umount -f /mnt/backup 2>/dev/null || log_info "Force unmount failed"
fi
fi
if mount | grep -q "/mnt/backup"; then
log_info "WARNING: Could not unmount /mnt/backup - mount may persist"
else
log_success "S3 bucket successfully unmounted"
fi
fi
log_info "Cleaning up temporary files..."
rm -f /etc/passwd-s3fs
rm -rf /tmp/s3fs_cache
log_info "S3 cleanup completed"
}
# ============================================================================
# USB Backend Functions
# ============================================================================
setup_usb_backend() {
log_info "Setting up USB storage backend..."
# Check if required environment variables are set
if [ -z "$USB_DEVICE_UUID" ]; then
log_error "USB_DEVICE_UUID environment variable is required for USB backend"
log_error "Find your USB device UUID with: sudo blkid"
exit 1
fi
log_debug "Looking for USB device with UUID: ${USB_DEVICE_UUID}"
# Find device by UUID using blkid
USB_DEVICE=$(blkid -U "$USB_DEVICE_UUID" 2>/dev/null)
if [ -z "$USB_DEVICE" ]; then
log_error "USB device with UUID '${USB_DEVICE_UUID}' not found!"
log_error "Please ensure the USB device is connected."
log_error "Available block devices:"
blkid 2>/dev/null || lsblk 2>/dev/null || log_error "Cannot list block devices"
exit 1
fi
log_info "Found USB device: ${USB_DEVICE} (UUID: ${USB_DEVICE_UUID})"
# Detect filesystem type
USB_FSTYPE=$(blkid -s TYPE -o value "$USB_DEVICE" 2>/dev/null)
log_debug "Filesystem type: ${USB_FSTYPE}"
# Mount the USB device
log_info "Mounting USB device to /mnt/backup..."
MOUNT_OPTS="rw"
case "$USB_FSTYPE" in
ntfs|ntfs-3g)
MOUNT_OPTS="rw,uid=0,gid=0"
;;
vfat|fat32)
MOUNT_OPTS="rw,uid=0,gid=0,umask=002"
;;
ext4|ext3|ext2|xfs|btrfs)
MOUNT_OPTS="rw"
;;
*)
log_info "Unknown filesystem type '${USB_FSTYPE}', using default mount options"
;;
esac
if mount -o "$MOUNT_OPTS" "$USB_DEVICE" /mnt/backup 2>&1; then
log_success "USB device mounted successfully"
else
log_error "Failed to mount USB device"
exit 1
fi
# Verify mount
verify_usb_mount
}
verify_usb_mount() {
if mount | grep -q "/mnt/backup"; then
if [ "$LOGGING" = "DEBUG" ]; then
echo "========================================="
echo "USB Mount Verification"
echo "========================================="
echo "Mount details:"
mount | grep "/mnt/backup"
echo ""
echo "Disk space:"
df -h /mnt/backup
echo ""
echo "Directory contents:"
ls -la /mnt/backup/
echo "========================================="
fi
# Test write access
if touch /mnt/backup/.write_test 2>/dev/null; then
rm -f /mnt/backup/.write_test
log_success "USB mount verified - read/write access confirmed"
else
log_error "USB mount is read-only or not writable"
exit 1
fi
else
log_error "USB device not mounted at /mnt/backup"
exit 1
fi
}
cleanup_usb_backend() {
log_info "Starting USB cleanup..."
if ! mount | grep -q "/mnt/backup"; then
log_info "USB device not mounted, skipping unmount"
else
# Sync filesystem before unmount
log_info "Syncing filesystem..."
sync
log_info "Unmounting USB device..."
if umount /mnt/backup 2>/dev/null; then
log_success "USB device unmounted successfully"
else
log_info "Normal unmount failed, trying lazy unmount..."
if umount -l /mnt/backup 2>/dev/null; then
log_info "Lazy unmount initiated"
else
log_info "Lazy unmount failed"
fi
fi
fi
log_info "USB cleanup completed"
}
# ============================================================================
# Local Path Backend Functions (for pre-mounted paths)
# ============================================================================
setup_local_backend() {
log_info "Setting up local storage backend..."
# Check if required environment variables are set
if [ -z "$LOCAL_BACKUP_PATH" ]; then
log_error "LOCAL_BACKUP_PATH environment variable is required for local backend"
exit 1
fi
# For local backend, the path is bind-mounted via docker-compose
# We just need to verify it's accessible
if [ ! -d "/mnt/backup" ]; then
log_error "Local backup path not mounted at /mnt/backup"
log_error "Ensure LOCAL_BACKUP_PATH is correctly configured in docker-compose.yml"
exit 1
fi
verify_local_mount
}
verify_local_mount() {
if [ -d "/mnt/backup" ]; then
if [ "$LOGGING" = "DEBUG" ]; then
echo "========================================="
echo "Local Mount Verification"
echo "========================================="
echo "Disk space:"
df -h /mnt/backup
echo ""
echo "Directory contents:"
ls -la /mnt/backup/
echo "========================================="
fi
# Test write access
if touch /mnt/backup/.write_test 2>/dev/null; then
rm -f /mnt/backup/.write_test
log_success "Local path verified - read/write access confirmed"
else
log_error "Local path is read-only or not writable"
exit 1
fi
else
log_error "Local path /mnt/backup does not exist"
exit 1
fi
}
cleanup_local_backend() {
log_info "Local backend cleanup (no unmount needed)..."
sync
log_info "Local cleanup completed"
}
# ============================================================================
# Main Setup Function
# ============================================================================
setup_storage_backend() {
echo "========================================="
echo "BorgBackup Storage Backend Setup"
echo "========================================="
echo "Storage Type: ${BACKUP_STORAGE_TYPE}"
echo ""
# Validate BORG_PASSPHRASE is set
if [ -z "${BORG_PASSPHRASE}" ]; then
log_error "BORG_PASSPHRASE environment variable is required but not set"
log_error "Please set BORG_PASSPHRASE in your .env file"
exit 1
fi
case "$BACKUP_STORAGE_TYPE" in
s3)
setup_s3_backend
;;
usb)
setup_usb_backend
;;
local)
setup_local_backend
;;
*)
log_error "Unknown storage backend type: ${BACKUP_STORAGE_TYPE}"
log_error "Supported types: s3, usb, local"
exit 1
;;
esac
log_success "Storage backend '${BACKUP_STORAGE_TYPE}' setup completed"
}
# ============================================================================
# Cleanup Function
# ============================================================================
cleanup() {
echo ""
echo "========================================="
echo "Starting cleanup process..."
echo "========================================="
case "$BACKUP_STORAGE_TYPE" in
s3)
cleanup_s3_backend
;;
usb)
cleanup_usb_backend
;;
local)
cleanup_local_backend
;;
esac
echo "Cleanup completed"
exit 0
}
# ============================================================================
# Main Execution
# ============================================================================
# Setup storage backend
setup_storage_backend
# Check if we're in an interactive session
if [ -t 0 ] && [ -t 1 ]; then
echo "Interactive session detected"
IS_INTERACTIVE=true
else
echo "Non-interactive session detected"
IS_INTERACTIVE=false
fi
# Set up signal handlers for graceful shutdown (only for non-interactive)
if [ "$IS_INTERACTIVE" = false ]; then
trap cleanup SIGINT SIGTERM
fi
# Execute the main command
if [ "$IS_INTERACTIVE" = true ]; then
echo "Starting interactive shell - storage backend will remain mounted"
exec "$@"
else
echo "Running command in non-interactive mode"
exec "$@" &
MAIN_PID=$!
# Wait for main process
wait $MAIN_PID
# Cleanup on exit
cleanup
fi