Day 5 of 90DaysOfDevOps : Advanced Linux Shell Scripting for DevOps Engineers with User Management

Pooja NaitamPooja Naitam
2 min read

1. Create Directories Using Shell Script:

  • Write a bash script createDirectories.sh that, when executed with three arguments (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.

  • Example 1: When executed as ./createDirectories.sh day 1 90, it creates 90 directories as day1 day2 day3 ... day90.

#!/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 arguments to variables
dir_name=$1
start_num=$2
end_num=$3

# Loop from start to end number
for (( i=start_num; i<=end_num; i++ ))
do
    mkdir "${dir_name}${i}"
done

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

2. Create a Backup Script

Backups are an important part of a DevOps Engineer's day-to-day activities. The video in the references will help you understand how a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, nothing is impossible).

#!/bin/bash

# Define source and backup directory
src="/path/to/your/work"  # Change this path
backup_dir="/path/to/backup"  # Change this path
timestamp=$(date +"%Y%m%d%H%M%S")

# Create backup
tar -czvf "$backup_dir/backup_$timestamp.tar.gz" "$src"

echo "Backup completed at $backup_dir/backup_$timestamp.tar.gz"

3.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.

Cron Setup:

  • Open the crontab editor with crontab -e.

  • It will run your backup script every 2 hours:

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

4.Read About User Management:

  • 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 within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.

  • Create 2 users and display their usernames.

      # Create two users
      sudo useradd tanjiro
      sudo useradd nezuko
    
      # Verify creation
      cat /etc/passwd | grep -E "tanjiro|nezuko"
    

    πŸš€ Conclusion

    These tasks offer a solid start to essential DevOps skills like automation, backup management, and user administration. πŸ“‚ Writing scripts to automate tasks, scheduling backups with Cron ⏰, and managing users πŸ§‘β€πŸ’» are all crucial for smooth, secure workflows. Completing these exercises boosts your confidence and builds a strong foundation for advanced DevOps tasks. Sharing your journey πŸ“£ connects you with the DevOps community, adding more value as you learn and grow. Keep goingβ€”each step gets you closer to becoming a DevOps pro! 🌟

    #DevOps, #90DaysofDevOps, #TWS, #linux, #Shellscripting

0
Subscribe to my newsletter

Read articles from Pooja Naitam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pooja Naitam
Pooja Naitam

πŸ‘‹ Hello! I'm Pooja Naitam, a passionate DevOps fresher with a solid foundation in the field. I hold the AWS Certified Cloud Practitioner (CCP) certification, and I'm eager to apply my knowledge to real-world projects while continuously learning cutting-edge technologies. Let's connect and grow together in the exciting world of DevOps!