init-mongo-rs.sh 758 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env bash
  2. for rs in "$@"; do
  3. mongo --host $rs --eval 'db'
  4. while [ $? -ne 0 ]; do
  5. echo "Waiting for $rs to become available"
  6. sleep 3
  7. mongo --host $rs --eval 'db'
  8. done
  9. done
  10. mongo --host rs0/$1 --eval 'db.accounts.find( { "username" : "admin" } )' prod | grep ADMIN_GLOBAL > /dev/null
  11. if [ $? -eq 0 ]; then
  12. echo "Replset with database exists"
  13. exit 0
  14. fi
  15. i=0
  16. for rs in "$@"; do
  17. if [ "$rs" != "$1" ]; then
  18. MEMBERS="$MEMBERS ,"
  19. fi
  20. MEMBERS="$MEMBERS {_id: $i, host: \"$rs\" }"
  21. i=$((i+1))
  22. done
  23. mongo --host $1 --eval "rs.initiate({_id: \"rs0\", version: 1, members: [$MEMBERS]})"
  24. sleep 3
  25. mongo --host $1 --eval 'rs.status()'
  26. sleep 10
  27. sleep 30
  28. cd /restore
  29. mongorestore --host rs0/$1 .