Linux - Day 5

Linux - Day 5

Advanced Linux Shell Scripting for DevOps Engineers with User Management


imagine you have to create 90 directories inside a folder. How would you do it?

Thankfully, there’s a faster way to get things done in Linux. With one simple command, I created all 90 directories in just a second:

mkdir day{1..90}

This is what makes Linux so powerful for DevOps work, it is all about automating boring, repetitive tasks.


📝 Tasks for Today:

Task 1: Create Directories Using a Shell Script

Your task is to write a bash script that can create a bunch of directories at once. But instead of hardcoding it like in the command above, we’ll make it more flexible. The script should take three inputs:

  1. The name of the directories.

  2. The starting number.

  3. The ending number.

For example:

  • Running ./createDirectories.sh day 1 90 should create directories named day1, day2, …, day90.

  • Running ./createDirectories.sh Movie 20 50 should create directories named Movie20, Movie21, …, Movie50.


Here’s What the Script Looks Like:

#!/bin/bash

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

# Assign inputs to variables
dirname=$1
start=$2
end=$3

# Loop through the numbers and create directories
for ((i=start; i<=end; i++)); do
  mkdir "${dirname}${i}"
done

echo "Directories ${dirname}${start} to ${dirname}${end} created successfully."

To use it:

  1. Save the script as createDirectories.sh.

  2. Make it executable by running:

     chmod +x createDirectories.sh
    
  3. Now, you can run it to create multiple directories with custom names in one go!


Task 2: Create a Backup Script

As a DevOps engineer, making backups is super important to ensure you don’t lose your work. Let’s automate that by creating a simple backup script!

The script will:

  • Compress your work into a .tar.gz file.

  • Store it in a folder called backups with the current date and time in the filename.

Here’s how the backup script looks:

#!/bin/bash

# Set the backup directory and filename with a timestamp
backup_dir="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

# Archive the current directory, excluding the backup folder itself
tar -czf $backup_file --exclude="$backup_dir" .

echo "Backup created at $backup_file"

To run it:

  1. Save this script as backup.sh.

  2. Make it executable with:

     chmod +x backup.sh
    
  3. Run it whenever you want to back up your work.


Task 3: Automate the Backup Script with Cron

Wouldn’t it be nice if your backup happened automatically? That’s where Cron comes in.

Cron is a tool that lets you schedule tasks in Linux. We’ll set it up to run your backup script daily at 2 AM.

Here’s how to do it:

  1. Open your crontab (which is the list of scheduled jobs) by typing:

     crontab -e
    
  2. Add this line to schedule your backup script to run every day at 2 AM:

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

Now your system will take care of backups for you automatically!


Task 4: User Management in Linux

Managing users is something you’ll often do as a DevOps engineer. In Linux, each user has their own unique ID and permissions.

To create two new users, run:

sudo useradd user1
sudo useradd user2

You can check if they were added successfully by displaying the last two users in your system:

cut -d: -f1 /etc/passwd | tail -n 2

This will show user1 and user2.


📚 Useful Resources:


By working through these tasks, i am learning how to automate common DevOps tasks like creating directories, setting up backups, and managing users. Automation saves you time and effort, making you more efficient and productive as a DevOps engineer.

Keep going, and if you have any questions, don’t hesitate to ask for help in the Discord community!