mongodb/mongo/initdb.sh
Martin Donnelly 8f89fe12d7 init
2018-04-04 22:13:47 +01:00

52 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Stop the mongo service, and start a custom one with our config
service mongod stop
killall mongod
sleep 5
rm -rf /var/lib/mongodb/*
# Start up main mongo
echo "=================== STARTING MAIN MONGODs ================================="
mongod --port 27017 --dbpath /var/lib/mongodb/ --smallfiles --oplogSize 128 &
sleep 10
#
# Create the user using mongo shell
#
echo "=================== ADDING ADMIN USER ================================="
mongo --port 27017 --eval "db = db.getSiblingDB('admin'); db.createUser({user: 'admin', pwd: 'abc123', roles:[{role: 'root', db: 'admin'}]})"
mongo --port 27017 --eval "db = db.getSiblingDB('MDB'); db.createUser({user: 'mdbadmin', pwd: 'abc123', roles:[{role: 'dbOwner', db: 'MDB'}]})"
#
# Restore the data to the database
#
if [ -d /vagrant/devdump/dump/MDB/ ];
then
echo "=================== RESTORE STARTING ================================="
mongorestore --host localhost:27017 -u mdbadmin -p abc123 --authenticationDatabase MDB --db=MDB --drop --numInsertionWorkersPerCollection=2 /vagrant/devdump/dump/MDB/
sleep 5
echo "=================== RESTORE DONE ================================="
else
echo "=================== NO BACKUP DATA TO RESTORE ==================="
echo "Initialise database."
mongo /vagrant/mongo/initialise_fresh_db.js --host localhost:27017 -u mdbadmin -p abc123 --authenticationDatabase MDB
fi
#
# Kill and restart the databases
#
killall mongod
sleep 5
#
# Restart the main mongo, plus 2 others for the replicaset
#
echo "================== STARTING MONGODs AGAIN WITH SSL ==============================="
mongod --port 27017 --dbpath /var/lib/mongodb/ --smallfiles --oplogSize 128 --sslMode requireSSL --sslPEMKeyFile /vagrant/mongo/keys/mongodb.pem &
sleep 5
echo "=================== DATABASE INITIALISATION DONE ================================="