Skip to main content

Command Palette

Search for a command to run...

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

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

     #!/bin/bash
    
     #First Way
     for i in {1..90};
     do
             mkdir "Day$i"
     done
    
     #Second Way
     mkdir day{01..90}
    

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

There is two ways to create backup file:

  • First Using Function

      # Assign the command-line argument to a variable for dir path
      dir=$1
    
      # This function perform a backup of the directory
    
      function create_backup {
                  local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')  # Get the current time of your device
                  local backup_dir="${dir}/backup_${timestamp}"
    
                          # Create the backup folder with the timestamped name
                              mkdir "$backup_dir"
                                  echo "Backup created successfully: $backup_dir"
                         }
    
      #This function is perform rotaion for backup dir
    
      function perform_rotation {
                  local backups=($(ls -t "${dir}/backup_"* 2>/dev/null))  # List existing backups sorted by timestamp
    
                      # Check if there are more than 3 backups
                          if [ "${#backups[@]}" -gt 3 ]; then
                                          local backups_to_remove="${backups[@]:3}"  # Get backups beyond the last 3
                                                  rm -rf "${backups_to_remove[@]}"  # Remove the oldest backups
                                                      fi
                                              }
    
      create_backup
      perform_rotation
    

  • Second is Using Variables

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

Backups are an important part of DevOps Engineer's Day to Day activities there is another concept of cron and crontab are introduce using this we can schedule the file execution and take back up of the files.

Cron and crontab are Linux system files or commands that allow users to schedule the execution of commands or scripts at regular intervals. The cron daemon is the program that runs the cron jobs according to the crontab file.

The crontab file is a table-like structure where each line represents a cron job and each field specifies the time and frequency of the job. The syntax of the crontab command can be somewhat challenging to get started with if you're a beginner. However, it is an important skill for aspiring system administrators to learn.

Here is an example of a crontab entry that runs a script every day at 2:30 AM:

30 2 * * * /path/to/script.sh

This entry specifies that the script should run at 2:30 AM every day. The five fields in the entry represent the minute, hour, day of the month, month, and day of the week, respectively. The asterisk (*) character is used as a wildcard to match any value.

♦crontab -l: check how many crontab are activated.

♦crontab -e: using which editor you make file.

  1. User Management: 🕵️‍♂️

    Below is type of users available in Linux:

    • To list out all the users in Linux:

      • use the awk command with -F option. Here, we are accessing a file and printing only first column with the help of print $1 and awk.

          awk -F':' '{ print $1}' /etc/passwd
        

  • Using id command, you can get the ID of any username. Every user has an id assigned to it and the user is identified with the help of this id. By default, this id is also the group id of the user.

      id aesha
    

  • The command to add a user. useradd command adds a new user to the directory. The user is given the ID automatically depending on which category it falls in. The username of the user will be as provided by us in the command.

  •   sudo useradd user1
    
  • Using password command to assign a password to a user. After using this command we have to enter the new password for the user and then the password gets updated to the new password.

      passwd user1
    

More from this blog

Untitled Publication

16 posts