@@ -6,10 +6,219 @@ Back Up a Sharded Cluster with Database Dumps
6
6
7
7
.. default-domain:: mongodb
8
8
9
+ To take a consistent backup of a sharded cluster with ``mongodump``, you must
10
+ first stop the balancer, stop writes, and stop any schema transformation
11
+ operations on the cluster. This ensures that the cluster remains in a
12
+ consistent state for the duration of the backup.
9
13
14
+ MongoDB provides backup and restore operations that can run with the balancer
15
+ and running transactions through the following services:
10
16
17
+ - `MongoDB Atlas <https://www.mongodb.com/atlas/database?tck=docs_server>`_
11
18
12
- .. note::
19
+ - `MongoDB Cloud Manager <https://www.mongodb.com/cloud/cloud-manager?tck=docs_server>`_
20
+
21
+ - `MongoDB Ops Manager <https://www.mongodb.com/products/ops-manager?tck=docs_server>`_
22
+
23
+ Before you Begin
24
+ ----------------
25
+
26
+ This task uses :program:`mongodump` to back up a sharded cluster. Ensure
27
+ that you have a cluster running that contains data in sharded collections.
28
+
29
+ Admin Privileges
30
+ ~~~~~~~~~~~~~~~~
31
+
32
+ To perform these tasks, your user must have the :authaction:`fsync`
33
+ authorization, which allows the user to run the :dbcommand:`fsync` and
34
+ :dbcommand:`fsyncUnlock` commands.
35
+
36
+ Steps
37
+ -----
38
+
39
+ To take a self-managed backup of a sharded cluster, complete the following
40
+ steps:
41
+
42
+ .. procedure::
43
+ :style: normal
44
+
45
+ .. step:: Find a Backup Window
46
+
47
+ To find a good time to perform a backup, monitor your application
48
+ and database usage to find a time when chunk migrations, resharding,
49
+ and schema transformation operations are unlikely to occur, as these can cause an
50
+ inconsistent backup.
51
+
52
+ For more information, see :ref:`sharded-schedule-backup`.
53
+
54
+ .. step:: Stop the Balancer
55
+
56
+ To prevent chunk migrations from distruping the backup, connect to
57
+ :program:`mongos` and use the :method:`sh.stopBalancer` method to stop
58
+ the balancer:
59
+
60
+ .. code-block:: javascript
61
+
62
+ sh.stopBalancer()
63
+
64
+ If a balancing round is in progress, the operation waits for balancing to
65
+ complete.
66
+
67
+ To confirm that the balancer is stopped, use the
68
+ :method:`sh.getBalancerState` method:
69
+
70
+ .. io-code-block::
71
+
72
+ .. input::
73
+ :language: javascript
74
+
75
+ sh.getBalancerState()
76
+
77
+ .. output::
78
+ :language: javascript
79
+
80
+ false
81
+
82
+ The command returns ``false`` when the balancer is stopped.
83
+
84
+ .. step:: Lock the Cluster
85
+
86
+ The sharded cluster must remain locked during the backup process to protect
87
+ the database from writes, which may cause inconsistencies in the backup.
88
+
89
+ To lock a sharded cluster, connect to :program:`mongos` and use the
90
+ :method:`db.fsyncLock` method:
91
+
92
+ .. code-block:: javascript
93
+
94
+ db.getSiblingDB("admin").fsyncLock()
95
+
96
+ To confirm the lock, on :program:`mongos` and the primary
97
+ :program:`mongod` of the config servers, run the following
98
+ aggregation pipeline and ensure that all the shards are
99
+ locked:
100
+
101
+ .. io-code-block::
102
+
103
+ .. input::
104
+ :language: javascript
105
+
106
+ db.getSiblingDB("admin").aggregate( [
107
+ { $currentOp: { } },
108
+ { $facet: {
109
+ "locked": [
110
+ { $match: { $and: [
111
+ { fsyncLock: { $exists: true } },
112
+ { fsyncLock: true }
113
+ ] } }],
114
+ "unlocked": [
115
+ { $match: { fsyncLock: { $exists: false } } }
116
+ ]
117
+ } },
118
+ { $project: {
119
+ "fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
120
+ "fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
121
+ } }
122
+ ] )
123
+
124
+ .. output::
125
+ :language: json
126
+
127
+ [ { fsyncLocked: true }, { fsyncUnlocked: false } ]
128
+
129
+ .. step:: Take Backup
130
+
131
+ To back up the sharded cluster, use ``mongodump`` to connect to
132
+ :program:`mongos` and perform the backup:
133
+
134
+ .. code-block:: bash
135
+
136
+ mongodump \
137
+ --host mongos.example.net \
138
+ --port 27017 \
139
+ --username user \
140
+ --password "passwd" \
141
+ --out /opt/backups/example-cluster-1
142
+
143
+ .. step:: Unlock the Cluster
144
+
145
+ After the backup completes, you can unlock the cluster to allow writes
146
+ to resume.
147
+
148
+ To unlock the cluster, use the :method:`db.fsyncUnlock` method:
149
+
150
+ .. code-block:: bash
151
+
152
+ db.getSibling("admin").fsyncUnlock()
153
+
154
+ To confirm the unlock, on :program:`mongos` and the primary
155
+ :program:`mongod` of the config servers, run the following
156
+ aggregation pipeline and ensure that all shards are unlocked:
157
+
158
+ .. io-code-block::
159
+
160
+ .. input::
161
+ :language: javascript
162
+
163
+ db.getSiblingDB("admin").aggregate( [
164
+ { $currentOp: { } },
165
+ { $facet: {
166
+ "locked": [
167
+ { $match: { $and: [
168
+ { fsyncLock: { $exists: true } },
169
+ { fsyncLock: true }
170
+ ] } }],
171
+ "unlocked": [
172
+ { $match: { fsyncLock: { $exists: false } } }
173
+ ]
174
+ } },
175
+ { $project: {
176
+ "fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
177
+ "fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
178
+ } }
179
+ ] )
180
+
181
+ .. output::
182
+ :language: json
183
+
184
+ [ { fsyncLocked: false }, { fsyncUnlocked: true } ]
185
+
186
+ .. step:: Restart the Balancer
187
+
188
+ To restart the balancer, use the :method:`sh.startBalancer` method:
189
+
190
+ .. code-block:: javascript
191
+
192
+ sh.startBalancer()
193
+
194
+ To confirm that the balancer is running, use the
195
+ :method:`sh.getBalancerState` method:
196
+
197
+ .. io-code-block::
198
+
199
+ .. input::
200
+ :language: javascript
201
+
202
+ sh.getBalancerState()
203
+
204
+ .. output::
205
+ :language: javascript
206
+
207
+ true
208
+
209
+ The command returns ``true`` when the balancer is running.
210
+
211
+ Next Steps
212
+ ----------
213
+
214
+ You can restore a database from a :program:`mongodump` backup using
215
+ :program:`mongorestore`.
216
+
217
+ - To restore a sharded cluster, execute ``mongorestore`` against
218
+ :program:`mongos`.
219
+
220
+ - To migrate to a replica set or standalone server, execute ``mongorestore``
221
+ against :program:`mongod`.
222
+
223
+ For more information, see :ref:`manual-tutorial-backup-and-restore`.
13
224
14
- .. include:: /includes/extracts/sharded-clusters-backup-restore-mongodump-mongorestore-restriction.rst
15
-
0 commit comments