Advance Shell Scripting Used in DevOps with User management-Day-5

Advance Shell Scripting Used in DevOps with User management-Day-5

In This Article We Will See Below Tasks on Shell Scripting

  1. You have to do the same using Shell Script i.e using Loops with start day and end day variables using arguments.

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

  3. About Cron and Crontab, to automate the backup Script.

  4. About User Management.

  5. Create 2 users and just display their Usernames.

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

In the below script, we have used 'for 'loop to satisfy the above situation

Script : when the script is executed as-

./directories.sh day 1 90

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

vim directories.sh
#!/bin/bash
name=$1
start_name=$2
end_name=$3
for (( i=start_name; i<=end_name; i++))
do mkdir "$name$i"
done

output :

Remove all created directories using script:

vim directories.sh
#!/bin/bash
name=$1
start_name=$2
end_name=$3
for (( i=start_name; i<=end_name; i++))
do rm -rf "$name$i"
done

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

Script :

#!/bin/bash
src_dir=/home/ubuntu/
tgt_dir=/home/ubuntu/kunja-backup
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S") backup_file=$tgt_dir/$curr_timestamp.tgz
echo "Taking backup on $curr_timestamp"
tar -cvzf $backup_file --absolute-names $src_dir
echo "Backup complete"
  • It prompts the user with a message "Do you want to create a backup?" using the "echo" command and reads the user's response using the "read" command, which stores the response in the "answer" variable.

  • It then echoes back the user's response with the message "as your response is $answer, we will take backup".

  • It takes the path to the file or directory to be backed up as an argument passed to the script, which is stored in the "path" variable.

  • Finally, it creates a tar archive of the specified file or directory named "backup.tar.gz" using the "tar" command with the option "-cvzf".

Output :

3.Cron and Crontab to automate the backup

Script.

Cron and Crontab :

Cron- cron is the general name for the service that runs scheduled actions. crond is the name of the daemon that runs in the background and reads crontab files.

Crontab- Crontab is a command that is found in the Linux Operating system to Schedule tasks(i.e., shell commands or scripts) to be run at specific times or intervals. Crontab stands for “cron table, ” because it uses the job scheduler cron to execute tasks.

Some steps are written below:

  1. Create the backup script: First, create the backup script that you want to automate. For example, let's create a script called "backup. sh" that backs up a specific directory:

  2. Schedule the backup script with Cron: To schedule this script to run automatically using "cron", follow these steps:

    • Open the crontab editor by running the command "crontab -e" in your terminal.

    • If prompted to select an editor, choose your preferred text editor.

      Add a new line to the crontab file with the following syntax:

The string 0 2 * is a cron expression that specifies when to run the command. In this case, it means "run the command at 2:00 AM every day".

0: The minute when the command should run (in this case, "0" means "at the start of the hour").

2: The hour when the command should run (in this case, "2" means "2:00 AM"). : The day of the month when the command should run (in this case, "" means "every day"). : The month when the command should run (in this case, "" means "every month").

: The day of the week when the command should run (in this case, "" means "every day of the week"). The command that follows the cron expression is /home/ubuntu/backup.sh /home/ubuntu/home/ubuntu /backup_directory. This means that the script located at /home/ubuntu/backup.sh will be executed, with /home/ubuntu as the source directory to be backed up and /home/ubuntu/backup_directory as the destination directory where the backup will be stored.

So the overall meaning of this cron job is to run the backup. sh script at 2:00 AM every day, using the specified source and destination directories.

4.User Management :

User management is a process to create, modify, and delete users in a Linux system. If we create a user in a Linux system, we have to make sure that the user name is not starting with a capital letter and only with sudo permission we are allowed to perform the activity like creating, modifying or deleting users.

adduser : to add a user to the system

$sudo adduser kunja

userdel : to delete a user from the system

usermod : to modify the user

passwd: The "passwd" command is used to set or change a user's password.

Here are some common options for the "passwd" command:

->"-l": Lock the user's account.

->"-u": Unlock the user's account.

->"-d": Remove the user's password.

5.Create 2 users and just display their Usernames.

command :

ubuntu@kbjena:~$ adduser kunja raju
adduser: Only root may add a user or group to the system.
ubuntu@kbjena:~$

If you are an Ubuntu user and do not have permission to add a new user you can see it as a user, I don't have permission to create a user because I don't have the power of Superman, like a super user called root.

To edit the visudo file, you need to have administrative privileges on the system. This means you must be a user with the sudoer's permissions or the root user.

Assuming you have the necessary permissions, you can edit the visudo file by following these steps:

  1. Open the terminal on your system.

  2. Type the following command and press Enter:

    sudo visudo

  3. Make the necessary changes to the file. Be very careful while editing this file, as incorrect changes can cause serious problems with your system.

  4. Once you have made the necessary changes, save the file and exit the editor.

# User alias specification
# Cmnd alias specification
# User privilege specification
root    ALL=(ALL:ALL) ALL
ubuntu  ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
%kunja  ALL=(ALL:ALL) NOPASSWD:ALL

Now , try to create the user via ubuntu user.

ubuntu@kbjena:~$ sudo adduser bihari-testing
Adding user `bihari-testing' ...
Adding new group `bihari-testing' (1003) ...
Adding new user `bihari-testing' (1002) with group `bihari-testing' ...
Creating home directory `/home/bihari-testing' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for bihari-testing
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] Y
ubuntu@kbjena:~$

Optionally, you can add the new user to any additional groups, such as the sudo group, by running the following command:

usermod -aG groupname username

Now, If anyone wants to see all users:

Replace "groupname" with the name of the group you want to add the user to, and "username" with the name of the user you just created.

ubuntu@kbjena:~$ getent passwd
# other way to see all users name
ubuntu@kbjena:~$ cat /etc/passwd