Skip to content

Commit ac4a3dd

Browse files
committed
fix for mongodb dumps without authentication
1 parent af12e4c commit ac4a3dd

2 files changed

Lines changed: 53 additions & 15 deletions

File tree

src/dump.py

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
}
2020

2121

22+
def __run_command(command: str):
23+
logger.info(f'{datetime.now()}: Running dm command: {command}')
24+
25+
wait_exit_status = os.system(command)
26+
exit_status = os.waitstatus_to_exitcode(wait_exit_status)
27+
28+
return exit_status
29+
30+
2231
def dump_general(template, file_ext):
2332
def wrapper(environment, output_path):
2433
dump_path = output_path + file_ext
@@ -32,12 +41,9 @@ def wrapper(environment, output_path):
3241
dump_path=dump_path,
3342
)
3443

35-
logger.info(f'{datetime.now()}: Running dm command: {dump_command}')
36-
37-
wait_exit_status = os.system(dump_command)
38-
exit_status = os.waitstatus_to_exitcode(wait_exit_status)
44+
exit_status = __run_command(dump_command)
3945

40-
if wait_exit_status != 0 or exit_status != 0:
46+
if exit_status != 0:
4147
logger.error("RETURN CODE OF DUMP PROCESS != 0. CHECK OUTPUT ABOVE FOR ERRORS!")
4248
send_slack_message(environment, "Failed to create DB dump. Please check the error in the container logs.", 'FAIL')
4349
raise Exception('Failed to create dump of database')
@@ -47,6 +53,37 @@ def wrapper(environment, output_path):
4753
return wrapper
4854

4955

56+
def dump_mongodb(environment, output_path):
57+
template_auth = '/bin/bash -c \'mongodump -h "{host}" --port {port} -u "{user}" -p "{password}" -d "{database}" --authenticationDatabase="{auth_database}" --out "{dump_path}.folder" && tar -czf {dump_path} -C "{dump_path}.folder" . && rm -rf "{dump_path}.folder"\''
58+
template_noauth = '/bin/bash -c \'mongodump -h "{host}" --port {port} -d "{database}" --out "{dump_path}.folder" && tar -czf {dump_path} -C "{dump_path}.folder" . && rm -rf "{dump_path}.folder"\''
59+
60+
dump_path = output_path + '.tar.gz'
61+
62+
if environment.get('DATABASE_USER') == '' and environment.get('DATABASE_PASSWORD') == '':
63+
template = template_noauth
64+
else:
65+
template = template_auth
66+
67+
dump_command = template.format(
68+
host=environment.get('DATABASE_HOST'),
69+
user=environment.get('DATABASE_USER'),
70+
password=environment.get('DATABASE_PASSWORD'),
71+
database=environment.get('DATABASE_NAME'),
72+
auth_database=environment.get('AUTH_DATABASE_NAME'),
73+
port=environment.get('DATABASE_PORT'),
74+
dump_path=dump_path,
75+
)
76+
77+
exit_status = __run_command(dump_command)
78+
if exit_status != 0:
79+
logger.error("RETURN CODE OF DUMP PROCESS != 0. CHECK OUTPUT ABOVE FOR ERRORS!")
80+
send_slack_message(environment, "Failed to create DB dump. Please check the error in the container logs.",
81+
'FAIL')
82+
raise Exception('Failed to create dump of database')
83+
84+
return dump_path
85+
86+
5087
def dump_clickhouse(environment, output_path):
5188
dump_path = output_path + '.sql.gz'
5289
def stringify_row(row):
@@ -104,7 +141,7 @@ def dump_database(environment):
104141
dump_database_methods = {
105142
'mysql': dump_general('/bin/bash -c \'set -o pipefail; mysqldump -h "{host}" -u "{user}" -p"{password}" --databases "{database}" -P {port} --protocol tcp | gzip -9 > {dump_path}\'', '.sql.gz'),
106143
'postgresql': dump_general('PGPASSWORD="{password}" pg_dump -h "{host}" -U "{user}" -d "{database}" -p {port} -Fp -Z9 > {dump_path}', 'sql.gz'),
107-
'mongodb': dump_general('/bin/bash -c \'rm -rf "*.folder" && mongodump -h "{host}" --port {port} -u "{user}" -p "{password}" -d "{database}" --authenticationDatabase="{auth_database}" --out "{dump_path}.folder" && tar -czf {dump_path} -C "{dump_path}.folder" .\'', '.tar.gz'),
144+
'mongodb': dump_mongodb,
108145
'clickhouse': dump_clickhouse,
109146
}
110147

@@ -125,9 +162,10 @@ def dump_database(environment):
125162
try:
126163
s3 = boto3.client('s3')
127164
try:
165+
region = environment.get('AWS_DEFAULT_REGION')
128166
s3.create_bucket(
129167
Bucket=environment.get('GLACIER_BUCKET_NAME'),
130-
CreateBucketConfiguration={'LocationConstraint': environment.get('AWS_DEFAULT_REGION')},
168+
CreateBucketConfiguration={'LocationConstraint': region} if region != 'us-east-1' else {},
131169
)
132170
except (s3.exceptions.BucketAlreadyExists, s3.exceptions.BucketAlreadyOwnedByYou):
133171
pass
@@ -183,12 +221,12 @@ def dump_database(environment):
183221
)
184222

185223
with open(dump_path, 'rb') as file:
186-
# logger.info(s3.put_object(
187-
# Bucket=environment.get('GLACIER_BUCKET_NAME'),
188-
# Key=filename,
189-
# Body=file,
190-
# StorageClass=storage_class_map[environment.get('GLACIER_STORAGE_CLASS')]
191-
# ))
224+
logger.info(s3.put_object(
225+
Bucket=environment.get('GLACIER_BUCKET_NAME'),
226+
Key=filename,
227+
Body=file,
228+
StorageClass=storage_class_map[environment.get('GLACIER_STORAGE_CLASS')]
229+
))
192230
logger.info('Archive upload done.')
193231
send_slack_message(environment, f"Successfully created and uploaded DB dump ({sizeof_fmt(file_size)}).")
194232
except Exception as e:

src/env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def get_env():
2323
'DATABASE_TYPE': {'type': str, 'enum': ['postgresql', 'mysql', 'clickhouse', 'mongodb']},
2424
'DATABASE_HOST': {'type': str},
2525
'DATABASE_NAME': {'type': str},
26-
'DATABASE_USER': {'type': str},
27-
'DATABASE_PASSWORD': {'type': str},
26+
'DATABASE_USER': {'type': str, 'required': False, 'default': ''},
27+
'DATABASE_PASSWORD': {'type': str, 'required': False, 'default': ''},
2828
'DATABASE_PORT': {'type': int, 'required': False, 'default': 0},
2929
'AUTH_DATABASE_NAME': {'type': str, 'required': False, 'default': 'admin'},
3030
'GLACIER_BUCKET_NAME': {'type': str, 'regex': '(?!(^xn--|-s3alias$))^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$'},

0 commit comments

Comments
 (0)