-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecovery_cli.py
More file actions
106 lines (81 loc) · 3.28 KB
/
Copy pathrecovery_cli.py
File metadata and controls
106 lines (81 loc) · 3.28 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
#!/usr/bin/env python3
"""
recovery_cli.py — Backup Recovery Tool
───────────────────────────────────────
List available backup versions and restore any archive interactively.
Usage:
python recovery_cli.py list [<source_dir>]
python recovery_cli.py restore <archive_path> <restore_to>
python recovery_cli.py interactive
"""
import argparse
import os
import sys
from backup_engine import list_backups, restore_backup
from logger_setup import get_logger
log = get_logger("recovery")
def cmd_list(source_dir=None):
backups = list_backups(source_dir)
if not backups:
print("No backups found.")
return
print(f"\n{'#':<4} {'Archive':<50} {'Size (MB)':<12} {'Created'}")
print("─" * 90)
for i, b in enumerate(backups, 1):
print(f"{i:<4} {b['file']:<50} {b['size_mb']:<12} {b['created']}")
print()
def cmd_restore(archive_path: str, restore_to: str):
ok = restore_backup(archive_path, restore_to)
if ok:
print(f"✔ Restore complete → {restore_to}")
else:
print("✘ Restore failed. Check logs for details.")
sys.exit(1)
def cmd_interactive():
print("\n─── Backup Recovery CLI ───────────────────────────────────────")
backups = list_backups()
if not backups:
print("No backups found.")
return
print(f"\n{'#':<4} {'Archive':<50} {'Size (MB)':<12} {'Created'}")
print("─" * 90)
for i, b in enumerate(backups, 1):
print(f"{i:<4} {b['file']:<50} {b['size_mb']:<12} {b['created']}")
print()
try:
choice = int(input("Enter backup number to restore (0 to cancel): "))
except (ValueError, EOFError):
print("Cancelled.")
return
if choice == 0 or choice > len(backups):
print("Cancelled.")
return
selected = backups[choice - 1]
restore_to = input(f"Restore to directory [{os.path.expanduser('~/restored_backup')}]: ").strip()
if not restore_to:
restore_to = os.path.expanduser("~/restored_backup")
cmd_restore(selected["path"], restore_to)
def main():
parser = argparse.ArgumentParser(
description="Backup Recovery Tool for System Backup Daemon",
formatter_class=argparse.RawTextHelpFormatter,
)
sub = parser.add_subparsers(dest="command")
p_list = sub.add_parser("list", help="List available backups")
p_list.add_argument("source_dir", nargs="?", help="Filter by source directory")
p_restore = sub.add_parser("restore", help="Restore a specific archive")
p_restore.add_argument("archive_path", help="Path to the .zip archive")
p_restore.add_argument("restore_to", help="Directory to extract into")
sub.add_parser("interactive", help="Interactive guided restore")
args = parser.parse_args()
if args.command == "list":
cmd_list(getattr(args, "source_dir", None))
elif args.command == "restore":
cmd_restore(args.archive_path, args.restore_to)
elif args.command == "interactive":
cmd_interactive()
else:
parser.print_help()
print("\nTip: run python recovery_cli.py interactive for guided restore.")
if __name__ == "__main__":
main()