Advanced Linux Shell Scripting for DevOps Engineers with User Management

Advanced Linux Shell Scripting for DevOps Engineers with User Management

Script 1: Shell script to create multiple directories with dynamic names:

Let's write a bash script with three given arguments (one is "the directory name", the second is "the start number of directories" and the third is "the end number of directories") which when executed, creates a specified number of directories with a dynamic directory name.

This is a Bash script that creates a specified number of directories with dynamic names based on a prefix and a range of numbers.

Here is a breakdown of what the script does:

  1. The script checks if there are exactly three command line arguments provided. If not, it displays a usage message and exits with an error code of 1.

  2. The three command line arguments are assigned to variables dir_name, start_number, and end_number.

  3. The script uses a for loop to create directories with dynamic names that include the prefix specified by dir_name and a number within the range specified by start_number and end_number.

  4. The mkdir command is used to create directories with the -p option to create intermediate directories as needed.

  5. After all directories have been created, the script prints a message to the console indicating that the directories were created successfully.

Overall, this script is a useful tool for creating a large number of directories with dynamic names based on a range of numbers.

Output with the command:

./createdirectories.sh MyFolder 234 240

Script 2: Shell script to backup specific files or directories

The above bash script will help us create a compressed backup archive of a specified directory and save it in a specified backup directory.

  1. The source directory is defined as /home/ubuntu/advscripts.

  2. The backup directory is defined as /home/ubuntu/backup.

  3. The backup filename will be defined as backup_$(date "+%Y-%m-%d-%H-%M-%S").tar.gz.

  4. The tar command is used to create a compressed backup archive of the source directory. The -c option specifies that a new archive should be created, -z specifies that the archive should be compressed using gzip, and -f specifies the filename of the archive.

  5. The script will print a message to the console indicating where the backup file is created, using the echo command.

Output with the command:

./backup.sh

What are Cron and Crontab?

Cron is a utility that allows users to schedule commands or scripts to run automatically at specified intervals or times. It is widely used for automating repetitive tasks such as backups, log rotation, system monitoring, and more.

The cron utility reads a file called the crontab which contains a list of commands or scripts to be executed and the schedule on which they should be run. The crontab file is edited using the crontab command, which opens the user's crontab file in a text editor for modification.

The syntax of a crontab entry includes fields for the minute, hour, day of the month, month, and day of the week on which the command or script should be run. The schedule can be specified using either numeric values or special characters that represent a range of values or intervals. For example, * represents all possible values, / represents a step value, and , represents a list of values.

Cron is a powerful tool for automating repetitive tasks and is widely used in server administration, system maintenance, and other IT-related tasks.

Example:

The above crontab entry will execute the echo command every day at 5:50 PM and redirect the output to the /home/ubuntu/test_cron_first.txt file. Specifically, it will write the text "Cron Job by Amrendra" to the file.

Let's create a crontab to backup a directory periodically

Step 1: Create a bash script which can take a backup and save it to a certain directory.

Step 2: Create a crontab using the below command

crontab -e

The above crontab entry will run the backup.sh script located at /home/ubuntu/advscripts/ every day at 6:40 PM (18:40 in 24-hour time format) using the bash shell.

Output:

User Management System in Linux

User management in Linux involves creating, modifying, and deleting user accounts. The following are some common commands that can be used for user management:

  1. adduser: This command is used to add a new user to the system.
  1. usermod: This command is used to modify an existing user account.
  1. passwd: This command is used to change the password of an existing user.
  1. deluser: This command is used to delete an existing user account.

These commands are just a few examples of what can be done with user management in Linux. There are many more commands and options available, depending on your specific needs. It's important to note that some of these commands require root or sudo privileges, so be sure to use them carefully.

Let's add 2 users:

Output:

To check if the users are created or not, run the command: cat /etc/passwd as the users' information is always saved in /etc/passwd

It shows that our users are created and along with that 2 different groups are also created with the same respected names.