|
| 1 | +import json |
1 | 2 | import os |
2 | 3 | import logging |
3 | 4 | import sys |
@@ -31,6 +32,7 @@ def __init__(self, state, uri, timer, config, base_dir, version, threads=0, dump |
31 | 32 | self.ssl_ca_file = self.config.ssl.ca_file |
32 | 33 | self.ssl_crl_file = self.config.ssl.crl_file |
33 | 34 | self.ssl_client_cert_file = self.config.ssl.client_cert_file |
| 35 | + self.read_pref_tags = self.config.replication.read_pref_tags |
34 | 36 | self.binary = self.config.backup.mongodump.binary |
35 | 37 |
|
36 | 38 | self.timer_name = "%s-%s" % (self.__class__.__name__, self.uri.replset) |
@@ -61,6 +63,22 @@ def do_ssl(self): |
61 | 63 | def do_ssl_insecure(self): |
62 | 64 | return parse_config_bool(self.config.ssl.insecure) |
63 | 65 |
|
| 66 | + def is_version_gte(self, compare): |
| 67 | + if os.path.isfile(self.binary) and os.access(self.binary, os.X_OK): |
| 68 | + if tuple(compare.split(".")) <= tuple(self.version.split(".")): |
| 69 | + return True |
| 70 | + return False |
| 71 | + |
| 72 | + def parse_read_pref(self, mode="secondary"): |
| 73 | + rp = {"mode": mode} |
| 74 | + if self.read_pref_tags: |
| 75 | + rp["tags"] = {} |
| 76 | + for pair in self.read_pref_tags.replace(" ", "").split(","): |
| 77 | + if ":" in pair: |
| 78 | + key, value = pair.split(":") |
| 79 | + rp["tags"][key] = str(value) |
| 80 | + return json.dumps(rp) |
| 81 | + |
64 | 82 | def parse_mongodump_line(self, line): |
65 | 83 | try: |
66 | 84 | line = line.rstrip() |
@@ -125,33 +143,59 @@ def mongodump_cmd(self): |
125 | 143 | mongodump_uri = self.uri.get() |
126 | 144 | mongodump_cmd = [self.binary] |
127 | 145 | mongodump_flags = ["--host", mongodump_uri.host, "--port", str(mongodump_uri.port), "--oplog", "--out", "%s/dump" % self.backup_dir] |
| 146 | + |
| 147 | + # --numParallelCollections |
128 | 148 | if self.threads > 0: |
129 | | - mongodump_flags.extend(["--numParallelCollections=" + str(self.threads)]) |
| 149 | + mongodump_flags.append("--numParallelCollections=%s" % str(self.threads)) |
| 150 | + |
| 151 | + # --gzip |
130 | 152 | if self.dump_gzip: |
131 | | - mongodump_flags.extend(["--gzip"]) |
132 | | - if tuple("3.4.0".split(".")) <= tuple(self.version.split(".")): |
133 | | - mongodump_flags.extend(["--readPreference=secondary"]) |
| 153 | + mongodump_flags.append("--gzip") |
| 154 | + |
| 155 | + # --readPreference |
| 156 | + if self.is_version_gte("3.2.0"): |
| 157 | + read_pref = self.parse_read_pref() |
| 158 | + if read_pref: |
| 159 | + mongodump_flags.append("--readPreference='%s'" % read_pref) |
| 160 | + elif self.read_pref_tags: |
| 161 | + logging.fatal("Mongodump must be >= 3.4.0 to set read preference!") |
| 162 | + sys.exit(1) |
| 163 | + |
| 164 | + # --username/--password/--authdb |
134 | 165 | if self.authdb and self.authdb != "admin": |
135 | 166 | logging.debug("Using database %s for authentication" % self.authdb) |
136 | | - mongodump_flags.extend(["--authenticationDatabase", self.authdb]) |
| 167 | + mongodump_flags.append("--authenticationDatabase=%s" % self.authdb) |
137 | 168 | if self.user and self.password: |
138 | 169 | # >= 3.0.2 supports password input via stdin to mask from ps |
139 | | - if tuple(self.version.split(".")) >= tuple("3.0.2".split(".")): |
140 | | - mongodump_flags.extend(["-u", self.user, "-p", '""']) |
| 170 | + if self.is_version_gte("3.0.2"): |
| 171 | + mongodump_flags.extend([ |
| 172 | + "--username=%s" % self.user, |
| 173 | + "--password=\"\"" |
| 174 | + ]) |
141 | 175 | self.do_stdin_passwd = True |
142 | 176 | else: |
143 | 177 | logging.warning("Mongodump is too old to set password securely! Upgrade to mongodump >= 3.0.2 to resolve this") |
144 | | - mongodump_flags.extend(["-u", self.user, "-p", self.password]) |
| 178 | + mongodump_flags.extend([ |
| 179 | + "--username=%s" % self.user, |
| 180 | + "--password=%s" % self.password |
| 181 | + ]) |
| 182 | + |
| 183 | + # --ssl |
145 | 184 | if self.do_ssl(): |
146 | | - mongodump_flags.append("--ssl") |
147 | | - if self.ssl_ca_file: |
148 | | - mongodump_flags.extend(["--sslCAFile", self.ssl_ca_file]) |
149 | | - if self.ssl_crl_file: |
150 | | - mongodump_flags.extend(["--sslCRLFile", self.ssl_crl_file]) |
151 | | - if self.client_cert_file: |
152 | | - mongodump_flags.extend(["--sslPEMKeyFile", self.ssl_cert_file]) |
153 | | - if self.do_ssl_insecure(): |
154 | | - mongodump_flags.extend(["--sslAllowInvalidCertificates", "--sslAllowInvalidHostnames"]) |
| 185 | + if self.is_version_gte("2.6.0"): |
| 186 | + mongodump_flags.append("--ssl") |
| 187 | + if self.ssl_ca_file: |
| 188 | + mongodump_flags.extend(["--sslCAFile", self.ssl_ca_file]) |
| 189 | + if self.ssl_crl_file: |
| 190 | + mongodump_flags.extend(["--sslCRLFile", self.ssl_crl_file]) |
| 191 | + if self.client_cert_file: |
| 192 | + mongodump_flags.extend(["--sslPEMKeyFile", self.ssl_cert_file]) |
| 193 | + if self.do_ssl_insecure(): |
| 194 | + mongodump_flags.extend(["--sslAllowInvalidCertificates", "--sslAllowInvalidHostnames"]) |
| 195 | + else: |
| 196 | + logging.fatal("Mongodump must be >= 2.6.0 to enable SSL!") |
| 197 | + sys.exit(1) |
| 198 | + |
155 | 199 | mongodump_cmd.extend(mongodump_flags) |
156 | 200 | return mongodump_cmd |
157 | 201 |
|
|
0 commit comments