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

·

6 min read

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

Creating Directories using shell script

#!/bin/bash

# Get the directory name from the first argument
directory_name=$1

# Get the starting and ending numbers from the remaining arguments
starting_number=$2
ending_number=$3

# Create the directories
for i in $(seq $starting_number $ending_number); do
  mkdir "$directory_name-$i"
done

How this script works:

  • The #!/bin/bash line at the top indicates that the script should be run using the Bash shell.

  • The directory_name=$1 line gets the first command-line argument passed to the script (which we assume is the name of the directory) and assigns it to the variable directory_name.

  • The starting_number=$2 and ending_number=$3 lines get the remaining command-line arguments passed to the script (which we assume are the starting and ending numbers for the range of directories to create).

  • The for loop starts at $starting_number and goes up to $ending_number, creating one directory per iteration with the name $directory_name-$i. The -i is a parameter expansion that iterates over the sequence of numbers generated by seq.
    To use the script, save it to a file named createDirectories.sh, make the file executable (chmod +x createDirectories.sh)

  • When the script is executed as

    ./createDirectories.sh day 1 90

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

Creating a script to backup work done until now

 #!/bin/bash
# Set the backup directory
backup_dir=/path/to/backup

# Set the target directory for the backup
target_dir=/path/to/target

# Create a tar archive of the current working directory
tar -czf "${target_dir}/backup.tar.gz" "${backup_dir}"

How the script works :

# Set the backup directory

This line sets the variable backup_dir to the path of the directory where the backup files will be stored.

  • # Set the target directory for the backup

This line sets the variable target_dir to the path of the directory where the backup files will be copied.

  • # Create a tar archive of the current working directory

This line uses the tar command to create a tar archive of the current working directory. The -czf options tell the tar command to create a compressed tar archive (.tar.gz) in the target_dir directory with the name backup.tar.gz. The backup_dir directory is the directory that is being archived.


Using Cron and Crontab to automate the backup script

Cron and crontab can be used to automate the backup script so that it runs at regular intervals without requiring manual intervention.

Here's how:
Cron is a time-based job scheduler that allows us to schedule tasks to run at specific times or intervals. Crontab is a text file that contains instructions for the cron daemon to execute certain commands at specified times.
To automate the backup script using cron and crontab, follow these steps:

  1. Open the crontab editor: We can open the crontab editor by typing the command crontab -e in the terminal. This will open the crontab file in the default editor.

  2. Add a new entry: In the crontab file, add a new entry with the following format:
    minute hour day month weekday command
    For example, to run the backup script every night at 2 AM, you would add the following entry:
    0 2 * * * /usr/local/bin/backup_script.sh
    Here's what each field represents:

  • minute: The minute at which the task should be executed (in this case, 0).

  • hour: The hour at which the task should be executed (in this case, 2).

  • day: The day of the month on which the task should be executed (in this case, *)

  • month: The month of the year on which the task should be executed (in this case, *).

  • weekday: The day of the week on which the task should be executed (in this case, *).

  • command: The command to be executed (in this case, /usr/local/bin/backup_script.sh).

Save and exit: Once we've added the new entry, save and exit the crontab editor. The new entry will take effect immediately.
Now, whenever the cron daemon checks the crontab file, it will execute the backup script at the specified time. You can verify this by checking the crontab file again or by looking at the cron log files (typically located in /var/log/cron).


User Management in Linux

Linux user management is the process of creating, modifying, and deleting user accounts, as well as managing user permissions and access to system resources. Here are some common user management tasks and examples of how to perform them:

  1. Creating a new user account:
    To create a new user account, we can use the useradd command followed by the desired username and password. For example:
sudo useradd pankaj

This will create a new user account named "pankaj" with a default home directory and a random password.

  1. Modifying an existing user account:

To modify an existing user account, we can use the usermod command followed by the appropriate options. For example, to add a user to a specific group, you can use the following command:

sudo usermod -aG admins pankaj

This will add the user "pankaj" to the "admins" group.

  1. Deleting a user account:

To delete a user account, we can use the userdel command followed by the username. For example:

sudo userdel pankaj

This will permanently delete the user account named "pankaj".

  1. Changing a user's password:

To change a user's password, we can use the passwd command followed by the username. For example:

sudo passwd pankaj

This will prompt the user to enter their current password, then enter and confirm a new password.

  1. Managing user permissions:

Linux uses a concept called "permissions" to control access to system resources such as files and directories. We can use the chmod command to set permissions for individual users or groups. For example, to give the user "pankaj" read, write, and execute permissions for the file "/home/pankaj/myfile",we can use the following command:

sudo chmod 755 /home/pankaj/myfile

This will give the user read, write, and execute permissions for the file, and the group and others will have read and execute permissions.

  1. Managing user groups:

Linux also uses groups to assign permissions to multiple users at once. You can use the groups command to list the groups a user belongs to, and the usermod command to add or remove users from groups. For example, to add the user "pankaj" to the group "admins", we can use the following command:

sudo usermod -aG admins pankaj

This will add the user "pankaj" to the group "admins".

Examples of user management tasks in Linux:

Task: Create a new user account named "pankaj" with a password of "password123".

Command:

sudo useradd pankaj password123

Task: Add the user "pankaj" to the group "developers".

Command:

sudo usermod -aG developers pankaj

Task: Change the password for the user "pankaj" to "newPassword123".

Command:

sudo passwd pankaj

Task: Give the user "pankaj" read, write, and execute permissions for the file "/home/pankaj/myfile".Command:

sudo chmod 755 /home/pankaj/myfile

Thank you for reading until here.See you next time.