def test_enough_disk_space(self):
    # Test with inadequate disk space.
    flexmock(backup_recovery_helper).should_receive(
      'get_available_disk_space').and_return(1)
    flexmock(backup_recovery_helper).should_receive(
      'get_backup_size').and_return(2)
    self.assertEquals(False, backup_recovery_helper.enough_disk_space('foo'))

    # Test with adequate disk space available.
    flexmock(backup_recovery_helper).should_receive(
      'get_available_disk_space').and_return(2)
    flexmock(backup_recovery_helper).should_receive(
      'get_backup_size').and_return(1)
    self.assertEquals(True, backup_recovery_helper.enough_disk_space('foo'))
    def test_enough_disk_space(self):
        # Test with inadequate disk space.
        flexmock(backup_recovery_helper).should_receive(
            'get_available_disk_space').and_return(1)
        flexmock(backup_recovery_helper).should_receive(
            'get_backup_size').and_return(2)
        self.assertEquals(False,
                          backup_recovery_helper.enough_disk_space('foo'))

        # Test with adequate disk space available.
        flexmock(backup_recovery_helper).should_receive(
            'get_available_disk_space').and_return(2)
        flexmock(backup_recovery_helper).should_receive(
            'get_backup_size').and_return(1)
        self.assertEquals(True,
                          backup_recovery_helper.enough_disk_space('foo'))
Esempio n. 3
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