Search code examples
mongodbmongodb-querymongodump

How can I backup a list of mongo collections to archive using mongodump


I am using mongodump ( version r3.6.18) and I want to backup a list of collections. I tried using the --exclude option but the problem I am facing is OSError: [Errno 7] Argument list too long which is an OS error if the argument size increases more than the ARGMAX buffer.

There is a way to backup a list of collections by looping over it, but I want to use the --archive and --gzip option on which it is not working. Can you please provide an alternative for the below problem statement -

  1. I want to backup a list of collections
  2. The backup archive must be a single file (gzip archive)

Solution

  • Here is the bash script that you can use to dump specific collections:

    #!/bin/bash
    colls=(coll1 coll2 coll3)
    for c in ${colls[@]}
    do
      mongodump --host {YOUR_HOST} -d {YOUR_DB} -u {USERNAME} -p {PASSWORD} -c $c --gzip --out OUTPUT
    done