본문 바로가기
WIKI 기술 지식 베이스

스냅샷 관리 (Manage Snapshots)

원문 보기 위키 갱신

Milvus 3.0.x와 호환돼요.

이 가이드에서는 스냅샷을 만들고 관리하는 방법을 배워요. 다음을 포함해요.

출처: Milvus 문서

본문

스냅샷 만들기 (Create snapshot)

스냅샷을 만들기 전에 가능한 데이터 손실을 피하기 위해 대상 컬렉션에 데이터 쓰기를 중지하고 flush()를 호출하는 것을 권장해요.

flush() 호출은 필수는 아니지만 데이터 손실을 피하기 위해 강력히 권장해요. 이 단계를 건너뛰면 스냅샷에는 이미 플러시된 데이터만 포함돼요.

스냅샷 이름을 지을 때는 "daily_backup_20240101" 또는 "v2.1_production_release"처럼 명확하고 설명적인 이름을 사용하고, "backup1", "test" 같은 일반적인 용어는 피하세요. 스냅샷 이름을 현명하게 사용해 버전, 환경, 단계에 걸쳐 스냅샷을 구분하세요.

아래 코드 예시는 이미 my_collection이라는 컬렉션이 있다고 가정해요.

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"
)
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)

Java, Node.js, RESTful 예시는 이 페이지의 원문에서 확인할 수 있어요.

스냅샷 나열 (List snapshots)

기존 스냅샷의 이름을 나열할 수 있어요.

# List all snapshots for a collection
snapshots = client.list_snapshots(
    collection_name="my_collection"
)
// List snapshots for collection
listOpt := milvusclient.NewListSnapshotsOption().
    WithCollectionName("my_collection")

snapshots, err := client.ListSnapshots(context.Background(), listOpt)

스냅샷 설명 (Describe snapshot)

특정 스냅샷의 상세 정보를 얻을 수 있어요.

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}")
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())

스냅샷 데이터 pin/unpin (Pin/unpin snapshot data)

복원 중에 스냅샷을 pin해 기본 데이터를 가비지 컬렉션으로부터 일시적으로 보호하고, unpin하면 데이터를 해제할 수 있어요.

또한 pin 작업에 TTL(수명 시간)을 설정할 수 있어서 기간이 만료되면 pin된 데이터가 해제돼요.

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
)
pinID, err := client.PinSnapshotData(
    context.Background(),
    milvusclient.NewPinSnapshotDataOption("backup_20240101", "my_collection").WithTTL(3600),
)
if err != nil {
    log.Fatal(err)
}

defer func() {
    _ = client.UnpinSnapshotData(
        context.Background(),
        milvusclient.NewUnpinSnapshotDataOption(pinID),
    )
}()

// Do work with pinned snapshot data.

스냅샷 복원 (Restore snapshot)

스냅샷을 새 컬렉션으로 복원할 수 있어요. 이 연산은 비동기이며 복원 진행 상황을 추적하기 위한 작업 ID를 반환해요.

복원은 데이터 가져오기 대신 copy-segment 메커니즘을 사용해요. 이는 다음 이유로 더 효율적이에요.

  • 스냅샷 저장소에서 세그먼트 파일(binlogs, deltalogs, index files)을 직접 복사해요.
  • 필드 ID와 인덱스 ID를 보존해 기존 데이터 파일과의 호환성을 보장해요.
  • 데이터 재작성과 인덱스 재구축을 피해 복원 시간이 크게 빨라져요.
  • 전통적인 백업·복원 방식에 비해 10배에서 100배의 성능 향상을 보장해요.

스냅샷을 복원하려면 다음과 같이 해요.

# Restore snapshot to new collection
job_id = client.restore_snapshot(
    snapshot_name="backup_20240101",
    collection_name="restored_collection",
)
restoreOpt := milvusclient.NewRestoreSnapshotOption(
    "backup_20240101",
    "restored_collection"
)

jobID, err := client.RestoreSnapshot(context.Background(), restoreOpt)
if err != nil {
    log.Fatal(err)
}

복원 작업의 진행 상황을 모니터링하는 방법은 복원 상태 가져오기를 참고하세요.

스냅샷 삭제 (Drop snapshot)

더 이상 필요 없으면 스냅샷을 삭제할 수 있어요. 저장 공간을 절약하기 위해 오래된 스냅샷을 정기적으로 제거하는 것을 권장해요.

client.drop_snapshot(
    snapshot_name="backup_20240101"
)
dropOpt := milvusclient.NewDropSnapshotOption("backup_20240101")
err := client.DropSnapshot(context.Background(), dropOpt)

복원 작업 나열 (List restoration jobs)

이 API를 사용해 대상 컬렉션에 이미 생성된 스냅샷 목록을 얻을 수 있어요.

# 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")
// 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)

복원 상태 가져오기 (Get restoration state)

복원 작업 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")
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())

더 알아보기 (Learn more)