Debian Codegraph @

docs/src/storage.md

raw
1 # Storage settings
2
3 One of the most helpful features of django-dbbackup is the ability to store
4 and retrieve backups from local or remote storage. This functionality is
5 mainly based on the Django Storage API.
6
7 A few common third party backends (provided by `django-storages`) are documented on this page for convenience.
8
9 ---
10
11 ## Quick Start
12
13 Configure backup storage via the `STORAGES` setting using the key `'dbbackup'`.
14 `BACKEND` is a dotted path to a Django storage class. The following example uses the local filesystem.
15
16 ```python
17 STORAGES = {
18 "dbbackup": {
19 "BACKEND": "django.core.files.storage.FileSystemStorage",
20 "OPTIONS": {
21 "location": "/my/backup/dir/",
22 },
23 },
24 }
25 ```
26
27 For more granularity, per backend options go into the nested `OPTIONS` dict:
28
29 ```python
30 STORAGES = {
31 "default": {
32 "BACKEND": "django.core.files.storage.FileSystemStorage",
33 "OPTIONS": {
34 ...your_options_here
35 },
36 },
37 "staticfiles": {
38 ...
39 },
40 }
41 ```
42
43 !!! warning
44
45 Do not configure backup storage with the same configuration as your media
46 files as you'll risk sharing backups inside public directories.
47
48 If no explicit `STORAGES['dbbackup']` is provided the default File System Storage
49 is used (pointing at your project media root). Consider isolating backups in a
50 dedicated directory with restricted permissions. Browse additional providers
51 at [Django Packages](https://djangopackages.org/grids/g/storage-backends/).
52
53 !!! note
54
55 Storing backups to local disk may also be useful for Dropbox if you
56 already have the official Dropbox client installed on your system.
57
58 !!! note
59
60 When a storage backend exposes a `location` or key prefix, django-dbbackup
61 treats backup names as relative to that location. This keeps
62 `listbackups`, `dbrestore`, and explicit `--input-filename` usage aligned
63 even if the backend returns prefixed names internally.
64
65 ---
66
67 ## File System Storage
68
69 ### Setup
70
71 To store your backups on the local file system, simply set up the required
72 settings below.
73
74 ```python
75 STORAGES = {
76 "dbbackup": {
77 "BACKEND": "django.core.files.storage.FileSystemStorage",
78 "OPTIONS": {
79 "location": "/my/backup/dir/",
80 },
81 },
82 }
83 ```
84
85 ### Settings
86
87 **`location`**
88
89 Absolute path to the directory that will hold the files.
90
91 **`file_permissions_mode`**
92
93 The file system permissions that the file will receive when it is saved.
94
95 **`directory_permissions_mode`**
96
97 The file system permissions that the directory will receive when it is saved.
98
99 See [FileSystemStorage's documentation](https://docs.djangoproject.com/en/stable/ref/files/storage/#the-filesystemstorage-class) for a full list of available settings.
100
101 ---
102
103 ## Google Cloud Storage
104
105 Our backend for Google cloud storage uses django-storages.
106
107 ### Setup
108
109 Create a Google Cloud project and bucket, then install:
110
111 ```bash
112 pip install django-storages[google]
113 ```
114
115 Add this snippet to settings:
116
117 ```python
118 STORAGES = {
119 "dbbackup": {
120 "BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
121 "OPTIONS": {
122 "bucket_name": "your_bucket_name",
123 "project_id": "your_project_id",
124 "blob_chunk_size": 1024 * 1024,
125 },
126 },
127 }
128 ```
129
130 ### Settings
131
132 Add the following to settings (only `bucket_name` is strictly required). See
133 the [django-storages docs](https://django-storages.readthedocs.io/en/latest/backends/gcloud.html) for advanced options.
134
135 ---
136
137 ## Amazon S3
138
139 Our S3 backend uses Django Storages which uses [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html).
140
141 ### Setup
142
143 Create an AWS account and S3 bucket, then install dependencies:
144
145 ```bash
146 pip install django-storages[boto3]
147 ```
148
149 Add this snippet to settings:
150
151 ```python
152 STORAGES = {
153 "dbbackup": {
154 "BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
155 "OPTIONS": {
156 "access_key": "my_id",
157 "secret_key": "my_secret",
158 "bucket_name": "my_bucket_name",
159 "default_acl": "private",
160 },
161 }
162 }
163 ```
164
165 ### Settings
166
167 !!! note
168
169 See the [Django Storage S3 storage official documentation](https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html) for all options.
170
171 The options listed here are a selection of dictionary keys returned by
172 `get_default_settings` in django-storages' [`storages/backends/s3boto3.py`](https://github.com/jschneier/django-storages/blob/master/storages/backends/s3boto3.py#L293-L324),
173 which allows us to write nicer code compared to using the `AWS_` prefixed
174 settings.
175
176 **`access_key`** - Required
177
178 Your AWS access key. Create one via the IAM console; avoid using root keys.
179
180 **`secret_key`** - Required
181
182 Your Amazon Web Services secret access key, as a string.
183
184 **`bucket_name`** - Required
185
186 Name of the existing bucket to store backups.
187
188 **`region_name`** - Optional
189
190 AWS region for the bucket (e.g. `'us-east-1'`).
191
192 **`endpoint_url`** - Optional
193
194 Override the endpoint (for S3-compatible services like MinIO). Must include
195 protocol, e.g. `https://minio.internal:9000`.
196
197 If you set a custom endpoint also set **`region_name`**.
198
199 **`default_acl`** - Required
200
201 Allowed values: `'private'` or `'public'`. Use `'private'` for backups.
202
203 _NOTE: This value will be removed in a future version of django-storages._
204 See their [CHANGELOG](https://github.com/jschneier/django-storages/blob/master/CHANGELOG.rst) for details.
205
206 **`location`** - Optional
207
208 Prefix inside the bucket; include trailing slash. Example:
209 `location: 'backups/prod/'`.
210
211 ---
212
213 ## Dropbox
214
215 Create a Dropbox app and obtain an access token, then configure the backend.
216
217 ### Setup
218
219 First, configure your Dropbox account by following these steps:
220
221 1. Login to Dropbox and navigate to [Developers ยป MyApps](https://www.dropbox.com/developers/apps).
222
223 2. Click the button to create a new app and name it whatever you like.
224 As an example, I named mine 'Website Backups'.
225
226 3. After your app is created, note the options button and more
227 importantly the 'App Key' and 'App Secret' values inside. You'll need
228 those later.
229
230 Then, configure your Django project by installing the required
231 dependencies:
232
233 ```bash
234 pip install dropbox django-storages
235 ```
236
237 And make sure you have the following required settings:
238
239 ```python
240 STORAGES = {
241 "dbbackup": {
242 "BACKEND": "storages.backends.dropbox.DropBoxStorage",
243 "OPTIONS": {
244 "oauth2_access_token": "my_token",
245 },
246 },
247 }
248 ```
249
250 ### Settings
251
252 !!! note
253
254 See [django-storages dropbox official documentation](https://django-storages.readthedocs.io/en/latest/backends/dropbox.html) for more details.
255
256 **`oauth2_access_token`** - Required
257
258 OAuth 2 access token generated for the app.
259
260 **`root_path`**
261
262 Restrict storage operations to this folder prefix.
263
264 ---
265
266 ## FTP
267
268 To store your database backups on a remote filesystem via FTP, simply
269 set up the required settings below.
270
271 ### Setup
272
273 ```bash
274 pip install django-storages
275 ```
276
277 !!! warning
278
279 This storage doesn't use a private connection for communication, so don't use it
280 if you're not certain about the security of the link between the client and the server.
281
282 ```python
283 STORAGES = {
284 "dbbackup": {
285 "BACKEND": "storages.backends.ftp.FTPStorage",
286 "OPTIONS": {
287 "location": "ftp://user:pass@server:21",
288 },
289 },
290 }
291 ```
292
293 ### Settings
294
295 **`location`** - Required
296
297 FTP URI including optional credentials and port. Example:
298 `ftp://user:[email protected]:21`.
299
300 ---
301
302 ## SFTP
303
304 To store your database backups on a remote filesystem via SFTP, simply
305 set up the required settings below.
306
307 ### Setup
308
309 This backend is from django-storages with the [Paramiko](https://www.paramiko.org/) backend.
310
311 ```bash
312 pip install paramiko django-storages
313 ```
314
315 The following configuration grants SSH server access to the local user:
316
317 ```python
318 STORAGES = {
319 "dbbackup": {
320 "BACKEND": "storages.backends.sftpstorage.SFTPStorage",
321 "OPTIONS": {
322 'host': 'myserver',
323 },
324 },
325 }
326 ```
327
328 ### Settings
329
330 **`host`** - Required
331
332 Host name or address of the SSH server.
333
334 **`root_path`** - Default `~/`
335
336 Jail storage to this directory.
337
338 **`params`** - Default `{}`
339
340 Argument used by method: `paramiko.SSHClient.connect()`.
341 See [paramiko SSHClient.connect() documentation](https://docs.paramiko.org/en/latest/api/client.html#paramiko.client.SSHClient.connect) for details.
342
343 **`interactive`** - Default `False`
344
345 A boolean indicating whether to prompt for a password if the connection cannot
346 be made using keys and there is not already a password in `params`.
347
348 **`file_mode`**
349
350 UID of the account that should be set as owner of the files on the remote.
351
352 **`dir_mode`**
353
354 GID of the group that should be set on the files on the remote host.
355
356 **`known_host_file`**
357
358 Absolute path of known_hosts file; if it isn't set `"~/.ssh/known_hosts"` will be used.