管理快照Compatible with Milvus 3.0.x
在本指南中,您将学习如何创建和管理快照。
创建快照
创建快照前,建议您停止向目标 Collections 写入数据,并调用flush() 以避免可能的数据丢失。
调用flush() 不是必须的,但强烈建议这样做以避免数据丢失。如果跳过这一步,快照将只包含已刷新的数据。
命名快照时,请使用清晰、描述性的名称,如"daily_backup_20240101" 或"v2.1_production_release" ,避免使用通用术语,如"backup1" 和"test" 。明智地使用快照名称,以区分不同版本、环境和阶段的快照。
下面的代码示例假定您已经有一个名为my_collection 的 Collections。
from pymilvus import MilvusClient
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)
# 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: "localhost:19530",
Token: "root:Milvus",
})
// 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
列出快照
您可以列出现有快照的名称。
# 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
描述快照
可以获取特定快照的详细信息。
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
还原快照
您可以将快照还原到新的 Collections 中。此操作是异步的,并返回一个作业 ID 以跟踪还原进度。
还原使用复制段机制,而不是数据导入,这样效率更高,因为它可以
直接从快照存储中复制段文件(binlogs、deltalogs、index 文件
保留字段 ID 和索引 ID,确保与现有数据文件兼容
避免了数据重写和索引重建,从而大大加快了恢复时间,并且
确保性能比传统备份和还原方法提高 10 到 100 倍
还原快照的步骤如下:
# 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
有关监控还原任务进度的详细信息,请参阅监控还原进度。
删除快照
如果不再需要快照,可以将其删除。建议您定期删除旧快照以节省存储空间。
client.drop_snapshot(
snapshot_name="backup_20240101"
)
// java
dropOpt := milvusclient.NewDropSnapshotOption("backup_20240101")
err := client.DropSnapshot(context.Background(), dropOpt)
// node.js
# restful
列出修复工作
您可以使用此 API 获取已为目标 Collections 创建的快照列表。
# 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,就可以使用它来检索还原进度。
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