Automatic snapshot policy
Updated at:2025-10-20
Create automatic snapshot policy
You can create automatic snapshot policy with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 args := &api.CreateASPArgs{
15 // Set the name of the automatic snapshot policy
16 Name: "sdkCreate",
17 // Set time points for snapshots per day, ranging from 0 to 23, where 0 represents midnight 12 o'clock
18 // For example, set the snapshot time to 2 PM:
19 TimePoints: []string{"14"},
20 // Set time for snapshots per week, ranging from 0 to 6, where 0 represents Sunday and 1 to 6 represent Monday to Saturday
21 // For example, set snapshot time to Friday:
22 RepeatWeekdays: []string{"5"},
23 // Set number of days to retain automatic snapshots. A value of -1 indicates permanent retention
24 RetentionDays: "7",
25 }
26 result, err := bccClient.CreateAutoSnapshotPolicy(args)
27 if err != nil {
28 fmt.Println("create auto snapshot policy failed:", err)
29 } else {
30 fmt.Println("create auto snapshot policy success: ", result)
31 }
32}
Bind automatic snapshot policy
You can bind an automatic snapshot policy to a CDS disk with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14
15 // Set ID of automatic snapshot policy to be bound
16 aspId := "asp-***"
17 args := &api.AttachASPArgs{
18 // Set list of disk IDs to be bound
19 VolumeIds: []string{"v-***", "v-***"},
20 }
21 err := bccClient.AttachAutoSnapshotPolicy(aspId, args)
22 if err != nil {
23 fmt.Println("attach auto snapshot policy with CDS volume failed:", err)
24 } else {
25 fmt.Println("attach auto snapshot policy with CDS volume success")
26 }
27}
Unbind automatic snapshot policy
You can unbind an automatic snapshot policy from a specified CDS disk with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14
15 // Set ID of automatic snapshot policy to be unbound
16 aspId := "asp-***"
17 args := &api.DetachASPArgs{
18 // Set list of disk IDs to be unbound
19 VolumeIds: []string{"v-***", "v-***"},
20 }
21 err := bccClient.DetachAutoSnapshotPolicy(aspId, args)
22 if err != nil {
23 fmt.Println("detach auto snapshot policy from CDS volume failed:", err)
24 } else {
25 fmt.Println("detach auto snapshot policy from CDS volume success")
26 }
27}
Delete automatic snapshot policy
You can delete an automatic snapshot policy with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14
15 // Set ID of automatic snapshot policy to be deleted
16 aspId := "asp-***"
17 err := bccClient.DeleteAutoSnapshotPolicy(aspId)
18 if err != nil {
19 fmt.Println("delete auto snapshot policy failed:", err)
20 } else {
21 fmt.Println("delete auto snapshot policy success")
22 }
23}
Query the list of automatic snapshot policies
You can query the list of all automatic snapshot policies in the current region of the user with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14
15 args := &api.ListASPArgs{}
16 result, err := bccClient.ListAutoSnapshotPolicy(args)
17 if err != nil {
18 fmt.Println("list all auto snapshot policy failed:", err)
19 } else {
20 fmt.Println("list all auto snapshot policy success: ", result)
21 }
22}
Query details of automatic snapshot policy
You can query detailed information about a specific automatic snapshot policy with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14
15 // Set ID of automatic snapshot policy to be queried
16 aspId := "asp-***"
17 result, err := bccClient.GetAutoSnapshotPolicy(aspId)
18 if err != nil {
19 fmt.Println("get auto snapshot policy detail failed:", err)
20 } else {
21 fmt.Println("get auto snapshot policy detail success", result)
22 }
23}
Update automatic snapshot policy
You can update automatic snapshot policy with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 aspId := "asp-***"
15 args := &api.UpdateASPArgs{
16 // 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.
17 Name: "changeDone",
18 // Time points for snapshots per day, with value ranging from 0 to 23.
19 TimePoints: []string{"10"},
20 // Time for snapshots per week, with value ranging from 0 to 6.
21 RepeatWeekdays: []string{"0", "1"},
22 // Number of days to retain automatic snapshots. A value of -1 indicates permanent retention.
23 RetentionDays: "2",
24 // ID of automatic policy
25 AspId: "asp-***",
26 }
27 err := bccClient.UpdateAutoSnapshotPolicy(args)
28 fmt.Println(err)
29}
