Advanced Linux Shell Scripting for Efficient DevOps: Day 5 Challenge πŸš€

Advanced Linux Shell Scripting for Efficient DevOps: Day 5 Challenge πŸš€

Β·

3 min read

Welcome to Day 5 of our DevOps journey, where we delve into the world of shell scripting with a focus on user management and automation. Today's challenge introduces practical tasks that underline the importance of scripting in everyday DevOps workflows.

πŸ“ Task 1: Dynamic Directory Creation with Shell Scripts

Ever wondered how to create numerous directories efficiently? Here's a secret: it's not done manually. With the power of shell scripting, we can generate multiple directories in seconds! Let's explore how.

The Power of mkdir

In Linux, the mkdir command is a staple for creating directories. But it's not just about single directories; you can create a sequence of directories using a simple syntax. For instance:

mkdir day{1..90}

This command creates directories named day1 to day90. Neat, isn't it?

Automating with createDirectories.sh Script

Now, let's automate this process with a shell script that takes arguments for dynamic directory creation:

#!/bin/bash
# createDirectories.sh - A script to create a range of directories.

# Check for the correct number of arguments
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <directory_name> <start_number> <end_number>"
    exit 1
fi

# Extract arguments
dir_name=$1
start_num=$2
end_num=$3

# Loop to create directories
for (( i=start_num; i<=end_num; i++ )); do
    mkdir "${dir_name}${i}"
done

echo "Directories ${dir_name}${start_num} to ${dir_name}${end_num} created successfully!"

πŸ”Ή Example Usage:

πŸ’Ύ Task 2: Backup Script for Safekeeping Your Work

As a DevOps Engineer, backups are critical. They ensure that your work is safe and can be restored in case of any mishap. Below is a basic script that creates a backup of your work.

#!/bin/bash
# backupScript.sh - A script to backup your work.

backup_dir="/path/to/backup"
source_dir="/path/to/source"

# Create a timestamped backup directory
backup_name="backup_$(date +%F_%R)"
mkdir "$backup_dir/$backup_name"

# Copy files
cp -a "$source_dir/." "$backup_dir/$backup_name/"

echo "Backup of $source_dir completed successfully at $backup_dir/$backup_name"

Automating Backups with cron

To automate your backup script, cron is your go-to scheduler. You can set up a crontab entry to run your backup script at regular intervals.

# Example of a crontab entry to run the backup script every day at midnight
0 0 * * * /path/to/backupScript.sh

πŸ‘₯ Task 3: User Management Essentials

Knowing your way around user management is like having the master key to your system’s kingdom. It’s all about adding, managing, and sometimes waving goodbye to users.

Creating Users with useradd

# Add new users with useradd
sudo useradd -m alice
sudo useradd -m bob

Displaying Usernames with id

# Display usernames
id -un alice
id -un bob

🌟 Conclusion and Next Steps

Wow, what a ride it's been today! We've automated directory creation, got our backup game on point, and unlocked the secrets of user management.

Ready for more? Hit me up on LinkedIn and let's get prepped for Day 6. Keep those scripts flowing and terminals glowing!

Catch you on the flip side, fellow scripters! πŸš€πŸ–₯️

Stay tuned and keep scripting! πŸ“šπŸ’»

Β