Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active February 11, 2021 18:27
Show Gist options
  • Save RichardBronosky/2d04c7c2e9a5bea67cd9760a35415a3f to your computer and use it in GitHub Desktop.
Save RichardBronosky/2d04c7c2e9a5bea67cd9760a35415a3f to your computer and use it in GitHub Desktop.
#!/bin/bash
url="https://gist.githubusercontent.com/RichardBronosky/2d04c7c2e9a5bea67cd9760a35415a3f/raw/install_mongo.sh"
script="/tmp/userdata.sh"
echo "Running userdata script as $(whoami) from $(pwd)"
# Log commands to stdout and vicariously /var/log/cloud-init-output.log
set -o xtrace
# Exit on error
set -o errexit
# Exit on use of unset variables
set -o nounset
# Exit and report on pipe failure
set -o pipefail
# Fetch setup script
curl -L "$url" > $script
# Make setup script executable
chmod +x $script
# Execute setup script
$script
#!/bin/bash
# Log commands to stdout and vicariously /var/log/cloud-init-output.log
set -o xtrace
# Exit on error
set -o errexit
# Exit on use of unset variables
set -o nounset
# Exit and report on pipe failure
set -o pipefail
# If not running as root, rerun via sudo
if [[ $EUID > 0 ]]; then
exec sudo "$0" "$@"
fi
# Let apt know not to error due to lack of Dialog
export DEBIAN_FRONTEND=noninteractive
# Add Mongo repo to sources
tee /etc/apt/sources.list.d/mongodb-org-3.4.list <<EOF
deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse
EOF
# Install packages
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
apt-get update
for package in docker.io mongodb-org linux-image-extra-$(uname -r) linux-image-extra-virtual; do
apt-get install -y $package
done
# Package specific actions
# add user:ubuntu to group:docker
usermod -aG docker ubuntu
# create mongodb data dir
mkdir -p /data/db/dump
chown ubuntu:ubuntu /data/db/dump
# Enable and start services
for service in mongod; do
systemctl status $service || true # will return code 3 the first time, but gives good log info
systemctl unmask $service
systemctl enable $service
systemctl start $service
systemctl status $service
done
# Reboot to make docker changes take effect
reboot
#!/bin/bash
# Usage:
# 1. Run this script with no arguements on every SECONDARY node in the replica set.
# 2. Run this script on the PRIMARY node with a single argument of `-a` followed by pingable names for each secondary (zero or more).
# cd /tmp; curl -LO https://gist.github.com/RichardBronosky/2d04c7c2e9a5bea67cd9760a35415a3f/raw/replica_set_init.sh
# Log commands to stdout
set -o xtrace
# Exit on error
set -o errexit
# Exit on use of unset variables
set -o nounset
# Exit and report on pipe failure
set -o pipefail
# from: https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
# Update MongoDB config to support the replica set
sudo cp /etc/mongod.conf /etc/mongod.rs-prod.conf
sudo mv /etc/mongod.conf /etc/mongod.org.conf
sudo ln -s /etc/mongod.rs-prod.conf /etc/mongod.conf
sudo patch /etc/mongod.rs-prod.conf <<EOF
--- /etc/mongod.conf 2017-01-22 20:37:38.628479600 +0000
+++ /etc/mongod.rs-prod.conf 2017-01-22 21:00:28.430689201 +0000
@@ -21,7 +21,7 @@
# network interfaces
net:
port: 27017
- bindIp: 127.0.0.1
+# bindIp: 127.0.0.1
#processManagement:
@@ -30,7 +30,8 @@
#operationProfiling:
-#replication:
+replication:
+ replSetName: rs-prod
#sharding:
EOF
sudo systemctl restart mongod
# "on one and only one member of the replica set" https://goo.gl/gS6Kw5
if [[ ${1-} == '-a' ]]; then
mongo -eval 'rs.initiate()'
shift
for node in $@; do
mongo -eval "rs.add('$node')"
done
fi
#!/bin/bash
# cd /tmp; curl -LO https://gist.githubusercontent.com/RichardBronosky/2d04c7c2e9a5bea67cd9760a35415a3f/raw/uat_mongodb_upgrade_from_prod.sh
# Log commands to stdout
set -o xtrace
# Exit on error
set -o errexit
# Exit on use of unset variables
set -o nounset
# Exit and report on pipe failure
set -o pipefail
cd /data/db
mongodump -h prodmongo12.ies
# Get major versions from https://hub.docker.com/r/library/mongo/tags/
step=0
for major_version in 2.6.12 3.0.14 3.2.11 3.4.1; do
sudo docker stop some-mongo ||:
sudo docker rm some-mongo ||:
sudo docker run --name some-mongo -v /data/db:/data/db -d mongo:$major_version
set +o errexit
false; while [[ $? > 0 ]]; do
sleep 0.5
sudo docker exec -it some-mongo mongo --eval 'printjson((new Mongo()).getDBNames())'
done
set -o errexit
if (( $step == 0 )); then
sudo docker exec -it some-mongo mongorestore /data/db/dump
fi
((step += 1))
done
# Finish up with docker
sudo rm -rf /data/db/dump/*
sudo docker exec -it some-mongo bash -c 'cd /data/db; mongodump'
sudo docker stop some-mongo
sudo docker rm some-mongo
# Load upgraded data into latest version of MongoDB (WiredTiger storage engine will be used)
mongorestore /data/db/dump
sudo rm -rf /data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment