-
Notifications
You must be signed in to change notification settings - Fork 0
/
intuitbackup
executable file
·134 lines (113 loc) · 4.46 KB
/
intuitbackup
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys
from intuitbackuputil import IntuitBackup
def select(options, prompt = None):
print()
if prompt is None:
print("Please select an option:")
else:
print(f"[{prompt}] Please select an option:")
for i, item in enumerate(options):
print(i + 1, item)
while True:
try:
choice = input("Enter your choice (num): ").strip()
if choice == '':
print("No choice.")
return None
choice = int(choice)
if 1 <= choice <= len(options):
return choice - 1
else:
print("Invalid choice. Please enter a number within the range.")
except ValueError:
print("Invalid input. Please enter a valid number.")
def main():
if os.geteuid() != 0:
os.execvp("sudo", ["sudo", * sys.argv])
intuit_backup = IntuitBackup()
record_dir = intuit_backup.get_record_dir()
dest_dir = intuit_backup.get_dest_dir()
options = ["backup", "extract", "restore", "list_files", "check_record", "cat_record", "backup_info", "restart", "exit"]
choiced_options = []
while True:
if len(choiced_options) == 0:
choice = select(options)
if choice is None:
continue
choiced_options.append(options[choice])
choiced_option = choiced_options[-1]
def get_record_names():
record_names = [f for f in os.listdir(record_dir) if os.path.isfile(os.path.join(record_dir, f))]
record_names.sort()
return record_names
def get_archive_names():
archive_names = [f for f in os.listdir(dest_dir) if os.path.isfile(os.path.join(dest_dir, f))]
archive_names.sort()
return archive_names
if choiced_option == "backup":
record_names = get_record_names()
choice = select(record_names, choiced_option)
if choice is None:
choiced_options.pop()
continue
intuit_backup.backup(record_names[choice])
elif choiced_option == "extract":
archive_names = get_archive_names()
choice = select(archive_names, choiced_option)
if choice is None:
choiced_options.pop()
continue
intuit_backup.extract(archive_names[choice])
elif choiced_option == "restore":
archive_names = get_archive_names()
choice = select(archive_names, choiced_option)
if choice is None:
choiced_options.pop()
continue
opts = ["yes", "no"]
ch = select(opts, "Note: Your files will be overwritten")
if ch is not None:
if opts[ch] == "yes":
intuit_backup.restore(archive_names[choice])
elif choiced_option == "extract":
archive_names = get_archive_names()
choice = select(archive_names, choiced_option)
if choice is None:
choiced_options.pop()
continue
intuit_backup.extract(archive_names[choice])
elif choiced_option == "list_files":
archive_names = get_archive_names()
choice = select(archive_names, choiced_option)
if choice is None:
choiced_options.pop()
continue
intuit_backup.list_files(archive_names[choice])
elif choiced_option == "check_record":
record_names = get_record_names()
choice = select(record_names, choiced_option)
if choice is None:
choiced_options.pop()
continue
intuit_backup.check_record(record_names[choice])
elif choiced_option == "cat_record":
record_names = get_record_names()
choice = select(record_names, choiced_option)
if choice is None:
choiced_options.pop()
continue
intuit_backup.cat_record(record_names[choice])
elif choiced_option == "backup_info":
intuit_backup.backup_info()
elif choiced_option == "restart":
exec = sys.executable
os.execl(exec, exec, * sys.argv)
elif choiced_option == "exit":
break;
else:
sys.stderr.write("Not match the option")
choiced_options.pop()
if __name__ == "__main__":
main()