Day 5 - Task: Advanced Linux Shell Scripting for DevOps Engineers with User Management (Dec 1, 2023)

Day 5 - Task: Advanced Linux Shell Scripting for DevOps Engineers with User Management (Dec 1, 2023)

Tasks

  1. You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -

So Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.

Example 1: When the script is executed as

./createDirectories.sh day 1 90

then it creates 90 directories as day1 day2 day3 .... day90

Example 2: When the script is executed as

./createDirectories.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

Notes: You may need to use loops or commands (or both), based on your preference . Check out this reference: https://www.geeksforgeeks.org/bash-scripting-for-loop/

  1. Create a Script to backup all your work done till now.

Backups are an important part of DevOps Engineers day to Day activities The video in References will help you to understand How a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, Nothing is impossible.) Watch this video

In case of Doubts, post it in Discord Channel for #90DaysOfDevOps

  1. Read About Cron and Crontab, to automate the backup Script

Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.

Watch This video as a Reference to Task 2 and 3 https://youtu.be/aolKiws4Joc

  1. Read about User Management and Let me know on Linkedin if you're ready for Day 6.

A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users and hence the ids for local user begins from 1000 onwards.

  1. Create 2 users and just display their Usernames

Check out this reference: https://www.geeksforgeeks.org/user-management-in-linux/

Post your daily work on Linkedin and le me know , writing an article is the best :)

Introduction

Happy 1 year to the 90 Days of DevOps journey! Today's tasks involve advanced Linux shell scripting, backup creation, and user management. Let's dive into each task step by step.

Task 1: Dynamic Directory Creation with Shell Scripting

Creating 90 Directories

If you've been exploring the repository, you might have noticed the existence of 90 subdirectories under '2023.' The question arises: were these directories created manually or through automation?

The answer is automation! Using a simple command:

mkdir day{1..90}

Now, let's recreate this using a bash script named createDirectories.sh:

#!/bin/bash

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

# Extract arguments
prefix=$1
start=$2
end=$3

# Create directories
for ((i = start; i <= end; i++)); do
    mkdir "${prefix}${i}"
done

echo "Directories created successfully."

Make the script executable:

chmod +x createDirectories.sh

Now, you can create directories using examples like:

./createDirectories.sh day 1 90

or

./createDirectories.sh Movie 20 50

Task 2: Backup Script

As DevOps engineers, backups are crucial. Let's create a script to back up all the work done until now. The following script, named backupScript.sh, will create a timestamped backup:

#!/bin/bash

backup_dir="backup_$(date +"%Y%m%d%H%M%S")"

# Create backup directory
mkdir "$backup_dir"

# Copy relevant files to the backup directory
cp -r /path/to/your/work/* "$backup_dir"

echo "Backup completed successfully."

Make the script executable:

chmod +x backupScript.sh

You can run this script whenever you want to create a backup.

Task 3: Automating Backups with Cron

Cron is a powerful tool for scheduling tasks in Linux. Let's automate our backup script. Open the crontab file for editing:

crontab -e

Add the following line to run the backup script daily at 2 AM:

0 2 * * * /path/to/backupScript.sh

Save and exit. Now, your backup script will run automatically every day at 2 AM.

Task 4: User Management

In Linux, user management is a critical aspect. Let's create two users and display their usernames:

#!/bin/bash

# Create users
useradd -m user1
useradd -m user2

# Display usernames
echo "User 1: $(grep 'user1' /etc/passwd | cut -d: -f1)"
echo "User 2: $(grep 'user2' /etc/passwd | cut -d: -f1)"

Make the script executable:

chmod +x

Run the script:

./userManagement.sh