nextcloud-s3-backup 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nc_s3_backup/__init__.py +1 -0
- nc_s3_backup/api/__init__.py +0 -0
- nc_s3_backup/api/backup.py +430 -0
- nc_s3_backup/api/config.py +54 -0
- nc_s3_backup/api/db.py +138 -0
- nc_s3_backup/cli.py +400 -0
- nc_s3_backup/py.typed +0 -0
- nextcloud_s3_backup-1.0.0.dist-info/METADATA +373 -0
- nextcloud_s3_backup-1.0.0.dist-info/RECORD +12 -0
- nextcloud_s3_backup-1.0.0.dist-info/WHEEL +4 -0
- nextcloud_s3_backup-1.0.0.dist-info/entry_points.txt +4 -0
- nextcloud_s3_backup-1.0.0.dist-info/licenses/LICENSE +202 -0
nc_s3_backup/cli.py
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
import logging.config
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import boto3
|
|
9
|
+
from boto3.s3.transfer import TransferConfig
|
|
10
|
+
from cloudpathlib import AnyPath, S3Client
|
|
11
|
+
from pydantic import TypeAdapter
|
|
12
|
+
from yaml import safe_dump, safe_load
|
|
13
|
+
|
|
14
|
+
from nc_s3_backup.api.backup import (
|
|
15
|
+
REPOSITORY_DIRNAME,
|
|
16
|
+
SNAPSHOT_DIRNAME,
|
|
17
|
+
NextcloudS3Backup,
|
|
18
|
+
)
|
|
19
|
+
from nc_s3_backup.api.config import NextcloudDirectoryConfig, NextCloudS3BackupConfig
|
|
20
|
+
from nc_s3_backup.api.db import DaoNextcloudFiles
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def logging_params(parser):
|
|
26
|
+
logging_group = parser.add_argument_group("Logging params")
|
|
27
|
+
logging_group.add_argument(
|
|
28
|
+
"-f",
|
|
29
|
+
"--logging-file",
|
|
30
|
+
type=Path,
|
|
31
|
+
help="Logging configuration file, (logging-level and logging-format "
|
|
32
|
+
"are ignored if provide)",
|
|
33
|
+
)
|
|
34
|
+
logging_group.add_argument("-l", "--logging-level", default="INFO")
|
|
35
|
+
logging_group.add_argument(
|
|
36
|
+
"--logging-format",
|
|
37
|
+
default="%(asctime)s - %(levelname)s (%(module)s%(funcName)s): %(message)s",
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def pg_params(parser):
|
|
42
|
+
gp = parser.add_argument_group("Postgresql connection")
|
|
43
|
+
gp.add_argument(
|
|
44
|
+
"--pg-dsn",
|
|
45
|
+
help="Postgresql connection string",
|
|
46
|
+
default=f"postgresql:///nc-backup?application_name={parser.prog}",
|
|
47
|
+
)
|
|
48
|
+
gp.add_argument("--pg-schema", help="Postgresql default schema", default="public")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def s3_params(parser):
|
|
52
|
+
group = parser.add_argument_group("S3 configuration")
|
|
53
|
+
|
|
54
|
+
group.add_argument(
|
|
55
|
+
"--s3-endpoint-url",
|
|
56
|
+
dest="s3_endpoint_url",
|
|
57
|
+
default="https://s3.fr-par.scw.cloud",
|
|
58
|
+
type=str,
|
|
59
|
+
help="S3 endpoint url",
|
|
60
|
+
)
|
|
61
|
+
group.add_argument(
|
|
62
|
+
"--s3-region-name",
|
|
63
|
+
dest="s3_region_name",
|
|
64
|
+
default="fr-par",
|
|
65
|
+
type=str,
|
|
66
|
+
help="S3 region name.",
|
|
67
|
+
)
|
|
68
|
+
group.add_argument(
|
|
69
|
+
"--s3-access-key", dest="s3_access_key_id", type=str, help="S3 access key ID."
|
|
70
|
+
)
|
|
71
|
+
group.add_argument(
|
|
72
|
+
"--s3-secret-key", dest="s3_secret_access_key", type=str, help="S3 secret key."
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
group_s3_transfer = parser.add_argument_group("S3 Transfer configuration")
|
|
76
|
+
|
|
77
|
+
group_s3_transfer.add_argument(
|
|
78
|
+
"--s3-multipart-threshold",
|
|
79
|
+
dest="s3_multipart_threshold_mb",
|
|
80
|
+
default=120,
|
|
81
|
+
type=int,
|
|
82
|
+
help=(
|
|
83
|
+
"The transfer size threshold for which "
|
|
84
|
+
"multipart uploads, downloads, and copies will automatically be "
|
|
85
|
+
"triggered.. (MB)"
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
group_s3_transfer.add_argument(
|
|
89
|
+
"--s3-multipart-chunksize",
|
|
90
|
+
dest="s3_multipart_chunksize_mb",
|
|
91
|
+
default=100,
|
|
92
|
+
type=int,
|
|
93
|
+
help=("The partition size of each part for a multipart transfer. (MB)"),
|
|
94
|
+
)
|
|
95
|
+
group_s3_transfer.add_argument(
|
|
96
|
+
"--s3-no-threads",
|
|
97
|
+
dest="s3_no_threads",
|
|
98
|
+
action="store_true",
|
|
99
|
+
help=(
|
|
100
|
+
"If False, threads will be used when performing "
|
|
101
|
+
"S3 transfers. If True, no threads will be used in "
|
|
102
|
+
"performing transfers; all logic will be run in the main thread."
|
|
103
|
+
),
|
|
104
|
+
)
|
|
105
|
+
group_s3_transfer.add_argument(
|
|
106
|
+
"--s3-max-bandwidth",
|
|
107
|
+
dest="s3_max_bandwidth_mb",
|
|
108
|
+
type=int,
|
|
109
|
+
help=(
|
|
110
|
+
"The maximum bandwidth that will be consumed "
|
|
111
|
+
"in uploading and downloading file content. The value is an integer "
|
|
112
|
+
"in terms of Mega bytes per second. (MB)"
|
|
113
|
+
),
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
group_s3_transfer.add_argument(
|
|
117
|
+
"--s3-max-concurrency",
|
|
118
|
+
dest="s3_max_concurrency",
|
|
119
|
+
default=10,
|
|
120
|
+
type=int,
|
|
121
|
+
help=(
|
|
122
|
+
"The maximum number of threads that will be "
|
|
123
|
+
"making requests to perform a transfer. If ``use_threads`` is "
|
|
124
|
+
"set to ``False``, the value provided is ignored as the transfer "
|
|
125
|
+
"will only ever use the main thread. (10)"
|
|
126
|
+
),
|
|
127
|
+
)
|
|
128
|
+
group_s3_transfer.add_argument(
|
|
129
|
+
"--s3-num-download-attempts",
|
|
130
|
+
dest="s3_num_download_attempts",
|
|
131
|
+
default=5,
|
|
132
|
+
type=int,
|
|
133
|
+
help=(
|
|
134
|
+
"The number of download attempts that "
|
|
135
|
+
"will be retried upon errors with downloading an object in S3. "
|
|
136
|
+
"Note that these retries account for errors that occur when "
|
|
137
|
+
"streaming down the data from s3 (i.e. socket errors and read "
|
|
138
|
+
"timeouts that occur after receiving an OK response from s3). "
|
|
139
|
+
"Other retryable exceptions such as throttling errors and 5xx "
|
|
140
|
+
"errors are already retried by botocore (this default is 5). This "
|
|
141
|
+
"does not take into account the number of exceptions retried by "
|
|
142
|
+
"botocore."
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
group_s3_transfer.add_argument(
|
|
146
|
+
"--s3-max-io-queue",
|
|
147
|
+
dest="s3_max_io_queue_mb",
|
|
148
|
+
default=100,
|
|
149
|
+
type=int,
|
|
150
|
+
help=(
|
|
151
|
+
"The maximum amount of read parts that can be "
|
|
152
|
+
"queued in memory to be written for a download. The size of each "
|
|
153
|
+
"of these read parts is at most the size of ``io_chunksize``. (MB)"
|
|
154
|
+
),
|
|
155
|
+
)
|
|
156
|
+
group_s3_transfer.add_argument(
|
|
157
|
+
"--s3-io-chunksize",
|
|
158
|
+
dest="s3_io_chunksize_mb",
|
|
159
|
+
default=1,
|
|
160
|
+
type=int,
|
|
161
|
+
help=(
|
|
162
|
+
"The max size of each chunk in the io queue. "
|
|
163
|
+
"Currently, this is size used when ``read`` is called on the "
|
|
164
|
+
"downloaded stream as well. (MB)"
|
|
165
|
+
),
|
|
166
|
+
)
|
|
167
|
+
group_s3_transfer.add_argument(
|
|
168
|
+
"--s3-progress",
|
|
169
|
+
dest="s3_progress",
|
|
170
|
+
action="store_true",
|
|
171
|
+
help=(
|
|
172
|
+
"Display upload/downloads progress in stdout. To be use with "
|
|
173
|
+
"--mt-thread-size=1 not properly working otherwise"
|
|
174
|
+
),
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def parse_setup_s3(arguments):
|
|
179
|
+
session_params = {}
|
|
180
|
+
if arguments.s3_region_name:
|
|
181
|
+
session_params["region_name"] = arguments.s3_region_name
|
|
182
|
+
if arguments.s3_access_key_id:
|
|
183
|
+
session_params["aws_access_key_id"] = arguments.s3_access_key_id
|
|
184
|
+
if arguments.s3_secret_access_key:
|
|
185
|
+
session_params["aws_secret_access_key"] = arguments.s3_secret_access_key
|
|
186
|
+
|
|
187
|
+
KB = 1024
|
|
188
|
+
MB = KB**2
|
|
189
|
+
|
|
190
|
+
transfer_config_params = {
|
|
191
|
+
"use_threads": not arguments.s3_no_threads,
|
|
192
|
+
"multipart_threshold": arguments.s3_multipart_threshold_mb * MB,
|
|
193
|
+
"multipart_chunksize": arguments.s3_multipart_chunksize_mb * MB,
|
|
194
|
+
"max_bandwidth": (
|
|
195
|
+
arguments.s3_max_bandwidth_mb * MB
|
|
196
|
+
if arguments.s3_max_bandwidth_mb
|
|
197
|
+
else None
|
|
198
|
+
),
|
|
199
|
+
"max_concurrency": arguments.s3_max_concurrency,
|
|
200
|
+
"num_download_attempts": arguments.s3_num_download_attempts,
|
|
201
|
+
"max_io_queue": arguments.s3_max_io_queue_mb * MB,
|
|
202
|
+
"io_chunksize": arguments.s3_io_chunksize_mb * MB,
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
client_kwargs = {
|
|
206
|
+
"boto3_transfer_config": TransferConfig(**transfer_config_params),
|
|
207
|
+
}
|
|
208
|
+
if arguments.s3_endpoint_url:
|
|
209
|
+
client_kwargs["endpoint_url"] = arguments.s3_endpoint_url
|
|
210
|
+
if session_params:
|
|
211
|
+
client_kwargs["boto3_session"] = boto3.Session(**session_params)
|
|
212
|
+
|
|
213
|
+
client = S3Client(**client_kwargs)
|
|
214
|
+
client.set_as_default_client()
|
|
215
|
+
return client
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def parse_config(config_file):
|
|
219
|
+
"""Content is NextCloudS3Config to be deserialized by pydantic
|
|
220
|
+
as far we are using yaml parser, you can define as json or yaml
|
|
221
|
+
format
|
|
222
|
+
"""
|
|
223
|
+
return NextCloudS3BackupConfig(**safe_load(config_file.read()))
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def main(testing: bool = False):
|
|
227
|
+
parser = argparse.ArgumentParser(
|
|
228
|
+
description="Nextcloud S3 backup",
|
|
229
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
230
|
+
)
|
|
231
|
+
parser.add_argument(
|
|
232
|
+
"config",
|
|
233
|
+
type=Path,
|
|
234
|
+
help=(
|
|
235
|
+
"Nextcloud S3 backup config file is a json/yaml file that "
|
|
236
|
+
"contains mapping of directories to backup."
|
|
237
|
+
),
|
|
238
|
+
)
|
|
239
|
+
logging_params(parser)
|
|
240
|
+
s3_params(parser)
|
|
241
|
+
pg_params(parser)
|
|
242
|
+
arguments = parser.parse_args()
|
|
243
|
+
|
|
244
|
+
logging.basicConfig(
|
|
245
|
+
level=getattr(logging, arguments.logging_level.upper()),
|
|
246
|
+
format=arguments.logging_format,
|
|
247
|
+
)
|
|
248
|
+
if arguments.logging_file:
|
|
249
|
+
try:
|
|
250
|
+
json_config = json.loads(arguments.logging_file.read_text())
|
|
251
|
+
logging.config.dictConfig(json_config)
|
|
252
|
+
except json.JSONDecodeError:
|
|
253
|
+
logging.config.fileConfig(str(arguments.logging_file))
|
|
254
|
+
|
|
255
|
+
parse_setup_s3(arguments)
|
|
256
|
+
with arguments.config.open("r") as config_file:
|
|
257
|
+
config = parse_config(config_file)
|
|
258
|
+
dao = DaoNextcloudFiles(arguments.pg_dsn, schema=arguments.pg_schema)
|
|
259
|
+
nextcloud_s3_backup = NextcloudS3Backup(dao, config)
|
|
260
|
+
errors = nextcloud_s3_backup.backup()
|
|
261
|
+
if testing:
|
|
262
|
+
return nextcloud_s3_backup
|
|
263
|
+
if errors > 0:
|
|
264
|
+
# at least one error leaves unsuccessful program
|
|
265
|
+
logger.error("%d file(s) didn't backup properly", errors)
|
|
266
|
+
sys.exit(99)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def purge(testing: bool = False):
|
|
270
|
+
parser = argparse.ArgumentParser(
|
|
271
|
+
description=(
|
|
272
|
+
"Nextcloud S3 backup purge\n\n"
|
|
273
|
+
"Use with caution! \n"
|
|
274
|
+
"This script loop over uniques backup_root_path present "
|
|
275
|
+
"in your config file to remove files present in "
|
|
276
|
+
f"the {REPOSITORY_DIRNAME} that are not present in "
|
|
277
|
+
f"the {SNAPSHOT_DIRNAME} dicectory"
|
|
278
|
+
),
|
|
279
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
280
|
+
)
|
|
281
|
+
parser.add_argument(
|
|
282
|
+
"config",
|
|
283
|
+
type=Path,
|
|
284
|
+
help=(
|
|
285
|
+
"Nextcloud S3 backup config file is a json/yaml file that "
|
|
286
|
+
"contains mapping of directories to backup."
|
|
287
|
+
),
|
|
288
|
+
)
|
|
289
|
+
logging_params(parser)
|
|
290
|
+
arguments = parser.parse_args()
|
|
291
|
+
|
|
292
|
+
logging.basicConfig(
|
|
293
|
+
level=getattr(logging, arguments.logging_level.upper()),
|
|
294
|
+
format=arguments.logging_format,
|
|
295
|
+
)
|
|
296
|
+
if arguments.logging_file:
|
|
297
|
+
try:
|
|
298
|
+
json_config = json.loads(arguments.logging_file.read_text())
|
|
299
|
+
logging.config.dictConfig(json_config)
|
|
300
|
+
except json.JSONDecodeError:
|
|
301
|
+
logging.config.fileConfig(str(arguments.logging_file))
|
|
302
|
+
|
|
303
|
+
with arguments.config.open("r") as config_file:
|
|
304
|
+
config = parse_config(config_file)
|
|
305
|
+
nextcloud_s3_backup = NextcloudS3Backup(None, config)
|
|
306
|
+
nextcloud_s3_backup.purge()
|
|
307
|
+
if testing:
|
|
308
|
+
return nextcloud_s3_backup
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def config_helper():
|
|
312
|
+
parser = argparse.ArgumentParser(
|
|
313
|
+
description="Helper to validate/convert NextCloudS3Config"
|
|
314
|
+
"and add entry. Do not change current files, display result in output.",
|
|
315
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
316
|
+
)
|
|
317
|
+
group = parser.add_argument_group("Optional add mapping")
|
|
318
|
+
|
|
319
|
+
group.add_argument(
|
|
320
|
+
"--storage-id",
|
|
321
|
+
dest="storage_id",
|
|
322
|
+
type=int,
|
|
323
|
+
help="Nextcloud storage id (`storage`), used to query oc_filecache table",
|
|
324
|
+
)
|
|
325
|
+
group.add_argument(
|
|
326
|
+
"--user-name",
|
|
327
|
+
dest="user_name",
|
|
328
|
+
type=str,
|
|
329
|
+
help=(
|
|
330
|
+
"User name used to create directory root tree "
|
|
331
|
+
"and make config file easier to read. "
|
|
332
|
+
"This value can differ from nextcloud database information"
|
|
333
|
+
),
|
|
334
|
+
)
|
|
335
|
+
group.add_argument(
|
|
336
|
+
"--bucket",
|
|
337
|
+
dest="bucket",
|
|
338
|
+
type=AnyPath,
|
|
339
|
+
help=(
|
|
340
|
+
"S3 bucket uri. the root directory where are stored Nextcloud user "
|
|
341
|
+
"storage. ie: s3://com.example.nextcloud/"
|
|
342
|
+
),
|
|
343
|
+
)
|
|
344
|
+
group.add_argument(
|
|
345
|
+
"--nextcloud-path",
|
|
346
|
+
dest="nextcloud_path",
|
|
347
|
+
# default="/",
|
|
348
|
+
type=str,
|
|
349
|
+
help=("Root path to archive for given storage user as seen in nextcloud"),
|
|
350
|
+
)
|
|
351
|
+
group.add_argument(
|
|
352
|
+
"--backup-root-path",
|
|
353
|
+
dest="backup_root_path",
|
|
354
|
+
# default="./",
|
|
355
|
+
type=Path,
|
|
356
|
+
help=("Root path where to backup files"),
|
|
357
|
+
)
|
|
358
|
+
parser.add_argument(
|
|
359
|
+
"parser",
|
|
360
|
+
type=str,
|
|
361
|
+
choices=["json", "yaml"],
|
|
362
|
+
help="choose output format",
|
|
363
|
+
)
|
|
364
|
+
parser.add_argument(
|
|
365
|
+
"config",
|
|
366
|
+
type=Path,
|
|
367
|
+
help=(
|
|
368
|
+
"Nextcloud S3 backup config file is a json/yaml file that "
|
|
369
|
+
"contains mapping of directories to backup."
|
|
370
|
+
),
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
arguments = parser.parse_args()
|
|
374
|
+
with arguments.config.open("r") as config_file:
|
|
375
|
+
config = parse_config(config_file)
|
|
376
|
+
if any(
|
|
377
|
+
[
|
|
378
|
+
arguments.storage_id,
|
|
379
|
+
arguments.user_name,
|
|
380
|
+
arguments.bucket,
|
|
381
|
+
arguments.nextcloud_path,
|
|
382
|
+
arguments.backup_root_path,
|
|
383
|
+
]
|
|
384
|
+
):
|
|
385
|
+
config.mapping.append(
|
|
386
|
+
NextcloudDirectoryConfig(
|
|
387
|
+
storage_id=arguments.storage_id,
|
|
388
|
+
user_name=arguments.user_name,
|
|
389
|
+
bucket=arguments.bucket,
|
|
390
|
+
nextcloud_path=arguments.nextcloud_path,
|
|
391
|
+
backup_root_path=arguments.backup_root_path,
|
|
392
|
+
)
|
|
393
|
+
)
|
|
394
|
+
json_data = (
|
|
395
|
+
TypeAdapter(NextCloudS3BackupConfig).dump_json(config, indent=2).decode()
|
|
396
|
+
)
|
|
397
|
+
if arguments.parser == "yaml":
|
|
398
|
+
print(safe_dump(safe_load(json_data), indent=2))
|
|
399
|
+
else:
|
|
400
|
+
print(json_data)
|
nc_s3_backup/py.typed
ADDED
|
File without changes
|