快照
从 v0.8.4 版本开始可用
快照是 tar
归档文件,其中包含特定节点上特定集合在特定时间的数据和配置。在分布式设置中,当您的集群中有多个节点时,处理单个集合时必须为每个节点单独创建快照。
此功能可用于存档数据或轻松复制现有部署。对于灾难恢复,Qdrant Cloud 用户可能更倾向于使用备份,后者是数据的物理磁盘级副本。
有关如何使用快照的分步指南,请参阅我们的教程。
创建快照
为现有集合创建新快照
POST /collections/{collection_name}/snapshots
from qdrant_client import QdrantClient
client = QdrantClient(url="http://localhost:6333")
client.create_snapshot(collection_name="{collection_name}")
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.createSnapshot("{collection_name}");
use qdrant_client::Qdrant;
let client = Qdrant::from_url("http://localhost:6334").build()?;
client.create_snapshot("{collection_name}").await?;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
QdrantClient client =
new QdrantClient(QdrantGrpcClient.newBuilder("localhost", 6334, false).build());
client.createSnapshotAsync("{collection_name}").get();
using Qdrant.Client;
var client = new QdrantClient("localhost", 6334);
await client.CreateSnapshotAsync("{collection_name}");
import (
"context"
"github.com/qdrant/go-client/qdrant"
)
client, err := qdrant.NewClient(&qdrant.Config{
Host: "localhost",
Port: 6334,
})
client.CreateSnapshot(context.Background(), "{collection_name}")
这是一个同步操作,将生成一个 tar
归档文件到 snapshot_path
中。
删除快照
从 v1.0.0 版本开始可用
DELETE /collections/{collection_name}/snapshots/{snapshot_name}
from qdrant_client import QdrantClient
client = QdrantClient(url="http://localhost:6333")
client.delete_snapshot(
collection_name="{collection_name}", snapshot_name="{snapshot_name}"
)
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.deleteSnapshot("{collection_name}", "{snapshot_name}");
use qdrant_client::qdrant::DeleteSnapshotRequestBuilder;
use qdrant_client::Qdrant;
let client = Qdrant::from_url("http://localhost:6334").build()?;
client
.delete_snapshot(DeleteSnapshotRequestBuilder::new(
"{collection_name}",
"{snapshot_name}",
))
.await?;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
QdrantClient client =
new QdrantClient(QdrantGrpcClient.newBuilder("localhost", 6334, false).build());
client.deleteSnapshotAsync("{collection_name}", "{snapshot_name}").get();
using Qdrant.Client;
var client = new QdrantClient("localhost", 6334);
await client.DeleteSnapshotAsync(collectionName: "{collection_name}", snapshotName: "{snapshot_name}");
import (
"context"
"github.com/qdrant/go-client/qdrant"
)
client, err := qdrant.NewClient(&qdrant.Config{
Host: "localhost",
Port: 6334,
})
client.DeleteSnapshot(context.Background(), "{collection_name}", "{snapshot_name}")
列出快照
集合的快照列表
GET /collections/{collection_name}/snapshots
from qdrant_client import QdrantClient
client = QdrantClient(url="http://localhost:6333")
client.list_snapshots(collection_name="{collection_name}")
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.listSnapshots("{collection_name}");
use qdrant_client::Qdrant;
let client = Qdrant::from_url("http://localhost:6334").build()?;
client.list_snapshots("{collection_name}").await?;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
QdrantClient client =
new QdrantClient(QdrantGrpcClient.newBuilder("localhost", 6334, false).build());
client.listSnapshotAsync("{collection_name}").get();
using Qdrant.Client;
var client = new QdrantClient("localhost", 6334);
await client.ListSnapshotsAsync("{collection_name}");
import (
"context"
"github.com/qdrant/go-client/qdrant"
)
client, err := qdrant.NewClient(&qdrant.Config{
Host: "localhost",
Port: 6334,
})
client.ListSnapshots(context.Background(), "{collection_name}")
检索快照
将集合中的指定快照作为文件下载
GET /collections/{collection_name}/snapshots/{snapshot_name}
curl 'http://{qdrant-url}:6333/collections/{collection_name}/snapshots/snapshot-2022-10-10.snapshot' \
-H 'api-key: ********' \
--output 'filename.snapshot'
恢复快照
快照可以通过以下三种方式恢复
- 从 URL 或本地文件恢复(适用于恢复远程服务器上或已存储在节点上的快照文件)
- 从上传的文件恢复(适用于将数据迁移到新集群)
- 启动时恢复(适用于运行自托管单节点 Qdrant 实例)
无论使用哪种方法,Qdrant 都将从快照中提取分片数据并正确注册到集群中。如果集群中存在已恢复分片的其他活动副本,Qdrant 将默认把它们复制到新恢复的节点,以保持数据一致性。
从 URL 或本地文件恢复
从 v0.11.3 版本开始可用
此恢复方法要求快照文件可从 URL 下载,或作为本地文件存在于节点上(例如,如果您之前在此节点上创建了快照)。如果您需要上传快照文件,请参阅下一节。
要从 URL 或本地文件恢复,请使用快照恢复端点。此端点接受 URL,例如 https://example.com
,或文件 URI,例如 file:///tmp/snapshot-2022-10-10.snapshot
。如果目标集合不存在,则将创建它。
PUT /collections/{collection_name}/snapshots/recover
{
"location": "http://qdrant-node-1:6333/collections/{collection_name}/snapshots/snapshot-2022-10-10.shapshot"
}
from qdrant_client import QdrantClient
client = QdrantClient(url="http://qdrant-node-2:6333")
client.recover_snapshot(
"{collection_name}",
"http://qdrant-node-1:6333/collections/collection_name/snapshots/snapshot-2022-10-10.shapshot",
)
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.recoverSnapshot("{collection_name}", {
location: "http://qdrant-node-1:6333/collections/{collection_name}/snapshots/snapshot-2022-10-10.shapshot",
});
从上传的文件恢复
快照文件也可以作为文件上传,并使用从上传的快照恢复端点进行恢复。此端点在请求体中接受原始快照数据。如果目标集合不存在,则将创建它。
curl -X POST 'http://{qdrant-url}:6333/collections/{collection_name}/snapshots/upload?priority=snapshot' \
-H 'api-key: ********' \
-H 'Content-Type:multipart/form-data' \
-F 'snapshot=@/path/to/snapshot-2022-10-10.shapshot'
此方法通常用于将数据从一个集群迁移到另一个集群,因此对于该用例,我们建议将优先级设置为“snapshot”。
启动时恢复
如果您有单节点部署,可以在启动时恢复任何集合,并且它将立即可用。快照恢复是通过 Qdrant CLI 在启动时通过 --snapshot
参数完成的,该参数接受一对列表,例如 <快照文件路径>:<目标集合名称>
例如
./qdrant --snapshot /snapshots/test-collection-archive.snapshot:test-collection --snapshot /snapshots/test-collection-archive.snapshot:test-copy-collection
目标集合必须不存在,否则程序将因错误退出。
如果您希望覆盖现有集合,请谨慎使用 --force_snapshot
标志。
快照优先级
将快照恢复到非空节点时,快照数据可能与现有数据发生冲突。“优先级”设置控制 Qdrant 如何处理这些冲突。优先级设置很重要,因为不同的优先级可能导致截然不同的最终结果。默认优先级可能并不适用于所有情况。
可用的快照恢复优先级有
replica
: (默认) 优先使用现有数据而不是快照数据。snapshot
: 优先使用快照数据而不是现有数据。no_sync
: 恢复快照,不进行任何额外同步。
要从快照恢复一个新集合,您需要将优先级设置为 snapshot
。使用 snapshot
优先级,快照中的所有数据都将恢复到集群上。使用 replica
优先级(默认),您将得到一个空集合,因为集群上的集合不包含任何点,并且优先使用了该源。
no_sync
用于特殊用例,不常用。它允许手动管理分片并在集群之间传输分片,而无需任何额外同步。使用不当会导致您的集群处于损坏状态。
要从 URL 恢复,您需要在请求体中指定一个附加参数
PUT /collections/{collection_name}/snapshots/recover
{
"location": "http://qdrant-node-1:6333/collections/{collection_name}/snapshots/snapshot-2022-10-10.shapshot",
"priority": "snapshot"
}
curl -X POST 'http://qdrant-node-1:6333/collections/{collection_name}/snapshots/upload?priority=snapshot' \
-H 'api-key: ********' \
-H 'Content-Type:multipart/form-data' \
-F 'snapshot=@/path/to/snapshot-2022-10-10.shapshot'
from qdrant_client import QdrantClient, models
client = QdrantClient(url="http://qdrant-node-2:6333")
client.recover_snapshot(
"{collection_name}",
"http://qdrant-node-1:6333/collections/{collection_name}/snapshots/snapshot-2022-10-10.shapshot",
priority=models.SnapshotPriority.SNAPSHOT,
)
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.recoverSnapshot("{collection_name}", {
location: "http://qdrant-node-1:6333/collections/{collection_name}/snapshots/snapshot-2022-10-10.shapshot",
priority: "snapshot"
});
整个存储的快照
从 v0.8.5 版本开始可用
有时,不仅为单个集合创建快照,还为整个存储(包括集合别名)创建快照可能很方便。Qdrant 也为此提供了专用的 API。它类似于集合级快照,但不要求指定 collection_name
。
创建全存储快照
POST /snapshots
from qdrant_client import QdrantClient
client = QdrantClient(url="http://localhost:6333")
client.create_full_snapshot()
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.createFullSnapshot();
use qdrant_client::Qdrant;
let client = Qdrant::from_url("http://localhost:6334").build()?;
client.create_full_snapshot().await?;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
QdrantClient client =
new QdrantClient(QdrantGrpcClient.newBuilder("localhost", 6334, false).build());
client.createFullSnapshotAsync().get();
using Qdrant.Client;
var client = new QdrantClient("localhost", 6334);
await client.CreateFullSnapshotAsync();
import (
"context"
"github.com/qdrant/go-client/qdrant"
)
client, err := qdrant.NewClient(&qdrant.Config{
Host: "localhost",
Port: 6334,
})
client.CreateFullSnapshot(context.Background())
删除全存储快照
从 v1.0.0 版本开始可用
DELETE /snapshots/{snapshot_name}
from qdrant_client import QdrantClient
client = QdrantClient(url="http://localhost:6333")
client.delete_full_snapshot(snapshot_name="{snapshot_name}")
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.deleteFullSnapshot("{snapshot_name}");
use qdrant_client::Qdrant;
let client = Qdrant::from_url("http://localhost:6334").build()?;
client.delete_full_snapshot("{snapshot_name}").await?;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
QdrantClient client =
new QdrantClient(QdrantGrpcClient.newBuilder("localhost", 6334, false).build());
client.deleteFullSnapshotAsync("{snapshot_name}").get();
using Qdrant.Client;
var client = new QdrantClient("localhost", 6334);
await client.DeleteFullSnapshotAsync("{snapshot_name}");
import (
"context"
"github.com/qdrant/go-client/qdrant"
)
client, err := qdrant.NewClient(&qdrant.Config{
Host: "localhost",
Port: 6334,
})
client.DeleteFullSnapshot(context.Background(), "{snapshot_name}")
列出全存储快照
GET /snapshots
from qdrant_client import QdrantClient
client = QdrantClient("localhost", port=6333)
client.list_full_snapshots()
import { QdrantClient } from "@qdrant/js-client-rest";
const client = new QdrantClient({ host: "localhost", port: 6333 });
client.listFullSnapshots();
use qdrant_client::Qdrant;
let client = Qdrant::from_url("http://localhost:6334").build()?;
client.list_full_snapshots().await?;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
QdrantClient client =
new QdrantClient(QdrantGrpcClient.newBuilder("localhost", 6334, false).build());
client.listFullSnapshotAsync().get();
using Qdrant.Client;
var client = new QdrantClient("localhost", 6334);
await client.ListFullSnapshotsAsync();
import (
"context"
"github.com/qdrant/go-client/qdrant"
)
client, err := qdrant.NewClient(&qdrant.Config{
Host: "localhost",
Port: 6334,
})
client.ListFullSnapshots(context.Background())
下载全存储快照
GET /snapshots/{snapshot_name}
恢复全存储快照
快照恢复只能在启动时通过 Qdrant CLI 完成。
例如
./qdrant --storage-snapshot /snapshots/full-snapshot-2022-07-18-11-20-51.snapshot
存储
创建、上传和恢复的快照存储为 .snapshot
文件。默认情况下,它们存储在本地文件系统上。您也可以配置使用S3 存储服务。
本地文件系统
默认情况下,使用我们的 Docker 镜像时,快照存储在 ./snapshots
或 /qdrant/snapshots
处。
可以通过配置控制目标目录
storage:
# Specify where you want to store snapshots.
snapshots_path: ./snapshots
另外,您也可以使用环境变量 QDRANT__STORAGE__SNAPSHOTS_PATH=./snapshots
。
从 v1.3.0 版本开始可用
创建快照时,默认情况下会将临时文件放在配置的存储目录中。在容量有限或网络附加磁盘较慢的情况下,您可以为临时文件指定一个单独的位置
storage:
# Where to store temporary files
temp_path: /tmp
S3
从 v1.10.0 版本开始可用
除了将快照存储在本地文件系统上,您还可以配置将快照存储在 S3 兼容的存储服务中。要启用此功能,您必须在配置文件中进行配置。
例如,为 AWS S3 配置
storage:
snapshots_config:
# Use 's3' to store snapshots on S3
snapshots_storage: s3
s3_config:
# Bucket name
bucket: your_bucket_here
# Bucket region (e.g. eu-central-1)
region: your_bucket_region_here
# Storage access key
# Can be specified either here or in the `QDRANT__STORAGE__SNAPSHOTS_CONFIG__S3_CONFIG__ACCESS_KEY` environment variable.
access_key: your_access_key_here
# Storage secret key
# Can be specified either here or in the `QDRANT__STORAGE__SNAPSHOTS_CONFIG__S3_CONFIG__SECRET_KEY` environment variable.
secret_key: your_secret_key_here
# S3-Compatible Storage URL
# Can be specified either here or in the `QDRANT__STORAGE__SNAPSHOTS_CONFIG__S3_CONFIG__ENDPOINT_URL` environment variable.
endpoint_url: your_url_here