How to Automate Disk Usage Monitoring with a Custom Shell Script

Write a custom shell script that offers a robust solution for monitoring disk usage

In system administration, ensuring efficient disk space management is paramount. AltSchool Africa has tasked us with crafting a solution to monitor disk usage within specified directories. To meet this challenge, we've developed a versatile shell script capable of precisely analysing disk usage and providing insightful reports. Let's delve into its functionality and how it streamlines this critical task.

Understanding the Task

Our mission was clear: develop a shell script that scrutinises disk usage in a designated directory. This script should offer flexibility, allowing users to tailor their queries by specifying optional arguments. The essential requirement is the ability to analyze disk usage within a specified directory. However, users may also opt to list all files and directories within the target directory or limit the output to a defined number of entries.

Deciphering the Script

Let's dissect the components of our script to grasp its inner workings:

P.S I prefer using my nano text editor to write the below scripts

#!/bin/bash

Check if source directory is provided

if [ -z "$1" ]; then echo "Usage: $0 <source_directory> <destination_directory>" exit 1 fi

timestamp=$(date '+%Y_%m_%d_%H-%M-%S') source_directory="$1" destination_directory="$2" backup_directory="$destination_directory$timestamp.tar.gz"

Check if source directory exists

if [ ! -d "$source_directory" ]; then echo "Error: Source directory does not exist." exit 1 fi

Create backup

sudo tar -czf "$backup_directory" "$source_directory"

Print backup directory

echo "Backup created at: $backup_directory"

Functionality Overview

  1. Input Validation: The script begins by ensuring that the user provides a source directory. Without this crucial input, the script won't be able to proceed. This step guarantees proper usage and prevents unexpected errors.

  2. Timestamp Generation: Before initiating the backup process, the script generates a timestamp, ensuring that each backup is uniquely identifiable. This feature aids in organizing and tracking backups effectively.

  3. Backup Creation: Leveraging the tar utility, the script compresses and archives the specified source directory. This step not only conserves disk space but also facilitates efficient data storage and retrieval.

  4. Feedback to User: Upon successful backup creation, the script concludes by informing the user about the generated backup directory. This feedback mechanism enhances user experience by providing clear, actionable insights.

Putting the Script to Work

To harness the power of our shell script, users can execute it with various command-line arguments:

  • -d: List all files and directories within the specified directory.

  • -n: Limit the output to the top N entries, providing insightful summaries.

  • List of Directories: Specify the directories to be analyzed for disk usage.

For instance, executing myscript.sh -n 5 /var will return the top 5 directories concerning disk usage within the /var directory. Similarly, myscript.sh -a /var will list both directories and files within /var.

In Conclusion

Our custom shell script offers a robust solution for monitoring disk usage, empowering users with actionable insights and streamlined operations. By understanding its functionality and leveraging its flexibility, system administrators can proactively manage disk space, ensuring optimal system performance and reliability.

Stay tuned for more innovative solutions from Altschool Africa as we continue to tackle real-world challenges through technology-driven approaches.