def restore_data(path, keyname):
  logging.info("Starting new zk restore.")

  running = subprocess.call(['service', CONTROLLER_SERVICE, 'status']) == 0
  if running:
    logging.error('Please stop AppScale before restoring ZooKeeper.')
    sys.exit(1)

  zk_ips = appscale_info.get_zk_node_ips()
  if len(zk_ips) < 1:
    raise BRException('Unable to find any ZooKeeper machines.')

  timestamp = int(time.time())
  restore_file = '{}/zk_restore_{}.tar.gz'.\
    format(BACKUP_DIR_LOCATION, timestamp)

  # Cache name of ZooKeeper service for each machine.
  zk_service_names = {}
  for zk_ip in zk_ips:
    zk_service_names[zk_ip] = utils.zk_service_name(zk_ip, keyname)

  # Copy restore file to and start ZooKeeper on relevant machines.
  logging.info('Copying data to ZooKeeper machines.')
  for zk_ip in zk_ips:
    zk_service = zk_service_names[zk_ip]
    try:
      utils.scp_to(zk_ip, keyname, path, restore_file)
      utils.ssh(zk_ip, keyname, 'service {} restart'.format(zk_service))
    except subprocess.CalledProcessError as error:
      logging.exception('Failed to prepare restore on {}'.format(zk_ip))
      utils.ssh(zk_ip, keyname, 'rm -f {}'.format(restore_file))
      utils.ssh(zk_ip, keyname, 'service {} stop'.format(zk_service))
      raise error

  # Save deployment-specific data.
  deployment_data = StringIO()
  hosts_template = ':{port},'.join(zk_ips) + ':{port}'
  zk = kazoo.client.KazooClient(
    hosts=hosts_template.format(port=zktransaction.DEFAULT_PORT))
  zk.start()
  for zk_node in ZK_KEEP_PATHS:
    recursive_dump(zk, zk_node, deployment_data)
  zk.stop()

  # Stop ZooKeeper and clear existing data directory.
  logging.info('Clearing existing data on ZooKeeper machines.')
  for zk_ip in zk_ips:
    zk_service = zk_service_names[zk_ip]
    try:
      utils.ssh(zk_ip, keyname, 'service {} stop'.format(zk_service))
      utils.ssh(zk_ip, keyname, 'rm -rf {}/*'.format(ZK_DATA_DIR))
    except subprocess.CalledProcessError as error:
      logging.exception('Unable to clear data on {}'.format(zk_ip))
      deployment_data.close()
      utils.ssh(zk_ip, keyname, 'rm -f {}'.format(restore_file))
      utils.ssh(zk_ip, keyname, 'service {} stop'.format(zk_service))
      raise error

  # Restore data and restart ZooKeeper on relevant machines.
  logging.info('Restoring data on ZooKeeper machines.')
  for zk_ip in zk_ips:
    zk_service = zk_service_names[zk_ip]
    try:
      utils.ssh(zk_ip, keyname,
        'tar xzf {} -C {}'.format(restore_file, ZK_DATA_DIR))
      utils.ssh(zk_ip, keyname, 'service {} start'.format(zk_service))
    except subprocess.CalledProcessError as error:
      logging.exception('Unable to restore on {}'.format(zk_ip))
      deployment_data.close()
      utils.ssh(zk_ip, keyname, 'rm -f {}'.format(restore_file))
      utils.ssh(zk_ip, keyname, 'service {} stop'.format(zk_service))
      raise error

  # Restore deployment-specific data.
  logging.info('Restoring deployment-specific data.')
  zk = kazoo.client.KazooClient(hosts=':2181,'.join(zk_ips) + ':2181')
  zk.start()
  for zk_node in ZK_KEEP_PATHS:
    recursive_flush(zk, zk_node)
  deployment_data.seek(0)
  restore_zk(zk, deployment_data)
  zk.stop()

  # Stop ZooKeeper on relevant machines.
  logging.info('Stopping ZooKeeper.')
  for zk_ip in zk_ips:
    zk_service = zk_service_names[zk_ip]
    try:
      utils.ssh(zk_ip, keyname, 'service {} stop'.format(zk_service))
      utils.ssh(zk_ip, keyname, 'rm -rf {}'.format(restore_file))
    finally:
      deployment_data.close()

  logging.info("Done with zk restore.")
  return True
Restores the Zookeeper snapshot.
dataset/ETHPy150Open AppScale/appscale/AppDB/backup/zookeeper_backup.py/restore_data