Skip to content

Commit b05f7f1

Browse files
authored
Merge pull request #149 from weaviate/feat/backup-api
Implement backup management features
2 parents ce1c76b + 97e74ea commit b05f7f1

File tree

15 files changed

+1966
-14
lines changed

15 files changed

+1966
-14
lines changed

docs/BACKUP_API_USAGE.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Backup API Usage Guide
2+
3+
This guide covers the Weaviate C# client's backup and restore functionality. It provides examples and best practices for using the modern, idiomatic backup API.
4+
5+
## Table of Contents
6+
7+
- [Overview](#overview)
8+
- [Backend Configuration](#backend-configuration)
9+
- [Creating Backups](#creating-backups)
10+
- [Restoring Backups](#restoring-backups)
11+
- [Monitoring Operations](#monitoring-operations)
12+
- [Concurrency and Coordination](#concurrency-and-coordination)
13+
- [Advanced Usage](#advanced-usage)
14+
15+
## Overview
16+
17+
The Backup API allows you to create and restore backups of your Weaviate collections. Backups can be stored on various backend storage providers, including the local filesystem, S3, GCS, and Azure Blob Storage.
18+
19+
Key features include:
20+
21+
- **Type-safe operations**: Separate `BackupCreateOperation` and `BackupRestoreOperation` types.
22+
- **Type-safe backends**: Separate `FilesystemBackend` and `ObjectStorageBackend` types.
23+
- **Automatic cleanup**: Resources are automatically disposed of when operations complete.
24+
- **Flexible patterns**: Async tracking, sync blocking, or fire-and-forget.
25+
- **Configurable timeouts**: Global defaults with per-call overrides.
26+
27+
## Backend Configuration
28+
29+
Backends are configured using the static factory methods on the `BackupBackend` class. Supported backends include:
30+
31+
### Filesystem Backend
32+
33+
```csharp
34+
var backend = BackupBackend.Filesystem(path: "/backups");
35+
Console.WriteLine(backend.Provider); // BackupStorageProvider.Filesystem
36+
```
37+
38+
### Object Storage Backends
39+
40+
#### S3
41+
42+
```csharp
43+
var backend = BackupBackend.S3(bucket: "my-backup-bucket", path: "backups/");
44+
Console.WriteLine(backend.Provider); // BackupStorageProvider.S3
45+
```
46+
47+
#### Google Cloud Storage
48+
49+
```csharp
50+
var backend = BackupBackend.GCS(bucket: "my-gcs-bucket", path: "weaviate-backups");
51+
Console.WriteLine(backend.Provider); // BackupStorageProvider.GCS
52+
```
53+
54+
#### Azure Blob Storage
55+
56+
```csharp
57+
var backend = BackupBackend.Azure(bucket: "my-container", path: "backups");
58+
Console.WriteLine(backend.Provider); // BackupStorageProvider.Azure
59+
```
60+
61+
## Creating Backups
62+
63+
To create a backup, use the `BackupClient.Create` method. This returns a `BackupCreateOperation` object that can be used to track the operation's status.
64+
65+
### Creating Backup Example
66+
67+
```csharp
68+
var operation = await client.Backups.Create(new BackupCreateRequest(
69+
id: "my-backup-id",
70+
backend: BackupBackend.Filesystem(path: "/backups")
71+
));
72+
73+
await operation.WaitForCompletion();
74+
Console.WriteLine(operation.Current.Status); // BackupStatus.Success
75+
```
76+
77+
## Restoring Backups
78+
79+
To restore a backup, use the `BackupClient.Restore` method. This returns a `BackupRestoreOperation` object.
80+
81+
### Restoring Backup Example
82+
83+
```csharp
84+
var operation = await client.Backups.Restore(new BackupRestoreRequest(
85+
id: "my-backup-id",
86+
backend: BackupBackend.Filesystem(path: "/backups")
87+
));
88+
89+
await operation.WaitForCompletion();
90+
Console.WriteLine(operation.Current.Status); // BackupStatus.Success
91+
```
92+
93+
## Monitoring Operations
94+
95+
Both `BackupCreateOperation` and `BackupRestoreOperation` support:
96+
97+
- **Status polling**: Use the `Current` property to get the latest status.
98+
- **Cancellation**: Call the `Cancel` method to abort the operation.
99+
100+
### Monitoring Operation Example
101+
102+
```csharp
103+
var operation = await client.Backups.Create(new BackupCreateRequest(
104+
id: "my-backup-id",
105+
backend: BackupBackend.Filesystem(path: "/backups")
106+
));
107+
108+
while (!operation.IsCompleted)
109+
{
110+
Console.WriteLine(operation.Current.Status);
111+
await Task.Delay(1000);
112+
}
113+
```
114+
115+
## Concurrency and Coordination
116+
117+
**Important:** Concurrent backup or restore operations are not allowed. Attempting to start a new operation while another is in progress will throw a `WeaviateBackupConflictException`.
118+
119+
To perform multiple operations, ensure each one is awaited to completion before starting the next.
120+
121+
### Sequential Operations Example
122+
123+
```csharp
124+
// Create the first backup
125+
var operation1 = await client.Backups.Create(new BackupCreateRequest(
126+
id: "backup-1",
127+
backend: BackupBackend.Filesystem(path: "/backups")
128+
));
129+
await operation1.WaitForCompletion();
130+
131+
// Create the second backup after the first completes
132+
var operation2 = await client.Backups.Create(new BackupCreateRequest(
133+
id: "backup-2",
134+
backend: BackupBackend.Filesystem(path: "/backups")
135+
));
136+
await operation2.WaitForCompletion();
137+
```
138+
139+
## Advanced Usage
140+
141+
### Configurable Timeouts
142+
143+
You can configure global defaults for polling intervals and timeouts using `BackupClient.Config`:
144+
145+
```csharp
146+
BackupClient.Config = new BackupClientConfig
147+
{
148+
PollInterval = TimeSpan.FromMilliseconds(500),
149+
Timeout = TimeSpan.FromMinutes(5)
150+
};
151+
```
152+
153+
### Selective Backup/Restore
154+
155+
Include or exclude specific collections:
156+
157+
```csharp
158+
var operation = await client.Backups.Create(new BackupCreateRequest(
159+
id: "my-backup-id",
160+
backend: BackupBackend.Filesystem(path: "/backups"),
161+
Include = new[] { "Collection1", "Collection2" }
162+
));
163+
await operation.WaitForCompletion();
164+
```

src/Weaviate.Client.Tests/Helpers.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33

44
namespace Weaviate.Client.Tests;
55

6+
public static class Helpers
7+
{
8+
// Useful for collection names, backups, aliases, etc.
9+
public static string GenerateUniqueIdentifier(string name)
10+
{
11+
// Sanitize the collection name
12+
name = SanitizeCollectionName(name);
13+
// Generate a random part using GUID
14+
var randomPart = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 12);
15+
return string.Concat(name, randomPart).ToLowerInvariant();
16+
}
17+
18+
public static string SanitizeCollectionName(string name)
19+
{
20+
name = name.Replace("[", "")
21+
.Replace("]", "")
22+
.Replace("-", "")
23+
.Replace(" ", "")
24+
.Replace(".", "");
25+
return char.ToUpper(name[0]) + name.Substring(1);
26+
}
27+
}
28+
629
public class LoggingHandler : DelegatingHandler
730
{
831
private readonly Action<string> _log;

0 commit comments

Comments
 (0)