Skip to main content

Command Palette

Search for a command to run...

Day 12: Linux - Shell Scripting use cases in DevOps : Part 3

Updated
12 min readView as Markdown
A

Experienced Senior DevOps Engineer with a passion for optimizing software development and delivery processes. Excels in designing and implementing CI/CD pipelines, automating infrastructure, and optimizing cloud architectures. Proficient in a wide range of DevOps tools such as Docker, Kubernetes, Jenkins, Ansible, Git, and AWS services. Strong collaborator, adept at fostering cross-functional teamwork and continuous improvement. Thrives in dynamic environments, utilizing problem-solving skills to overcome complex challenges. Dedicated to delivering high-quality software products on time and within budget.

Welcome back to the third installment of our blog series on "Linux - Shell Scripting Use Cases in DevOps." In the previous blogs (Part 1 and Part 2), we explored the fundamentals of shell scripting and its importance in the DevOps world. We also discussed various use cases like automating server configurations, managing version control, and continuous integration. In this blog, we will continue our journey and explore more practical examples where shell scripting plays a crucial role in the DevOps workflow.

XI. Database Operations

Create a shell script that automates database operations such as backups, restores, and schema migrations. For this example, we'll assume you're working with a MySQL database.

https://github.com/techwithankush/shell-scripting/blob/91e4692ffc8f81c7ae849e76ee629bb29bfafbfb/11.Database_Operations.sh

Save the script in a file called database_operations.sh. To use this script, make sure it is executable:

chmod +x database_operations.sh

Now, you can perform different database operations using the script:

  1. Backup the database:
./database_operations.sh backup

This will create a timestamped SQL backup file in the specified backup directory.

  1. Restore the database:
./database_operations.sh restore /path/to/backup_file.sql

This will restore the database from the specified backup file.

  1. Run schema migrations:
./database_operations.sh migrate

This will execute any schema migration commands you define in the run_migrations function.

Please note that this is a simplified example, and in real projects, you may need to handle more complex scenarios and error cases. Also, ensure that you have proper security measures in place to protect sensitive database credentials and backup files.

Remember to adapt the script to your specific database setup and requirements. Shell scripting can be a powerful tool for automating routine database operations in a DevOps workflow, saving time and reducing the risk of manual errors.

XII. Security Scanning and Compliance

Performing security scans and compliance checks is crucial in a DevOps environment to ensure the safety and integrity of applications and systems. Shell scripts can be used to automate these checks and scans, making the process more efficient and consistent. Let's create a shell script that performs a basic security scan on a web application and checks for compliance with security best practices.

For this example, we'll use a simple web application running on the Apache web server. We will use the curl command to check the HTTP response status and nmap for a basic port scan. Additionally, we'll ensure that certain security headers are present in the HTTP response.
https://github.com/techwithankush/shell-scripting/blob/618d14bc69b19244dabe6747fa6fa17930810f30/11.Database_Operations.sh

Save the script in a file called security_scan.sh and make it executable:

chmod +x security_scan.sh

Now, you can run the script to perform the security scan and compliance checks on the web application:

./security_scan.sh

The script will output whether the web application is responding as expected, and perform a basic port scan using nmap, and check for the presence of specific security headers in the HTTP response.

Keep in mind that this is a basic example, and for a real-world scenario, you might need to include more comprehensive security checks and scans, as well as compliance checks for industry-specific regulations and standards.

Always consider security best practices and ensure that you have proper authorization to perform security scans on the target application or system. Security scanning should be done responsibly and in a controlled environment.

XIII. Git Hooks

Git hooks are scripts that can be triggered at various points in the Git workflow, allowing you to enforce standards, perform validations, and trigger actions automatically. By using shell scripts as Git hooks, you can ensure that certain checks and actions are performed consistently before or after certain Git operations.

Here's a brief overview of some common Git hooks and how you can utilize shell scripts for each of them refer to GitHub URL:
https://github.com/techwithankush/shell-scripting/blob/aec87fb6edcbed52d4cbc6278940963bd82d929f/13.Git_Hooks.sh

  1. Pre-commit Hook: This hook is triggered before a commit is created. It allows you to perform checks on the changes being committed.

Example: Enforce code formatting standards using a shell script.

  1. Pre-receive Hook: This hook is triggered on the server side when a push is received but before the remote refs are updated. It is useful for enforcing policies on the repository.

Example: Enforce a policy that requires a commit message to include a JIRA issue number.

  1. Post-receive Hook: This hook is triggered on the server side after a push is completed. It is useful for triggering actions like deployment or notification.

Example: Trigger a deployment to the staging server after a successful push.

  1. Pre-push Hook: This hook is triggered before a push is executed. It can be used for running tests or other checks before pushing changes to the remote repository.

Example: Run automated tests before pushing changes.

To use these hooks, create the appropriate shell script for each hook and place them in the .git/hooks/ directory of your Git repository. You can rename the script to match the hook's name (e.g., pre-commit, pre-receive, etc.), and Git will automatically execute the script at the appropriate times.

Remember to make the scripts executable:

chmod +x .git/hooks/pre-commit
chmod +x .git/hooks/pre-receive
chmod +x .git/hooks/post-receive
chmod +x .git/hooks/pre-push

By utilizing shell scripts as Git hooks, you can enforce project standards, automate checks, and streamline your development workflow, ensuring code quality and consistency across the team.

XIV. Application Configuration Management

Automating the management of application configurations and environment variables is essential for maintaining consistency and ensuring smooth deployments in a DevOps environment. Shell scripts can be used to automate the process of updating, versioning, and distributing configuration files and environment variables across different environments.

Here's a basic example of how you can use shell scripts to manage application configurations and environment variables:

  1. Configuration Template and Versioning: Create a configuration template file for your application, which contains placeholders for environment-specific values. For example, you could use a JSON or YAML file with placeholders like {{DB_HOST}}, {{DB_USER}}, {{DB_PASSWORD}}, etc.
{
  "database": {
    "host": "{{DB_HOST}}",
    "user": "{{DB_USER}}",
    "password": "{{DB_PASSWORD}}"
  }
}

To manage different versions of the configuration, you can create separate files for each environment, such as config_dev.json, config_staging.json, and config_prod.json, with actual values specific to each environment.

  1. Shell Script for Environment Configuration: Create a shell script that takes environment-specific variables and generates the appropriate configuration file from the template. For this example, let's use a bash script.
    https://github.com/techwithankush/shell-scripting/blob/9882f87946fa0140df654cc1d71bd23befa746e8/14.Application_Configuration_Management.sh

  2. Usage: When deploying the application to a specific environment, run the shell script to generate the appropriate configuration file for that environment.

For example, to set up the application in the development environment, execute:

./configure_app.sh

The script will replace the placeholders in the template with the actual values for the development environment, creating the config_dev.json file.

Similarly, repeat the process for the staging and production environments by setting the appropriate environment variables before running the script.

By using shell scripts to automate configuration management, you can easily version and distribute configurations across different environments, reducing the risk of errors and ensuring consistency in your deployments. Additionally, you can integrate this script into your continuous integration/continuous deployment (CI/CD) pipelines to automate the configuration process during deployments.

XV. Continuous Monitoring

Continuous monitoring is a critical aspect of the DevOps process, as it allows teams to proactively detect and address issues in real time. Shell scripts can be used to set up simple monitoring solutions that provide real-time insights into application and infrastructure performance. In this example, we'll create a basic shell script that monitors the CPU and memory usage on a Linux server.

https://github.com/techwithankush/shell-scripting/blob/3abb469d7249935005c91a3cf908a09e2c649e3a/15.Continuous_Monitoring.sh

To use this monitoring script, save it in a file called monitor.sh and make it executable:

chmod +x monitor.sh

Run the script to start monitoring:

./monitor.sh

The script will continuously display the CPU and memory usage percentages in real-time.

While this is a simple example, you can extend the script to monitor other aspects of your application and infrastructure. For instance, you can add commands to check disk usage, network bandwidth, or specific application performance metrics. Additionally, you can integrate this script into monitoring tools like Grafana, Prometheus, or Nagios to collect and visualize data over time, enabling more sophisticated monitoring and alerting capabilities.

Keep in mind that shell scripts might have limitations when it comes to complex monitoring scenarios, especially in large-scale or high-availability environments. In such cases, dedicated monitoring tools or monitoring services are preferred.

Shell scripts can be a valuable tool for quick and straightforward monitoring solutions or for automating custom monitoring tasks, providing valuable insights into your application and infrastructure performance.

XVI. Scaling and Auto-scaling

Automating the scaling of applications or infrastructure based on predefined metrics is a crucial aspect of modern cloud-based deployments. Auto-scaling allows you to dynamically adjust resources based on the demand, ensuring optimal performance and cost efficiency. While shell scripts alone may not be sufficient for complex auto-scaling scenarios, they can play a role in the process, especially when combined with cloud provider APIs and other automation tools.

Let's create a basic example of how you can use a shell script to trigger auto-scaling based on CPU utilization for a cloud-based virtual machine.

Note: This example assumes you are using a cloud provider that supports auto-scaling groups, such as Amazon Web Services (AWS) with Amazon EC2 Auto Scaling.
https://github.com/techwithankush/shell-scripting/blob/d197ff8c0075ead67f810ee0059c2b16e9fae42c/16.Auto-scaling.sh

  1. Set Up Prerequisites: Ensure you have the necessary command-line tools for interacting with your cloud provider. For AWS, you would need the AWS CLI and the appropriate IAM credentials configured.

  2. Scaling Script: Create a shell script that monitors CPU utilization and triggers scaling actions based on predefined thresholds.

Please note that the scaling actions (scale_up and scale_down functions) in this example are placeholders, and you'll need to replace them with appropriate commands or API calls to your cloud provider's auto-scaling service.

  1. Run the Script: Make the script executable:
chmod +x scaling_script.sh

Run the script:

./scaling_script.sh

The script will monitor the CPU utilization every minute and trigger scaling actions if the utilization exceeds the defined threshold.

In real-world scenarios, auto-scaling often requires the use of specialized tools and services provided by cloud providers or third-party solutions. These tools typically handle more complex scaling logic, health checks, and integration with load balancers.

While shell scripts help script basic logic and call cloud provider APIs, consider using dedicated auto-scaling solutions for production-grade auto-scaling in complex environments. These solutions often integrate seamlessly with cloud infrastructures and provide additional features like predictive scaling, cooldown periods, and integration with application monitoring.

XVII. Integration with Third-Party Tools

Shell scripts are versatile and can be used to interact with various third-party tools and services, making them valuable for automating workflows and integrating different components in a DevOps environment. Here are some examples of how shell scripts can be utilized to interact with external tools and services:

https://github.com/techwithankush/shell-scripting/blob/afed87a5780f01b3b411cf05f21e369db3ec720c/17.%20Integration_with_Third-Party_Tools.sh

These examples demonstrate how shell scripts can act as glue between different components in your DevOps ecosystem. They enable seamless integration with various third-party tools and services, streamlining your workflows and automating routine tasks. However, for more complex and production-ready integrations, consider using dedicated tools and libraries that provide better error handling, security, and more extensive functionalities.

XVIII. Error and Exception Handling

Handling errors gracefully and notifying stakeholders about critical issues is essential in maintaining a reliable and stable DevOps workflow. Shell scripts can be equipped with error-handling mechanisms to ensure that failures are properly handled, and relevant stakeholders are promptly informed. Let's create an example of a shell script that handles errors and sends notifications using email.

https://github.com/techwithankush/shell-scripting/blob/f656de73202bc644cfe725baac079ef874a3026b/18.Error_and_Exception_Handling.sh

In this example, we have a shell script that performs a critical operation (dividing by zero in this case, which will result in an error). We redirect the error output (stderr) to a temporary file using 2> "$temp_file".

If the critical operation encounters an error (as expected in this example), the perform_critical_operation function will return a non-zero exit code. The script will then send an email notification to the specified recipient, informing them of the error.

To use this script, save it in a file called error_handling.sh and make it executable:

chmod +x error_handling.sh

When you run the script:

./error_handling.sh

It will perform the critical operation, encounter an error, and send an email notification to admin@example.com with the details of the error.

Customize the perform_critical_operation function with the actual critical operation relevant to your use case. Additionally, adjust the email notification function (send_notification) as per your email configuration.

By incorporating error and exception handling mechanisms in your shell scripts, you can ensure that errors are dealt with gracefully, and stakeholders are promptly notified about critical issues. Proper error handling enhances the reliability of your DevOps workflow and helps you take timely actions to address problems.

XIX. Dependency Management

Automating the installation and management of application dependencies is crucial for ensuring smooth and consistent deployments in a DevOps environment. Shell scripts can be used to streamline the process of installing dependencies, checking for version compatibility, and managing updates. Here's an example of a shell script that automates the installation of application dependencies using package managers.

In this example, we'll use a bash script to handle dependency management for a Python-based application using pip as the package manager.

https://github.com/techwithankush/shell-scripting/blob/d983c852e21a617b4b148d2d4905ed34fbfbc321/19.Dependency_Management.sh

To use this script, save it in a file called install_dependencies.sh and make it executable:

chmod +x install_dependencies.sh

Place a requirements.txt file in the same directory containing the Python dependencies required for your application:

requests==2.26.0
numpy==1.21.1

Now, when you run the script:

./install_dependencies.sh

It will install the specified Python dependencies using pip.

You can modify this script to accommodate other package managers (e.g., apt, yum, npm, etc.) based on the requirements of your application. Additionally, you can add checks to ensure that specific versions of dependencies are installed to maintain compatibility and avoid any unexpected issues due to version mismatches.

For more complex scenarios, consider using package manager-specific tools like virtualenv for Python or npm for Node.js, or configuration management tools like Ansible or Chef, which provide more advanced dependency management capabilities, especially in larger-scale DevOps workflows.

By automating dependency management with shell scripts, you ensure consistency and reliability in your deployments while reducing the manual effort required to set up and maintain application dependencies.

XX. Custom Build and Deployment Workflows

Custom build and deployment workflows often require tailored scripts to meet the specific requirements of an application or project. Shell scripts can be instrumental in orchestrating the entire build and deployment process, integrating various tools, and performing custom actions as needed. Let's create an example of a custom build and deployment script for a web application.

In this example, we'll use a bash script to build a Node.js web application, run tests, package the application, and deploy it to a remote server.

https://github.com/techwithankush/shell-scripting/blob/65acff5fc477483d5384c1ae4f943b9248f50ebf/20.Custom_Build_and_Deployment_Workflows.sh

To use this script, save it in a file called build_and_deploy.sh and make it executable:

chmod +x build_and_deploy.sh

Ensure that the necessary build and deployment tools are installed (e.g., Node.js, npm, etc.), and that the SSH keys are set up for seamless remote deployment.

Now, when you run the script:

./build_and_deploy.sh

It will execute the custom build and deployment workflow, including building the application, running tests, packaging the application, and deploying it to the specified remote server.

Custom build and deployment scripts allow you to adapt your workflow to the specific requirements of your project, enabling efficient and reliable automation. Tailor your scripts to include any additional steps or integration with other tools necessary for your application's unique needs.

For more complex build and deployment workflows or to manage multiple environments (e.g., staging, production), consider using continuous integration/continuous deployment (CI/CD) tools such as Jenkins, GitLab CI/CD, or GitHub Actions. These tools provide a more robust and scalable approach to automate and manage build and deployment pipelines for different environments and branches.

Thank you for reading! :)

More from this blog

Untitled Publication

68 posts