How to backup and restore MongoDB database Databases 15.02.2016

We have to use mongodump and mongorestore that isn't installed by default.

# ubuntu
# install mongo with mongodump
sudo apt-key adv --keyserver hkp:// --recv EA312927

echo "deb trusty/mongodb-org/3.2 multiverse" | sudo 
tee /etc/apt/sources.list.d/mongodb-org-3.2.list

sudo apt-get update
sudo apt-get install -y mongodb-org

# archlinux
sudo pacman -S mongodb # mongo
sudo pacman -S mongodb-tools # mongodump

Let’s start with the backup process. In version 3.2 was introduced an additional mode of creating backups - the archive mode, which writes all the dump data, even from disparate databases and collections, to a single output file.

mongodump -h localhost:27017 -d testdb -u user -p password 
--archive=testdb_`date "+%Y_%m_%d"`.archive 

You can also backup a single collection

mongodump -h localhost:27017 -d testdb --collection users -u user -p password 
--archive=testdb_users_`date "+%Y_%m_%d"`.archive 

Backup entire server

mongodump -h localhost:27017 -u user -p password --archive=server_`date "+%Y_%m_%d"`.archive 

Next is restore process. Drop all existing collections and restore database

mongorestore -h localhost:27017 -d testdb --drop --archive=testdb_2016_02_15.archive