Can I move a mongo db by copying its data directory?

8,895 views
Skip to first unread message

Adam Fields

unread,
May 25, 2012, 1:57:06 PM5/25/12
to mongodb-user
Can I safely move a database from one machine to another by just moving the files for that database (directoryperdb = true) to another machine, or is there other registration info that needs to happen? Specifically - is there any way to do this without copying the data / files?

I'm trying to find a good way to archive data that's organized by month on EC2, and I'm considering putting each month on a separate EBS volume (on mountpoints directly under the main data directory), and then moving each successive volume to a different server each month.

Is this feasible? Can this be done while the server is running?

Dan Crosta

unread,
May 25, 2012, 2:30:41 PM5/25/12
to mongodb-user
You can copy or move a database's files to another machine, then start
a mongod instance using that data path, and everything will work --
with one caveat: if you are not using journaling, you must stop (or
fsync & lock) the source database before copying files, and leave it
locked until the copy is done; on the other hand, if you are using
journaling, you will just have to ensure that the journal files are
copied last.

With EBS, you can detach and reattach the volumes to separate servers
as you suggest -- in this case, you will definitely need to fully stop
the mongod process before unmounting and detaching the EBS volume.

- Dan

Adam Fields

unread,
May 25, 2012, 3:05:14 PM5/25/12
to mongod...@googlegroups.com

On May 25, 2012, at 2:30 PM, Dan Crosta wrote:

> You can copy or move a database's files to another machine, then start
> a mongod instance using that data path, and everything will work --
> with one caveat: if you are not using journaling, you must stop (or
> fsync & lock) the source database before copying files, and leave it
> locked until the copy is done; on the other hand, if you are using
> journaling, you will just have to ensure that the journal files are
> copied last.
>
> With EBS, you can detach and reattach the volumes to separate servers
> as you suggest -- in this case, you will definitely need to fully stop
> the mongod process before unmounting and detaching the EBS volume.
>

I don't want to move _all_ of the databases on the server. Just one at a time. Does each db maintain its own journal?

> - Dan
>
> On May 25, 1:57 pm, Adam Fields <fie...@street86.com> wrote:
>> Can I safely move a database from one machine to another by just moving the files for that database (directoryperdb = true) to another machine, or is there other registration info that needs to happen? Specifically - is there any way to do this without copying the data / files?
>>
>> I'm trying to find a good way to archive data that's organized by month on EC2, and I'm considering putting each month on a separate EBS volume (on mountpoints directly under the main data directory), and then moving each successive volume to a different server each month.
>>
>> Is this feasible? Can this be done while the server is running?
>
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb

Scott Hernandez

unread,
May 25, 2012, 3:18:35 PM5/25/12
to mongod...@googlegroups.com
No, the journal is for the whole system/process.

Once you have fsync/locked you can copy any databases files by
database. The journal is committed, and the database files consistent,
before the lock on fsync/lock.

Adam Fields

unread,
May 25, 2012, 4:35:30 PM5/25/12
to mongod...@googlegroups.com

On May 25, 2012, at 3:18 PM, Scott Hernandez wrote:

> No, the journal is for the whole system/process.
>
> Once you have fsync/locked you can copy any databases files by
> database. The journal is committed, and the database files consistent,
> before the lock on fsync/lock.
>

Ok.

The target db needs to be shut down while the transfer is happening and started after the files are already in place? Or will it pick up the new db the first time I try to reference it, without a restart?

Scott Hernandez

unread,
May 25, 2012, 5:03:15 PM5/25/12
to mongod...@googlegroups.com
It is best to put the files in place while it is shutdown, yes.
Reply all
Reply to author
Forward
0 new messages