Automatic snapshot policy
Updated at:2025-10-20
Create automatic snapshot policy
To create an automatic snapshot policy, refer to the following code:
Java
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.asp.CreateAspRequest;
7import com.baidubce.services.bcc.model.asp.CreateAspResponse;
8import java.util.ArrayList;
9import java.util.List;
10public class Main {
11 public static void main(String[] args) {
12 // Set your AK, SK and the endpoint to be accessed
13 String endpoint = "bcc.bj.baidubce.com";
14 String ak = "ak";
15 String sk = "sk";
16 // Set default configuration
17 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
18 .withProtocol(Protocol.HTTP)
19 .withCredentials(new DefaultBceCredentials(ak, sk))
20 .withEndpoint(endpoint);
21 // Create a BCC client
22 BccClient bccClient = new BccClient(bccClientConfiguration);
23 CreateAspRequest createAspRequest = new CreateAspRequest();
24 // Set snapshot name
25 createAspRequest.setName("test-asp-name");
26 // Set time points for snapshots per day, ranging from 0 to 23, where 0 represents midnight 12 o'clock
27 // For example, set the time points for taking snapshots to 2 AM and 2 PM:
28 List<Integer> timePoints = new ArrayList<Integer>();
29 timePoints.add(2);
30 timePoints.add(14);
31 createAspRequest.setTimePoints(timePoints);
32 // Set time for snapshots per week, ranging from 0 to 6, where 0 represents Sunday and 1 to 6 represent Monday to Saturday
33 // For example, set snapshot time to Friday:
34 List<Integer> repeatWeekdays = new ArrayList<Integer>();
35 repeatWeekdays.add(5);
36 createAspRequest.setRepeatWeekdays(repeatWeekdays);
37 // Set number of days to retain automatic snapshots. A value of -1 indicates permanent retention
38 createAspRequest.setRetentionDays("7");
39 CreateAspResponse response = bccClient.createAsp(createAspRequest);
40 System.out.println(response);
41 }
42}
Bind automatic snapshot policy
To bind an automatic snapshot policy, refer to the following code:
Java
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.asp.AttachAspRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class Main {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient bccClient = new BccClient(bccClientConfiguration);
22 AttachAspRequest attachAspRequest = new AttachAspRequest();
23 // Set ID of automatic snapshot policy to be bound
24 attachAspRequest.setAspId("asp-***");
25 // Set the disk ID to be bound
26 List<String> volumeIds = new ArrayList<String>();
27 volumeIds.add("v-***");
28 volumeIds.add("v-***");
29 attachAspRequest.setVolumeIds(volumeIds);
30 bccClient.attachAsp(attachAspRequest);
31 }
32}
Unbind automatic snapshot policy
To unbind an automatic snapshot policy, refer to the following code:
Java
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.asp.DetachAspRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class Main {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient bccClient = new BccClient(bccClientConfiguration);
22 DetachAspRequest detachAspRequest = new DetachAspRequest();
23 // Set ID of automatic snapshot policy to be unbound
24 detachAspRequest.setAspId("asp-***");
25 // Set the disk ID to be unbound
26 List<String> volumeIds = new ArrayList<String>();
27 volumeIds.add("v-***");
28 volumeIds.add("v-***");
29 detachAspRequest.setVolumeIds(volumeIds);
30 bccClient.detachAsp(detachAspRequest);
31 }
32}
Delete automatic snapshot policy
To delete an automatic snapshot policy, refer to the following code:
Java
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.asp.DeleteAspRequest;
7public class Main {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient bccClient = new BccClient(bccClientConfiguration);
20 DeleteAspRequest deleteAspRequest = new DeleteAspRequest();
21 // Set ID of automatic snapshot policy to be deleted
22 deleteAspRequest.setAspId("asp-***");
23 bccClient.deleteAsp(deleteAspRequest);
24 }
25}
Query the list of automatic snapshot policies
To query the list of automatic snapshot policies, refer to the following code:
Java
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.AutoSnapshotPolicyModel;
7import com.baidubce.services.bcc.model.asp.ListAspsRequest;
8import com.baidubce.services.bcc.model.asp.ListAspsResponse;
9public class Main {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient bccClient = new BccClient(bccClientConfiguration);
22 ListAspsRequest listAspsRequest = new ListAspsRequest();
23 // Set the name of automatic snapshot policy to be searched
24 listAspsRequest.setAspName("test_stratery");
25 // Set ID of automatic snapshot policy to be searched
26 listAspsRequest.setMarker("asp-***");
27 // Set the size of returned data
28 listAspsRequest.setMaxKeys(10);
29 ListAspsResponse listAspsResponse = bccClient.listAsps(listAspsRequest);
30 for (AutoSnapshotPolicyModel asp : listAspsResponse.getAutoSnapshotPolicys()) {
31 System.out.println(asp);
32 }
33 }
34}
Query details of automatic snapshot policy
To query the details of automatic snapshot policies, refer to the following code:
Java
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.asp.GetAspRequest;
7import com.baidubce.services.bcc.model.asp.GetAspResponse;
8public class Main {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient bccClient = new BccClient(bccClientConfiguration);
21 GetAspRequest getAspRequest = new GetAspRequest();
22 // Set ID of automatic snapshot policy to be searched
23 getAspRequest.setAspId("asp-***");
24 GetAspResponse getAspResponse = bccClient.getAsp(getAspRequest);
25 System.out.println(getAspResponse);
26 }
27}
Update automatic snapshot policy
To update an automatic snapshot policy, refer to the following code. Supported changes include policy name, creation time, recurrence days, and retention period.
Java
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.asp.UpdateAspRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class UpdateAsp {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "http://bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient client = new BccClient(bccClientConfiguration);
22 String aspId = "asp-osTov***";
23 String name= "changeDown";
24 List<String> timePoints = new ArrayList<String>(){{add("12"); add("22");}};
25 List<String> repeatWeekdays = new ArrayList<String>(){{add("1"); add("5");}};
26 String retentionDays = "5";
27 UpdateAspRequest updateAspRequest = new UpdateAspRequest();
28 // Required, ID of the automatic policy
29 updateAspRequest.setAspId(aspId);
30 // Required, automatic snapshot policy name, which supports uppercase and lowercase letters, numbers, Chinese characters, and -_/special characters, starting with a letter and a length of 1-65.
31 updateAspRequest.setName(name);
32 // Required, time points for snapshots per day, with value ranging from 0 to 23.
33 updateAspRequest.setTimePoints(timePoints);
34 // Required, time point during the day when snapshots are taken, with values ranging from 0 ~ 6.
35 updateAspRequest.setRepeatWeekdays(repeatWeekdays);
36 // Required, number of days to retain automatic snapshots. A value of -1 indicates permanent retention.
37 updateAspRequest.setRetentionDays(retentionDays);
38 client.updateAsp(updateAspRequest);
39 }
40}
