管理快照内测版
本指南介绍如何创建和管理快照。
创建快照
创建快照前,建议您停止向目标 Collection 写入数据,并调用 flush(),以避免可能的数据丢失。
📘说明
调用 flush() 并非强制要求,但强烈建议执行,以避免数据丢失。如果跳过此步骤,快照只会包含已 Flush 的数据。
为快照命名时,请使用清晰且有描述性的名称,例如 "daily_backup_20240101" 或 "v2.1_production_release",避免使用 "backup1" 和 "test" 等通用名称。合理使用快照名称,以区分不同版本、环境和阶段的快照。
以下代码示例假设您已有一个名为 my_collection 的 Collection。
- Python
- Java
- Go
- NodeJS
- cURL
from pymilvus import MilvusClient
client = MilvusClient(
uri="https://{cluster-id}.{region}.vectordb.zilliz.com.cn:19530",
token="YOUR_CLUSTER_TOKEN"
)
# Recommended: Flush data before creating snapshot to ensure all data is included
client.flush(collection_name="my_collection")
# Create snapshot for entire collection
client.create_snapshot(
collection_name="my_collection",
snapshot_name="backup_20240101",
description="Daily backup for January 1st, 2024"
)
// java
import (
"context"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
client, err := milvusclient.New(context.Background(), &milvusclient.ClientConfig{
Address: "https://{cluster-id}.{region}.vectordb.zilliz.com.cn:19530",
Token: "YOUR_CLUSTER_TOKEN",
})
// Recommended: Flush data before creating snapshot to ensure all data is included
err = client.Flush(context.Background(), milvusclient.NewFlushOption("my_collection"))
if err != nil {
log.Fatal(err)
}
// Create snapshot
createOpt := milvusclient.NewCreateSnapshotOption("backup_20240101", "my_collection").
WithDescription("Daily backup for January 1st, 2024")
err = client.CreateSnapshot(context.Background(), createOpt)
// node.js
# restful
列出快照
您可以列出现有快照的名称。
- Python
- Java
- Go
- NodeJS
- cURL
# List all snapshots for a collection
snapshots = client.list_snapshots(
collection_name="my_collection"
)
// java
// List snapshots for collection
listOpt := milvusclient.NewListSnapshotsOption().
WithCollectionName("my_collection")
snapshots, err := client.ListSnapshots(context.Background(), listOpt)
// node.js
# bash
查看快照详情
您可以获取指定快照的详细信息。
- Python
- Java
- Go
- NodeJS
- cURL
snapshot_info = client.describe_snapshot(
snapshot_name="backup_20240101",
include_collection_info=True
)
print(f"Snapshot ID: {snapshot_info.id}")
print(f"Collection: {snapshot_info.collection_name}")
print(f"Created: {snapshot_info.create_ts}")
print(f"Description: {snapshot_info.description}")
// java
describeOpt := milvusclient.NewDescribeSnapshotOption("backup_20240101")
resp, err := client.DescribeSnapshot(context.Background(), describeOpt)
fmt.Printf("Snapshot ID: %d\n", resp.GetSnapshotInfo().GetId())
fmt.Printf("Collection: %s\n", resp.GetSnapshotInfo().GetCollectionName())
// node.js
# restful
固定/取消固定快照数据
在恢复过程中,您可以固定快照,以临时保护其底层数据不被垃圾回收;也可以取消固定,以释放这些数据。
您还可以为固定操作设置存活时间(TTL)。当 TTL 到期后,被固定的数据将自动释放。
- Python
- Java
- Go
- NodeJS
- cURL
pin_id = client.pin_snapshot_data(
snapshot_name="backup_20240101",
collection_name="my_collection",
ttl_seconds=3600,
)
client.unpin_snapshot_data(
pin_id=pin_id
)
// java
pinID, err := cli.PinSnapshotData(
ctx,
client.NewPinSnapshotDataOption("backup_20240101", "my_collection").WithTTLSeconds(3600),
)
if err != nil {
return err
}
defer func() {
_ = cli.UnpinSnapshotData(ctx, client.NewUnpinSnapshotDataOption(pinID))
}()
// do work with pinned snapshot data
// node.js
# restful
恢复快照
您可以将快照恢复到新的 Collection。该操作为异步操作,并会返回一个任务 ID,用于跟踪恢复进度。
恢复过程使用 Copy-segment 机制,而不是数据导入,因此效率更高,原因如下:
-
直接从快照存储中复制 Segment 文件(Binlog、Deltalog 和索引文件);
-
保留 Field ID 和索引 ID,确保与现有数据文件兼容;
-
避免数据重写和索引重建,从而显著缩短恢复时间;
-
与传统备份和恢复方法相比,可实现 10 到 100 倍的性能提升。
要恢复快照,请按如下方式操作:
- Python
- Java
- Go
- NodeJS
- cURL
# Restore snapshot to new collection
job_id = client.restore_snapshot(
snapshot_name="backup_20240101",
collection_name="restored_collection",
)
// java
restoreOpt := milvusclient.NewRestoreSnapshotOption(
"backup_20240101",
"restored_collection"
)
jobID, err := client.RestoreSnapshot(context.Background(), restoreOpt)
if err != nil {
log.Fatal(err)
}
// node.js
# restful
删除快照
如果不再需要某个快照,您可以将其删除。建议您定期删除旧快照以节省存储空间。
- Python
- Java
- Go
- NodeJS
- cURL
client.drop_snapshot(
snapshot_name="backup_20240101"
)
// java
dropOpt := milvusclient.NewDropSnapshotOption("backup_20240101")
err := client.DropSnapshot(context.Background(), dropOpt)
// node.js
# restful
列出恢复任务
您可以使用此 API 获取恢复任务列表。
- Python
- Java
- Go
- NodeJS
- cURL
# List all restore jobs
jobs = client.list_restore_snapshot_jobs()
for job in jobs:
print(f"Job {job.job_id}: {job.snapshot_name} -> Collection {job.collection_id}")
print(f" State: {job.state}, Progress: {job.progress}%")
# List restore jobs for a specific collection
jobs = client.list_restore_snapshot_jobs(collection_name="my_collection")
// java
// List all restore jobs
listOpt := milvusclient.NewListRestoreSnapshotJobsOption()
jobs, err := client.ListRestoreSnapshotJobs(context.Background(), listOpt)
if err != nil {
log.Fatal(err)
}
for _, job := range jobs {
fmt.Printf("Job %d: %s -> Collection %d\n",
job.GetJobId(), job.GetSnapshotName(), job.GetCollectionId())
fmt.Printf(" State: %s, Progress: %d%%\n",
job.GetState(), job.GetProgress())
}
// List restore jobs for a specific collection
listOpt = milvusclient.NewListRestoreSnapshotJobsOption().
WithCollectionName("my_collection")
jobs, err = client.ListRestoreSnapshotJobs(context.Background(), listOpt)
// node.js
# restful
获取恢复状态
获得恢复任务 ID 后,您可以使用它查询恢复进度。
- Python
- Java
- Go
- NodeJS
- cURL
state = client.get_restore_snapshot_state(job_id=12345)
print(f"Job ID: {state.job_id}")
print(f"Snapshot Name: {state.snapshot_name}")
print(f"Collection ID: {state.collection_id}")
print(f"State: {state.state}")
print(f"Progress: {state.progress}%")
if state.state == "RestoreSnapshotFailed":
print(f"Failure Reason: {state.reason}")
print(f"Time Cost: {state.time_cost}ms")
// java
stateOpt := milvusclient.NewGetRestoreSnapshotStateOption(12345)
state, err := client.GetRestoreSnapshotState(context.Background(), stateOpt)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Job ID: %d\n", state.GetJobId())
fmt.Printf("Snapshot Name: %s\n", state.GetSnapshotName())
fmt.Printf("Collection ID: %d\n", state.GetCollectionId())
fmt.Printf("State: %s\n", state.GetState())
fmt.Printf("Progress: %d%%\n", state.GetProgress())
if state.GetState() == milvuspb.RestoreSnapshotState_RestoreSnapshotFailed {
fmt.Printf("Failure Reason: %s\n", state.GetReason())
}
fmt.Printf("Time Cost: %dms\n", state.GetTimeCost())
// node.js
# restful