Mastering Bash Scripting in DevOps – Essential Scripts for Production : Day 27 of 50 days DevOps Tools Series

RMAG news

Welcome to Day 27 of our “50 DevOps Tools in 50 Days” series. Today, we’re diving into the world of Bash scripting—a fundamental skill for any DevOps engineer. Bash scripting allows you to automate tasks, streamline operations, and create powerful workflows that enhance productivity and efficiency. In this blog, we’ll explore the basics of Bash scripting, discuss its importance in DevOps, and present five production-level Bash scripts with examples to showcase its capabilities.

Understanding Bash Scripting

Bash, short for Bourne Again Shell, is a command-line interpreter used to interact with the operating system. Bash scripts are essentially sequences of commands written in a file that the Bash shell executes. They are widely used in Unix-based systems to automate repetitive tasks, manage system configurations, and perform various administrative functions.

Importance of Bash Scripting in DevOps

Bash scripting plays a vital role in the DevOps workflow due to its ability to:

Automate Routine Tasks: Repetitive tasks such as backups, system updates, and deployments can be automated, freeing up valuable time for more strategic work.
Enhance Efficiency: Scripts enable you to execute multiple commands in sequence, reducing the need for manual intervention and minimizing human error.
Integrate Systems: Bash scripts can be used to integrate different tools and systems, creating cohesive workflows that facilitate continuous integration and delivery (CI/CD).
Simplify Configuration Management: Scripts can be used to configure servers, set environment variables, and manage software installations, ensuring consistency across environments.

Production-Level Bash Scripts with Examples

Let’s explore five production-level Bash scripts that demonstrate the versatility and power of Bash scripting in a DevOps context.

1. Automated Backup Script

This script automates the backup of a specified directory to a remote server using rsync. It ensures that data is securely transferred and stored.

#!/bin/bash

# Variables
SOURCE_DIR=”/path/to/source/directory”
BACKUP_DIR=”/path/to/backup/directory”
REMOTE_USER=”username”
REMOTE_HOST=”remote.server.com”
REMOTE_PATH=”/path/to/remote/backup”

# Create a timestamp
TIMESTAMP=$(date +”%Y%m%d%H%M%S”)

# Rsync backup with compression and progress
rsync -avz –progress “$SOURCE_DIR” “${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/backup_${TIMESTAMP}”

# Check if rsync was successful
if [ $? -eq 0 ]; then
echo “Backup completed successfully at ${TIMESTAMP}.”
else
echo “Backup failed at ${TIMESTAMP}.”
fi

Real-World Use Cases:

Backing up databases or configuration files.
Creating regular snapshots of project directories.
Preserving user data for disaster recovery.

2. Log Rotation Script

This script rotates and compresses log files, ensuring that logs do not consume excessive disk space.

#!/bin/bash

# Variables
LOG_DIR=”/var/log/myapp”
ARCHIVE_DIR=”/var/log/myapp/archive”
RETENTION_DAYS=30

# Find and compress log files older than retention period
find “$LOG_DIR” -type f -mtime +$RETENTION_DAYS -exec gzip {} ;

# Move compressed files to archive directory
find “$LOG_DIR” -type f -name “*.gz” -exec mv {} “$ARCHIVE_DIR” ;

echo “Log rotation completed.”

Real-World Use Cases:

Managing application logs that grow rapidly.
Keeping historical logs for auditing purposes while ensuring current logs are easily accessible.
Automating log management for various services.

3. Server Health Check Script

This script checks the health of a server by monitoring CPU and memory usage, disk space, and running processes.

#!/bin/bash

# Function to check CPU usage
check_cpu() {
CPU_USAGE=$(top -bn1 | grep “Cpu(s)” | awk ‘{print $2 + $4}’)
echo “CPU Usage: ${CPU_USAGE}%”
}

# Function to check memory usage
check_memory() {
MEMORY_USAGE=$(free -m | awk ‘/Mem:/ { printf(“%.2f”), $3/$2 * 100.0 }’)
echo “Memory Usage: ${MEMORY_USAGE}%”
}

# Function to check disk space
check_disk() {
DISK_USAGE=$(df -h / | awk ‘NR==2 { print $5 }’)
echo “Disk Usage: ${DISK_USAGE}”
}

# Function to check running processes
check_processes() {
PROCESS_COUNT=$(ps aux | wc -l)
echo “Running Processes: ${PROCESS_COUNT}”
}

# Perform health checks
check_cpu
check_memory
check_disk
check_processes

echo “Server health check completed.”

Real-World Use Cases:

Monitoring production servers for resource usage.
Detecting anomalies in server performance.
Automating server health checks to reduce manual monitoring efforts.

4. User Management Script

This script manages user accounts on a Linux system by creating, deleting, and listing users.

#!/bin/bash

# Function to create a user
create_user() {
read -p “Enter username to create: ” USERNAME
if id “$USERNAME” &>/dev/null; then
echo “User $USERNAME already exists.”
else
sudo useradd “$USERNAME”
if [ $? -eq 0 ]; then
echo “User $USERNAME created successfully.”
else
echo “Failed to create user $USERNAME.”
fi
fi
}

# Function to delete a user
delete_user() {
read -p “Enter username to delete: ” USERNAME
if id “$USERNAME” &>/dev/null; then
sudo userdel -r “$USERNAME”
if [ $? -eq 0 ]; then
echo “User $USERNAME deleted successfully.”
else
echo “Failed to delete user $USERNAME.”
fi
else
echo “User $USERNAME does not exist.”
fi
}

# Function to list users
list_users() {
echo “List of users:”
cut -d: -f1 /etc/passwd | less
}

# Main menu
while true; do
echo “User Management Script”
echo “1. Create User”
echo “2. Delete User”
echo “3. List Users”
echo “4. Exit”
read -p “Choose an option [1-4]: ” OPTION

case $OPTION in
1)
create_user
;;
2)
delete_user
;;
3)
list_users
;;
4)
echo “Exiting the script.”
break
;;
*)
echo “Invalid option. Please choose between 1 and 4.”
;;
esac
done

Real-World Use Cases:

Automating onboarding and offboarding processes.
Managing users in a consistent manner across multiple systems.
Enhancing security by regularly reviewing user accounts.

5. Continuous Deployment Script

This script automates the deployment of a web application by pulling the latest code from a Git repository and restarting the web server.

#!/bin/bash

# Variables
REPO_DIR=”/var/www/myapp”
GIT_REPO=”https://github.com/username/myapp.git”
WEB_SERVER=”apache2″

# Pull latest code from Git repository
cd “$REPO_DIR”
git pull “$GIT_REPO” main

# Restart web server
systemctl restart “$WEB_SERVER”

echo “Deployment completed successfully.”

Real-World Use Cases:

Automatically deploying web applications when new code is pushed to a repository.
Streamlining the release process to reduce manual steps.
Ensuring that deployments are consistent and repeatable across environments.

Conclusion

Bash scripting is a powerful tool for DevOps engineers, enabling them to automate tasks, streamline workflows, and manage complex systems efficiently. By mastering Bash scripting, you can enhance your productivity, improve system reliability, and contribute to the success of your DevOps initiatives.

These production-level Bash scripts provide a glimpse into the capabilities of Bash scripting in real-world scenarios. As you continue your journey in DevOps, consider incorporating Bash scripting into your toolkit to unlock its full potential.

Stay tuned for more exciting tools and concepts in the upcoming posts of our series!

👉 Make sure to follow me on LinkedIn for the latest updates: Shiivam Agnihotri

Please follow and like us:
Pin Share