🚀 Day 5: Advanced Linux Shell Scripting & User Management for DevOps Engineers

🚀 Day 5: Advanced Linux Shell Scripting & User Management for DevOps Engineers

Welcome to Day 5 of the #90DaysOfDevOps challenge! 🎉 Today, we’ll dive into advanced Linux shell scripting, user management, and even learn how to automate tasks like backups. Don’t worry—these concepts may sound complicated, but I'll break them down into easy-to-follow steps. Let’s get started! 💻

📝 Task 1: Create Multiple Directories with a Shell Script 🗂️

Imagine you need to create 90 directories with names like day1, day2, day3, and so on. Doing it manually one by one would take forever, right? 😩

Thankfully, we can use a simple bash script to create multiple directories in seconds. 🎯

The Problem:

How can we create a dynamic number of directories using a script?

Solution:

We will write a script that accepts three arguments:

  1. Directory Name: This is the prefix (e.g., day or Project).

  2. Start Number: The first directory number (e.g., 1).

  3. End Number: The last directory number (e.g., 90).

Here’s the script:

#!/bin/bash

# Check if three arguments are passed
if [ $# -ne 3 ]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi

# Assign the arguments to variables
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}"
  echo "Created directory: ${dir_name}${i}"
done

Example 1:

If you run:

./createDirectories.sh day 1 90

You’ll create 90 directories: day1, day2, ..., day90. 🚀

Example 2:

For movie directories:

./createDirectories.sh Movie 20 50

You’ll get Movie20, Movie21, ..., Movie50 in seconds. 🎬

📝 Task 2: Create a Backup Script 🗄️

Backups are essential in the life of a DevOps engineer. A good backup system ensures you never lose important work. In this task, we’ll create a script that backs up your files automatically.

The Problem:

How do we create a backup of our important work regularly?

Solution:

We’ll use a bash script to compress (zip) your project files and store them in a backup folder. 📂

Here’s the script:

#!/bin/bash

# Set your project directory and backup destination
SOURCE_DIR="$HOME/my_project"  # Change this to your project directory
BACKUP_DIR="$HOME/backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_FILE="${BACKUP_DIR}/backup_${TIMESTAMP}.tar.gz"

# Create the backup directory if it doesn’t exist
mkdir -p $BACKUP_DIR

# Compress and back up the source directory
tar -czf $BACKUP_FILE $SOURCE_DIR

# Check if the backup was successful
if [ $? -eq 0 ]; then
  echo "Backup successful! File stored at: $BACKUP_FILE"
else
  echo "Backup failed!"
fi

How it Works:

  • It compresses your project folder and creates a backup in the backups folder.

  • The backup filename includes a timestamp for easy tracking (e.g., backup_20231011_143000.tar.gz).

  • If the backup succeeds, you’ll see a success message. ✅

You can now run this script manually whenever you need a backup!

📝 Task 3: Automate the Backup with Cron ⏰

Let’s automate the backup process using cron—a Linux tool for scheduling tasks. Instead of running the backup script manually, we’ll schedule it to run automatically every day at midnight. 🌙

Steps:

  1. Open the cron job editor:

     crontab -e
    
  2. Add this line to schedule the backup:

     0 0 * * * /path/to/backup.sh
    

    This tells the system to run backup.sh every day at 12:00 AM.

Now you’ll never forget to back up your work! 🎉

📝 Task 4: Create Two Users 👥

Linux allows you to manage users on your system easily. Let’s create two new users using the useradd command.

Steps:

  1. Create two users:

     sudo useradd -m user1
     sudo useradd -m user2
    
  2. Set passwords for the users:

     sudo passwd user1
     sudo passwd user2
    
  3. Display the usernames:

     awk -F':' '{ print $1 }' /etc/passwd | tail -n 2
    

The above command will show the last two users created on the system, which are user1 and user2.

🎉 My Day 5 Experience

Today was a great learning experience! I built scripts that saved me from boring, repetitive tasks and even automated my backups! I also learned how user management works in Linux, a key skill for DevOps. These tasks may seem advanced, but with some practice, they become second nature. 💪

Key Takeaways:

  • Shell scripting saves time ⏳ and effort 💪.

  • Backups are essential in DevOps.

  • Cron jobs automate tasks like a pro.

  • User management is critical for system security.

Are you following the #90DaysOfDevOps challenge too? Let me know how your Day 5 went and share your experiences! You can even write a blog post about it to help others. 💡

If you found this helpful, feel free to share it! 📢 Let’s continue learning and growing together in this DevOps journey. 😊

#90DaysofDevOps #TrainWithShubham #CloudComputing #Automation #Scaling #Infrastructure