String getPrefixKey(BackupRestoreContext ctx) throws URISyntaxException { URI uri = new URI(ctx.getExternalLocation()); String[] segments = uri.getPath().split("/"); int startIndex = uri.getScheme().equals(AmazonS3Client.S3_SERVICE_NAME) ? 1 : 2; String prefixKey = ""; for (int i=startIndex; i<segments.length; i++) { prefixKey += segments[i]; if (i < segments.length - 1) { prefixKey += "/"; } } prefixKey = (prefixKey.length() > 0 && !prefixKey.endsWith("/")) ? prefixKey + "/" : prefixKey; prefixKey += ctx.getName(); // append backup name return prefixKey; }
@Override public void run() { try { LOGGER.info("Starting DownloadSnapshot task using context: {}", context); // Send TASK_RUNNING sendStatus(driver, Protos.TaskState.TASK_RUNNING, "Started downloading snapshot"); // cleanup downloaded snapshot directory recursively if exists. Path rootPath = Paths.get(context.getLocalLocation() + File.separator + context.getName()); if (rootPath.toFile().exists()) { Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete); } backupStorageDriver.download(context); // Send TASK_FINISHED sendStatus(driver, Protos.TaskState.TASK_FINISHED, "Finished downloading snapshots"); } catch (Throwable t) { LOGGER.error("Download snapshot failed",t); sendStatus(driver, Protos.TaskState.TASK_FAILED, t.getMessage()); } }
final String accountKey = ctx.getSecretKey(); final String localLocation = ctx.getLocalLocation(); final String backupName = ctx.getName(); final String nodeId = ctx.getNodeId();
@Override public void upload(BackupRestoreContext ctx) throws Exception { final String localLocation = ctx.getLocalLocation(); final String backupName = ctx.getName(); final String nodeId = ctx.getNodeId(); final String key = getPrefixKey(ctx) + "/" + nodeId;
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof BackupRestoreContext)) return false; BackupRestoreContext that = (BackupRestoreContext) o; return Objects.equals(getNodeId(), that.getNodeId()) && Objects.equals(getName(), that.getName()) && Objects.equals(getExternalLocation(), that.getExternalLocation()) && Objects.equals(getLocalLocation(), that.getLocalLocation()) && Objects.equals(getAccountId(), that.getAccountId()) && Objects.equals(getSecretKey(), that.getSecretKey()) && Objects.equals(getRestoreType(), that.getRestoreType()); }
@Override public void run() { try { // Send TASK_RUNNING sendStatus(driver, Protos.TaskState.TASK_RUNNING, "Started uploading snapshots"); // Upload snapshots to external location. backupStorageDriver.upload(context); // Once we have uploaded all existing snapshots, let's clear on-disk snapshots daemon.clearSnapShot(context.getName()); // Send TASK_FINISHED sendStatus(driver, Protos.TaskState.TASK_FINISHED, "Finished uploading snapshots"); } catch (Throwable t) { LOGGER.error("Upload snapshot failed", t); sendStatus(driver, Protos.TaskState.TASK_FAILED, t.getMessage()); } }
@Override public void run() { try { // Send TASK_RUNNING sendStatus(driver, Protos.TaskState.TASK_RUNNING, "Started taking snapshot"); final String snapshotName = this.cassandraTask.getBackupRestoreContext().getName(); final List<String> nonSystemKeyspaces = daemon.getNonSystemKeySpaces(); LOGGER.info("Started taking snapshot for non system keyspaces: {}", nonSystemKeyspaces); for (String keyspace : nonSystemKeyspaces) { LOGGER.info("Clearing snapshot {} for keyspace: {}", snapshotName, keyspace); daemon.clearSnapshot(snapshotName, keyspace); LOGGER.info("Taking snapshot {} for keyspace: {}", snapshotName, keyspace); daemon.takeSnapShot(snapshotName, keyspace); } // Send TASK_FINISHED sendStatus(driver, Protos.TaskState.TASK_FINISHED, "Finished taking snapshot for non system keyspaces: " + nonSystemKeyspaces); } catch (Throwable t) { LOGGER.error("Snapshot failed",t); sendStatus(driver, Protos.TaskState.TASK_FAILED, t.getMessage()); } }
final String keyspaceDirectory = context.getLocalLocation() + File.separator + context.getName() + File.separator + context.getNodeId(); Path rootPath = Paths.get(context.getLocalLocation() + File.separator + context.getName()); if (rootPath.toFile().exists()) { Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)
@Override public void download(BackupRestoreContext ctx) throws Exception { final String backupName = ctx.getName(); final String nodeId = ctx.getNodeId(); final File[] keyspaces = getNonSystemKeyspaces(ctx);
@Override public int hashCode() { return Objects.hash(getNodeId(), getName(), getExternalLocation(), getLocalLocation(), getAccountId(), getSecretKey(), getRestoreType()); }
@Override public void download(BackupRestoreContext ctx) throws IOException { final String accountName = ctx.getAccountId(); final String accountKey = ctx.getSecretKey(); final String localLocation = ctx.getLocalLocation(); final String backupName = ctx.getName(); final String nodeId = ctx.getNodeId(); final String containerName = StringUtils.lowerCase(getContainerName(ctx.getExternalLocation())); // https://<account_name>.blob.core.windows.net/<container_name> final CloudBlobContainer container = getCloudBlobContainer(accountName, accountKey, containerName); if (container == null) { logger.error("Error uploading snapshots. Unable to connect to {}, for container {}.", ctx.getExternalLocation(), containerName, localLocation); return; } String keyPrefix = String.format("%s/%s", backupName, nodeId); final Map<String, Long> snapshotFileKeys = getSnapshotFileKeys(container, keyPrefix); logger.info("Snapshot files for this node: {}", snapshotFileKeys); for (String fileKey : snapshotFileKeys.keySet()) { downloadFile(localLocation, container, fileKey, snapshotFileKeys.get(fileKey)); } }
public static final CassandraData createBackupSchemaData( final String hostname, final BackupRestoreContext context) { return new CassandraData( CassandraTask.TYPE.BACKUP_SCHEMA, hostname, context.getNodeId(), context.getName(), context.getExternalLocation(), context.getLocalLocation(), context.getAccountId(), context.getSecretKey(), context.getUsesEmc(), context.getRestoreType()); }
public static final CassandraData createBackupSnapshotData( final String hostname, final BackupRestoreContext context) { return new CassandraData( CassandraTask.TYPE.BACKUP_SNAPSHOT, hostname, context.getNodeId(), context.getName(), context.getExternalLocation(), context.getLocalLocation(), context.getAccountId(), context.getSecretKey(), context.getUsesEmc(), context.getRestoreType()); }
public static final CassandraData createBackupUploadData( final String hostname, final BackupRestoreContext context) { return new CassandraData( CassandraTask.TYPE.BACKUP_UPLOAD, hostname, context.getNodeId(), context.getName(), context.getExternalLocation(), context.getLocalLocation(), context.getAccountId(), context.getSecretKey(), context.getUsesEmc(), context.getRestoreType()); }
public static final CassandraData createSnapshotDownloadData( final String hostname, final BackupRestoreContext context) { return new CassandraData( CassandraTask.TYPE.SNAPSHOT_DOWNLOAD, hostname, context.getNodeId(), context.getName(), context.getExternalLocation(), context.getLocalLocation(), context.getAccountId(), context.getSecretKey(), context.getUsesEmc(), context.getRestoreType()); }
public static final CassandraData createRestoreSnapshotData( final String hostname, final BackupRestoreContext context) { return new CassandraData( CassandraTask.TYPE.SNAPSHOT_RESTORE, hostname, context.getNodeId(), context.getName(), context.getExternalLocation(), context.getLocalLocation(), context.getAccountId(), context.getSecretKey(), context.getUsesEmc(), context.getRestoreType()); }
public static final CassandraData createRestoreSchemaData( final String hostname, final BackupRestoreContext context) { return new CassandraData( CassandraTask.TYPE.SCHEMA_RESTORE, hostname, context.getNodeId(), context.getName(), context.getExternalLocation(), context.getLocalLocation(), context.getAccountId(), context.getSecretKey(), context.getUsesEmc(), context.getRestoreType()); }