使用python 脚本备份zk中的配置
python 备份代码 python3 back.py
import os
import os.path
from kazoo.client import KazooClient
from kazoo.client import KazooState
zk = KazooClient('test:2181')
zk.start()
zkBashPath = "/conf/base/"
backUp_path = "/tmp/test/"
cs = zk.get_children(zkBashPath)
for name in cs:
print(name)
file_path = backUp_path + name
if os.path.exists(file_path):
os.remove(file_path)
print('del file %s', file_path)
b, stat = zk.get(zkBashPath + name)
f = open(file_path, 'w')
f.write(str(b, "utf-8"))
f.close()
zk.stop()
还原代码 reback.py
运行: python3 reback.py name
import os
import os.path
import sys
from kazoo.client import KazooClient
from kazoo.client import KazooState
reback_name = sys.argv[1]
print(reback_name)
if reback_name is None:
print("fine name not null")
sys.exit(1)
zk = KazooClient('test:2181')
zk.start()
zkBashPath = "/conf/base/"
backUp_path = "/tmp/test/"
f = open(backUp_path+reback_name, 'r')
conf = f.readline()
if conf:
zk.set(zkBashPath+reback_name, bytes(conf, "utf8"))
else:
print("file content is blank")
参考:
https://kazoo.readthedocs.io/en/latest/basic_usage.html