自动快照策略
更新时间:2024-07-02
创建自动快照策略
如下代码可以创建一个自动快照策略:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
args := &api.CreateASPArgs{
// 设置自动快照策略名称
Name: "sdkCreate",
// 设置一天中做快照的时间点,取值为0~23,0为午夜12点
// 例如设置做快照的时间点为下午两点:
TimePoints: []string{"14"},
// 设置一周中做快照的时间,取值为0~6,0代表周日,1~6代表周一到周六
// 例如设置做快照的时间为礼拜五:
RepeatWeekdays: []string{"5"},
// 设置自动快照保留天数,取-1则永久保留
RetentionDays: "7",
}
result, err := bccClient.CreateAutoSnapshotPolicy(args)
if err != nil {
fmt.Println("create auto snapshot policy failed:", err)
} else {
fmt.Println("create auto snapshot policy success: ", result)
}
}
绑定自动快照策略
如下代码可以将自动快照策略绑定到某个CDS磁盘:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
// 设置需要绑定的自动快照策略ID
aspId := "asp-***"
args := &api.AttachASPArgs{
// 设置需要绑定的磁盘ID列表
VolumeIds: []string{"v-***", "v-***"},
}
err := bccClient.AttachAutoSnapshotPolicy(aspId, args)
if err != nil {
fmt.Println("attach auto snapshot policy with CDS volume failed:", err)
} else {
fmt.Println("attach auto snapshot policy with CDS volume success")
}
}
解绑自动快照策略
如下代码可以将自动快照策略与指定CDS磁盘解除绑定:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
// 设置需要解绑的自动快照策略ID
aspId := "asp-***"
args := &api.DetachASPArgs{
// 设置需要解绑的磁盘ID列表
VolumeIds: []string{"v-***", "v-***"},
}
err := bccClient.DetachAutoSnapshotPolicy(aspId, args)
if err != nil {
fmt.Println("detach auto snapshot policy from CDS volume failed:", err)
} else {
fmt.Println("detach auto snapshot policy from CDS volume success")
}
}
删除自动快照策略
如下代码可以删除自动快照策略:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
// 设置需要删除的自动快照策略ID
aspId := "asp-***"
err := bccClient.DeleteAutoSnapshotPolicy(aspId)
if err != nil {
fmt.Println("delete auto snapshot policy failed:", err)
} else {
fmt.Println("delete auto snapshot policy success")
}
}
查询自动快照策略列表
如下代码可以查询到用户当前地域所有自动快照策略的列表:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
args := &api.ListASPArgs{}
result, err := bccClient.ListAutoSnapshotPolicy(args)
if err != nil {
fmt.Println("list all auto snapshot policy failed:", err)
} else {
fmt.Println("list all auto snapshot policy success: ", result)
}
}
查询自动快照策略详情
如下代码可以查询到特定自动快照策略的详细信息:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
// 设置需要查询的自动快照策略ID
aspId := "asp-***"
result, err := bccClient.GetAutoSnapshotPolicy(aspId)
if err != nil {
fmt.Println("get auto snapshot policy detail failed:", err)
} else {
fmt.Println("get auto snapshot policy detail success", result)
}
}
自动快照策略变更
如下代码可以更新一个自动快照策略:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
aspId := "asp-***"
args := &api.UpdateASPArgs{
// 自动快照策略名称,支持大小写字母、数字、中文以及-_ /.特殊字符,必须以字母开头,长度1-65。
Name: "changeDone",
// 一天中做快照时间点,取值为0~23。
TimePoints: []string{"10"},
// 一周中做快照的时间,取值为0~6。
RepeatWeekdays: []string{"0", "1"},
// 自动快照保留天数,取-1则永久保留。
RetentionDays: "2",
// 自动策略的id
AspId: "asp-***",
}
err := bccClient.UpdateAutoSnapshotPolicy(args)
fmt.Println(err)
}