Example #1
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
Example #2
0
def backup_data(storage, path=''):
  """ Backup Cassandra snapshot data directories/files.

  Args:
    storage: A str, the storage that is used for storing the backup.
    path: A str, the full backup filename path to use for cloud backup.
  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 db backup.")
  clear_old_snapshots()

  if not create_snapshot():
    logging.error("Failed to create Cassandra snapshots. Aborting backup...")
    return None

  files = backup_recovery_helper.get_snapshot_paths('cassandra')
  if not files:
    logging.error("No Cassandra files were found to tar up. Aborting backup...")
    return None

  if not backup_recovery_helper.enough_disk_space('cassandra'):
    logging.error("There's not enough available space to create another db"
      "backup. Aborting...")
    return None

  tar_file = backup_recovery_helper.tar_backup_files(files,
    CASSANDRA_BACKUP_FILE_LOCATION)
  if not tar_file:
    logging.error('Error while tarring up snapshot files. Aborting backup...')
    clear_old_snapshots()
    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 db backup!")
    clear_old_snapshots()
    backup_recovery_helper.\
      delete_secondary_backup(CASSANDRA_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 db backup!")
      backup_recovery_helper.\
        delete_secondary_backup(CASSANDRA_BACKUP_FILE_LOCATION)

    # Remove local backup file(s).
    clear_old_snapshots()
    backup_recovery_helper.delete_local_backup_file(tar_file)
    return return_value
 def test_tar_backup_files(self):
   flexmock(backup_recovery_helper).should_receive("rename").and_return(True)
   fake_tar = FakeTar()
   flexmock(tarfile).should_receive('open').and_return(fake_tar)
   backup_recovery_helper.tar_backup_files(['1', '2'], 'some/tar')
   self.assertEquals(fake_tar.add_count, 2)