Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tools/extract_backup_node_keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name="backup.tar.gz"
dest="cleared_backup_$(hostname)_$(date +%s).tar.gz"
ton_db=""
tmp_dir="tmp/backup"
user=$(logname)


# Get arguments
while getopts n:d:t: flag
do
case "${flag}" in
n) name=${OPTARG};;
d) dest=${OPTARG};;
t) ton_db=${OPTARG};;
*)
echo "Flag -${flag} is not recognized. Aborting"
exit 1 ;;
esac
done

rm -rf $tmp_dir
mkdir tmp/backup -p

if [ ! -z "$ton_db" ]; then
mkdir ${tmp_dir}/db
cp -r "$ton_db"/db/keyring ${tmp_dir}/db
cp "$ton_db"/db/config.json ${tmp_dir}/db
else
tar -xzf $name -C $tmp_dir
fi

rm -rf ${tmp_dir}/mytoncore
rm -rf ${tmp_dir}/keys
mv ${tmp_dir}/db/keyring ${tmp_dir}/db/old_keyring
mkdir ${tmp_dir}/db/keyring

keys=$(python3 -c "import json;import base64;f=open('${tmp_dir}/db/config.json');config=json.load(f);f.close();keys=set();[([keys.add(base64.b64decode(key['key']).hex().upper()) for key in v['temp_keys']], [keys.add(base64.b64decode(adnl['id']).hex().upper()) for adnl in v['adnl_addrs']]) for v in config['validators']];print('\n'.join(list(keys)))")

for key in $keys; do
mv ${tmp_dir}/db/old_keyring/${key} ${tmp_dir}/db/keyring
done

rm -rf ${tmp_dir}/db/old_keyring

tar -zcf $dest -C $tmp_dir .
chown $user:$user $dest

echo -e "Node keys backup successfully created in ${dest}!"
49 changes: 49 additions & 0 deletions tools/inject_backup_node_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import argparse
import base64
import json
import subprocess
tmp_dir = "tmp/cleared_backup"


def b64tohex(b64str: str):
return base64.b64decode(b64str).hex().upper()


def run_vc(cmd: str):
args = ['/usr/bin/ton/validator-engine-console/validator-engine-console', '-k', '/var/ton-work/keys/client', '-p', '/var/ton-work/keys/server.pub', '-a', vc_address, '--cmd', cmd]
subprocess.run(args)


parser = argparse.ArgumentParser()
parser.add_argument('-n')
parser.add_argument('-a')

args = parser.parse_args()
name = args.n
vc_address = args.a

if not name or not vc_address:
print("Usage: inject_backup_node_keys.py -n <backup_name> -a <vc_address>")
exit(1)


subprocess.run(f"rm -rf {tmp_dir}", shell=True)
subprocess.run(f"mkdir -p {tmp_dir}", shell=True)

subprocess.run(f'tar -xzf {name} -C {tmp_dir}', shell=True)

subprocess.run(f'cp -rf {tmp_dir}/db/keyring /var/ton-work/db/', shell=True)
subprocess.run(f'chown -R validator:validator /var/ton-work/db/keyring', shell=True)

with open(f'{tmp_dir}/db/config.json', 'r') as f:
config = json.load(f)

for v in config['validators']:
run_vc(f'addpermkey {b64tohex(v["id"])} {v["election_date"]} {v["expire_at"]}')
for tkey in v['temp_keys']:
run_vc(f'addtempkey {b64tohex(v["id"])} {b64tohex(tkey["key"])} {v["expire_at"]}')
for adnl in v['adnl_addrs']:
run_vc(f'addadnl {b64tohex(adnl["id"])} 0')
run_vc(f'addvalidatoraddr {b64tohex(v["id"])} {b64tohex(adnl["id"])} {v["expire_at"]}')

subprocess.run(f'systemctl restart validator', shell=True)