Configure storage¶
This guide explains how to set up the storage backend for django-snapshots.
Use the local filesystem (default)¶
LocalFileSystemBackend stores snapshots as plain
files on the local filesystem. This is the simplest option and supports the
full AdvancedSnapshotStorage interface.
# settings.py
from django_snapshots.storage import LocalFileSystemBackend
SNAPSHOTS = {
"STORAGE": LocalFileSystemBackend(location="/var/backups/snapshots"),
}
The directory is created automatically if it does not exist.
Use an existing Django storage backend¶
If your project already uses django-storages (e.g. S3, GCS, Azure),
you can wrap any Storage instance with
DjangoStorageBackend:
# settings.py
from storages.backends.s3boto3 import S3Boto3Storage
from django_snapshots.storage import DjangoStorageBackend
SNAPSHOTS = {
"STORAGE": DjangoStorageBackend(
storage=S3Boto3Storage(bucket_name="my-backup-bucket")
),
}
Note
DjangoStorageBackend only satisfies the basic
SnapshotStorage protocol. Features that require
the AdvancedSnapshotStorage tier (e.g. archive
format) are not available with this backend.
Write a custom storage backend¶
Implement the five methods of SnapshotStorage on
any class:
from typing import IO
class InMemoryBackend:
"""Trivial in-memory backend — useful for testing."""
def __init__(self):
self._store: dict[str, bytes] = {}
def read(self, path: str) -> IO[bytes]:
import io
return io.BytesIO(self._store[path])
def write(self, path: str, content: IO[bytes]) -> None:
self._store[path] = content.read()
def list(self, prefix: str) -> list[str]:
return [p for p in self._store if p.startswith(prefix)]
def delete(self, path: str) -> None:
self._store.pop(path, None)
def exists(self, path: str) -> bool:
return path in self._store
SNAPSHOTS = {"STORAGE": InMemoryBackend()}
Use the dict configuration style¶
If you prefer to keep the storage backend configuration as a plain dict
(e.g. for environment-specific overrides), you can pass a dict with
BACKEND and OPTIONS keys:
SNAPSHOTS = {
"STORAGE": {
"BACKEND": "django_snapshots.storage.LocalFileSystemBackend",
"OPTIONS": {"location": "/var/backups/snapshots"},
},
}