Project 1: System Management Automation Script "basic-auto.sh"

Ayodeji HamedAyodeji Hamed
4 min read

Introduction

Welcome to the first project in my series, "H A R S H H A A's 50 DevOps Projects: Building Skills from Beginner to Advanced"! This project focuses on creating a set of Bash scripts to automate essential administrative tasks such as cleaning up log files, backing up critical data, and updating the system.

Designed as a beginner-level project, it introduces foundational scripting concepts like conditionals, loops, and the effective use of shell commands. By completing this project, you'll take your first steps toward mastering Bash scripting and automation, setting the stage for more advanced challenges in your DevOps journey.

You can find the complete script in my GitHub repository here: basic-auto.sh.


Overview

System administration tasks are often repetitive and time-consuming, making automation a critical skill for DevOps engineers and system administrators. This project introduces a simple yet powerful Bash script to automate common administrative tasks such as:

  • System updates and upgrades.

  • Log file management.

  • Data backups and restoration.

The basic-auto script is modular, interactive, and user-friendly. It features detailed logging, error handling, and compatibility with various Linux distributions. With an intuitive interactive menu, you can:

  • Update and upgrade the system.

  • Clear log files selectively or in bulk.

  • Create backups and restore data effortlessly.

By using this script, you'll save time, reduce human error, and gain hands-on experience with Bash scripting fundamentals. Whether you're a beginner learning to script or an experienced professional seeking inspiration, this project provides a practical approach to enhancing your automation skills. Check out the script on GitHub to get started!


Approach

The development of the basic-auto script follows a systematic approach that emphasizes modularity, usability, and compatibility across Linux distributions. Here’s a detailed breakdown:

1. Understanding the Problem Scope

The script automates repetitive system management tasks, including:

  • Updating and upgrading the system.

  • Managing log files efficiently.

  • Backing up and restoring important data.

It is designed to work seamlessly across multiple Linux distributions with minimal configuration.


2. Building Compatibility Across Linux Distributions

Dynamic Distro Detection

The script uses /etc/os-release to identify the specific Linux distribution dynamically:

. /etc/os-release
OS=$ID

This OS variable maps to the appropriate package manager, ensuring compatibility across distributions.

Extensive Package Manager Support

The script supports various package managers, ensuring broad compatibility:

  • Debian-based (apt): Ubuntu, Mint, etc.

  • RHEL-based (yum, dnf): CentOS, Fedora, etc.

  • SUSE-based (zypper): OpenSUSE, SLES.

  • Arch-based (pacman): Arch, Manjaro.

  • Alpine-based (apk).


3. Using mapfile for Dynamic File Retrieval

A key feature of the script is its ability to dynamically list and interact with files using mapfile:

mapfile -t LOG_FILES < <(find "$LOG_DIR" -type f -name "*.log")

This retrieves all .log files in the /var/log directory and stores them in the LOG_FILES array.

Creating Dynamic Menus

Using the LOG_FILES array, the script generates menus dynamically:

for i in "${!LOG_FILES[@]}"; do
    echo "$((i + 1))) ${LOG_FILES[$i]}"
done

This approach ensures adaptability to runtime file changes.


4. Key Features and Functional Highlights

a. Log Management

  • Selective Clearing: Clear specific log files interactively.

  • Bulk Clearing: Clear all log files without deletion using truncate -s 0, preserving file structure.

  • Log Rotation: Automatically rotates the main log file if it exceeds 10 MB.

b. Backup and Restore Mechanism

  • Backup: Compresses files or directories into .tar.gz archives in /mnt/backup.

  • Restore: Lists available backups dynamically and allows interactive restoration.

  • Validation: Ensures paths exist and prompts users before proceeding.

c. Logging and Error Handling

  • Comprehensive Logging: Logs all actions in system_automation.log with ISO 8601 timestamps.

  • Error Handling: Implements traps for signals like EXIT, SIGINT, and ERR for clean exits.

  • Interactive Prompts: Confirmation prompts reduce the risk of unintentional actions.

d. User Experience and Design

  • Colorful Terminal Output: Enhances readability with color-coded messages (e.g., green for success, red for errors).

  • Interactive Menus: Dynamically generated options for seamless user interaction.


5. Script Architecture and Design Principles

  • Modular Functions: Tasks like backup, restore, and clean logs are encapsulated in reusable functions.

  • Global Variables: Configurable paths (e.g., BACKUP_DIR, LOG_DIR) for easy customization.

  • Conditional Logic and Loops: Ensures dynamic behavior and interactive prompts.


6. Testing Across Linux Distributions

Comprehensive testing ensures compatibility with:

  • Ubuntu, CentOS, Fedora, Debian, Rocky Linux, AlmaLinux.

Testing includes:

  • Updating and upgrading systems.

  • Validating non-destructive log clearing.

  • Dynamically backing up and restoring files.

For detailed testing results and screenshots, visit the full documentation.


This project lays the foundation for advanced automation tasks. Download the script, try it out, and begin your journey into DevOps automation today!


0
Subscribe to my newsletter

Read articles from Ayodeji Hamed directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayodeji Hamed
Ayodeji Hamed

🚀 Welcome to my tech playground on Hashnode! I'm Hamed Ayodeji, a DevOps/Cloud Engineer with a passion for all things innovative and cutting-edge in the world of technology. Join me as I explore the latest trends, share insights, and embark on exciting coding adventures. Let's connect, learn, and grow together in this ever-evolving digital landscape! #DevOps #CloudEngineering #Innovation