Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Devbrat SinghDevbrat Singh
4 min read

This is #day5 of #90DaysofDevops challenge under the guidance of Shubham Londhe Sir !

Introduction👍:

  • Welcome to Day 5 of our DevOps journey! Today, we're going to dive into some advanced Linux shell scripting techniques, specifically focusing on user management and automation. Don't worry if you're new to this – we'll break it down step by step.

Step 1: Understanding Directory Creation

  • Let's say you have a task where you need to create multiple directories in Linux, maybe for organizing files or projects. Doing this manually can be time-consuming and tedious. Instead, you can use a simple command to create multiple directories at once.

  • Example:

mkdir d
  • This command creates 90 directories named 'day1' through 'day90' in your current directory. It's like magic! You just saved yourself a ton of time and effort.

    Step 2: Task Assignment – Shell Scripting for Directory Creation

  • Now, imagine you need to create directories dynamically based on certain parameters. This is where shell scripting comes in handy. You can write a script that automates the directory creation process, making your life much easier.

  • Example Script: create_directories.sh

      #!/bin/bash
    
      # Loop through days
      for day in {1..90}; do
        # Check if directory already exists
        if [[ -d "day$day" ]]; then
          echo "Directory 'day$day' already exists. Skipping..."
        else
          # Create directory
          mkdir "day$day"
          echo "Directory 'day$day' created successfully."
        fi
      done
    
      echo "Directory creation completed."
    

Step 3: Creating a Backup Script

  • Backups are essential for protecting your work. You can create a simple backup script that copies your important files to a safe location.

  • Example Backup Script: backup.sh

      #!/bin/bash
    
      # Define source and target directories
      src="/home/ubuntu/scripts"
      trg="/home/ubuntu/backups"
    
      # Ensure that the target directory exists
      mkdir -p $trg
    
      # Get the current timestamp
      curr_timestamp=$(date "+%d-%m-%Y-%H-%M-%S")
      # Create backup file name with timestamp
      backup=$trg/$curr_timestamp.tgz
    
      # Inform the user about the backup process
      echo "Taking backup on $curr_timestamp"
      #echo $backup
    
      # Create the backup archive using tar
      tar czf $backup --absolute-names $src
      echo "backup complete"
    

    • This script compresses your files into a tarball with a timestamp and saves it to the backup destination.

ANd the results how the backup is created in backups/

Step 4: Automating with Cron and Crontab

  • Cron is a powerful tool for automating tasks in Linux. You can use it to schedule your backup script to run at regular intervals without manual intervention.

  • Example Cron Job:

      * 10 * * * /path/to/backup.sh
    

    • This cron schedule will execute the backup.sh script every minute during the 10:00 AM hour.

    • Once testing is complete, you can switch to running it daily at 10:00 AM using the following schedule:

        0 10 * * * /home/ubuntu/scripts/backup.sh
      

      Step 5: User Management Basics

      • In Linux, users are central to the system. You can create and manage users using simple commands.

      • To create two users and display their usernames in Linux, you can follow these steps:

  1. Use the adduser command to create the users. Provide a username when prompted and complete the user creation process for each user.

    Example:

         sudo adduser username

  1. To display the usernames of the newly created users, you can use the id command followed by the usernames.

Example:

        id username
  1. After executing the above commands, you'll see output displaying information about each user, including their usernames.

    • By following these steps, you'll have created two users and displayed their usernames in your Linux system.

Conclusion🎉:

Today, we've unlocked the power of Linux shell scripting, from dynamic directories to automated backups and user management. Keep exploring, learning, and growing on your DevOps journey. Together, we'll shape the future of technology and make a positive impact in the tech world.

If you found this guide on Advanced Linux Shell Scripting for DevOps Engineers with User Management helpful. If you did, please consider giving it a thumbs up 👍 to show your support😄!

Thank you for taking the time to read! 💚

0
Subscribe to my newsletter

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

Written by

Devbrat Singh
Devbrat Singh

"Aspiring DevOps & Cloud Enthusiasts" | LINUX | Git | GITHUB | AWS | DOCKER | KUBERNETES | ANSIBLE | TERRAFORM | JENKINS | NGINX