Restoring the database dump of an older version of mongo to a new version of mongo

sp497 picture sp497 · Jun 15, 2016 · Viewed 8.6k times · Source

Currently, I have an older version of mongo, i.e 2.6 running on my system. I already have my site in production and have a lot of client data. I am planning an upgrade to mongo 3.2.

So, my question is whether mongorestore of mongo v3.2 work with data dump of v2.6? Or, is it known to create problems?

Any answers will be invaluable! Thanks

Answer

Bruno Bronosky picture Bruno Bronosky · Jan 25, 2017

I asked this same question on the official MongoDB mailing list. They said not to upgrade more than 1 major version at a time. (Major versions being: 2.2, 2.4, 2.6, 3.0, 3.2, 3.4)

I didn't want to follow the normal upgrade process of installing every version Just to launch mongod and then shut it down. That feels to me like it would leave cruft behind and I like to have my infrastructure building scripted and version controlled. So, I decided to launch new EC2 instances with the latest Ubuntu (since my Mongo v2.4 servers were also 2 LTS versions behind) and the latest MongoDB. I used docker images of intermediate versions of MongoDB to do the data upgrades.

https://gist.github.com/RichardBronosky/2d04c7c2e9a5bea67cd9760a35415a3f#file-uat_mongodb_upgrade_from_prod-sh

The bulk of the solution is this:

# mongo.conf is using the default dbPath: /var/lib/mongodb
# this path is for temporary use by the mongo docker container
mkdir -p /data/db/dump
# see: https://hub.docker.com/_/mongo/ (search for /data/db)
# see: https://github.com/docker-library/mongo/blob/30d09dbd6343d3cbd1bbea2d6afde49f5d9a9295/3.4/Dockerfile#L59
cd /data/db
mongodump -h prodmongo.int

# Get major versions from https://hub.docker.com/r/library/mongo/tags/
step=0
for major_version in 2.6.12 3.0.14 3.2.11 3.4.1; do
    sudo docker stop some-mongo || true
    sudo docker rm   some-mongo || true
    sudo docker run --name some-mongo -v /data/db:/data/db -d mongo:$major_version
    false; while [[ $? > 0 ]]; do
        sleep 0.5
        sudo docker exec -it some-mongo mongo --eval 'printjson((new Mongo()).getDBNames())'
    done
    if (( $step == 0 )); then
        sudo docker exec -it some-mongo mongorestore /data/db/dump
    fi
    ((step += 1))
done

# Finish up with docker
sudo rm -rf /data/db/dump/*
sudo docker exec -it some-mongo bash -c 'cd /data/db; mongodump'
sudo docker stop some-mongo
sudo docker rm   some-mongo

# Load upgraded data into latest version of MongoDB (WiredTiger storage engine will be used)
mongorestore /data/db/dump
sudo rm -rf /data