-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1314 lines (1182 loc) · 52.3 KB
/
main.py
File metadata and controls
1314 lines (1182 loc) · 52.3 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
"""
main.py - Backup Handler CLI Entry Point and Orchestrator
Central entry point that coordinates the entire backup pipeline:
1. Parse CLI arguments and resolve configuration
2. Execute early-exit commands (--status, --verify, --restore, --show-setup)
3. Run pre-backup hooks
4. Execute selected backup modes (local, SSH, S3, database)
5. Save manifests, encrypt, deduplicate, and apply retention policies
6. Run post-backup hooks and send notifications
7. Support scheduled mode with configurable times and graceful shutdown
All backup operations are orchestrated through ``backup_operation()`` which
handles both one-off CLI invocations and scheduled recurring runs.
"""
import atexit
import contextlib
import logging
import os
import signal
import sys
import time
from datetime import datetime
from pathlib import Path
from colorama import init
from banner.banner_show import print_banner
from bot.BotHandler import TelegramBot
from src.argparse_setup import setup_argparse, validate_args
from src.config import extract_config_values
from src.db_sync import perform_db_backup
from src.dedup import deduplicate_backup_dirs
from src.email_notify import send_smtp_email
from src.encryption import encrypt_directory
from src.heartbeat import send_heartbeat
from src.installer import run_installer
# ─── Internal Module Imports ────────────────────────────────────────────────
from src.logger import AppLogger, current_run_id, new_run_id
from src.manifest import BackupManifest, load_latest_manifest
from src.preflight import (
PreflightConfig,
run_preflight,
send_local_mail,
write_status_sentinel,
)
from src.restore import restore_backup
from src.retention import cleanup_old_backups
from src.s3_sync import sync_to_s3
from src.snapshot import create_snapshot, diff_snapshots, generate_restore_script
from src.sync import (
perform_differential_backup,
perform_full_backup,
perform_incremental_backup,
sync_ssh_servers_concurrently,
)
from src.tailscale import tailscale_down, tailscale_up
from src.utils import (
get_last_backup_time,
get_last_full_backup_time,
run_hook,
update_last_backup_time,
update_last_full_backup_time,
)
from src.verify import print_verify_report, verify_backup_integrity
from src.webhook_notify import send_webhook
# ─── Project Paths ──────────────────────────────────────────────────────────
_PROJECT_ROOT = Path(__file__).parent
CONFIG_PATH = str(_PROJECT_ROOT / "config" / "config.ini")
LOG_PATH = str(_PROJECT_ROOT / "Logs" / "application.log")
LOCK_FILE = _PROJECT_ROOT / ".backup-handler.lock"
# ─── Instance Locking ───────────────────────────────────────────────────────
def _proc_looks_like_backup_handler(pid: int) -> bool:
"""
Confirm that a PID corresponds to a backup-handler process.
PIDs are recycled by the OS. A stale lock file can point at an
unrelated process that happens to share the old PID. Cross-check
``/proc/<pid>/comm`` and ``/proc/<pid>/cmdline`` before trusting it.
Returns True only when the process's identifiers reference python or
the backup-handler entry point.
"""
comm = Path(f"/proc/{pid}/comm")
cmdline = Path(f"/proc/{pid}/cmdline")
try:
comm_value = comm.read_text().strip().lower() if comm.exists() else ""
cmdline_value = cmdline.read_text().replace("\x00", " ").lower() if cmdline.exists() else ""
except OSError:
return False
hints = ("python", "backup-handler", "main.py")
return any(h in comm_value or h in cmdline_value for h in hints)
def _acquire_lock(logger):
"""
Acquire a PID lock file to prevent duplicate scheduled instances.
Checks if an existing lock file references a still-running backup-handler
process. Stale lock files (from crashed instances or recycled PIDs) are
automatically cleaned up. Registers ``_release_lock`` via ``atexit``.
"""
if LOCK_FILE.exists():
try:
old_pid = int(LOCK_FILE.read_text().strip())
os.kill(old_pid, 0)
except (ValueError, ProcessLookupError, PermissionError):
logger.warning("Removing stale lock file (PID in file no longer running).")
else:
if _proc_looks_like_backup_handler(old_pid):
logger.error(
f"Another backup-handler instance is already running (PID {old_pid}). "
f"Remove {LOCK_FILE} if this is incorrect."
)
sys.exit(1)
logger.warning(
f"Lock file references PID {old_pid} but that process is not "
f"backup-handler (likely recycled). Reclaiming the lock."
)
LOCK_FILE.write_text(str(os.getpid()))
atexit.register(_release_lock)
def _release_lock():
"""Remove the PID lock file on exit."""
with contextlib.suppress(OSError):
LOCK_FILE.unlink(missing_ok=True)
# Initialize colorama with autoreset to ensure color codes are reset after each print
init(autoreset=True)
# ─── Configuration Resolution ───────────────────────────────────────────────
def _resolve_config_path(args):
"""
Resolve the configuration file path from CLI arguments.
Priority: ``--profile`` > ``--config`` > default ``config/config.ini``.
Profiles resolve to ``config/config.<name>.ini``.
"""
if args.profile:
profile_path = str(_PROJECT_ROOT / "config" / f"config.{args.profile}.ini")
if not os.path.exists(profile_path):
print(f"Error: Profile config not found: {profile_path}", file=sys.stderr)
sys.exit(1)
return profile_path
if args.config and os.path.exists(args.config):
return args.config
return CONFIG_PATH
# ─── Status Dashboard ───────────────────────────────────────────────────────
def show_status(logger, config_path):
"""
Display a backup status dashboard including last backup timestamps,
scheduled times, backup directory sizes, and latest manifest summary.
"""
print("\n=== Backup Status ===\n")
# Last backup timestamps
last_backup = get_last_backup_time()
last_full = get_last_full_backup_time()
if last_backup:
print(f"Last backup: {datetime.fromtimestamp(last_backup).strftime('%Y-%m-%d %H:%M:%S')}")
else:
print("Last backup: Never")
if last_full:
print(f"Last full backup: {datetime.fromtimestamp(last_full).strftime('%Y-%m-%d %H:%M:%S')}")
else:
print("Last full backup: Never")
# Load config for schedule and backup dirs
try:
config_values = extract_config_values(logger, config_path, skip_validation=True)
except Exception:
config_values = {}
# Scheduled times
schedule_times = config_values.get("schedule_times", [])
if schedule_times:
print(f"\nScheduled times: {', '.join(schedule_times)}")
else:
print("\nScheduled times: Not configured")
# Backup directory sizes
backup_dirs = config_values.get("backup_dirs", [])
if backup_dirs:
print("\nBackup directories:")
for bdir in backup_dirs:
bpath = Path(bdir)
if bpath.exists():
total_size = sum(f.stat().st_size for f in bpath.rglob("*") if f.is_file())
# Human-readable size
if total_size >= 1073741824:
size_str = f"{total_size / 1073741824:.2f} GB"
elif total_size >= 1048576:
size_str = f"{total_size / 1048576:.2f} MB"
elif total_size >= 1024:
size_str = f"{total_size / 1024:.2f} KB"
else:
size_str = f"{total_size} B"
print(f" {bdir}: {size_str}")
else:
print(f" {bdir}: (not found)")
# Latest manifest summary
if backup_dirs:
print("\nLatest manifest:")
found_manifest = False
for bdir in backup_dirs:
manifest = load_latest_manifest(bdir)
if manifest:
found_manifest = True
print(f" Directory: {bdir}")
print(f" Timestamp: {manifest.get('timestamp', 'Unknown')}")
print(f" Mode: {manifest.get('mode', 'Unknown')}")
print(f" Duration: {manifest.get('duration_seconds', 0):.1f}s")
print(f" Copied: {manifest.get('files_copied', 0)} files")
print(f" Skipped: {manifest.get('files_skipped', 0)} files")
print(f" Failed: {manifest.get('files_failed', 0)} files")
total_bytes = manifest.get("total_bytes", 0)
if total_bytes >= 1048576:
print(f" Size: {total_bytes / 1048576:.2f} MB")
else:
print(f" Size: {total_bytes / 1024:.2f} KB")
break
if not found_manifest:
print(" No manifests found")
print()
# ─── Main Entry Point ───────────────────────────────────────────────────────
def main():
"""CLI entry point — parses arguments, routes to the appropriate operation."""
# Initialize the logger BEFORE anything else (banner, argparse, filesystem).
# On 2026-04-16 the script crashed in a pre-logger code path and 16 days of
# cron firings produced zero log lines. Logger first, always.
logger = AppLogger(LOG_PATH, logging.DEBUG).logger
new_run_id()
try:
print_banner()
except Exception as e:
logger.warning(f"Banner failed (non-fatal): {e}")
args = setup_argparse()
# Validate the parsed arguments
validate_args(args, logger)
# Resolve config path (--config, --profile, or default)
config_path = _resolve_config_path(args)
# Handle --install early exit. Runs BEFORE AppLogger / banner write
# anything to disk because the installer is invoked via sudo and we
# do not want root-owned files left behind in the project tree.
if args.install:
try:
install_config = extract_config_values(logger, config_path, skip_validation=True)
except Exception as e:
logger.error(f"Cannot load config for installer: {e}")
sys.exit(1)
rc = run_installer(install_config, _PROJECT_ROOT, dry_run=args.dry_run)
sys.exit(rc)
# Handle --status early exit
if args.status:
show_status(logger, config_path)
return
# Handle --verify early exit
if args.verify:
try:
verify_config = extract_config_values(logger, config_path, skip_validation=True)
except Exception:
verify_config = {}
backup_dirs = args.backup_dirs or verify_config.get("backup_dirs", [])
if not backup_dirs:
logger.error(
"No backup directories to verify. Specify --backup-dirs or configure [BACKUPS] backup_dirs."
)
sys.exit(1)
enc_passphrase = verify_config.get("encryption_passphrase")
enc_key_file = verify_config.get("encryption_key_file")
results = verify_backup_integrity(
logger, backup_dirs, encryption_passphrase=enc_passphrase, encryption_key_file=enc_key_file
)
all_ok = print_verify_report(results)
sys.exit(0 if all_ok else 1)
# Handle --snapshot early exit
if args.snapshot:
output_path = args.snapshot_output or str(_PROJECT_ROOT / "snapshots")
snapshot_file = create_snapshot(logger, output_dir=output_path)
print(f"\nSnapshot saved to: {snapshot_file}")
return
# Handle --restore-snapshot early exit
if args.restore_snapshot:
output = args.snapshot_output
if output is None:
snapshot_name = Path(args.restore_snapshot).stem
output = str(_PROJECT_ROOT / "snapshots" / f"{snapshot_name}_restore.sh")
script_path = generate_restore_script(logger, args.restore_snapshot, output_path=output)
if script_path:
print(f"\nRestore script generated: {script_path}")
print("Review it, then run: chmod +x restore.sh && sudo ./restore.sh")
else:
print("Failed to generate restore script.", file=sys.stderr)
sys.exit(1)
return
# Handle --snapshot-diff early exit
if args.snapshot_diff:
diff = diff_snapshots(logger, args.snapshot_diff[0], args.snapshot_diff[1])
if not diff:
print("\nNo differences found between snapshots.")
else:
print("\n=== Snapshot Diff ===\n")
for category, changes in diff.items():
added = changes.get("added", [])
removed = changes.get("removed", [])
print(f" {category}:")
for item in added:
print(f" + {item}")
for item in removed:
print(f" - {item}")
print()
return
# Handle --restore early exit
if args.restore:
# Load config to get encryption/SSH/S3 params for restore
try:
restore_config = extract_config_values(logger, config_path, skip_validation=True)
except Exception:
restore_config = {}
enc_passphrase = restore_config.get("encryption_passphrase")
enc_key_file = restore_config.get("encryption_key_file")
logger.info(f"Restoring from {args.from_dir} to {args.to_dir}")
success = restore_backup(
logger,
args.from_dir,
args.to_dir,
timestamp=args.restore_timestamp,
encryption_passphrase=enc_passphrase,
encryption_key_file=enc_key_file,
ssh_password=restore_config.get("ssh_password"),
s3_region=restore_config.get("s3_region"),
s3_access_key=restore_config.get("s3_access_key"),
s3_secret_key=restore_config.get("s3_secret_key"),
dry_run=args.dry_run,
)
if success:
logger.info("Restore completed successfully.")
print("Restore completed successfully.")
else:
logger.error("Restore completed with errors.")
print("Restore completed with errors.", file=sys.stderr)
sys.exit(1)
return
# Initialize TelegramBot if --notifications flag is used
telegram_bot = None
if args.notifications:
try:
telegram_bot = TelegramBot(logger)
except FileNotFoundError:
logger.error(
"Telegram bot config not found. Create config/bot_config.ini from config/bot_config.ini.example"
)
print(
"Error: config/bot_config.ini not found. Copy config/bot_config.ini.example and fill in your values.",
file=sys.stderr,
)
sys.exit(1)
except KeyError as e:
logger.error(
f"Missing key in bot_config.ini: {e}. Check that [TELEGRAM] api_token and [USERS] interacted_users are set."
)
print(
f"Error: Missing key in config/bot_config.ini: {e}. Ensure api_token and interacted_users are set.",
file=sys.stderr,
)
sys.exit(1)
# Use the provided receiver emails if notifications are enabled
receiver_emails = args.receiver if args.notifications else None
# Parse exclude patterns from CLI (overrides config)
exclude_patterns = None
if args.exclude:
exclude_patterns = [p.strip() for p in args.exclude.split(",") if p.strip()]
if args.scheduled:
try:
scheduled_operation(
logger,
config_path,
telegram_bot=telegram_bot,
exclude_patterns=exclude_patterns,
retain=args.retain,
)
except Exception as e:
logger.error(f"Failed to load configuration file: {config_path}. Error: {e}")
sys.exit(1)
else:
# Fall back to config-defined source_dir / backup_dirs when CLI omits them.
# Lets cron lines pass --config and skip --source-dir / --backup-dirs.
cli_source_dir = args.source_dir
cli_backup_dirs = args.backup_dirs
if not cli_source_dir or not cli_backup_dirs:
try:
_cv = extract_config_values(logger, config_path, skip_validation=True)
except Exception:
_cv = {}
cli_source_dir = cli_source_dir or _cv.get("source_dir")
cli_backup_dirs = cli_backup_dirs or _cv.get("backup_dirs")
if args.backup_mode and (not cli_source_dir or not cli_backup_dirs):
logger.error(
"Source directory and backup directories must be specified when using --backup-mode."
)
sys.exit(1)
rc = backup_operation(
logger,
source_dir=cli_source_dir,
backup_dirs=cli_backup_dirs,
ssh_servers=args.ssh_servers,
operation_modes=args.operation_modes,
backup_mode=args.backup_mode,
compress=args.compress,
receiver=receiver_emails,
show_setup=args.show_setup,
notifications=args.notifications,
telegram_bot=telegram_bot,
dry_run=args.dry_run,
exclude_patterns=exclude_patterns,
retain=args.retain,
config_path=config_path,
encrypt=args.encrypt,
dedup=args.dedup,
tailscale=args.tailscale,
tailscale_authkey=args.tailscale_authkey,
)
if rc:
sys.exit(rc)
# ─── Scheduled Mode ─────────────────────────────────────────────────────────
def scheduled_operation(logger, config_file, telegram_bot=None, exclude_patterns=None, retain=None):
"""
Run backups on a configurable schedule with graceful shutdown support.
Acquires a PID lock to prevent duplicate instances, then enters a polling
loop that checks the current time against configured schedule times every
30 seconds (matching the ±30s tolerance window). Handles SIGINT/SIGTERM
for clean shutdown.
Parameters:
logger: Logger instance.
config_file (str): Path to the INI configuration file.
telegram_bot (TelegramBot, optional): Telegram bot for notifications.
exclude_patterns (list, optional): Glob patterns to exclude.
retain (int, optional): CLI override for max_count retention policy.
"""
_acquire_lock(logger)
# Handle SIGINT/SIGTERM for clean shutdown
_shutdown_requested = False
def _handle_shutdown(signum, frame):
nonlocal _shutdown_requested
sig_name = signal.Signals(signum).name
logger.info(f"Received {sig_name}, shutting down scheduler gracefully...")
_shutdown_requested = True
signal.signal(signal.SIGINT, _handle_shutdown)
signal.signal(signal.SIGTERM, _handle_shutdown)
try:
# Loading the config file (with schedule validation)
config_values = extract_config_values(logger, config_file, require_schedule=True)
# Access the schedule times and interval
times = config_values.get("schedule_times", [])
config_values.get("interval_minutes", 60)
# Ensure all times are in the correct format
scheduled_times = []
for t in times:
try:
scheduled_times.append(datetime.strptime(t, "%H:%M").time())
except ValueError:
logger.error(f"Time format error for value: {t}")
continue
# Use CLI exclude patterns if provided, otherwise use config
if exclude_patterns is None:
exclude_patterns = config_values.get("exclude_patterns", [])
logger.info(f"Scheduled times: {scheduled_times}")
while not _shutdown_requested:
now = datetime.now()
current_time = now.time()
logger.info(f"Current time: {current_time}")
# Check for matching scheduled time within a ±30 second tolerance
matched = False
for scheduled_time in scheduled_times:
scheduled_dt = now.replace(
hour=scheduled_time.hour, minute=scheduled_time.minute, second=0, microsecond=0
)
diff = abs((now - scheduled_dt).total_seconds())
if diff <= 30:
matched = True
break
if matched:
logger.info("Scheduled time matched. Performing backup operation...")
# Pre-flight: verify backup directories are accessible
sched_backup_dirs = config_values.get("backup_dirs", [])
if sched_backup_dirs:
inaccessible = _check_backup_dirs_accessible(logger, sched_backup_dirs)
if inaccessible:
msg = (
f"Scheduled backup aborted: destination(s) inaccessible: "
f"{', '.join(inaccessible)}. Check that the disk is mounted."
)
logger.error(msg)
if telegram_bot:
try:
telegram_bot.send_notification(msg)
except Exception as e:
logger.error(f"Failed to send Telegram notification: {e}")
time.sleep(30)
continue
# Build operation_modes from config flags
operation_modes = []
if config_values.get("local_mode"):
operation_modes.append("local")
if config_values.get("ssh_mode"):
operation_modes.append("ssh")
if config_values.get("s3_mode"):
operation_modes.append("s3")
if config_values.get("db_mode"):
operation_modes.append("db")
rc = backup_operation(
logger,
source_dir=config_values["source_dir"],
backup_dirs=config_values["backup_dirs"],
ssh_servers=config_values.get("ssh_servers"),
operation_modes=operation_modes,
backup_mode=config_values["mode"],
compress=config_values["compress_type"],
receiver=config_values["receiver_emails"],
notifications=bool(telegram_bot),
telegram_bot=telegram_bot,
ssh_username=config_values.get("ssh_username"),
ssh_password=config_values.get("ssh_password"),
exclude_patterns=exclude_patterns,
retain=retain,
config_path=None,
config_values=config_values,
)
if rc:
logger.error(f"Scheduled run returned exit code {rc}; scheduler continues.")
else:
logger.info("No scheduled time matched.")
# Wait for 30 seconds before checking again (matches tolerance window)
time.sleep(30)
logger.info("Scheduler stopped cleanly.")
except Exception as e:
logger.error(f"Error in scheduled_operation: {e}")
sys.exit(1)
# ─── Notification Helpers ───────────────────────────────────────────────────
def _critical_alert(
logger,
config_values,
telegram_bot,
notifications,
subject: str,
body: str,
) -> None:
"""
Emit a high-priority alert through every available channel.
Used for failures that MUST reach an operator: preflight aborts, stale
backups, lost destinations. Tries Telegram/SMTP/webhook (best-effort —
these can fail silently when DNS is broken, which is exactly the
scenario this guards), then always attempts a local-MTA mail to
[PREFLIGHT] local_mail_to. Local mail does not need DNS or external
network and survives the failure modes that disabled our other
channels on 2026-04-16.
"""
_notify(logger, telegram_bot, notifications, f"{subject}: {body}", config_values=config_values)
if config_values:
local_to = config_values.get("preflight_local_mail_to")
if local_to:
send_local_mail(logger, local_to, f"[backup-handler] {subject}", body)
def _notify(logger, telegram_bot, notifications, message, config_values=None):
"""
Dispatch notifications via all configured channels (Telegram and SMTP).
Telegram notifications require the ``--notifications`` flag and a valid bot.
SMTP notifications are sent when ``[SMTP]`` host and recipients are configured
in ``config_values``, regardless of the ``--notifications`` flag.
"""
if notifications and telegram_bot:
try:
telegram_bot.send_notification(message)
except Exception as e:
logger.error(f"Failed to send Telegram notification: {e}")
# Webhook notification
if config_values:
webhook_url = config_values.get("webhook_url")
if webhook_url:
try:
headers = {}
auth_header = config_values.get("webhook_auth_header")
if auth_header:
headers["Authorization"] = auth_header
send_webhook(logger, webhook_url, message, headers=headers or None)
except Exception as e:
logger.error(f"Failed to send webhook notification: {e}")
# SMTP email notification
if config_values:
smtp_host = config_values.get("smtp_host")
smtp_to = config_values.get("smtp_to", [])
if smtp_host and smtp_to:
try:
send_smtp_email(
logger,
smtp_host=smtp_host,
smtp_port=config_values.get("smtp_port", 587),
smtp_user=config_values.get("smtp_user"),
smtp_password=config_values.get("smtp_password"),
from_addr=config_values.get("smtp_from", config_values.get("smtp_user", "")),
to_addrs=smtp_to,
subject=f"Backup Handler: {message[:50]}",
body=message,
use_tls=config_values.get("smtp_tls", True),
)
except Exception as e:
logger.error(f"Failed to send SMTP notification: {e}")
def _run_backup(logger, telegram_bot, notifications, mode_name, backup_fn, config_values=None):
"""
Execute a backup function with standardized notification and error handling.
Wraps the actual backup call in a try/except to ensure failure notifications
are always sent, even if the backup raises an unexpected exception. Returns
True on success, False on failure so the caller can track partial failures
and propagate a non-zero exit code.
"""
try:
backup_fn()
_notify(
logger,
telegram_bot,
notifications,
f"Local {mode_name} backup completed.",
config_values=config_values,
)
return True
except Exception as e:
logger.error(f"{mode_name.capitalize()} backup failed: {e}")
_notify(
logger,
telegram_bot,
notifications,
f"{mode_name.capitalize()} backup failed.",
config_values=config_values,
)
return False
# ─── Pre-flight Checks ─────────────────────────────────────────────────────
def _check_backup_dirs_accessible(logger, backup_dirs):
"""
Verify that backup directories are accessible before starting a backup.
For paths under a mount point (e.g. /mnt/*), checks that the mount point
is actually mounted. Also ensures each backup directory exists or can be
created. Returns a list of inaccessible directories (empty = all OK).
"""
inaccessible = []
for bdir in backup_dirs or []:
bpath = Path(bdir)
# Check if the path is under a mount point (e.g. /mnt/data/...)
parts = bpath.parts
if len(parts) >= 3 and parts[1] == "mnt":
mount_point = Path("/") / parts[1] / parts[2] # e.g. /mnt/data
if not os.path.ismount(str(mount_point)):
logger.error(
f"Mount point {mount_point} is not mounted. Backup directory {bdir} is inaccessible."
)
inaccessible.append(bdir)
continue
# Check if the directory exists or its parent is writable
if not bpath.exists():
try:
bpath.mkdir(parents=True, exist_ok=True)
logger.info(f"Created backup directory: {bdir}")
except OSError as e:
logger.error(f"Cannot create backup directory {bdir}: {e}")
inaccessible.append(bdir)
return inaccessible
# ─── Core Backup Pipeline ───────────────────────────────────────────────────
def backup_operation(
logger,
source_dir=None,
backup_dirs=None,
ssh_servers=None,
operation_modes=None,
backup_mode=None,
compress=None,
receiver=None,
show_setup=False,
notifications=False,
telegram_bot=None,
ssh_username=None,
ssh_password=None,
dry_run=False,
exclude_patterns=None,
retain=None,
config_path=None,
config_values=None,
encrypt=False,
dedup=False,
tailscale=False,
tailscale_authkey=None,
):
"""
Orchestrate the full backup pipeline for a single run.
Execution order:
1. Pre-backup hook (failure aborts the run)
2. Local / SSH / S3 / Database backup modes (based on ``operation_modes``)
3. Save backup manifests to each backup directory
4. Encrypt backup files (AES-256-GCM, if enabled)
5. Deduplicate via hardlinks (if enabled)
6. Update backup timestamps
7. Apply retention policies (age-based and count-based)
8. Post-backup hook (failure is logged but does not affect backup status)
9. Send completion notification
All parameters can be sourced from CLI args, config file, or both (CLI wins).
Returns a process exit code:
0 - all selected modes succeeded (or dry-run / show-setup completed)
2 - pre-flight failure (backup dir inaccessible, pre-hook failed)
3 - at least one backup mode failed
Callers (main / scheduled_operation) decide whether to ``sys.exit()`` or
continue looping.
"""
# Show setup command (skip validation so incomplete configs can be inspected)
if show_setup:
extract_config_values(logger, config_path or CONFIG_PATH, show=True, skip_validation=True)
return 0
# Load config values if not provided (for hooks, retention, parallel, bandwidth, S3)
if config_values is None and config_path:
try:
config_values = extract_config_values(logger, config_path, skip_validation=True)
except Exception:
config_values = {}
if config_values is None:
config_values = {}
# Use config exclude patterns if CLI didn't provide them
if exclude_patterns is None:
exclude_patterns = config_values.get("exclude_patterns", [])
# ─── Preflight self-check + sentinel ───────────────────────────────────
# Resolve sentinel path (relative paths anchor at project root).
pf_cfg = PreflightConfig(
enabled=config_values.get("preflight_enabled", True),
expected_mount=config_values.get("preflight_expected_mount"),
expected_label=config_values.get("preflight_expected_label"),
expected_uuid=config_values.get("preflight_expected_uuid"),
expected_fs_type=config_values.get("preflight_expected_fs_type", "xfs"),
expected_owner=config_values.get("preflight_expected_owner"),
auto_mount=config_values.get("preflight_auto_mount", True),
auto_fix_ownership=config_values.get("preflight_auto_fix_ownership", False),
ensure_fstab=config_values.get("preflight_ensure_fstab", True),
staleness_factor=config_values.get("preflight_staleness_factor", 2.0),
local_mail_to=config_values.get("preflight_local_mail_to"),
status_sentinel=config_values.get("preflight_status_sentinel", "Logs/last_run_status.json"),
)
sentinel_relpath = Path(pf_cfg.status_sentinel)
sentinel_path = sentinel_relpath if sentinel_relpath.is_absolute() else _PROJECT_ROOT / sentinel_relpath
write_status_sentinel(
sentinel_path,
status="started",
run_id=current_run_id(),
message="backup run started",
extra={"backup_dirs": backup_dirs or [], "modes": operation_modes or []},
)
if not dry_run and not show_setup:
pf_result = run_preflight(
logger,
pf_cfg,
backup_dirs=backup_dirs or [],
interval_minutes=config_values.get("interval_minutes", 60),
)
if not pf_result.ok:
logger.error(f"Preflight FAILED: {pf_result.message}")
_critical_alert(
logger,
config_values,
telegram_bot,
notifications,
"Preflight FAILED",
pf_result.message,
)
write_status_sentinel(
sentinel_path,
status="failure",
run_id=current_run_id(),
message=f"preflight: {pf_result.message}",
extra={"phase": "preflight", "details": pf_result.details},
)
return 2
if pf_result.healed:
logger.info(f"Preflight self-healed: {pf_result.message}")
stale_msg = pf_result.details.get("stale_alert") if pf_result.details else None
if stale_msg:
_critical_alert(
logger,
config_values,
telegram_bot,
notifications,
"Backup STALE",
stale_msg,
)
# Hooks
pre_hook = config_values.get("pre_backup_hook")
post_hook = config_values.get("post_backup_hook")
# Retention (CLI --retain overrides config max_count)
max_age_days = config_values.get("max_age_days", 0)
max_count = retain if retain is not None else config_values.get("max_count", 0)
# Parallel copies
parallel_copies = config_values.get("parallel_copies", 1)
# Bandwidth limit
bandwidth_limit = config_values.get("bandwidth_limit", 0)
# S3 config
s3_bucket = config_values.get("s3_bucket")
s3_prefix = config_values.get("s3_prefix", "")
s3_region = config_values.get("s3_region")
s3_access_key = config_values.get("s3_access_key")
s3_secret_key = config_values.get("s3_secret_key")
# Run pre-backup hook
if pre_hook and not run_hook(logger, pre_hook, "pre_backup"):
logger.error("Pre-backup hook failed. Aborting backup.")
_critical_alert(
logger,
config_values,
telegram_bot,
notifications,
"Backup aborted",
"pre-backup hook failed.",
)
write_status_sentinel(
sentinel_path,
status="failure",
run_id=current_run_id(),
message="pre-backup hook failed",
extra={"phase": "pre_hook"},
)
return 2
# Track per-mode failures so we can exit non-zero if any mode failed.
# Systemd and Prometheus rely on this to page an operator.
mode_failures: list[str] = []
# Create manifest for this backup run
manifest = BackupManifest(mode=backup_mode or "full")
# Execute selected backup modes
if operation_modes is None:
operation_modes = []
if "local" in operation_modes:
if not backup_dirs:
logger.warning("Local mode selected but no backup directories specified. Skipping local backup.")
elif dry_run:
logger.info(
f"[DRY RUN] Would perform {backup_mode or 'full'} backup: '{source_dir}' -> {backup_dirs}"
)
print(f"[DRY RUN] Would perform {backup_mode or 'full'} backup")
print(f" Source: {source_dir}")
print(f" Destinations: {', '.join(backup_dirs)}")
if compress:
print(f" Compression: {compress}")
if exclude_patterns:
print(f" Excluding: {', '.join(exclude_patterns)}")
elif backup_mode == "incremental":
if compress:
logger.error("Invalid option for incremental backup.")
sys.exit(1)
last_backup_time = get_last_backup_time()
if not _run_backup(
logger,
telegram_bot,
notifications,
"incremental",
lambda: perform_incremental_backup(
logger,
source_dir,
backup_dirs,
last_backup_time,
bot=telegram_bot,
receiver_emails=receiver,
exclude_patterns=exclude_patterns,
manifest=manifest,
),
config_values=config_values,
):
mode_failures.append("local-incremental")
elif backup_mode == "differential":
if compress:
logger.error("Invalid option for differential backup.")
sys.exit(1)
last_full_backup_time = get_last_full_backup_time()
if not _run_backup(
logger,
telegram_bot,
notifications,
"differential",
lambda: perform_differential_backup(
logger,
source_dir,
backup_dirs,
last_full_backup_time,
bot=telegram_bot,
receiver_emails=receiver,
exclude_patterns=exclude_patterns,
manifest=manifest,
),
config_values=config_values,
):
mode_failures.append("local-differential")
else:
_notify(
logger, telegram_bot, notifications, "Starting full backup...", config_values=config_values
)
if _run_backup(
logger,
telegram_bot,
notifications,
"full",
lambda: perform_full_backup(
logger,
source_dir,
backup_dirs,
compress=compress,
bot=telegram_bot,
receiver_emails=receiver,
exclude_patterns=exclude_patterns,