1+ import os
2+ import subprocess
3+ import time
4+
15import pkg_resources
26
37from modules .module import MtcModule
4- from mypylib .mypylib import color_print , ip2int , run_as_root
8+ from mypylib .mypylib import color_print , ip2int , run_as_root , parse
59from mytoninstaller .config import get_own_ip
610
711
812class BackupModule (MtcModule ):
913
14+ def create_keyring (self , dir_name ):
15+ keyring_dir = dir_name + '/keyring'
16+ self .ton .validatorConsole .Run (f'exportallprivatekeys { keyring_dir } ' )
17+
18+ def create_tmp_ton_dir (self ):
19+ result = self .ton .validatorConsole .Run ("getconfig" )
20+ text = parse (result , "---------" , "--------" )
21+ dir_name = self .ton .tempDir + f'/ton_backup_{ int (time .time () * 1000 )} '
22+ dir_name_db = dir_name + '/db'
23+ os .makedirs (dir_name_db )
24+ with open (dir_name_db + '/config.json' , 'w' ) as f :
25+ f .write (text )
26+ self .create_keyring (dir_name_db )
27+ return dir_name
28+
1029 def create_backup (self , args ):
1130 if len (args ) > 2 :
12- color_print ("{red}Bad args. Usage:{endc} create_backup [path_to_archive ] [-y]" )
31+ color_print ("{red}Bad args. Usage:{endc} create_backup [filename ] [-y]" )
1332 return
1433 if '-y' not in args :
1534 res = input (f'Mytoncore service will be stopped for few seconds while backup is created, Proceed [y/n]?' )
@@ -18,19 +37,23 @@ def create_backup(self, args):
1837 return
1938 else :
2039 args .pop (args .index ('-y' ))
21- command_args = ["-m" , self .ton .local .buffer .my_work_dir ]
40+ dir_ = self .create_tmp_ton_dir ()
41+ command_args = ["-m" , self .ton .local .buffer .my_work_dir , "-t" , dir_ ]
2242 if len (args ) == 1 :
2343 command_args += ["-d" , args [0 ]]
2444 backup_script_path = pkg_resources .resource_filename ('mytonctrl' , 'scripts/create_backup.sh' )
25- if run_as_root (["bash" , backup_script_path ] + command_args ) == 0 :
45+ process = subprocess .run (["bash" , backup_script_path ] + command_args , timeout = 5 )
46+
47+ if process .returncode == 0 :
2648 color_print ("create_backup - {green}OK{endc}" )
2749 else :
2850 color_print ("create_backup - {red}Error{endc}" )
51+ return process .returncode
2952 # end define
3053
3154 def restore_backup (self , args ):
3255 if len (args ) == 0 or len (args ) > 2 :
33- color_print ("{red}Bad args. Usage:{endc} restore_backup <path_to_archive > [-y]" )
56+ color_print ("{red}Bad args. Usage:{endc} restore_backup <filename > [-y]" )
3457 return
3558 if '-y' not in args :
3659 res = input (
0 commit comments