Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 8e47a30

Browse files
authored
Merge pull request #211 from stacklok/restore_backup_in_image
feat: expose an entrypoint to restore from backup
2 parents 2b16c9d + 6c24bee commit 8e47a30

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

Dockerfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
3030
&& rm -rf /var/lib/apt/lists/*
3131

3232
# Create a non-root user and switch to it
33-
RUN adduser --system --no-create-home codegate --uid 1000
33+
RUN useradd -m -u 1000 -r codegate
3434
USER codegate
3535
WORKDIR /app
3636

@@ -41,9 +41,6 @@ COPY --from=builder /app /app
4141
# Set the PYTHONPATH environment variable
4242
ENV PYTHONPATH=/app/src
4343

44-
# Allow to expose weaviate_data volume
45-
VOLUME ["/app/weaviate_data"]
46-
4744
# Set the container's default entrypoint
4845
EXPOSE 8989
49-
ENTRYPOINT ["python", "-m", "src.codegate.cli", "serve", "--port", "8989", "--host", "0.0.0.0"]
46+
ENTRYPOINT ["/app/scripts/entrypoint.sh", "/app/weaviate_backup", "backup"]

scripts/entrypoint.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# Check if the backup directory exists and handle the restore
4+
if [ -d "$1" ] && [ -d "$1/$2" ]; then
5+
echo "Restoring backup from $1/$2..."
6+
# Your restore logic here, e.g., running a Python script or restoring a database
7+
python -m src.codegate.cli restore_backup "$1" "$2"
8+
else
9+
echo "No backup found at $1/$2. Skipping restore."
10+
fi
11+
12+
# Step 2: Start the main application (serve)
13+
echo "Starting the application..."
14+
exec python -m src.codegate.cli serve --port 8989 --host 0.0.0.0

scripts/import_packages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ async def run_import(self):
113113
await self.add_data()
114114
self.take_backup()
115115

116+
116117
if __name__ == "__main__":
117118
importer = PackageImporter()
118119
asyncio.run(importer.run_import())

src/codegate/cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Dict, Optional
66

77
import click
8+
from src.codegate.storage.utils import restore_storage_backup
89
import structlog
910

1011
from codegate.codegate_logging import LogFormat, LogLevel, setup_logging
@@ -191,6 +192,29 @@ def serve(
191192
sys.exit(1)
192193

193194

195+
@cli.command()
196+
@click.option(
197+
"--backup-path",
198+
type=click.Path(exists=True, file_okay=False, path_type=Path),
199+
required=True,
200+
help="Directory path where the backup file is located.",
201+
)
202+
@click.option(
203+
"--backup-name",
204+
type=str,
205+
required=True,
206+
help="Name of the backup file to restore.",
207+
)
208+
def restore_backup(backup_path: Path, backup_name: str) -> None:
209+
"""Restore the database from the specified backup."""
210+
try:
211+
restore_storage_backup(backup_path, backup_name)
212+
click.echo(f"Successfully restored the backup '{backup_name}' from {backup_path}.")
213+
except Exception as e:
214+
click.echo(f"Error restoring backup: {e}", err=True)
215+
sys.exit(1)
216+
217+
194218
def main() -> None:
195219
"""Main entry point for the CLI."""
196220
cli()

src/codegate/storage/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import weaviate
2+
from weaviate.embedded import EmbeddedOptions
3+
4+
5+
def restore_storage_backup(backup_path: str, backup_name: str):
6+
client = weaviate.WeaviateClient(
7+
embedded_options=EmbeddedOptions(
8+
persistence_data_path="./weaviate_data", grpc_port=50052,
9+
additional_env_vars={"ENABLE_MODULES": "backup-filesystem",
10+
"BACKUP_FILESYSTEM_PATH": backup_path}
11+
)
12+
)
13+
client.connect()
14+
try:
15+
client.backup.restore(backup_id=backup_name,
16+
backend="filesystem", wait_for_completion=True)
17+
except Exception as e:
18+
print(f"Failed to restore backup: {e}")
19+
finally:
20+
client.close()

0 commit comments

Comments
 (0)