Automating PostgreSQl Installation Using Shell Script


This post taught you how to install PostgreSQL Database on Linux using source code. That approach is good when number of servers is less. But what if it is to be installed on a large number of servers? πŸ˜‰ Β Such as in a clustered environment? Here’s how to achieve it using a shell script. πŸ™‚

Better enable password-less SSH from your master server/jumpbox to all destination servers to avoid typing passwords. Then copy the installer tarball from jumpbox to all servers using below command.

for hst in `cat /home/shashank/hosts.txt`; do scp $hst:/home/shashank; done

Now that installer has been copied to destination servers, its time to run the script from jumpbox to install it on all destination servers in one go πŸ™‚ Issue below command to execute automation script & install PostgreSQL on all our target servers.

for hst in `cat /home/shashank/hosts.txt`; do ssh -t $hst 'bash -s' < /home/shashank/postgre_installer.sh; done

Below is the installer script.

#PostgreSQL Automation Installer Script version 1
#Author - Shashank Srivastava
#set -x
echo "Checking availability of pre-required packages"
sleep 1s
sudo rpm -q readline-devel.x86_64
if [ $? != 0 ]
then
echo "Readline package not found. Installing it...."
sudo yum install readline-devel.x86_64 -y
fi
sudo rpm -q zlib-devel-1.2.3-29.el6.x86_64
if [ $? != 0 ]
then
echo "zlib package not found. Installing it...."
sudo yum install zlib-devel.x86_64 -y
fi
echo "Unpacking PostgreSQL tarball..."
sleep 2s
sudo tar -zxf /home/shashank/postgresql-9.3.5.tar.gz
echo ""
echo "Tarball unpacked. Creating destination directory for Postgre...."
sudo mkdir -p /opt/PostgreSQL/9.3
cd /home/shashank/postgresql-9.3.5/
echo ""
echo "Destination directory created. Building installer from source coude...."
sleep 1s
sudo ./configure --prefix=/opt/PostgreSQL/9.3
sudo make
sleep 2s
echo ""
echo "Installer built. Installing it now...."
sudo make install
sleep 1s
id -a postgres
if [ $? != 0 ]
then
echo "postgres user not found. Adding it...."
sudo useradd postgres
else
echo "postgres user already exists. Proceeding to next step...."
fi
echo ""
echo "Configuring PostgreSQL...."
sudo mkdir -p /opt/PostgreSQL/9.3/data
sudo chown postgres /opt/PostgreSQL/9.3/data
echo ""
echo "Defining data directory for Postgre & starting it...."
sudo su - postgres -c "/opt/PostgreSQL/9.3/bin/initdb /opt/PostgreSQL/9.3/data/; sleep 2s; /opt/PostgreSQL/9.3/bin/pg_ctl -D /opt/PostgreSQL/9.3/data/ -l logfile start"
echo ""
echo "Checking version...."
echo "-------------------------------------------"
/opt/PostgreSQL/9.3/bin/psql --version
/opt/PostgreSQL/9.3/bin/postgres --version
echo ""
echo "Script executed successfully!"
Advertisements

Automate Apache Tomcat Installation on Linux Server Using Shell Script


My work involves installing & managing a lot of Linux servers. At times I have to do same work on numerous servers. One such task is installing Tomcat. In these posts I showed you how to install Apache Tomcat & how to check its version on Linux servers.
In this post, I will show you how you can automate the installation process across any number of servers using a shell script πŸ™‚

My approach is to : –

  • Setup password-less SSH on all servers from one jumpbox machine. Jumpbox is a server from which you will execute this script.
  • Create a list of servers in a text file. All servers are typed line by line.
  • Copy Java & Tomcat installers to all servers in one go from this jumpbox.
  • And finally execute the script.

Below are the commands & script through which I accomplished this. All of these are self-explanatory πŸ˜‰

for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/jdk-8u25-linux-x64.tar.gz $hst:/home/shashank; done
for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/apache-tomcat-8.0.15.tar.gz $hst:/home/shashank; done

What it does above? For loop reads the contents of text file (server IPs) line by line & copies tarballs there.

for hst in `cat /home/shashank/hosts.txt`; do ssh $hst -t 'bash -s' < ~shashank/tomcat_installer.sh; done

The trick above is to use ‘bash -s’. It will accept the script as an argument & execute it. After execution, it will move to next server. Below is my installer script.

Do let me know if it was helpful or not πŸ™‚ I will come up with some more posts soon πŸ™‚

#Apache Tomcat Installer Script
#Author : Shashank Srivastava
#set -x
echo "Logged into `hostname`. Installing here."
#Checking if installer tarballs are present or not. If they are not found, script will print error message & quit.
if [ -f /home/shashank/jdk-8u25-linux-x64.tar.gz ] && [ -f /home/shashank/apache-tomcat-8.0.15.tar.gz ]
then
echo "Unpacking Java installer tarball.......";
sleep 2s
#waiting for 2 seconds to show you what is being done.
sudo tar -xzvf /home/shashank/jdk-8u25-linux-x64.tar.gz;
echo ""
echo "Java tarball unpacked.";
echo ""
echo "Unpacking Tomcat installer tarball.......";
sleep 2s;
sudo tar -xzvf /home/shashank/apache-tomcat-8.0.15.tar.gz;
echo ""
echo "Tomcat tarball unpacked.";
echo ""
echo "Installing Java & Tomcat to /opt/app directory....";
sudo cp -rp /home/shashank/jdk1.8.0_25 /opt/app;
sudo cp -rp /home/shashank/apache-tomcat-8.0.15 /opt/app;
echo ""
echo "Exporting necessary variables......";
export JAVA_HOME=/opt/app/jdk1.8.0_25;
export PATH=$PATH:/opt/app/jdk1.8.0_25/bin:$PATH;
export CATALINA_HOME=/opt/app/apache-tomcat-8.0.15;
echo "Variables exported.";
echo ""
echo $JAVA_HOME;
echo ""
echo $PATH;
echo ""
echo $CATALINA_HOME;
sleep 2s;
echo ""
echo "Checking Java & Tomcat versions.";
echo ""
echo "Java is installed at `which java` Directory";
echo ""
java -version;
echo ""
java -cp $CATALINA_HOME/lib/catalina.jar org.apache.catalina.util.ServerInfo;
echo ""
echo "Starting Tomcat server"
echo ""
cd $CATALINA_HOME/bin
./startup.sh
echo "'
echo "Removing unpacked tarballs from PWD.";
sudo rm -rf /home/shashank/jdk1.8.0_25;
sudo rm -rf /home/shashank/apache-tomcat-8.0.15;
else
echo "Installer tarballs not found in /home/shashank. Please make sure they exist there. Exiting installation process now."
exit
fi

How to Replace a Path Name String Containing a . (dot) & / (slash) with a / Inside Text-file?


If you have ever come across a text file that has some unnecessary characters added before file/directory paths & you need to get rid of that, how would you do it? E.g.

./opt//home/shashank/scripts

to

/home/shashank/scripts

Easy way, just edit the file and that’s it πŸ˜€

Good one πŸ™‚ But what if the file has hundreds of lines? πŸ˜‰

So, below is how to do it inside Vi editor πŸ™‚ Open the file & issue this –

%s/.\/opt\/\//\/

Mission accomplished πŸ˜€

Since there was ./ in path, we escaped that using \ Then there were two // so we escaped them using \/\/ And since we had to replace it with a /, we added another /\/

Syntax of replacing text in Vi is –

:%s/text_to_be_replaced/text_to_be_replaced_with

How to Run sqlplus as a cron Job & Fix “sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory” Error?


If you have tried running Oracle sqlplus (of course through a script) in a cron job, then you might have encountered this error: –

sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory

What happens is that, despite defining all the environment variables in /etc/profile or bash_profile, cron doesn’t recognise that & throws above error. Close inspection after debugging the script took me to a conclusion that sqlplus is not able to connect to database, because its not finding its shared library.

If you have encountered this issue, here is how to fix it? πŸ˜‰

Make sure you have exported required variables. Below are my variables in my .bash_profile. Change yours accordingly.

export ORACLE_HOME=/u01/oracle/product/11.2.0/client_1/
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=/u01/oracle/product/11.2.0/client_1/lib:usr/local/lib

Make sure you source your .bash_profile before executing the script in cron. Edit your script & add below line at the top.

source /home/shashank/.bash_profile

Now try executing the script in cron. It will be successful πŸ˜‰ cron will first source the bash_profile, initialise all the variables & then execute the script πŸ™‚

How To Check Apache Tomcat Version on RHEL/CentOS?


In my last post, I explained how to install Tomcat on Linux. At times, you will need to know what version of Tomcat is installed. So below is how to find it out πŸ™‚

Just go the directory where Apache Tomcat is installed & issue below command.

java -cp lib/catalina.jar org.apache.catalina.util.ServerInfo

It will return info like this : –

Server version: Apache Tomcat/8.0.23
Server built: May 19 2015 14:58:38 UTC
Server number: 8.0.23.0
OS Name: Linux
OS Version: 2.6.32-504.el6.x86_64
Architecture: amd64
JVM Version: 1.8.0_25-b17
JVM Vendor: Oracle Corporation

Installing Apache Tomcat on RHEL/CentOS


Apache Tomcat is an Open-Source web server meant to run Java code. Just like Apache serves websites written, e.g, PHP, Tomcat serves websites written in Java. Installing Tomcat on Linux is a whole new experience, compared to if you have done it on Windows πŸ˜‰ Lets begin this tutorial πŸ™‚

Install Java. Apache Tomcat requires Java to be installed first. So, first install it from its official website.

Download Tomcat. Download Apache Tomcat from its official website. I have installed version 8.0.23. You may choose any compatible version. P.S. This tarball contains all the binaries & software to run Tomcat.

Unpack the Tomcat tarball. Once the tarball has been downloaded, just unpack it to any location where you want it to be installed using command : –

tar -xfΒ apache-tomcat-8.0.23.tar.gzΒ 

Start Tomcat. Move to the Tomcat directory & run below script.

./bin/startup.sh

To stop Tomcat, just issue –

./bin/shutdown.sh

So, that’s it! Apache Tomcat has been installed & running fine. If you thought you will need to click next..next..continue..etc, then you’re wrong πŸ˜‰ You just need to place the archive(tarball) in the right location & it is installed!! Installation on Linux is far different πŸ˜‰

Hope you found this post useful πŸ™‚ Do let me know how was it.

How To Check PostgreSQL Version in RHEL


In the previous post, I wrote about how to install PostgreSQL. In this post, I will describe how we can check PostgreSQL version. Issue the below commands for client (psql) & server (postgres) versions.

[root@server shashank]# /opt/PostgreSQL/9.3/bin/psql --version
psql (PostgreSQL) 9.3.5
[root@server shashank]# /opt/PostgreSQL/9.3/bin/postgres --version
postgres (PostgreSQL) 9.3.5