AWS SDK for Java를 사용하여 스냅샷 관리
다음 예는 아래와 같이 스냅샷과 관련된 공통 작업에 대해 나타냅니다.
-
클러스터의 수동 스냅샷을 생성합니다.
-
모든 클러스터 스냅샷에 대한 정보를 표시합니다.
-
클러스터의 수동 스냅샷을 삭제합니다.
이번 예에서는 클러스터의 스냅샷부터 시작합니다. 스냅샷이 성공적으로 생성되면 해당 클러스터에서 새로운 스냅샷 이전에 생성된 수동 스냅샷은 모두 삭제됩니다. 수동 스냅샷의 생성이 시작되어도 스냅샷을 바로 사용할 수 있는 것은 아닙니다. 따라서 이번 예에서는 describeClusterSnapshot
메서드를 호출하여 스냅샷 상태를 폴링할 수 있는 루프를 사용합니다. 일반적으로 생성 시작 후 스냅샷을 사용하려면 약간의 시간이 필요합니다. 스냅샷 복사에 대한 자세한 내용은 단원을 참조하십시오Amazon Redshift 스냅샷 및 백업
다음 예를 실행하기 위한 단계별 지침은 Eclipse를 사용하여 Amazon Redshift의 Java 예 실행 단원을 참조하십시오. 코드를 업데이트하고 클러스터 식별자를 입력해야 합니다.
/** * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ // snippet-sourcedescription:[CreateAndDescribeSnapshot demonstrates how to create an Amazon Redshift cluster snapshot and describe existing snapshots.] // snippet-service:[redshift] // snippet-keyword:[Java] // snippet-keyword:[Amazon Redshift] // snippet-keyword:[Code Sample] // snippet-keyword:[CreateClusterSnapshot] // snippet-keyword:[DeleteClusterSnapshot] // snippet-keyword:[DescribeClusterSnapshots] // snippet-sourcetype:[full-example] // snippet-sourcedate:[2019-01-30] // snippet-sourceauthor:[AWS] // snippet-start:[redshift.java.CreateAndDescribeSnapshot.complete] package com.amazonaws.services.redshift; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import com.amazonaws.services.redshift.model.*; public class CreateAndDescribeSnapshot { public static AmazonRedshift client; public static String clusterIdentifier = "***provide a cluster identifier***"; public static long sleepTime = 20; public static void main(String[] args) throws IOException { // Default client using the {@link com.amazonaws.auth.DefaultAWSCredentialsProviderChain} client = AmazonRedshiftClientBuilder.defaultClient(); try { // Unique snapshot identifier String snapshotId = "my-snapshot-" + (new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")).format(new Date()); Date createDate = createManualSnapshot(snapshotId); waitForSnapshotAvailable(snapshotId); describeSnapshots(); deleteManualSnapshotsBefore(createDate); describeSnapshots(); } catch (Exception e) { System.err.println("Operation failed: " + e.getMessage()); } } private static Date createManualSnapshot(String snapshotId) { CreateClusterSnapshotRequest request = new CreateClusterSnapshotRequest() .withClusterIdentifier(clusterIdentifier) .withSnapshotIdentifier(snapshotId); Snapshot snapshot = client.createClusterSnapshot(request); System.out.format("Created cluster snapshot: %s\n", snapshotId); return snapshot.getSnapshotCreateTime(); } private static void describeSnapshots() { DescribeClusterSnapshotsRequest request = new DescribeClusterSnapshotsRequest() .withClusterIdentifier(clusterIdentifier); DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(request); printResultSnapshots(result); } private static void deleteManualSnapshotsBefore(Date creationDate) { DescribeClusterSnapshotsRequest request = new DescribeClusterSnapshotsRequest() .withEndTime(creationDate) .withClusterIdentifier(clusterIdentifier) .withSnapshotType("manual"); DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(request); for (Snapshot s : result.getSnapshots()) { DeleteClusterSnapshotRequest deleteRequest = new DeleteClusterSnapshotRequest() .withSnapshotIdentifier(s.getSnapshotIdentifier()); Snapshot deleteResult = client.deleteClusterSnapshot(deleteRequest); System.out.format("Deleted snapshot %s\n", deleteResult.getSnapshotIdentifier()); } } private static void printResultSnapshots(DescribeClusterSnapshotsResult result) { System.out.println("\nSnapshot listing:"); for (Snapshot snapshot : result.getSnapshots()) { System.out.format("Identifier: %s\n", snapshot.getSnapshotIdentifier()); System.out.format("Snapshot type: %s\n", snapshot.getSnapshotType()); System.out.format("Snapshot create time: %s\n", snapshot.getSnapshotCreateTime()); System.out.format("Snapshot status: %s\n\n", snapshot.getStatus()); } } private static Boolean waitForSnapshotAvailable(String snapshotId) throws InterruptedException { Boolean snapshotAvailable = false; System.out.println("Wating for snapshot to become available."); while (!snapshotAvailable) { DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(new DescribeClusterSnapshotsRequest() .withSnapshotIdentifier(snapshotId)); String status = (result.getSnapshots()).get(0).getStatus(); if (status.equalsIgnoreCase("available")) { snapshotAvailable = true; } else { System.out.print("."); Thread.sleep(sleepTime*1000); } } return snapshotAvailable; } } // snippet-end:[redshift.java.CreateAndDescribeSnapshot.complete]