python 백업 zk 설정 항목

1426 단어 zookeeperpython
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
실행:python 3  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

좋은 웹페이지 즐겨찾기