Beispiel #1
0
def delete_local_backup_file(local_file=BACKUP_FILE_LOCATION):
  """ Removes the local backup file.

  Args:
    local_file: A str, the path to the backup file to delete.
  """
  if not backup_recovery_helper.remove(local_file):
    logging.warning("No local backup file to remove. Skipping...")
Beispiel #2
0
def delete_local_backup_file(local_file=BACKUP_FILE_LOCATION):
    """ Removes the local backup file.

  Args:
    local_file: A str, the path to the backup file to delete.
  """
    if not backup_recovery_helper.remove(local_file):
        logging.warning("No local backup file to remove. Skipping...")
Beispiel #3
0
def restore_data(storage, path=''):
    """ Restores the Zookeeper snapshot.

  Args:
    storage: A str, one of the StorageTypes class members.
    path: A str, the name of the backup file to restore from.
  """
    if storage not in StorageTypes().get_storage_types():
        logging.error("Storage '{0}' not supported.")
        return False

    logging.info("Starting new zk restore.")

    if storage == StorageTypes.GCS:
        # Download backup file and store locally with a fixed name.
        if not gcs_helper.download_from_bucket(path,
                                               ZOOKEEPER_BACKUP_FILE_LOCATION):
            logging.error("Download from GCS failed. Aborting recovery...")
            return False

    # TODO Make sure there's a snapshot to rollback to if restore fails.
    # Not pressing for fresh deployments.

    flush_zk()
    try:
        backup_recovery_helper.untar_backup_files(
            ZOOKEEPER_BACKUP_FILE_LOCATION)
    except backup_exceptions.BRException as br_exception:
        logging.exception(
            "Error while unpacking backup files. Exception: {0}".format(
                str(br_exception)))
        if storage == StorageTypes.GCS:
            backup_recovery_helper.\
              delete_local_backup_file(ZOOKEEPER_BACKUP_FILE_LOCATION)
        return False
    restore_zk(TMP_ZOOKEEPER_BACKUP)

    # Local cleanup.
    backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
    if storage == StorageTypes.GCS:
        backup_recovery_helper.\
          delete_local_backup_file(ZOOKEEPER_BACKUP_FILE_LOCATION)

    logging.info("Done with zk restore.")
    return True
def restore_data(storage, path=''):
  """ Restores the Zookeeper snapshot.

  Args:
    storage: A str, one of the StorageTypes class members.
    path: A str, the name of the backup file to restore from.
  """
  if storage not in StorageTypes().get_storage_types():
    logging.error("Storage '{0}' not supported.")
    return False

  logging.info("Starting new zk restore.")

  if storage == StorageTypes.GCS:
    # Download backup file and store locally with a fixed name.
    if not gcs_helper.download_from_bucket(path,
        ZOOKEEPER_BACKUP_FILE_LOCATION):
      logging.error("Download from GCS failed. Aborting recovery...")
      return False

  # TODO Make sure there's a snapshot to rollback to if restore fails.
  # Not pressing for fresh deployments.

  flush_zk()
  try:
    backup_recovery_helper.untar_backup_files(ZOOKEEPER_BACKUP_FILE_LOCATION)
  except backup_exceptions.BRException as br_exception:
    logging.exception("Error while unpacking backup files. Exception: {0}".
      format(str(br_exception)))
    if storage == StorageTypes.GCS:
      backup_recovery_helper.\
        delete_local_backup_file(ZOOKEEPER_BACKUP_FILE_LOCATION)
    return False
  restore_zk(TMP_ZOOKEEPER_BACKUP)

  # Local cleanup.
  backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
  if storage == StorageTypes.GCS:
    backup_recovery_helper.\
      delete_local_backup_file(ZOOKEEPER_BACKUP_FILE_LOCATION)

  logging.info("Done with zk restore.")
  return True
Beispiel #5
0
def backup_data(storage, path=''):
    """ Backup Zookeeper directories/files.

  Args:
    storage: A str, one of the StorageTypes class members.
    path: A str, the name of the backup file to be created.
  Returns:
    The path to the backup file on success, None otherwise.
  """
    if storage not in StorageTypes().get_storage_types():
        logging.error("Storage '{0}' not supported.")
        return None

    logging.info("Starting new zk backup.")
    dump_zk(TMP_ZOOKEEPER_BACKUP)

    tar_file = backup_recovery_helper.tar_backup_files(
        [TMP_ZOOKEEPER_BACKUP], ZOOKEEPER_BACKUP_FILE_LOCATION)
    if not tar_file:
        logging.error(
            'Error while tarring up Zookeeper files. Aborting backup...')
        backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
        backup_recovery_helper.delete_local_backup_file(tar_file)
        backup_recovery_helper.move_secondary_backup(tar_file)
        return None

    if storage == StorageTypes.LOCAL_FS:
        logging.info("Done with local zk backup!")
        backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
        backup_recovery_helper.\
          delete_secondary_backup(ZOOKEEPER_BACKUP_FILE_LOCATION)
        return tar_file
    elif storage == StorageTypes.GCS:
        return_value = path
        # Upload to GCS.
        if not gcs_helper.upload_to_bucket(path, tar_file):
            logging.error("Upload to GCS failed. Aborting backup...")
            backup_recovery_helper.move_secondary_backup(tar_file)
            return_value = None
        else:
            logging.info("Done with zk backup!")
            backup_recovery_helper.\
              delete_secondary_backup(ZOOKEEPER_BACKUP_FILE_LOCATION)

        # Remove local backup files.
        backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
        backup_recovery_helper.delete_local_backup_file(tar_file)
        return return_value
def backup_data(storage, path=''):
  """ Backup Zookeeper directories/files.

  Args:
    storage: A str, one of the StorageTypes class members.
    path: A str, the name of the backup file to be created.
  Returns:
    The path to the backup file on success, None otherwise.
  """
  if storage not in StorageTypes().get_storage_types():
    logging.error("Storage '{0}' not supported.")
    return None

  logging.info("Starting new zk backup.")
  dump_zk(TMP_ZOOKEEPER_BACKUP)

  tar_file = backup_recovery_helper.tar_backup_files([TMP_ZOOKEEPER_BACKUP],
    ZOOKEEPER_BACKUP_FILE_LOCATION)
  if not tar_file:
    logging.error('Error while tarring up Zookeeper files. Aborting backup...')
    backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
    backup_recovery_helper.delete_local_backup_file(tar_file)
    backup_recovery_helper.move_secondary_backup(tar_file)
    return None

  if storage == StorageTypes.LOCAL_FS:
    logging.info("Done with local zk backup!")
    backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
    backup_recovery_helper.\
      delete_secondary_backup(ZOOKEEPER_BACKUP_FILE_LOCATION)
    return tar_file
  elif storage == StorageTypes.GCS:
    return_value = path
    # Upload to GCS.
    if not gcs_helper.upload_to_bucket(path, tar_file):
      logging.error("Upload to GCS failed. Aborting backup...")
      backup_recovery_helper.move_secondary_backup(tar_file)
      return_value = None
    else:
      logging.info("Done with zk backup!")
      backup_recovery_helper.\
        delete_secondary_backup(ZOOKEEPER_BACKUP_FILE_LOCATION)

    # Remove local backup files.
    backup_recovery_helper.remove(TMP_ZOOKEEPER_BACKUP)
    backup_recovery_helper.delete_local_backup_file(tar_file)
    return return_value
Beispiel #7
0
def delete_secondary_backup():
  """ Deletes the secondary backup if it exists, upon successful backup. """
  if not backup_recovery_helper.remove("{0}{1}".
      format(BACKUP_FILE_LOCATION, BACKUP_ROLLBACK_SUFFIX)):
    logging.warning("No secondary backup to remove. Skipping...")
Beispiel #8
0
  def test_remove(self):
    flexmock(os).should_receive('remove').and_return().at_least().times(1)
    self.assertEquals(True, backup_recovery_helper.remove('foo'))

    flexmock(os).should_receive('remove').and_raise(OSError)
    self.assertEquals(False, backup_recovery_helper.remove('foo'))
Beispiel #9
0
def delete_secondary_backup():
    """ Deletes the secondary backup if it exists, upon successful backup. """
    if not backup_recovery_helper.remove("{0}{1}".format(
            BACKUP_FILE_LOCATION, BACKUP_ROLLBACK_SUFFIX)):
        logging.warning("No secondary backup to remove. Skipping...")