title: running parallel mongodump in bash script
body: <p><sup>Editor's note: This is a follow-up question to a more general one about <a href="https://stackoverflow.com/q/42651475/45375">running a specified number of commands in parallel</a>.</sup></p>
  
  <p>I'm trying to run this mongodb backup script for 20 mongodb servers.   </p>
  
  <pre><code>#!/bin/bash
  #daily backup for mongo db's
  
  BACKUP_HOSTS=(example.com staging.example.com prod.example.com example1.com)
  #d=$(date +%Y-%m-%dT%H:%M:%S --date "-65 days")
  d=$(date +%Y-%m-%dT%H:%M:%S --date "-5 days")
  oid=$(mongo --quiet --eval "ObjectId.fromDate(ISODate('$d'))")
  
  cd /data/daily/
  rm -r /data/daily/*
  
  TODAY=$(date +"%y-%m-%d")
  mkdir "$TODAY"
  
  cd $TODAY
  
  #create subfolders
  
  for HOST in ${BACKUP_HOSTS[@]}
  do
          mkdir $HOST
  done
  
  #extract mongo dumps
  
  echo "$(date '+%Y-%m-%d %H:%M:%S') start retrieving Mongodb backups"
  
  for h in ${BACKUP_HOSTS[@]};do
      dbs=`mongo --eval "db.getMongo().getDBNames()" --host $h | grep '"' | tr -d '",' `
      for db in $dbs; do
         col=`mongo  $db --host $h --quiet --eval "db.getCollectionNames()" | tr -d ',"[]' `
         for collection in $col; do
              xargs -P 0 -n 1 mongodump --host $h -q "{_id:{\$gt:$oid}}" -d $db -c $collection --out /data/daily/$TODAY/$h
         done
      done
  done
  </code></pre>
  
  <p>But is not working.</p>
  
  <p>Tried also with:</p>
  
  <pre><code>parallel -P 0 -n 1 mongodump --host $h "{_id:{\$gt:$oid}}" -d $db -c $collection --out /data/daily/$TODAY/$h
  </code></pre>
  
  <p>but I get:</p>
  
  <pre><code>bin/bash: -c: line 0: syntax error near unexpected token `('
  </code></pre>