/
restore.py
92 lines (78 loc) · 3.75 KB
/
restore.py
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
#!/usr/bin/env python3
import os
import sys
from pathlib import Path
import yaml
from colorama import Fore, Style
import utils
from utils import _print
from models import Container
from models import Log
from simple_term_menu import TerminalMenu
def restore():
# Set flags
utils.set_flags(sys.argv)
# Load config
config = Path(__file__).parent / "config.yml"
with open(config) as file:
# Load config
config_list = yaml.full_load(file)
log = Log(config_list['log']['log_dir'])
containers = Container.instantiate_containers(config_list)
# If any container names were passed as parameters, do only restore them
containers_wanted = {name: container for name, container in containers.items() if name in sys.argv}
if containers_wanted:
containers = containers_wanted
# If no container was chosen ask for it
elif not utils.all_containers:
containers_to_choose_from = [container.name for container in containers.values()]
terminal_menu = TerminalMenu(containers_to_choose_from, title="For which Nextcloud instance do you want "
"to restore a backup?")
choice_index = terminal_menu.show()
containers = {containers_to_choose_from[choice_index]: containers.get(containers_to_choose_from[choice_index])}
container: Container
for container in containers.values():
# Start restore
_print("----------------------------------------------")
_print(F"Restore backup for {container.name}")
backup_dir = os.scandir(container.backup_dir)
backup_files = {file.name: file for file in backup_dir if
file.is_file() and file.name.startswith(container.name) and file.name.endswith(".tar.gz")}
if len(backup_files) < 1:
_print(F"{Fore.YELLOW}No backups found for {container.name}{Style.RESET_ALL}")
break
backup_files_to_choose_from = [file.name for file in backup_files.values()]
backup_files_to_choose_from.sort(reverse=True)
_print()
# Choose backup to restore from
terminal_menu = TerminalMenu(backup_files_to_choose_from, title="Which backup do you want to restore?")
choice_index = terminal_menu.show()
backup_file = backup_files.get(backup_files_to_choose_from[choice_index])
print(backup_file.path)
# Confirm restore
if not utils.no_confirm:
confirm = input(F"Are you sure that you want to restore {backup_files_to_choose_from[choice_index]}? "
F"(Type: yes)\n").lower() == "yes"
else:
confirm = False
# Do the restore
if confirm or utils.no_confirm:
result = container.restore_backup(backup_file.path)
else:
break
# Print result and log
if result:
_print(F"{Fore.GREEN}Backup {container.restore_tar_file} for {container.name} successfully restored.{Style.RESET_ALL}")
if not utils.no_log and config_list['log']['logging']:
log.log(F"Restore backup ; {container.name} ; {container.restore_tar_file_path} ; SUCCESS")
else:
_print(F"{Fore.RED}Could not restore {container.restore_tar_file} for {container.name}.{Style.RESET_ALL}")
for func, traceback in container.exceptions.items():
_print()
_print(F"{Fore.YELLOW}Exception occurred in method: Container.{func}(){Style.RESET_ALL}")
_print(traceback)
_print()
if not utils.no_log and config_list['log']['logging']:
log.log(F"Restore backup ; {container.name} ; {container.restore_tar_file_path} ; FAIL")
if __name__ == '__main__':
restore()