-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
1085 lines (909 loc) · 36.6 KB
/
backup.sh
File metadata and controls
1085 lines (909 loc) · 36.6 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# =============================================================================
# Database Backup Script V7 - PRODUCTION GRADE WITH 15-MIN OPTIMIZATION
# PostgreSQL 15 + MongoDB 4.4 + MariaDB 11.4 + Redis 8.2.2
#
# ✅ ALL DATABASES BACKUP (Not just defaultdb)
# ✅ Individual database backup support
# ✅ Backup verification
# ✅ Lock mechanism (prevents concurrent runs)
# ✅ Fast compression for frequent backups
# ✅ Detailed logging
# ✅ Container health checking
# =============================================================================
# =============================================================================
# Yapılandırma — .env dosyasından oku ya da ortam değişkenlerinden al
# =============================================================================
# Stack root dizini (compose ile aynı yer). DB_BASE_DIR ile override edilebilir.
DB_BASE_DIR="${DB_BASE_DIR:-/opt/databases}"
BACKUP_DIR="${BACKUP_DIR:-${DB_BASE_DIR}/backups}"
LOG_DIR="${LOG_DIR:-${DB_BASE_DIR}/logs}"
DATE=$(date +%Y%m%d_%H%M%S)
# DB_PASSWORD — .env'den oku ya da ortamdan al
if [ -z "${DB_PASSWORD}" ] && [ -f "${DB_BASE_DIR}/.env" ]; then
# shellcheck source=/dev/null
DB_PASSWORD=$(grep -E '^DB_PASSWORD=' "${DB_BASE_DIR}/.env" | cut -d= -f2-)
fi
DB_PASSWORD="${DB_PASSWORD:?DB_PASSWORD ortam değişkeni veya .env dosyasında tanımlı olmalı}"
LOG_FILE="$LOG_DIR/backup_$(date +%Y%m%d).log"
RETENTION_DAYS=7 # 15 dk'da bir backup = 7 gün yeterli
MAX_BACKUP_SIZE="50G"
COMPRESSION_LEVEL=1 # 1=fast, 9=best (15 dk için 1 önerilir)
# Lock mechanism
LOCK_FILE="/tmp/db_backup.lock"
LOCK_TIMEOUT=840 # 14 dakika (15 dk cycle için)
# Renk kodları
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# =============================================================================
# LOCK MECHANISM - Prevents concurrent backups
# =============================================================================
acquire_lock() {
if [ -f "$LOCK_FILE" ]; then
local lock_pid=$(cat "$LOCK_FILE" 2>/dev/null)
local lock_age=$(( $(date +%s) - $(stat -c %Y "$LOCK_FILE" 2>/dev/null || echo 0) ))
# Process hala çalışıyor mu kontrol et
if [ -n "$lock_pid" ] && kill -0 "$lock_pid" 2>/dev/null; then
# Lock timeout kontrolü
if [ "$lock_age" -gt "$LOCK_TIMEOUT" ]; then
log "WARNING" "Stale lock detected (${lock_age}s old), removing..."
rm -f "$LOCK_FILE"
else
log "ERROR" "Another backup is running (PID: $lock_pid, Age: ${lock_age}s)"
log "ERROR" "If this is incorrect, remove: $LOCK_FILE"
exit 1
fi
else
log "WARNING" "Orphaned lock file found (PID $lock_pid not running), removing..."
rm -f "$LOCK_FILE"
fi
fi
# Lock dosyasını oluştur
echo $$ > "$LOCK_FILE"
trap cleanup EXIT INT TERM
log "INFO" "Lock acquired (PID: $$)"
}
release_lock() {
rm -f "$LOCK_FILE"
log "INFO" "Lock released"
}
cleanup() {
release_lock
}
# =============================================================================
# DIRECTORIES & LOGGING
# =============================================================================
mkdir -p "$BACKUP_DIR"/{mariadb,postgresql,mongodb,redis}/{full,incremental,single}
mkdir -p "$LOG_DIR"
# Logging function with colors
log() {
local level=$1
shift
local message="$@"
local timestamp="[$(date '+%Y-%m-%d %H:%M:%S')]"
case $level in
"INFO")
echo -e "${BLUE}${timestamp} [INFO]${NC} $message" | tee -a "$LOG_FILE"
;;
"SUCCESS")
echo -e "${GREEN}${timestamp} [SUCCESS]${NC} $message" | tee -a "$LOG_FILE"
;;
"WARNING")
echo -e "${YELLOW}${timestamp} [WARNING]${NC} $message" | tee -a "$LOG_FILE"
;;
"ERROR")
echo -e "${RED}${timestamp} [ERROR]${NC} $message" | tee -a "$LOG_FILE"
;;
"DEBUG")
echo -e "${CYAN}${timestamp} [DEBUG]${NC} $message" | tee -a "$LOG_FILE"
;;
*)
echo -e "${timestamp} $level $message" | tee -a "$LOG_FILE"
;;
esac
}
# =============================================================================
# UTILITY FUNCTIONS
# =============================================================================
# Check disk space
check_disk_space() {
local available=$(df -h "$BACKUP_DIR" | awk 'NR==2 {print $4}')
local available_kb=$(df "$BACKUP_DIR" | awk 'NR==2 {print $4}')
log "INFO" "Available disk space: $available"
if [ "$available_kb" -lt 5242880 ]; then # 5GB in KB - Critical
log "ERROR" "CRITICAL: Less than 5GB available! Backup aborted."
return 1
elif [ "$available_kb" -lt 10485760 ]; then # 10GB in KB - Warning
log "WARNING" "Low disk space! Less than 10GB available"
fi
return 0
}
# Check if container is running
check_container() {
local container=$1
if ! docker ps --format '{{.Names}}' | grep -q "^${container}$"; then
log "ERROR" "Container '$container' is not running!"
return 1
fi
return 0
}
# Calculate duration
format_duration() {
local seconds=$1
local minutes=$((seconds / 60))
local secs=$((seconds % 60))
echo "${minutes}m ${secs}s"
}
# =============================================================================
# DATABASE LIST FUNCTIONS
# =============================================================================
# Get list of all databases from MariaDB
get_mariadb_databases() {
docker exec mariadb mariadb -u root -p$DB_PASSWORD -N -e "SHOW DATABASES;" 2>/dev/null | \
grep -Ev "^(information_schema|performance_schema|mysql|sys)$" || \
docker exec mariadb mysql -u root -p$DB_PASSWORD -N -e "SHOW DATABASES;" 2>/dev/null | \
grep -Ev "^(information_schema|performance_schema|mysql|sys)$"
}
# Get list of all PostgreSQL databases
get_postgresql_databases() {
docker exec postgresql psql -U root -d postgres -t -c \
"SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres';" 2>/dev/null | \
grep -v "^$" | xargs
}
# Get list of all MongoDB databases
get_mongodb_databases() {
docker exec mongodb mongo -u root -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval \
"db.adminCommand('listDatabases').databases.forEach(function(d) { print(d.name); })" 2>/dev/null | \
grep -v "^$" | grep -Ev "^(admin|config|local)$"
}
# =============================================================================
# MARIADB BACKUP FUNCTIONS
# =============================================================================
# Backup ALL MariaDB databases
backup_mariadb() {
local backup_type=${1:-"full"}
local backup_name="mariadb_${backup_type}_${DATE}"
local backup_path="$BACKUP_DIR/mariadb/$backup_type"
local start_time=$(date +%s)
log "INFO" "========================================="
log "INFO" "Starting MariaDB FULL BACKUP"
log "INFO" "========================================="
# Pre-checks
check_container "mariadb" || return 1
check_disk_space || return 1
# Get all databases
local databases=$(get_mariadb_databases)
local db_count=$(echo "$databases" | wc -l)
log "INFO" "Found $db_count databases to backup"
echo "$databases" | while read -r db; do
[ -n "$db" ] && log "INFO" " - $db"
done
# Create backup with optimized options
log "INFO" "Creating comprehensive backup..."
docker exec mariadb mariadb-dump \
-u root \
-p$DB_PASSWORD \
--all-databases \
--single-transaction \
--quick \
--lock-tables=false \
--routines \
--triggers \
--events \
--hex-blob \
--add-drop-database \
--add-drop-table \
--complete-insert \
--skip-comments \
2>> "$LOG_FILE" | gzip -${COMPRESSION_LEVEL} > "$backup_path/${backup_name}.sql.gz"
local exit_code=${PIPESTATUS[0]}
if [ $exit_code -eq 0 ] && [ -s "$backup_path/${backup_name}.sql.gz" ]; then
local size=$(du -h "$backup_path/${backup_name}.sql.gz" | cut -f1)
local duration=$(( $(date +%s) - start_time ))
log "SUCCESS" "✓ MariaDB backup completed"
log "INFO" " Size: $size | Duration: $(format_duration $duration)"
log "INFO" " Location: $backup_path/${backup_name}.sql.gz"
log "INFO" " Databases: $db_count"
# Verify backup integrity
verify_backup "$backup_path/${backup_name}.sql.gz"
return 0
else
log "ERROR" "✗ MariaDB backup failed (exit code: $exit_code)"
rm -f "$backup_path/${backup_name}.sql.gz"
return 1
fi
}
# Backup SINGLE MariaDB database
backup_mariadb_single() {
local db_name=$1
local backup_name="mariadb_${db_name}_${DATE}"
local backup_path="$BACKUP_DIR/mariadb/single"
if [ -z "$db_name" ]; then
log "ERROR" "Database name required for single backup"
echo "Usage: $0 mariadb-single <database_name>"
return 1
fi
check_container "mariadb" || return 1
log "INFO" "Creating backup for database: $db_name"
docker exec mariadb mariadb-dump \
-u root \
-p$DB_PASSWORD \
--single-transaction \
--quick \
--routines \
--triggers \
--events \
"$db_name" \
2>> "$LOG_FILE" | gzip -${COMPRESSION_LEVEL} > "$backup_path/${backup_name}.sql.gz"
if [ ${PIPESTATUS[0]} -eq 0 ] && [ -s "$backup_path/${backup_name}.sql.gz" ]; then
local size=$(du -h "$backup_path/${backup_name}.sql.gz" | cut -f1)
log "SUCCESS" "✓ Database '$db_name' backed up - Size: $size"
return 0
else
log "ERROR" "✗ Failed to backup database '$db_name'"
rm -f "$backup_path/${backup_name}.sql.gz"
return 1
fi
}
# =============================================================================
# POSTGRESQL BACKUP FUNCTIONS
# =============================================================================
backup_postgresql() {
local backup_type=${1:-"full"}
local backup_name="postgresql_${backup_type}_${DATE}"
local backup_path="$BACKUP_DIR/postgresql/$backup_type"
local start_time=$(date +%s)
log "INFO" "========================================="
log "INFO" "Starting PostgreSQL FULL BACKUP"
log "INFO" "========================================="
check_container "postgresql" || return 1
check_disk_space || return 1
# Get all databases
local databases=$(get_postgresql_databases)
local db_count=$(echo "$databases" | wc -w)
log "INFO" "Found $db_count databases to backup"
echo "$databases" | tr ' ' '\n' | while read -r db; do
[ -n "$db" ] && log "INFO" " - $db"
done
# Create comprehensive backup
log "INFO" "Creating comprehensive backup..."
docker exec postgresql pg_dumpall -U root \
--clean \
--if-exists \
--quote-all-identifiers \
2>> "$LOG_FILE" | gzip -${COMPRESSION_LEVEL} > "$backup_path/${backup_name}.sql.gz"
local exit_code=${PIPESTATUS[0]}
if [ $exit_code -eq 0 ] && [ -s "$backup_path/${backup_name}.sql.gz" ]; then
local size=$(du -h "$backup_path/${backup_name}.sql.gz" | cut -f1)
local duration=$(( $(date +%s) - start_time ))
log "SUCCESS" "✓ PostgreSQL backup completed"
log "INFO" " Size: $size | Duration: $(format_duration $duration)"
log "INFO" " Location: $backup_path/${backup_name}.sql.gz"
log "INFO" " Databases: $db_count"
verify_backup "$backup_path/${backup_name}.sql.gz"
return 0
else
log "ERROR" "✗ PostgreSQL backup failed (exit code: $exit_code)"
rm -f "$backup_path/${backup_name}.sql.gz"
return 1
fi
}
# =============================================================================
# MONGODB BACKUP FUNCTIONS
# =============================================================================
backup_mongodb() {
local backup_type=${1:-"full"}
local backup_name="mongodb_${backup_type}_${DATE}"
local backup_path="$BACKUP_DIR/mongodb/$backup_type"
local start_time=$(date +%s)
log "INFO" "========================================="
log "INFO" "Starting MongoDB FULL BACKUP"
log "INFO" "========================================="
check_container "mongodb" || return 1
check_disk_space || return 1
# Get all databases
local databases=$(get_mongodb_databases)
local db_count=$(echo "$databases" | wc -l)
log "INFO" "Found $db_count user databases to backup"
echo "$databases" | while read -r db; do
[ -n "$db" ] && log "INFO" " - $db"
done
# Clean temp directory
docker exec mongodb rm -rf /tmp/backup 2>/dev/null
# Create backup with mongodump
log "INFO" "Creating comprehensive backup..."
docker exec mongodb mongodump \
--host localhost \
--port 27017 \
--username root \
--password $DB_PASSWORD \
--authenticationDatabase admin \
--out /tmp/backup \
--gzip \
--quiet \
2>> "$LOG_FILE"
if [ $? -eq 0 ]; then
# Create tar archive
docker exec mongodb tar -cf - -C /tmp backup 2>/dev/null | \
gzip -${COMPRESSION_LEVEL} > "$backup_path/${backup_name}.tar.gz"
if [ $? -eq 0 ] && [ -s "$backup_path/${backup_name}.tar.gz" ]; then
docker exec mongodb rm -rf /tmp/backup 2>/dev/null
local size=$(du -h "$backup_path/${backup_name}.tar.gz" | cut -f1)
local duration=$(( $(date +%s) - start_time ))
log "SUCCESS" "✓ MongoDB backup completed"
log "INFO" " Size: $size | Duration: $(format_duration $duration)"
log "INFO" " Location: $backup_path/${backup_name}.tar.gz"
log "INFO" " Databases: $db_count"
verify_backup "$backup_path/${backup_name}.tar.gz"
return 0
fi
fi
log "ERROR" "✗ MongoDB backup failed"
docker exec mongodb rm -rf /tmp/backup 2>/dev/null
rm -f "$backup_path/${backup_name}.tar.gz"
return 1
}
# =============================================================================
# REDIS BACKUP FUNCTIONS
# =============================================================================
backup_redis() {
local backup_type=${1:-"full"}
local backup_name="redis_${backup_type}_${DATE}"
local backup_path="$BACKUP_DIR/redis/$backup_type"
local start_time=$(date +%s)
log "INFO" "========================================="
log "INFO" "Starting Redis BACKUP"
log "INFO" "========================================="
check_container "redis" || return 1
check_disk_space || return 1
# Get Redis info
local db_keys=$(docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning DBSIZE 2>/dev/null | grep -oE '[0-9]+')
log "INFO" "Redis contains $db_keys keys"
# Trigger BGSAVE
log "INFO" "Triggering BGSAVE..."
docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning BGSAVE >> "$LOG_FILE" 2>&1
if [ $? -eq 0 ]; then
# Wait for BGSAVE to complete
log "INFO" "Waiting for BGSAVE to complete..."
local max_wait=30
local waited=0
while [ $waited -lt $max_wait ]; do
local lastsave=$(docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning LASTSAVE 2>/dev/null)
sleep 1
local newsave=$(docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning LASTSAVE 2>/dev/null)
if [ "$lastsave" != "$newsave" ] || [ $waited -gt 5 ]; then
break
fi
((waited++))
done
# Copy dump.rdb
docker exec redis cat /data/dump.rdb 2>/dev/null | \
gzip -${COMPRESSION_LEVEL} > "$backup_path/${backup_name}.rdb.gz"
if [ $? -eq 0 ] && [ -s "$backup_path/${backup_name}.rdb.gz" ]; then
local size=$(du -h "$backup_path/${backup_name}.rdb.gz" | cut -f1)
local duration=$(( $(date +%s) - start_time ))
log "SUCCESS" "✓ Redis backup completed"
log "INFO" " Size: $size | Duration: $(format_duration $duration)"
log "INFO" " Location: $backup_path/${backup_name}.rdb.gz"
log "INFO" " Keys: $db_keys"
verify_backup "$backup_path/${backup_name}.rdb.gz"
return 0
fi
fi
log "ERROR" "✗ Redis backup failed"
rm -f "$backup_path/${backup_name}.rdb.gz"
return 1
}
# =============================================================================
# BACKUP ALL DATABASES
# =============================================================================
backup_all() {
acquire_lock
log "INFO" "========================================="
log "INFO" "Starting FULL BACKUP of ALL DATABASES"
log "INFO" "========================================="
log "INFO" "Backup directory: $BACKUP_DIR"
log "INFO" "Timestamp: $DATE"
log "INFO" "Compression level: $COMPRESSION_LEVEL"
log "INFO" ""
local start_time=$(date +%s)
local success_count=0
local fail_count=0
# MariaDB
backup_mariadb "full" && ((success_count++)) || ((fail_count++))
echo ""
# PostgreSQL
backup_postgresql "full" && ((success_count++)) || ((fail_count++))
echo ""
# MongoDB
backup_mongodb "full" && ((success_count++)) || ((fail_count++))
echo ""
# Redis
backup_redis "full" && ((success_count++)) || ((fail_count++))
# Summary
local end_time=$(date +%s)
local duration=$((end_time - start_time))
echo ""
log "INFO" "========================================="
log "INFO" "BACKUP SUMMARY"
log "INFO" "========================================="
log "INFO" "Total time: $(format_duration $duration)"
log "SUCCESS" "Successful backups: $success_count"
[ $fail_count -gt 0 ] && log "ERROR" "Failed backups: $fail_count" || log "INFO" "Failed backups: $fail_count"
# Calculate total backup size
local total_size=$(du -sh "$BACKUP_DIR" 2>/dev/null | cut -f1)
local available=$(df -h "$BACKUP_DIR" | awk 'NR==2 {print $4}')
log "INFO" "Total backup size: $total_size"
log "INFO" "Available space: $available"
log "INFO" "========================================="
return $fail_count
}
# =============================================================================
# BACKUP VERIFICATION
# =============================================================================
verify_backup() {
local backup_file=$1
if [ -z "$backup_file" ] || [ ! -f "$backup_file" ]; then
log "ERROR" "Backup file not found: $backup_file"
return 1
fi
log "INFO" "Verifying backup integrity..."
if [[ $backup_file == *.tar.gz ]]; then
if tar -tzf "$backup_file" > /dev/null 2>&1; then
log "SUCCESS" "✓ Backup integrity verified (tar.gz)"
return 0
fi
elif [[ $backup_file == *.gz ]]; then
if gzip -t "$backup_file" 2>&1; then
log "SUCCESS" "✓ Backup integrity verified (gz)"
return 0
fi
else
log "INFO" "✓ Backup file exists (uncompressed)"
return 0
fi
log "ERROR" "✗ Backup file is corrupted!"
return 1
}
# =============================================================================
# RESTORE FUNCTIONS
# =============================================================================
restore_mariadb() {
local backup_file=$1
if [ -z "$backup_file" ]; then
echo "Usage: $0 restore-mariadb <backup_file.sql.gz>"
return 1
fi
if [ ! -f "$backup_file" ]; then
log "ERROR" "Backup file not found: $backup_file"
return 1
fi
log "WARNING" "⚠️ RESTORING MARIADB - This will overwrite existing data!"
log "INFO" "From: $backup_file"
echo ""
read -p "Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
log "INFO" "Restore cancelled"
return 1
fi
log "INFO" "Starting MariaDB restore..."
if [[ $backup_file == *.gz ]]; then
gunzip -c "$backup_file" | docker exec -i mariadb mariadb -u root -p$DB_PASSWORD
else
docker exec -i mariadb mariadb -u root -p$DB_PASSWORD < "$backup_file"
fi
if [ $? -eq 0 ]; then
log "SUCCESS" "✓ MariaDB restore completed successfully"
return 0
else
log "ERROR" "✗ MariaDB restore failed"
return 1
fi
}
restore_postgresql() {
local backup_file=$1
if [ -z "$backup_file" ]; then
echo "Usage: $0 restore-postgresql <backup_file.sql.gz>"
return 1
fi
if [ ! -f "$backup_file" ]; then
log "ERROR" "Backup file not found: $backup_file"
return 1
fi
log "WARNING" "⚠️ RESTORING POSTGRESQL - This will overwrite existing data!"
log "INFO" "From: $backup_file"
echo ""
read -p "Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
log "INFO" "Restore cancelled"
return 1
fi
log "INFO" "Starting PostgreSQL restore..."
if [[ $backup_file == *.gz ]]; then
gunzip -c "$backup_file" | docker exec -i postgresql psql -U root -d postgres
else
docker exec -i postgresql psql -U root -d postgres < "$backup_file"
fi
if [ $? -eq 0 ]; then
log "SUCCESS" "✓ PostgreSQL restore completed successfully"
return 0
else
log "ERROR" "✗ PostgreSQL restore failed"
return 1
fi
}
restore_mongodb() {
local backup_file=$1
if [ -z "$backup_file" ]; then
echo "Usage: $0 restore-mongodb <backup_file.tar.gz>"
return 1
fi
if [ ! -f "$backup_file" ]; then
log "ERROR" "Backup file not found: $backup_file"
return 1
fi
log "WARNING" "⚠️ RESTORING MONGODB - This will overwrite existing data!"
log "INFO" "From: $backup_file"
echo ""
read -p "Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
log "INFO" "Restore cancelled"
return 1
fi
log "INFO" "Starting MongoDB restore..."
# Extract and restore
cat "$backup_file" | docker exec -i mongodb tar -xzf - -C /tmp
if [ $? -eq 0 ]; then
docker exec mongodb mongorestore \
--host localhost \
--port 27017 \
--username root \
--password $DB_PASSWORD \
--authenticationDatabase admin \
--drop \
--gzip \
/tmp/backup
local result=$?
docker exec mongodb rm -rf /tmp/backup
if [ $result -eq 0 ]; then
log "SUCCESS" "✓ MongoDB restore completed successfully"
return 0
fi
fi
log "ERROR" "✗ MongoDB restore failed"
docker exec mongodb rm -rf /tmp/backup 2>/dev/null
return 1
}
restore_redis() {
local backup_file=$1
if [ -z "$backup_file" ]; then
echo "Usage: $0 restore-redis <backup_file.rdb.gz>"
return 1
fi
if [ ! -f "$backup_file" ]; then
log "ERROR" "Backup file not found: $backup_file"
return 1
fi
log "WARNING" "⚠️ RESTORING REDIS - This will overwrite existing data!"
log "INFO" "From: $backup_file"
echo ""
read -p "Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
log "INFO" "Restore cancelled"
return 1
fi
log "INFO" "Starting Redis restore..."
# Stop Redis, replace dump, restart
docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning SHUTDOWN NOSAVE 2>/dev/null
sleep 2
# Backup old dump
docker exec redis mv /data/dump.rdb /data/dump.rdb.old 2>/dev/null
# Restore new dump
if [[ $backup_file == *.gz ]]; then
gunzip -c "$backup_file" | docker exec -i redis sh -c 'cat > /data/dump.rdb'
else
cat "$backup_file" | docker exec -i redis sh -c 'cat > /data/dump.rdb'
fi
# Restart Redis
docker restart redis
sleep 5
# Verify
if docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning PING 2>/dev/null | grep -q "PONG"; then
log "SUCCESS" "✓ Redis restore completed successfully"
return 0
else
log "ERROR" "✗ Redis restore failed - container may not be healthy"
return 1
fi
}
# =============================================================================
# MAINTENANCE FUNCTIONS
# =============================================================================
clean_old_backups() {
local days=${1:-$RETENTION_DAYS}
log "INFO" "========================================="
log "INFO" "Cleaning backups older than $days days..."
log "INFO" "========================================="
local before_size=$(du -sh "$BACKUP_DIR" 2>/dev/null | cut -f1)
# Count files before deletion
local gz_count=$(find "$BACKUP_DIR" -type f -name "*.gz" -mtime +$days 2>/dev/null | wc -l)
local tar_count=$(find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +$days 2>/dev/null | wc -l)
local total_count=$((gz_count + tar_count))
if [ $total_count -gt 0 ]; then
log "INFO" "Found $total_count old backup files"
# List files to be deleted
find "$BACKUP_DIR" -type f \( -name "*.gz" -o -name "*.tar.gz" \) -mtime +$days 2>/dev/null | \
head -10 | while read -r f; do
log "INFO" " Deleting: $(basename $f)"
done
[ $total_count -gt 10 ] && log "INFO" " ... and $((total_count - 10)) more"
# Delete old backups
find "$BACKUP_DIR" -type f -name "*.gz" -mtime +$days -delete 2>/dev/null
find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +$days -delete 2>/dev/null
local after_size=$(du -sh "$BACKUP_DIR" 2>/dev/null | cut -f1)
log "SUCCESS" "✓ Cleaned $total_count old backup file(s)"
log "INFO" "Space before: $before_size"
log "INFO" "Space after: $after_size"
else
log "INFO" "No old backups found to clean"
fi
log "INFO" "========================================="
}
list_backups() {
log "INFO" "========================================="
log "INFO" "AVAILABLE BACKUPS"
log "INFO" "========================================="
log "INFO" "Backup directory: $BACKUP_DIR"
echo ""
# MariaDB backups
echo -e "${BLUE}MariaDB Backups (last 10):${NC}"
find "$BACKUP_DIR/mariadb" -type f -name "*.gz" 2>/dev/null | \
xargs ls -lht 2>/dev/null | head -10 | \
awk '{printf " %s %s %s %s %s\n", $6, $7, $8, $5, $9}'
[ $(find "$BACKUP_DIR/mariadb" -type f -name "*.gz" 2>/dev/null | wc -l) -eq 0 ] && echo " (no backups)"
echo ""
# PostgreSQL backups
echo -e "${BLUE}PostgreSQL Backups (last 10):${NC}"
find "$BACKUP_DIR/postgresql" -type f -name "*.gz" 2>/dev/null | \
xargs ls -lht 2>/dev/null | head -10 | \
awk '{printf " %s %s %s %s %s\n", $6, $7, $8, $5, $9}'
[ $(find "$BACKUP_DIR/postgresql" -type f -name "*.gz" 2>/dev/null | wc -l) -eq 0 ] && echo " (no backups)"
echo ""
# MongoDB backups
echo -e "${BLUE}MongoDB Backups (last 10):${NC}"
find "$BACKUP_DIR/mongodb" -type f -name "*.tar.gz" 2>/dev/null | \
xargs ls -lht 2>/dev/null | head -10 | \
awk '{printf " %s %s %s %s %s\n", $6, $7, $8, $5, $9}'
[ $(find "$BACKUP_DIR/mongodb" -type f -name "*.tar.gz" 2>/dev/null | wc -l) -eq 0 ] && echo " (no backups)"
echo ""
# Redis backups
echo -e "${BLUE}Redis Backups (last 10):${NC}"
find "$BACKUP_DIR/redis" -type f -name "*.gz" 2>/dev/null | \
xargs ls -lht 2>/dev/null | head -10 | \
awk '{printf " %s %s %s %s %s\n", $6, $7, $8, $5, $9}'
[ $(find "$BACKUP_DIR/redis" -type f -name "*.gz" 2>/dev/null | wc -l) -eq 0 ] && echo " (no backups)"
echo ""
# Total size
local total_size=$(du -sh "$BACKUP_DIR" 2>/dev/null | cut -f1)
log "INFO" "Total backup size: $total_size"
log "INFO" "========================================="
}
backup_stats() {
log "INFO" "========================================="
log "INFO" "BACKUP SYSTEM STATISTICS"
log "INFO" "========================================="
# Disk usage
local total_size=$(du -sh "$BACKUP_DIR" 2>/dev/null | cut -f1)
local available=$(df -h "$BACKUP_DIR" | awk 'NR==2 {print $4}')
local used_percent=$(df -h "$BACKUP_DIR" | awk 'NR==2 {print $5}')
log "INFO" "Storage Information:"
log "INFO" " Total backup size: $total_size"
log "INFO" " Available space: $available"
log "INFO" " Disk usage: $used_percent"
echo ""
# Count by type
local mariadb_count=$(find "$BACKUP_DIR/mariadb" -type f -name "*.gz" 2>/dev/null | wc -l)
local postgresql_count=$(find "$BACKUP_DIR/postgresql" -type f -name "*.gz" 2>/dev/null | wc -l)
local mongodb_count=$(find "$BACKUP_DIR/mongodb" -type f -name "*.tar.gz" 2>/dev/null | wc -l)
local redis_count=$(find "$BACKUP_DIR/redis" -type f -name "*.gz" 2>/dev/null | wc -l)
local total_count=$((mariadb_count + postgresql_count + mongodb_count + redis_count))
log "INFO" "Backup Counts:"
log "INFO" " MariaDB: $mariadb_count backups"
log "INFO" " PostgreSQL: $postgresql_count backups"
log "INFO" " MongoDB: $mongodb_count backups"
log "INFO" " Redis: $redis_count backups"
log "INFO" " TOTAL: $total_count backups"
echo ""
# Size by type
local mariadb_size=$(du -sh "$BACKUP_DIR/mariadb" 2>/dev/null | cut -f1)
local postgresql_size=$(du -sh "$BACKUP_DIR/postgresql" 2>/dev/null | cut -f1)
local mongodb_size=$(du -sh "$BACKUP_DIR/mongodb" 2>/dev/null | cut -f1)
local redis_size=$(du -sh "$BACKUP_DIR/redis" 2>/dev/null | cut -f1)
log "INFO" "Size by Type:"
log "INFO" " MariaDB: $mariadb_size"
log "INFO" " PostgreSQL: $postgresql_size"
log "INFO" " MongoDB: $mongodb_size"
log "INFO" " Redis: $redis_size"
echo ""
# Most recent backups
log "INFO" "Most Recent Backups:"
local recent_mariadb=$(find "$BACKUP_DIR/mariadb" -type f -name "*.gz" -printf '%T@ %p\n' 2>/dev/null | sort -nr | head -1 | cut -d' ' -f2)
local recent_postgresql=$(find "$BACKUP_DIR/postgresql" -type f -name "*.gz" -printf '%T@ %p\n' 2>/dev/null | sort -nr | head -1 | cut -d' ' -f2)
local recent_mongodb=$(find "$BACKUP_DIR/mongodb" -type f -name "*.tar.gz" -printf '%T@ %p\n' 2>/dev/null | sort -nr | head -1 | cut -d' ' -f2)
local recent_redis=$(find "$BACKUP_DIR/redis" -type f -name "*.gz" -printf '%T@ %p\n' 2>/dev/null | sort -nr | head -1 | cut -d' ' -f2)
[ -n "$recent_mariadb" ] && log "INFO" " MariaDB: $(ls -lh "$recent_mariadb" 2>/dev/null | awk '{print $6, $7, $8, "-", $5}')"
[ -n "$recent_postgresql" ] && log "INFO" " PostgreSQL: $(ls -lh "$recent_postgresql" 2>/dev/null | awk '{print $6, $7, $8, "-", $5}')"
[ -n "$recent_mongodb" ] && log "INFO" " MongoDB: $(ls -lh "$recent_mongodb" 2>/dev/null | awk '{print $6, $7, $8, "-", $5}')"
[ -n "$recent_redis" ] && log "INFO" " Redis: $(ls -lh "$recent_redis" 2>/dev/null | awk '{print $6, $7, $8, "-", $5}')"
echo ""
log "INFO" "Configuration:"
log "INFO" " Retention: $RETENTION_DAYS days"
log "INFO" " Compression: gzip -$COMPRESSION_LEVEL"
log "INFO" " Lock timeout: ${LOCK_TIMEOUT}s"
log "INFO" "========================================="
}
list_all_databases() {
log "INFO" "========================================="
log "INFO" "ALL DATABASES IN SYSTEM"
log "INFO" "========================================="
echo -e "\n${BLUE}MariaDB Databases:${NC}"
if check_container "mariadb" 2>/dev/null; then
get_mariadb_databases | while read -r db; do
[ -n "$db" ] && echo " - $db"
done
else
echo " (container not running)"
fi
echo -e "\n${BLUE}PostgreSQL Databases:${NC}"
if check_container "postgresql" 2>/dev/null; then
get_postgresql_databases | tr ' ' '\n' | while read -r db; do
[ -n "$db" ] && echo " - $db"
done
else
echo " (container not running)"
fi
echo -e "\n${BLUE}MongoDB Databases:${NC}"
if check_container "mongodb" 2>/dev/null; then
get_mongodb_databases | while read -r db; do
[ -n "$db" ] && echo " - $db"
done
else
echo " (container not running)"
fi
echo -e "\n${BLUE}Redis:${NC}"
if check_container "redis" 2>/dev/null; then
local redis_keys=$(docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning DBSIZE 2>/dev/null)
echo " - Keys: $redis_keys"
local redis_memory=$(docker exec redis redis-cli -a $DB_PASSWORD --no-auth-warning INFO memory 2>/dev/null | grep "used_memory_human" | cut -d: -f2 | tr -d '\r')
echo " - Memory: $redis_memory"
else
echo " (container not running)"
fi
log "INFO" "========================================="
}
# =============================================================================
# HELP & MAIN
# =============================================================================
show_help() {
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN} Database Backup Script V7 - 15-MIN OPTIMIZED${NC}"
echo -e "${GREEN}================================================${NC}"
echo ""
echo "System Configuration:"
echo " MariaDB: v11.4 (ALL databases backup)"
echo " PostgreSQL: v15 (full backup)"
echo " MongoDB: v4.4 (full backup)"
echo " Redis: v8.2.2 (RDB snapshot)"
echo ""
echo "✅ Features:"
echo " - Lock mechanism (prevents concurrent runs)"
echo " - ALL databases backup (not just defaultdb)"
echo " - Individual database backup"
echo " - Backup verification"
echo " - Disk space checking"
echo " - Container health checking"
echo " - Fast compression (gzip -$COMPRESSION_LEVEL)"
echo " - Detailed logging with colors"
echo ""
echo "Usage: $0 {command} [options]"
echo ""
echo -e "${BLUE}Backup Commands:${NC}"
echo " $0 all - Backup ALL databases (RECOMMENDED)"
echo " $0 mariadb [type] - Backup ALL MariaDB databases"
echo " $0 mariadb-single <db_name> - Backup single MariaDB database"
echo " $0 postgresql [type] - Backup ALL PostgreSQL databases"
echo " $0 mongodb [type] - Backup ALL MongoDB databases"
echo " $0 redis [type] - Backup Redis"
echo ""
echo -e "${BLUE}Restore Commands:${NC}"
echo " $0 restore-mariadb <file> - Restore MariaDB from backup"
echo " $0 restore-postgresql <file> - Restore PostgreSQL from backup"
echo " $0 restore-mongodb <file> - Restore MongoDB from backup"
echo " $0 restore-redis <file> - Restore Redis from backup"
echo ""