Search code examples
mongodbshmongodump

Mongodump multiple dbs in sh script on same server/auth credentials


I have created this sh script

#!/bin/sh
DIR=`date +%d%m%Y`
DEST=/home/user/backup/$DIR
mkdir $DEST
mongodump --host=same --port=same --username=same --password="same" --db=db1 --out=$DEST
mongodump --host=same --port=same --username=same --password="same" --db=db2 --out=$DEST

to take backup all the collections of two dbs on the same server, with same authentication credentials. How can I write a single mongodump command and include both db names so that only one directory is created, and inside there 2 folders with the db names and the collections respectively are created? I have tried to use mongodump with --uri (without db names) but get authentication error..


Solution

  • How can I write a single mongodump command and include both db names so that only one directory is created

    This is not how Unix utilities work. In Unix if you want to organize your data this is done by explicitly organizing the data rather than asking a program that performs a different function (in this case dumping a MongoDB database) to organize your data for you.

    In this case, create the directory structure you want in the shell script.