Debian Codegraph @

docs/src/databases.md

raw
1 # Database settings
2
3 The following databases are supported by this application:
4
5 - SQLite
6 - MySQL
7 - PostgreSQL
8 - MongoDB
9 - ... and any other Django-supported database (via `DjangoConnector`)
10
11 DBBackup uses “connector” classes to implement backend specific dump and
12 restore logic. Each connector may expose additional settings documented
13 below.
14
15 ---
16
17 ## Settings Overrides
18
19 By default DBBackup reuses connection details from `settings.DATABASES`.
20 Sometimes you want different credentials or a different host (e.g. read-only
21 replica) just for backups. Use `DBBACKUP_CONNECTORS` for that purpose; it
22 mirrors Django's `DATABASES` structure but only overrides the keys you supply.
23
24 ```python
25 DBBACKUP_CONNECTORS = {
26 'default': {
27 'USER': 'backupuser',
28 'PASSWORD': 'backuppassword',
29 'HOST': 'replica-for-backup'
30 }
31 }
32 ```
33
34 This configuration will allow you to use a replica with a different host and user,
35 which is a great practice if you don't want to overload your main database.
36
37 ---
38
39 ## Common Settings
40
41 All connectors have the following parameters:
42
43 | Setting | Description | Default |
44 | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
45 | CONNECTOR | Absolute path to connector class. For example: `dbbackup.db.django.DjangoConnector`. Prometheus wrappers are also supported mapping to the same connectors. | Auto-detected from `ENGINE` |
46 | EXCLUDE | List of table names to exclude from dump (may be unsupported for raw file copy snapshot approaches). Example below. | None |
47 | EXTENSION | File extension used for the generated dump archive. | `dump` |
48
49 Example for `EXCLUDE` usage:
50
51 ```python
52 DBBACKUP_CONNECTORS = {
53 'default': {
54 'EXCLUDE': ['django_session', 'temp_data'],
55 }
56 }
57 ```
58
59 Additionally, some connectors within this repository use a command line tool as a dump engine, (`MysqlDumpConnector` for example). These kinds of connectors have additional common attributes:
60
61 | Setting | Description | Default |
62 | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- |
63 | DUMP_CMD | Path to command used to create a backup (e.g. `mysqldump`, `pg_dump`, `mongodump`). Applies only to command-line connectors (subclasses of `BaseCommandDBConnector`). | Tool name inferred / must be in `PATH` |
64 | RESTORE_CMD | Path to command used for restore (e.g. `mysql`, `psql`, `pg_restore`, `mongorestore`). | Tool name inferred / must be in `PATH` |
65 | DUMP_PREFIX / RESTORE_PREFIX | String inserted (with a space) before the actual dump/restore command (e.g. to add `time` or `nice`). | None |
66 | DUMP_SUFFIX / RESTORE_SUFFIX | String appended (with a space) after the dump/restore command (e.g. to pipe or redirect). | None |
67 | ENV | Base environment variables applied to every external command. | `{}` |
68 | DUMP_ENV / RESTORE_ENV | Environment overrides/extensions applied only to dump / restore phases. | `{}` |
69 | USE_PARENT_ENV | Whether to inherit parent process environment (e.g. to keep `PATH`). | `True` |
70
71 Example forcing a specific SSL mode for PostgreSQL dump:
72
73 ```python
74 DBBACKUP_CONNECTORS = {
75 'default': {
76 'DUMP_ENV': {'PGSSLMODE': 'require'}
77 }
78 }
79 ```
80
81 ---
82
83 ## Database Connectors
84
85 These connectors are provided by default and are designed to work with specific database engines. They provide optimized backup and restore functionality.
86
87 ### SQLite
88
89 #### Settings
90
91 SQLite connectors do not define any configurable settings beyond the common settings documented above.
92
93 However, note that only the `SqliteConnector` supports the common `EXCLUDE` setting.
94
95 #### SqliteBackupConnector
96
97 The `dbbackup.db.sqlite.SqliteBackupConnector` makes a copy of the SQLite database file using the `.backup` command, which is safe to execute while the database has ongoing/active connections.
98
99 This is the default connector for SQLite databases.
100
101 #### SqliteConnector
102
103 It is in pure Python and is similar to the Sqlite `.dump` command for creating a SQL dump.
104
105 This connector can be used to restore a backup to an existing (dirty) database due to it's generation of raw SQL statements. However, that is generally not recommended and can lead to unexpected results depending on your schema.
106
107 #### SqliteCPConnector
108
109 The `dbbackup.db.sqlite.SqliteCPConnector` connector can be used to make a simple raw copy of your database file, like a snapshot.
110
111 In-memory databases are **not** dumpable with it. Since it works by copying the database file directly, it is not suitable for databases that have ongoing connections.
112
113 ### MySQL
114
115 #### Settings
116
117 MySQL connectors do not define any configurable settings beyond the common settings documented above.
118
119 However, note that when the common `EXCLUDE` setting is configured in your Django database, each table name is passed to `mysqldump` as `--ignore-table=<database>.<table>`, so the values should be plain table
120 names from the target database.
121
122 #### MysqlDumpConnector
123
124 The `dbbackup.db.mysql.MysqlDumpConnector` creates a SQL dump with `mysqldump` and restores it by piping the dump into `mysql`.
125
126 This is the default connector for MySQL databases.
127
128 ### PostgreSQL
129
130 #### Settings
131
132 | Setting | Description | Default |
133 | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
134 | SINGLE_TRANSACTION | Wrap restore in a single transaction so errors cause full rollback (`--single-transaction` for `psql` / `pg_restore`). | `True` |
135 | DROP | Include / execute drop statements when restoring (`--clean` with `pg_dump` / `pg_restore`). In binary mode drops happen during restore. | `True` |
136 | IF_EXISTS | Add `IF EXISTS` to destructive statements in clean mode. Enabled by default, and when `DROP=True` to prevent identity column errors. | `True` |
137 | PSQL_CMD | Path to `psql` used for admin tasks (extension creation, etc.). | `psql` |
138 | PASSWORD | Sets `PGPASSWORD` if provided (prefer `.pgpass`). Set to `None` for `--no-password` | `""` |
139 | ADMIN_USER | Privileged user for administrative actions like enabling PostGIS. | None |
140 | ADMIN_PASSWORD | Password for `ADMIN_USER` when needed. | None |
141 | SCHEMAS | Limit dump to specific schemas (PostgreSQL connectors only). | All non-system schemas |
142
143 #### PgDumpBinaryConnector
144
145 The `dbbackup.db.postgresql.PgDumpBinaryConnector` is similar to PgDumpConnector, but it uses `pg_dump` in binary mode and restores using `pg_restore`.
146
147 This is the default connector for PostgreSQL databases, and it allows for faster and parallel-capable restores. This connector may invoke `psql` for administrative tasks.
148
149 #### PgDumpConnector
150
151 The `dbbackup.db.postgresql.PgDumpConnector` uses `pg_dump` to create RAW SQL files and `psql` to restore them.
152
153 It is recommended to use the binary connector for better performance.
154
155 #### PgDumpGisConnector
156
157 Set in `dbbackup.db.postgresql.PgDumpGisConnector`, it does the same as
158 PostgreSQL but launches `CREATE EXTENSION IF NOT EXISTS postgis;` before
159 restoring the database.
160
161 ### MongoDB
162
163 #### Settings
164
165 | Setting | Description | Default |
166 | ------------ | --------------------------------------------------- | ------- |
167 | OBJECT_CHECK | Validate documents before inserting (`--objcheck`). | `True` |
168 | DROP | Replace existing objects during restore (`--drop`). | `True` |
169
170 #### MongoDumpConnector
171
172 MongoDB uses by default `dbbackup.db.mongodb.MongoDumpConnector`. It
173 uses `mongodump` and `mongorestore` for its job.
174
175 For authentication enabled MongoDB deployments add the `AUTH_SOURCE` option to
176 indicate the database used to verify credentials.
177
178 ```python
179 DBBACKUP_CONNECTORS = {
180 'default': {
181 ...
182 'AUTH_SOURCE': 'admin',
183 }
184 }
185 ```
186
187 ... or in `DATABASES`:
188
189 ```python
190 DATABASES = {
191 'default': {
192 ...
193 'AUTH_SOURCE': 'admin',
194 }
195 }
196 ```
197
198 ### All Databases (Universal)
199
200 The Django connector (`dbbackup.db.django.DjangoConnector`) provides database-agnostic backup and restore functionality using Django's built-in `dumpdata` and `loaddata` management commands. This connector works with any Django-supported database backend.
201
202 Key features include:
203
204 - **Universal compatibility**: Works with any database backend supported by Django
205 - **No external dependencies**: Uses Django's serialization system
206 - **Model-level backups**: Preserves foreign key relationships and data integrity
207 - **JSON format**: Creates human-readable backups in JSON format
208
209 The Django connector is ideal for:
210
211 - Oracle databases (used by default)
212 - Custom or third-party database backends not explicitly supported
213 - Development environments where simplicity is preferred
214 - Cases where external database tools are not available
215
216 #### DjangoConnector
217
218 This connector is automatically used for any unmapped database engines. If needed, you can explicitly configure it:
219
220 ```python
221 DBBACKUP_CONNECTORS = {
222 'default': {
223 'CONNECTOR': 'dbbackup.db.django.DjangoConnector',
224 }
225 }
226 ```
227
228 #### Limitations
229
230 - **Performance**: Slower than native database tools for large datasets
231 - **Database structure**: Only provides backups of data; not database schema, indices, or procedures
232
233 ### Django Prometheus (External)
234
235 All supported built-in connectors are described in more detail below. Following database wrappers from `django-prometheus` are supported:
236
237 - `django_prometheus.db.backends.postgresql`
238 - `django_prometheus.db.backends.sqlite3`
239 - `django_prometheus.db.backends.mysql`
240 - `django_prometheus.db.backends.postgis`
241
242 ---
243
244 ## Custom Connectors
245
246 To implement a custom connector, subclass `dbbackup.db.base.BaseDBConnector`
247 and implement `_create_dump` / `_restore_dump`. If you need to run external
248 commands, subclass `dbbackup.db.base.BaseCommandDBConnector` to inherit
249 argument assembly helpers and environment handling.
250
251 Here is an example, on how to easily use a custom connector that you have created or even that you simply want to reuse:
252
253 ```python
254 DBBACKUP_CONNECTOR_MAPPING = {
255 'transaction_hooks.backends.postgis': 'dbbackup.db.postgresql.PgDumpGisConnector',
256 }
257 ```
258
259 Swap in any custom connector path you created. The left hand side engine name
260 should match the `ENGINE` value Django reports for that database alias.