Advanced Linux Shell Scripting for DevOps Engineers with User Management

Table of contents

No heading

No headings in the article.

Advanced Linux Shell Scripting for DevOps Engineers with User Management

Welcome back to Day 5 of our DevOps journey! Today, we'll dive into advanced Linux shell scripting and explore user management. But before we start, let's take a moment to appreciate the efficiency of the command used to create 90 directories within seconds:

mkdir day{1..90}

Now, let's move on to the tasks for today:

Task 1: Creating Dynamic Directories with Shell Script

In this task, we'll write a bash script called createDirectories.sh that will create a specified number of directories with a dynamic directory name.

#!/bin/bash

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

directory_name=$1
start=$2
end=$3

for ((i = $start; i <= $end; i++)); do
  mkdir "${directory_name}${i}"
done

Save the above script in a file named createDirectories.sh and give it executable permissions using chmod +x createDirectories.sh.

Example Usage:

./createDirectories.sh day 1 90

This will create 90 directories as day1, day2, day3, ..., day90.

./createDirectories.sh Movie 20 50

This will create 31 directories as Movie20, Movie21, Movie22, ..., Movie50.

Task 2: Backup Script for DevOps Engineers

As DevOps Engineers, backups are crucial to ensure data integrity and disaster recovery. We'll create a backup script to safeguard all our work.

#!/bin/bash

# Define backup directory
backup_dir="/path/to/backup"

# Create a timestamp for the backup
timestamp=$(date +%Y%m%d_%H%M%S)

# Create a tarball of the entire repository
tar -czvf "${backup_dir}/backup_${timestamp}.tar.gz" /path/to/repository

Replace /path/to/backup and /path/to/repository with appropriate paths.

Task 3: Automate the Backup Script using Cron

To automate the backup process, we'll use cron to schedule the backup script to run at specific intervals.

# Open the crontab file for editing
crontab -e

Add the following line to run the backup script every day at 1:00 AM:

0 1 * * * /path/to/backup_script.sh

Replace /path/to/backup_script.sh with the actual path to your backup script.

Task 4: User Management

In this task, we'll create two new users and display their usernames.

#!/bin/bash

# Create two new users
useradd user1
useradd user2

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

Save the above script in a file and give it executable permissions using chmod +x script_name.sh.

Note: For security reasons, ensure that you use strong passwords and follow best practices when creating users in a production environment.

With this, you've completed Day 5 of the DevOps journey! We've explored advanced Linux shell scripting, created dynamic directories with a bash script, and automated backups using cron. Additionally, we touched upon user management by creating and displaying two new users.

Keep practicing and learning to become a skilled DevOps Engineer! If you have any doubts, feel free to ask on the Discord channel. Don't forget to read about Cron and Crontab to explore more about scheduling tasks in Linux.

Tomorrow, on Day 6, we'll dive deeper into user management and further enhance our DevOps skills. Until then, happy scripting and see you on Linkedin to let me know you're ready for Day 6!