1
- =============================
2
- How to Copy or Clone Database
3
- =============================
1
+ ===========================================
2
+ Copy Databases Between ``mongod`` Instances
3
+ ===========================================
4
4
5
5
.. default-domain:: mongodb
6
6
7
7
Sypnosis
8
8
--------
9
9
10
- One may need to copy a :term:`database` from one server to another,
11
- rename a database, move a database within the same server, seed a
12
- development server from a production server, or create a test
13
- environment with production data. The :dbcommand:`copydb` and
14
- :dbcommand:`clone` function in MongoDB can help one in these
15
- situations.
10
+ In some situations, you may need to copy a :term:`database` from one
11
+ MongoDB instance to another. For example, you may use these commands
12
+ to support migrations and data warehousing, seeding test environments,
13
+ or to help perform backups. The :dbcommand:`copydb` and
14
+ :dbcommand:`clone` function in MongoDB can help in these situations.
16
15
17
16
This document oulines the procedure to copy MongoDB databases using
18
17
the :dbcommand:`copydb` and :dbcommand:`clone` command.
19
18
20
19
The :dbcommand:`copydb` and :dbcommand:`clone` are faster than
21
- :program:`mongodump` and :program:`mongorestore` because there are no
22
- intermediate files to be created in the :dbcommand:`copydb` process .
20
+ :program:`mongodump` and :program:`mongorestore` because the commands
21
+ do not require intermediate files .
23
22
24
23
.. note::
25
24
26
25
:dbcommand:`copydb` and :dbcommand:`clone` do not produce
27
26
point-in-time snapshots of the source database. Write traffic to
28
- the source database during the copy process may result in databases
29
- with different contents.
27
+ the source or destination database during the copy process may
28
+ result in databases with different contents.
30
29
31
30
Considerations
32
31
--------------
33
32
34
- - :dbcommand:`copydb` or :dbcommand:`clone` must be run on the
33
+ - You must run :dbcommand:`copydb` or :dbcommand:`clone` on the
35
34
destination server.
36
35
37
- - :dbcommand:`copydb` or :dbcommand:`clone` do not work with a sharded
38
- database, such as a database a from :term:`shard cluster`.
36
+ - You can use :dbcommand:`copydb` or :dbcommand:`clone` with unsharded
37
+ databases in a :term:`shard cluster` when you're connected directly
38
+ to the :program:`mongod` instance.
39
39
40
- - :dbcommand:`copydb` or :dbcommand:`clone` commands may be run
41
- on a :term:`secondary` member.
40
+ - You can run :dbcommand:`copydb` or :dbcommand:`clone` commands on a
41
+ :term:`secondary` member.
42
42
43
43
- There must be enough free disk storage on the destination server for the
44
- database from the source server.
44
+ database from the source server. You can check for the size of the
45
+ database on the source server by issuing command:
46
+
47
+ .. code-block::
48
+
49
+ db.runCommand()
45
50
46
51
Process
47
52
-------
48
53
49
54
Copy a Database to Another Server
50
55
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
56
52
- Use the :dbcommand:`copydb` command when you need to transfer a database from
53
- one server to another, such as from a development server a production
54
- server, changing the database name in the process .
57
+ Use the :dbcommand:`copydb` command to transfer a database from
58
+ one MongoDB instance to another, such as from a development instance
59
+ to a production instance .
55
60
56
61
This is useful when you want to move a database from the development
57
62
environment to the production environment, or create a remote-branch of the database
58
63
to archive the current database on a different server.
59
64
60
65
To copy the database named ``test`` on server ``db0.example.net`` to
61
- another server ``db1.example.net`` renaming it ``records``.
66
+ another server ``db1.example.net`` renaming it ``records`` in the process .
62
67
63
68
- Verify name of database, ``test``, you want to copy on the source
64
69
server, ``db0.example.net``.
@@ -75,10 +80,10 @@ another server ``db1.example.net`` renaming it ``records``.
75
80
Rename a Database
76
81
~~~~~~~~~~~~~~~~~
77
82
78
- Use the :dbcommand:`copydb` command to rename a database within the same
79
- server. This is useful when the contents of the database has changed
80
- from the initial naming of the database and a new name would be more
81
- appropriate.
83
+ Use the :dbcommand:`copydb` command to copy a database within the same
84
+ server also renaming the database . This is useful when the contents of
85
+ the database has changed from the initial naming of the database and a
86
+ new name would be more appropriate.
82
87
83
88
To rename a database from ``test`` to ``records`` within the
84
89
same server, ``db1.example.net``.
@@ -95,11 +100,11 @@ same server, ``db1.example.net``.
95
100
Copy a Database with Authentication
96
101
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97
102
98
- If the source server requires ``username`` and ``password``
99
- authentication, you can include these parameters in the
100
- :dbcommand:`copydb` command. This is useful where you need to create
101
- offsite backups, a local development system which has data from the
102
- production system, or create a system to produce reports.
103
+ If the source server requires ``username`` and ``password``, you can
104
+ include these parameters in the :dbcommand:`copydb` command. This is
105
+ useful where you need to create offsite backups, a local development
106
+ system which has data from the production system, or create a system
107
+ to produce reports.
103
108
104
109
To copy ``test`` to ``records`` from the source server
105
110
``db0.example.net``, which requires ``username`` and ``password``
@@ -112,23 +117,16 @@ authentication, to ``db1.example.net`` use the following procedure.
112
117
113
118
.. code-block:: javascript
114
119
115
- db.copyDatabase( "test", "records", db0.example.net, username, password)
116
-
117
- .. TODO verify that username & password do not have to be in quotes...?
120
+ db.copyDatabase( "test", "records", db0.example.net, "username", "password")
118
121
119
122
Clone a Database
120
123
~~~~~~~~~~~~~~~~
121
124
122
125
The :dbcommand:`clone` command copies a database between :program:`mongod`
123
126
instances; however, :dbcommand:`clone` preserves the database
124
- name. For many operations :dbcommand:`clone` has a more
125
- straightforward syntax than :dbcommand:`copydb` but are functionally
126
- the same.
127
-
128
- You can use :dbcommand:`clone` to quickly create a :term:`secondary`
129
- member from the :term:`primary` dataset, to seed a development system
130
- with production data, or to create a system to run reports on instead
131
- of the main production system.
127
+ name on the destination server. For many operations,
128
+ :dbcommand:`clone` has identical functionality and a simpler syntax
129
+ than :dbcommand:`copydb`.
132
130
133
131
To clone a database from ``db0.example.net`` to ``db1.example.net``, on
134
132
``db1.example.net`` follow these procedures.
0 commit comments