MongoDB 3.2 Bash Install Script

MongoDB 3.2 Bash Install Script

Get Social!

The below script installs MongoDB 3.2.x on Debian. Copy and paste the script into a file called install_mongo.sh and execute it as root.

#!/bin/bash
set -e

echo "Installing repo"
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main" > /etc/apt/sources.list.d/mongodb-org-3.2.list


echo "Installing binaries"
apt-get update
apt-get install -y mongodb-org
service mongod stop


echo "Setting up default settings"
rm -rf /var/lib/mongodb/*
cat > /etc/mongod.conf <<'EOF'
storage:
  dbPath: /var/lib/mongodb
  directoryPerDB: true
  journal:
    enabled: true
  engine: "wiredTiger"

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27017
  bindIp: 0.0.0.0
  maxIncomingConnections: 100

replication:
  oplogSizeMB: 128
  replSetName: "rs1"

security:
  authorization: enabled

EOF

service mongod start
sleep 5

mongo admin <<'EOF'
use admin
rs.initiate()
exit
EOF

sleep 5

echo "Adding admin user"
mongo admin <<'EOF'
use admin
rs.initiate()
var user = {
  "user" : "admin",
  "pwd" : "admin",
  roles : [
      {
          "role" : "userAdminAnyDatabase",
          "db" : "admin"
      }
  ]
}
db.createUser(user);
exit
EOF

echo "Complete"

Then connect to the local MongoDB instance

mongo admin -u admin -p admin

MongoDB shell version: 3.2.7
connecting to: admin
rs1:PRIMARY>

 


Script To Create A Swapfile On Linux

Category : How-to

Get Social!

This 5 line script will quickly create a 512MB SWAP file that will be automatically mounted on each machine reboot. It assumes you don’t already have a swap file enabled.

The script creates a 512MB file called .swapfile on your root partition, makes it SWAP format and enables it as available system SWAP. An fstab entry is also added so that it’s mounted after a machine reboot.

You can find more detailed instructions and explanations on this blog post. With some file systems, fallocate may not work – again, take a look at  this for a work around.

fallocate -l 512M /.swapfile
chmod 600 /.swapfile
mkswap /.swapfile
swapon /.swapfile
echo "/.swapfile none swap defaults 0 0" >> /etc/fstab

How To Find Your Public IP Address From The Terminal

Tags :

Category : How-to

Get Social!

ip-address-deconstructionFinding the IP address of your local machine couldn’t be easier – ipconfig on Windows or ifconfig on Linux will bring up all of your local network interfaces and list the IP addresses associated with them. Finding out your public IP address, which is the IP addresses associated with your internet connection can be a little more tricky.

Whatsmyip will show your your public IP address on their webpage when you visit them. Perhaps more interestingly, when visited with Linux curl or wget only the IP public address is displayed without any HTML or other content around it.

You can therefore use curl to read your public IP address into a variable, and then use it in as you need to.

IP=$(curl -s http://www.whatsmyip.website)
echo $IP

My Proxmox VE Bash Scripts on GitHub

Category : Knowledge

Get Social!

octocat-githubI’ve created a public repository on GitHub with few Bash scripts which may (or may not) be helpful for anyone administering a Proxmox VE Server.

I’ll add to this repository over time with any new scripts I write. Let me know if they are useful, or if you have any of your own in the comments.

Currently the scripts are:

  • backup-all – will backup all OpenVZ Containers on the Proxmox cluster.
  • ct-disk-space – a print out of the diskspace used by all OpenVZ Containers on the server.
  • migrate – will migrate a Container to a different storage ID.
  • restore-all -will restore all OpenVZ Containers on a cluster to the latest available backup.

Link to GitHub proxmox-scripts repository: https://github.com/JAC2703/proxmox-scripts


Bash getops Example

Tags :

Category : How-to

Get Social!

getopts is a way of adding intuitive options to custom bash scripts in Linux. getopts is a command that’s built into all recent versions of Bash, and is a more recent version of getopt (notice there is no ‘s’).

You’re not able to do anything too fancy with getopts, such as GNU long options like –filter, but you are able to easily pass in semi-meaningful arguments with relative ease.

Let’s take a look at the below example to understand what we’re getting into. 

Calling getopts Scripts

So that we know what we’re getting into, lets start with calling a script that accepts 3 arguments.

  • A port
  • A Hostname
  • A force parameter

The script we’re creating is just an example usage for getopts so each of the above parameters won’t really do anything, so I’ll leave your imagination to come up with a reason for wanting to collect such information. That said, we’re going to call our example script send_curl_command.sh and it will expect the above mentioned parameters to be passed in to work. 

send_curl_command.sh -p 443 -h api.jamescoyle.net -f

getopts Example

#!/bin/bash

# Echo usage if something isn't right.
usage() { 
    echo "Usage: $0 [-p <80|443>] [-h <string>] [-f]" 1>&2; exit 1; 
}

while getopts ":p:h:f" o; do
    case "${o}" in
        p)
            PORT=${OPTARG}
            [[ $PORT != "80" && $PORT != "443" ]] && usage
            ;;
        h)
            HOST=${OPTARG}
            ;;
        f)  
            FORCE=1
            ;;
        :)  
            echo "ERROR: Option -$OPTARG requires an argument"
            usage
            ;;
        \?)
            echo "ERROR: Invalid option -$OPTARG"
            usage
            ;;
    esac
done
shift $((OPTIND-1))

# Check required switches exist
if [ -z "${PORT}" ] || [ -z "${HOST}" ]; then
    usage
fi

echo "p = ${PORT}"
echo "h = ${HOST}"

The above snippet shows a relatively simple example of how to use getopts and can be broken into 3 main parts.

usage is a bash function that echos the script name, and some literal text on which parameters are accepted and then exits the script. The idea here is to call this function if anything is wrong with the arguments passed, so that a helpful hint can be given to the user and the script can exit.

The if block at the bottom of the script runs a few checks on the switches to ensure that they exist. The -z attribute is a simple bash test to ensure that the variable is set – it doesn’t do anything more clever than that. In addition, in the middle block you’ll find another check  [[ $PORT != “80” && $PORT != “443” ]] which checks that the p variable is set and is equal to either 80 or 443. You can check in either location, or both, just remember to call usage if something isn’t right so that the script can exit.

That means only the central block of code remains – and this is the meat of the getopts command. Much of this block can be considered boilerplate code, however there are 2 parts you’ll need to edit: the first line of the code between the quotation marks, which are the parameters that instruct getopts how to work, and the case statement. 

getopts Arguments and case Statement

The quoted arguments following the getopts command can look fairly cryptic at first, but once it’s broken down into its individual parts it’s really easy to build up the functionality you need. Keeping with the above example, let’s take a look at the getopts arguments “:p:h:f” o.

  • “:” (colon) is there to denote if getopts should throw an error if parameters are missing, which are specified as required (more on that in the last bullet point below). If you omit the : then getopts would throw generic errors – something that we’ll handle manually for this example to provide a more meaningful output in the context of our script.
  • “p:” is the first parameter that we tell getopts to expect. p specifies the character that will be passed on the command line when calling the script – for example myscript.sh -p. The colon specifies that an attribute must follow the switch, for example myscript.sh -p 443. During processing, the value 443 will be parsed with the -p switch.
  • “h:” is exactly the same as the above -p switch, only the character here is p.
  • “f” is, as above, specifying that the -p switch is expected however here there is no colon :. That’s because the -f switch will not have an attribute payload following it, for example 
    myscript.sh -p 443 -h myhost.com -f
  • “o” outside of the parenthesis is a lonely o. This value will be filled with either:
    • The switch being received, if it’s a valid switch such as ph or f.
    • a ? if the switch being passed is not an accepted switch (not specified in the getopts configuration). This is only available if the getopts argument begins with : as per the first bullet point above.
    • : if the switch is acceptable but doesn’t contain an argument when it should (configured with a : such as “p:”). This is only available if the getopts argument begins with : as per the first bullet point above.

That’s all the configuration needed for the getopts command to function however, to make useful we need to understand the arguments that are passed in, check them to ensure they’re sensible and make them available for the script to use.

case Statement

The case statement is called multiple times for each argument of getopts. In the above example, the first case check is for p which would be called when the -p argument is passed with variables. The code will then check that p is either 80 or 443 and set the variable PORT to equal the value passed in. case items h and f should be easy enough to understand and are very similar to the above p

Where things get more interesting are with the bottom two case items:

  • : (colon) here is used to denote that a switch has been passed into the script that’s expected, however an argument was expected but was not found. In this code block we simply call the usage function to remind the user what the script expects and to exit.
  • \? is simply a question mark but escaped for bash.  This represents that the switch currently being processed is not expected. Again, we just call our usage function.
  • * (asterisk) is not actually used in our sample script but it’s essentially a default which would catch anything that hasn’t been caught by a previous case element. You could use it to combine the above * and ?.

Bash Script to Install a mariadb-galera-server Cluster on Multiple Servers

Get Social!

The following script will install the MariaDB Galera Server binaries on multiple servers with a basic cluster configuration.

Let me start by saying it’s a quick and dirty script – it may not work in all scenarios and was written on a set of Debian servers. Use it at your own risk! It may work on a MySQL Server cluster but you’ll have to remove or change the repository locations to the MySQL Server ones.

Before running the script you’ll need to have SSH key Authentication set up and working between all the nodes. See my post on Create ssh key authentication between nodes for setting up server key authentication.

You’ll also need to change a few variables in the script to match your environment.

The first variable IP is an array of all the IP addresses you’d like to use to install Maria DB Server. Currently there are 3 IPs specified – replace them with your own IP addresses and add any more if you have more than 3 servers.

The other two variables CLUSTER_NAME which is the name MariaDB will use for your cluster; and MAINT_USER_PASSWORD which is the password that will be set for the maintenance user.

#!/bin/bash

IP[0]=10.27.3.51
IP[1]=10.27.3.52
IP[2]=10.27.3.53
## IP[99]=10.10.10.10

CLUSTER_NAME=cluster_test
MAINT_USER_PASSWORD=OkVLGvp5qhM0eCNk

## DO NOT EDIT ##

CLUSTER_STRING="gcomm://"$(IFS=, ; echo "${IP[*]}")

for I in "${IP[@]}"
do :
	echo "Setting up repo for $I"
	ssh root@$I "apt-get update"
	ssh root@$I "apt-get upgrade -y"
	ssh root@$I "apt-get install -y software-properties-common"
	ssh root@$I "apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db"
	ssh root@$I "add-apt-repository 'deb http://lon1.mirrors.digitalocean.com/mariadb/repo/10.0/ubuntu trusty main'"
	
	echo "Installing DB for $I"
	ssh root@$I "apt-get update"
	ssh root@$I "apt-get install -y mariadb-galera-server rsync"
	
	ssh root@$I "echo '
[client]
host     = localhost
user     = debian-sys-maint
password = '$MAINT_USER_PASSWORD'
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = '$MAINT_USER_PASSWORD'
socket   = /var/run/mysqld/mysqld.sock
basedir  = /usr
' > /etc/mysql/debian.cnf"
	ssh root@$I "echo '
[mysqld]
query_cache_size=0
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_type=0
bind-address=0.0.0.0

# Galera Provider Configuration
wsrep_provider=/usr/lib/galera/libgalera_smm.so
#wsrep_provider_options="gcache.size=32G"

# Galera Cluster Configuration
wsrep_cluster_name=\"'$CLUSTER_NAME'\"
wsrep_cluster_address=\"'$CLUSTER_STRING'\"

# Galera Synchronization Congifuration
wsrep_sst_method=rsync
#wsrep_sst_auth=user:pass

# Galera Node Configuration
wsrep_node_address=\"'$IP'\"
wsrep_node_name=\"'$IP'\"' > /etc/mysql/conf.d/cluster.cnf"

done

ssh root@$IP "service mysql stop"
ssh root@$IP "service mysql start --wsrep-new-cluster"

 


Visit our advertisers

Quick Poll

Do you use ZFS on Linux?

Visit our advertisers