<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Backup_and_Restore_in_MongoDB</id>
	<title>Backup and Restore in MongoDB - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Backup_and_Restore_in_MongoDB"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Backup_and_Restore_in_MongoDB&amp;action=history"/>
	<updated>2026-06-02T21:25:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=Backup_and_Restore_in_MongoDB&amp;diff=71388&amp;oldid=prev</id>
		<title>Kbaran: /* Backup of Replica Set */</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Backup_and_Restore_in_MongoDB&amp;diff=71388&amp;oldid=prev"/>
		<updated>2019-04-28T11:51:09Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Backup of Replica Set&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:MongoDB]]&lt;br /&gt;
&lt;br /&gt;
== Backup and Restore ==&lt;br /&gt;
* backups generally should be done on secondaries&lt;br /&gt;
* or on standalone servers at an off time&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Method #1: Filesystem Snapshot ===&lt;br /&gt;
* the simplest way to make backup&lt;br /&gt;
* filesystem must support snapshotting&lt;br /&gt;
* mongod must run with journaling enabled&lt;br /&gt;
* stop mongod before restoring&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create the snapshot volume&lt;br /&gt;
lvcreate -L128M -s -n dbbackup /dev/ops/databases&lt;br /&gt;
&lt;br /&gt;
# Mount the snapshot volume&lt;br /&gt;
mkdir /mnt/ops/dbbackup&lt;br /&gt;
mount /dev/ops/dbbackup /mnt/ops/dbbackup&lt;br /&gt;
&lt;br /&gt;
# Do the backup&lt;br /&gt;
tar -cf /dev/rmt0 /mnt/ops/dbbackup&lt;br /&gt;
&lt;br /&gt;
# Remove the snapshot&lt;br /&gt;
umount /mnt/ops/dbbackup&lt;br /&gt;
lvremove /dev/ops/dbbackup &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Method #2: Copying Data Files  ===&lt;br /&gt;
* use fsyncLock() before copying files&lt;br /&gt;
** fsycnLock() prevents all databases against any further write&lt;br /&gt;
** saves all dirty data to disk&lt;br /&gt;
** queues all write operations&lt;br /&gt;
* now files are consistent and copying of all files is possible&lt;br /&gt;
* fsyncUnlock() releases the lock and brings back database to normal operations&lt;br /&gt;
* if using authentication do not log out from the console between fsyncLock() and fsyncUnlock()&lt;br /&gt;
* stop mongod before restoring&lt;br /&gt;
** do not restore single database if crash or hard shutdown occurs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt; db.fsyncLock()&lt;br /&gt;
&amp;gt; db.fsyncUnlock()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Method #3: Using mongodump and mongorestore ===&lt;br /&gt;
* mongorestore is slower than previous methods and has other downsides&lt;br /&gt;
* good way to backup individual databases or collections&lt;br /&gt;
* it will create a dump directory (with subdirectories for each database) in current directory&lt;br /&gt;
* data is stored in .bson files&lt;br /&gt;
* mongodump can be used when mongod is not running&lt;br /&gt;
** do not use --dbpath when mongod is running&lt;br /&gt;
* when mongodump is running writes are allowed so already backuped data may change before mongodump will finish&lt;br /&gt;
** do not use fsyncLock() when using mongodump&lt;br /&gt;
** use --oplog if you are running mongod with --replSet&lt;br /&gt;
* mongodump will choose secondary when connected to replica set&lt;br /&gt;
* use mongodump and mongorestore in the same version&lt;br /&gt;
* try to avoid this type of backup if you have unique indexes other than _id&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mongodump --help&lt;br /&gt;
mongodump --port 27017&lt;br /&gt;
mongodump --dbpath /var/lib/mongodb&lt;br /&gt;
mongorestore --port 27017 --drop&lt;br /&gt;
mongodump --port 27017 --oplog&lt;br /&gt;
mongorestore --port 27017 --oplogReplay dump/&lt;br /&gt;
mongorestore -d dstDB -c dstCollection dump/srcDB/scrColl.bson&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Backup of Replica Set ===&lt;br /&gt;
* all previous methods are OK but 1st and 2nd are recommended (without any modifications)&lt;br /&gt;
* when using mongodump use --oplog&lt;br /&gt;
* when restoring with mongorestore:&lt;br /&gt;
*# start a server as a standalone and&lt;br /&gt;
*# restore the data with --oplogReplay and&lt;br /&gt;
*# restore the oplog collection&lt;br /&gt;
*# restart the server as a member of replica&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt; use local&lt;br /&gt;
&amp;gt; db.createCollection(&amp;quot;oplog.rs&amp;quot;, {&amp;quot;capped&amp;quot; : true, &amp;quot;size&amp;quot; : 1000000})&lt;br /&gt;
$ mongorestore -d local -c oplog.rs dump/oplog.bson&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Backup of Sharded Cluster ===&lt;br /&gt;
* usually it can not be done perfectly&lt;br /&gt;
* instead of backing up everything at once, backup servers separately&lt;br /&gt;
* turn off balancer before making backup&lt;br /&gt;
* run mongodump through mongos to backup entire cluster&lt;br /&gt;
* problems with restoring single shard&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kbaran</name></author>
	</entry>
</feed>