Skip to content

Commit c383356

Browse files
committed
move backups to separate module
1 parent c00f8c3 commit c383356

File tree

4 files changed

+72
-54
lines changed

4 files changed

+72
-54
lines changed

modules/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class Setting:
5959
'subscribe_tg_channel': Setting('validator', False, 'Disables warning about subscribing to the `TON STATUS` channel'),
6060
'BotToken': Setting('alert-bot', None, 'Alerting Telegram bot token'),
6161
'ChatId': Setting('alert-bot', None, 'Alerting Telegram chat id'),
62-
'auto_backup': Setting('validator', True, 'Make backup of validator every election'),
63-
'auto_backup_path': Setting('validator', '/tmp/mytoncore/backups/', 'Path to store backups'),
62+
'auto_backup': Setting('validator', None, 'Make validator backup every election'),
63+
'auto_backup_path': Setting('validator', '/tmp/mytoncore/auto_backups/', 'Path to store auto-backups'),
6464
}
6565

6666

modules/backups.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pkg_resources
2+
3+
from modules.module import MtcModule
4+
from mypylib.mypylib import color_print, ip2int, run_as_root
5+
from mytoninstaller.config import get_own_ip
6+
7+
8+
class BackupModule(MtcModule):
9+
10+
def create_backup(self, args):
11+
if len(args) > 2:
12+
color_print("{red}Bad args. Usage:{endc} create_backup [path_to_archive] [-y]")
13+
return
14+
if '-y' not in args:
15+
res = input(f'Mytoncore service will be stopped for few seconds while backup is created, Proceed [y/n]?')
16+
if res.lower() != 'y':
17+
print('aborted.')
18+
return
19+
else:
20+
args.pop(args.index('-y'))
21+
command_args = ["-m", self.ton.local.buffer.my_work_dir]
22+
if len(args) == 1:
23+
command_args += ["-d", args[0]]
24+
backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh')
25+
if run_as_root(["bash", backup_script_path] + command_args) == 0:
26+
color_print("create_backup - {green}OK{endc}")
27+
else:
28+
color_print("create_backup - {red}Error{endc}")
29+
# end define
30+
31+
def restore_backup(self, args):
32+
if len(args) == 0 or len(args) > 2:
33+
color_print("{red}Bad args. Usage:{endc} restore_backup <path_to_archive> [-y]")
34+
return
35+
if '-y' not in args:
36+
res = input(
37+
f'This action will overwrite existing configuration with contents of backup archive, please make sure that donor node is not in operation prior to this action. Proceed [y/n]')
38+
if res.lower() != 'y':
39+
print('aborted.')
40+
return
41+
else:
42+
args.pop(args.index('-y'))
43+
print('Before proceeding, mtc will create a backup of current configuration.')
44+
self.create_backup(['-y'])
45+
ip = str(ip2int(get_own_ip()))
46+
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-n", args[0], "-i", ip]
47+
48+
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
49+
if run_as_root(["bash", restore_script_path] + command_args) == 0:
50+
color_print("restore_backup - {green}OK{endc}")
51+
self.local.exit()
52+
else:
53+
color_print("restore_backup - {red}Error{endc}")
54+
# end define
55+
56+
def add_console_commands(self, console):
57+
console.AddItem("create_backup", self.create_backup, self.local.translate("create_backup_cmd"))
58+
console.AddItem("restore_backup", self.restore_backup, self.local.translate("restore_backup_cmd"))

mytoncore/mytoncore.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,16 +1476,21 @@ def clear_tmp(self):
14761476
def make_backup(self, election_id: str):
14771477
if not self.local.db.get("auto_backup"):
14781478
return
1479-
from mytonctrl.mytonctrl import create_backup
1479+
from modules.backups import BackupModule
1480+
module = BackupModule(self, self.local)
14801481
args = []
14811482
name = f"/mytonctrl_backup_elid{election_id}.zip"
14821483
backups_dir = self.tempDir + "/auto_backups"
14831484
if self.local.db.get("auto_backup_path"):
14841485
backups_dir = self.local.db.get("auto_backup_path")
1485-
os.makedirs(self.tempDir + "/auto_backups", exist_ok=True)
1486+
os.makedirs(backups_dir, exist_ok=True)
14861487
args.append(backups_dir + name)
14871488
self.clear_dir(backups_dir)
1488-
create_backup(self.local, self, args + ['-y'])
1489+
exit_code = module.create_backup(args + ['-y'])
1490+
if exit_code != 0:
1491+
self.local.add_log(f"Backup failed with exit code {exit_code}", "error")
1492+
else:
1493+
self.local.add_log(f"Backup created successfully", "info")
14891494

14901495
def GetValidatorKeyByTime(self, startWorkTime, endWorkTime):
14911496
self.local.add_log("start GetValidatorKeyByTime function", "debug")

mytonctrl/mytonctrl.py

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,17 @@ def inject_globals(func):
8585
console.AddItem("get", inject_globals(GetSettings), local.translate("get_cmd"))
8686
console.AddItem("set", inject_globals(SetSettings), local.translate("set_cmd"))
8787
console.AddItem("rollback", inject_globals(rollback_to_mtc1), local.translate("rollback_cmd"))
88-
console.AddItem("create_backup", inject_globals(create_backup), local.translate("create_backup_cmd"))
89-
console.AddItem("restore_backup", inject_globals(restore_backup), local.translate("restore_backup_cmd"))
9088

9189
#console.AddItem("xrestart", inject_globals(Xrestart), local.translate("xrestart_cmd"))
9290
#console.AddItem("xlist", inject_globals(Xlist), local.translate("xlist_cmd"))
9391
#console.AddItem("gpk", inject_globals(GetPubKey), local.translate("gpk_cmd"))
9492
#console.AddItem("ssoc", inject_globals(SignShardOverlayCert), local.translate("ssoc_cmd"))
9593
#console.AddItem("isoc", inject_globals(ImportShardOverlayCert), local.translate("isoc_cmd"))
9694

95+
from modules.backups import BackupModule
96+
module = BackupModule(ton, local)
97+
module.add_console_commands(console)
98+
9799
from modules.custom_overlays import CustomOverlayModule
98100
module = CustomOverlayModule(ton, local)
99101
module.add_console_commands(console)
@@ -939,53 +941,6 @@ def disable_mode(local, ton, args):
939941
#end define
940942

941943

942-
def create_backup(local, ton, args):
943-
if len(args) > 2:
944-
color_print("{red}Bad args. Usage:{endc} create_backup [path_to_archive] [-y]")
945-
return
946-
if '-y' not in args:
947-
res = input(f'Mytoncore service will be stopped for few seconds while backup is created, Proceed [y/n]?')
948-
if res.lower() != 'y':
949-
print('aborted.')
950-
return
951-
else:
952-
args.pop(args.index('-y'))
953-
command_args = ["-m", ton.local.buffer.my_work_dir]
954-
if len(args) == 1:
955-
command_args += ["-d", args[0]]
956-
backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh')
957-
if run_as_root(["bash", backup_script_path] + command_args) == 0:
958-
color_print("create_backup - {green}OK{endc}")
959-
else:
960-
color_print("create_backup - {red}Error{endc}")
961-
#end define
962-
963-
964-
def restore_backup(local, ton, args):
965-
if len(args) == 0 or len(args) > 2:
966-
color_print("{red}Bad args. Usage:{endc} restore_backup <path_to_archive> [-y]")
967-
return
968-
if '-y' not in args:
969-
res = input(f'This action will overwrite existing configuration with contents of backup archive, please make sure that donor node is not in operation prior to this action. Proceed [y/n]')
970-
if res.lower() != 'y':
971-
print('aborted.')
972-
return
973-
else:
974-
args.pop(args.index('-y'))
975-
print('Before proceeding, mtc will create a backup of current configuration.')
976-
create_backup(local, ton, ['-y'])
977-
ip = str(ip2int(get_own_ip()))
978-
command_args = ["-m", ton.local.buffer.my_work_dir, "-n", args[0], "-i", ip]
979-
980-
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
981-
if run_as_root(["bash", restore_script_path] + command_args) == 0:
982-
color_print("restore_backup - {green}OK{endc}")
983-
local.exit()
984-
else:
985-
color_print("restore_backup - {red}Error{endc}")
986-
#end define
987-
988-
989944
def Xrestart(inputArgs):
990945
if len(inputArgs) < 2:
991946
color_print("{red}Bad args. Usage:{endc} xrestart <timestamp> <args>")

0 commit comments

Comments
 (0)