Search code examples
mongodbmongorestore

Restoring mongodb database with different name


I've been doing periodic (every 24 hour) backups of my mongodb instance. This works great, and I can restore them on my staging server with no problem:

time mongorestore  --ssl --gzip --authenticationDatabase=admin \
    --host=fra-mongo-staging-1.example.com --port=27017        \
    --username=restore --password=secret --archive="$snapshot_name"

But the dbname in production is example_prod, whereas on the staging server I'd like to restore to example_staging. So I type this:

time mongorestore  --ssl --gzip --db "$dbname" --authenticationDatabase=admin \
    --host=fra-mongo-staging-1.example.com --port=27017                       \
    --username=restore --password=secret --archive="$snapshot_name"

The only difference is the --db "$dbname" (where $dbname is example_staging). This doesn't work: I see the lines about preparing, then it says done, but nothing is restored.

2019-02-07T11:16:36.743+0000    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-02-07T11:16:36.772+0000    archive prelude example_prod.surveys
2019-02-07T11:16:36.773+0000    archive prelude example_prod.settings
2019-02-07T11:16:36.773+0000    archive prelude example_prod.spines
2019-02-07T11:16:36.773+0000    archive prelude example_prod.reduced_authors
2019-02-07T11:16:36.773+0000    archive prelude example_prod.delayed_backend_mongoid_jobs
2019-02-07T11:16:36.773+0000    archive prelude example_prod.email_events
2019-02-07T11:16:36.773+0000    archive prelude example_prod.authors
2019-02-07T11:16:36.774+0000    archive prelude example_prod.crowberry
2019-02-07T11:16:36.774+0000    archive prelude example_prod.bitly
2019-02-07T11:16:36.774+0000    archive prelude example_prod.mytestcollection
2019-02-07T11:16:36.774+0000    archive prelude example_prod.reviews
2019-02-07T11:16:36.774+0000    archive prelude example_prod.books
2019-02-07T11:16:36.774+0000    archive prelude example_prod.candy_events
2019-02-07T11:16:36.774+0000    archive prelude example_prod.features
2019-02-07T11:16:36.774+0000    archive prelude example_prod.elderberry
2019-02-07T11:16:36.776+0000    preparing collections to restore from
2019-02-07T11:17:02.403+0000    done

I've also tried using --tsFrom=example_prod --tsTo=example_staging, no joy.

Any suggestions on the right way to do this?


Solution

  • I've also tried using --tsFrom=example_prod --tsTo=example_staging, no joy.

    I don't see tsFrom & tsTo in the mongorestore docs -- which version are you using? https://docs.mongodb.com/manual/reference/program/mongorestore/

    It looks like there's nsFrom and nsTo options that take a namespace, so doing --nsFrom='example_prod.*' and --nsTo='example_staging.*' should work.

    From the docs:

    For simple replacements, use asterisks (*) as wild cards. Escape all literal asterisks and backslashes with a backslash. Replacements correspond linearly to matches: each asterisk in --nsFrom must correspond to an asterisk in --nsTo, and the first asterisk in --nsFrom matches the first asterisk in nsTo.