# django-dbs

> Redundant, self-healing, encrypted single-file backup for Django projects. One command
> writes an encrypted container holding two copies of the payload plus Reed-Solomon parity,
> so silent corruption is detected and repaired on restore. Backups stream with flat memory.
> The library comes with a panel at /admin/dbs/ (schedule, health, hash-chained audit,
> restore) and the `django-dbs run` manager for the Django applications on many servers,
> whether they run on the server, in a Compose service or in a container, under Docker or
> Podman.

## Install

```
pip install django-dbs   # the library, the guard's detector, dbs.transports and the manager
```

```python
INSTALLED_APPS = [..., "django.contrib.admin", "dbs"]
MIDDLEWARE = [..., "dbs.security.middleware.DBSSecurityMiddleware"]  # below AuthenticationMiddleware
DBS_BACKUP_DIR = "/var/backups/myproject"
```

```
python manage.py migrate
python manage.py dbs backup backup.dbs
```

Upgrading requires `manage.py dbs upgrade`, which applies DBS migrations.

The manager runs outside any project: `django_dbs run` (also `django-dbs run` or
`dbs run`; a local app that opens the browser), `django_dbs export` / `import` to move it,
`django_dbs mcp` for Claude. A server is an SSH machine; an application is one Django
project on it, added from a folder with `django_dbs app add NAME --server S --folder F`,
which finds the containers, the Python, `manage.py` and the `.env`, guided by how the
developer says it runs (`--deployment runs_in=compose`, the database, where settings come
from). `django_dbs server move S --to T` carries every application on a server, with its
code, `.git`, images, volumes, `.env` and data, onto a server that may have nothing
installed; it rehearses unless `--real`. Everything the
interface does is also `django_dbs server|app|backup|plan|activity|env|files|redeploy VERB`;
`createuser`, `account` and `password` manage its accounts. The interface also has a
console for these commands and, for admins, a terminal on a server or in a container.

## Commands
- `manage.py dbs backup OUTPUT` - write an encrypted backup; refuses a path that exists
- `manage.py dbs backup --stream [--progress-seconds N]` - write it to stdout and keep a copy in DBS_BACKUP_DIR
- `manage.py dbs restore INPUT [--dry-run] [--flush]` - restore; merges unless flushed
- `manage.py dbs validate INPUT [--passphrase | --passphrase-stdin]` - check structure, blocks and decryption
- `manage.py dbs schedule [--once]` - follow the schedule set in the panel
- `manage.py dbs schedule --interval 6h --output-dir DIR --keep 5` - a fixed plan, 1 to 5 kept
- `manage.py dbs health [--json]` - backup health report
- `manage.py dbs audit verify` - check the audit trail's hash chain
- `manage.py dbs connection [--json]` - what the DBS manager reads from this project, and whether it runs in a container
- `manage.py dbs key --show` - print the passphrase derived from SECRET_KEY
- `manage.py dbs security unlock USER` - clear an anomaly lockout
- `manage.py dbs ai` - install these instructions for AI coding assistants
- `manage.py dbs upgrade [--check]` - bring a project up to date with the installed version

`manage.py django_dbs` and `manage.py django-dbs` are the same command. `dbs_backup`,
`dbs_restore`, `dbs_validate` and `dbs_schedule` still work and are not deprecated.

## For assistants

Run `manage.py dbs upgrade --check` before working in a project, and `dbs upgrade` after
upgrading the package. Never confirm the abandonment of unconvertible backups: stop and ask
a human.

## Key facts
- The passphrase comes from `settings.SECRET_KEY` unless `DBS_PASSPHRASE` overrides it, and
  it must never change: older backups would no longer open. `SECRET_KEY_FALLBACKS` is tried
  on restore. A backup under a lost key cannot be opened.
- The admin panel is mounted automatically at `/admin/dbs/`, for superusers only.
- The backup frequency is set in the panel. `DBS_SCHEDULER` = "thread" (default), "command"
  or "off" picks where it runs. The server keeps at most five backups. Every backup, restore
  and validation is audited by the process itself, never naming which program started it.
- Panel backups, restores and scheduled runs run in a child process,
  `python -m dbs.job_process`, stopped at `DBS_JOB_TIME_LIMIT` (6h) or
  `DBS_JOB_MEMORY_LIMIT` (2G); `DBS_JOB_PROCESS = False` keeps them in the web process.
- The server pushes nothing: the DBS manager connects over SSH and pulls backups, resuming a
  transfer that broke. The panel has no SFTP targets and no console; there is no dbs-client,
  and `dbs.contrib` was removed in 0.8.0.
- An application's backup passphrase is stored in the manager once and never changed. Deploy
  steps come from a fixed catalogue; Claude's MCP tools rehearse deploys, restores, moves and
  server moves but never run them for real, and never reach the console or the terminal.
- Admin sessions are scored by an IsolationForest; a flagged session cannot change or
  export anything, and a high score ends the session.
  `manage.py dbs security unlock USER` always gets you back in.

## Documentation
- Skill for AI assistants: `dbs/ai/SKILL.md` inside the installed package
- Settings: `dbs/ai/reference/settings.md`
- Commands: `dbs/ai/reference/commands.md`
- Transports: `dbs/ai/reference/transports.md`
- Session guard: `dbs/ai/reference/security.md`
