Skip to content

Commit 6a9a2c5

Browse files
committed
get keys and node config for backups from v-console
1 parent c383356 commit 6a9a2c5

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

modules/backups.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1+
import os
2+
import subprocess
3+
import time
4+
15
import pkg_resources
26

37
from 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
59
from mytoninstaller.config import get_own_ip
610

711

812
class 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(

mytonctrl/scripts/create_backup.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ dest="mytonctrl_backup_$(hostname)_$(date +%s).tar.gz"
22
mtc_dir="$HOME/.local/share/mytoncore"
33
user=$(logname)
44
ton_dir="/var/ton-work"
5+
keys_dir="/var/ton-work/keys"
56
# Get arguments
6-
while getopts d:m:t: flag
7+
while getopts d:m:t:k: flag
78
do
89
case "${flag}" in
910
d) dest=${OPTARG};;
1011
m) mtc_dir=${OPTARG};;
1112
t) ton_dir=${OPTARG};;
13+
k) keys_dir=${OPTARG};;
1214
*)
1315
echo "Flag -${flag} is not recognized. Aborting"
1416
exit 1 ;;
@@ -30,7 +32,7 @@ mkdir $tmp_dir/db
3032

3133
cp $ton_dir/db/config.json ${tmp_dir}/db
3234
cp -r $ton_dir/db/keyring ${tmp_dir}/db
33-
cp -r $ton_dir/keys ${tmp_dir}
35+
cp -r $keys_dir ${tmp_dir}
3436
cp -r $mtc_dir $tmp_dir
3537

3638
python3 -c "import json;f=open('${tmp_dir}/db/config.json');json.load(f);f.close()" || exit 1 # Check if config.json is copied correctly

0 commit comments

Comments
 (0)