def test_get_snapshot_paths(self): os_mock = flexmock(os) os_mock.should_call('walk') os_mock.should_receive('walk').and_yield(('/snapshots/xxx', ['dirname'], '')).once() self.assertEquals(['/snapshots/xxx'], backup_recovery_helper.get_snapshot_paths('cassandra')) # Test with no files present that match the filters. self.assertEquals([], backup_recovery_helper.get_snapshot_paths('foo'))
def test_get_snapshot_paths(self): os_mock = flexmock(os) os_mock.should_call('walk') os_mock.should_receive('walk').and_yield( ('/snapshots/xxx', ['dirname'], '')).once() self.assertEquals( ['/snapshots/xxx'], backup_recovery_helper.get_snapshot_paths('cassandra')) # Test with no files present that match the filters. self.assertEquals([], backup_recovery_helper.get_snapshot_paths('foo'))
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