Real-Life Applications of Bash Scripting Explained
Practical Examples of Bash Scripting in Action
Prerequisites:
Read this blog, which covers basic to advanced bash scripting, and for understanding, please go through the blog linked below:
Let's start with the real-life application of bash scripting:
Backing Up Files:
Taking a backup of your files is the most important thing you can do daily.
src_dir=/home/ubuntu/scriptfolder
trgt_dir=/home/ubuntu/backups
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
backup_file=$trgt_dir/$curr_timestamp.tgz
echo "taking backup on $curr_timestamp"
echo "$backup_file"
tar czf $backup_file --absolute-names $src_dir
echo "Backup complete"
Add Firewall and Rules:
Adding a firewall and rules is crucial for all servers, and scripting makes it an easy job.
#isntall the firewall
sudo yum install firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo firewall-cmd --state
#Setting Firewall Rules
sudo firewall-cmd --permanent --add-port=PORT/tcp
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="IP_ADDRESS" port protocol="tcp" port="PORT" accept'
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
These Two Things come in handy when you create a server for testing or production too
Let's dive deeper into using microservice tools such as Docker, Redis, RabbitMQ, and Apache Kafka.
Docker:
Docker is a platform that enables developers to develop, ship, and run applications in containers. Containers allow you to package an application with all its dependencies into a standardized unit for software development. Docker provides tools and a platform to manage these containers efficiently.
#!/bin/bash
# Install required packages
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Add Docker repository
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# Install Docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
# Start Docker service
sudo systemctl start docker
# Enable Docker service to start on boot
sudo systemctl enable docker
# Add current user to the Docker group to manage Docker without sudo
sudo usermod -aG docker $USER
# Display Docker version
docker --version
RabbitMQ:
RabbitMQ is an open-source message-broker software that initially supported the Advanced Message Queuing Protocol but now includes a plug-in architecture for other protocols like Streaming Text Oriented Messaging Protocol and MQ Telemetry Transport.
#!/bin/bash
#Update the dependency
sudo yum update
sudo yum install wget
sudo yum install package.rpm
#Accessing the repo
sudo rpm --import https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
sudo rpm -Uvh https://dl.bintray.com/rabbitmq/rabbitmq-server/9.0.x/rabbitmq-server-9.0.x-1.el7.noarch.rpm
#Installing RabbitMQ
sudo yum install rabbitmq-server
sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server
sudo rabbitmqctl status
#Enable the RabbitMQ Plugins
sudo rabbitmq-plugins enable rabbitmq_management
sudo rabbitmq-plugins enable rabbitmq_shovel
sudo rabbitmq-plugins enable rabbitmq_federation
sudo rabbitmq-plugins enable rabbitmq_federation
#Restart RabbitMQ Server
sudo systemctl restart rabbitmq-server
#Add User TO RabbitMQ Server
sudo rabbitmqctl add_user rabbitmq rabbitmq
sudo rabbitmqctl set_user_tags rabbitmq administrator
sudo rabbitmqctl set_permissions -p / rabbitmq ".*" ".*" ".*"
Redis:
Redis, previously open-source and now "source available," serves as a distributed, in-memory key-value database, cache, and message broker, offering optional durability.
#!/bin/bash
# Update system packages
sudo yum update -y
# Install Redis
sudo yum install -y redis
# Start and enable Redis service
sudo systemctl start redis
sudo systemctl enable redis
# Test Redis connection
redis-cli ping
# Install Redis Desktop Manager (RDM) for GUI
# Add EPEL repository if not installed
sudo yum install -y epel-release
# Install Redis Desktop Manager
sudo yum install -y redis-desktop-manager
# Allow Redis Desktop Manager through firewall
sudo firewall-cmd --zone=public --add-port=6379/tcp --permanent
sudo firewall-cmd --reload
Apache Kafka:
A distributed event store and stream-processing platform.
#!/bin/bash
# Define variables
KAFKA_VERSION="2.8.0"
SCALA_VERSION="2.13"
KAFKA_HOME="/opt/kafka"
KAFKA_URL="https://downloads.apache.org/kafka/${KAFKA_VERSION}/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz"
# Install required packages
sudo yum install -y java-1.8.0-openjdk
# Download and extract Kafka
sudo mkdir -p $KAFKA_HOME
wget $KAFKA_URL -O /tmp/kafka.tgz
sudo tar -xzf /tmp/kafka.tgz -C $KAFKA_HOME --strip-components=1
rm /tmp/kafka.tgz
# Configure permissions
sudo chown -R $USER:$USER $KAFKA_HOME
sudo chmod +x $KAFKA_HOME/bin/*.sh
# Setup environment variables
echo "export KAFKA_HOME=$KAFKA_HOME" >> ~/.bashrc
echo "export PATH=\$PATH:\$KAFKA_HOME/bin" >> ~/.bashrc
source ~/.bashrc
# Start Kafka server (you may need to adjust configurations as per your requirements)
$KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties
Conclusion:
I've spent a lot of time working with queue systems like Docker, Redis, RabbitMQ, and Apache Kafka. It's helpful to have a script file that can be executed anytime or to create one based on your daily server management needs. In future blogs, we'll dive into creating more real-life applications using bash scripting.
Thanks for reading this blog.