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

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

ยท

8 min read

๐ŸŒŸ Introduction ๐ŸŒŸ

Welcome to Day 5 of the #90DaysOfDevOps challenge! Today, we'll dive deep into advanced Linux shell scripting, where we'll focus on essential topics like Creating Dynamic Directories, Automated Backup scripts, Automating the Backup Script with Cron, User Management in Linux, and Creating and Displaying Usernames. As DevOps engineers, mastering shell scripting is vital as it empowers us to automate tasks and optimize processes. ๐Ÿ’ป๐Ÿš€

๐Ÿ“ Creating Dynamic Directories

Before we dive into the script, let's first understand how to create dynamic directories using a simple command in Linux: mkdir day{1..N}

The above command will create N directories named day1, day2, day3, and so on, up to dayN. This demonstrates a quick and efficient way to create multiple directories with sequential names.

๐Ÿ“ Creating Dynamic using Shell Scripting Directories

Let's create a user-friendly shell script named task1.sh that generates directories with dynamic names.

#!/bin/bash

# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi

# Extract arguments
directory_name="$1"
start_number="$2"
end_number="$3"

# Check if start_number and end_number are integers
if ! [[ "$start_number" =~ ^[0-9]+$ ]] || ! [[ "$end_number" =~ ^[0-9]+$ ]]; then
  echo "Error: Start and end numbers must be integers."
  exit 1
fi # we can also skip this part but it is good practice to do so

# Loop to create directories
for ((i=start_number; i<=end_number; i++)); do
  directory="${directory_name}${i}"
  mkdir "$directory"
done

echo "Directories created successfully."

this can also be used to check the condition whether the numbers are integers or not

if [[ "$start_number" =~ ^[0-9]+$ && "$end_number" =~ ^[0-9]+$ ]]; then
  # Loop to create directories
  for ((i=start_number; i<=end_number; i++)); do
    directory="${directory_name}${i}"
    mkdir "$directory"
  done
else
  echo "Error: Start and end numbers must be integers."
  exit 1
fi
  1. Shebang (#!/bin/bash): The first line of the script starts with #!/bin/bash. It's called a shebang, and it tells the system that the script should be interpreted and executed using the Bash shell.

  2. Checking Arguments: The script begins by checking if it received exactly three arguments when it was executed. Arguments are pieces of information that you provide when running a script. In this case, the script expects three arguments: the directory name, the start number, and the end number of directories to create.

  3. Extracting Arguments: If the script receives three arguments, it proceeds to extract those arguments into separate variables for later use. For example, if you run the script with ./createDirectories.sh day 1 90, the variables will be set as follows:

    • directory_name: "day"

    • start_number: 1

    • end_number: 90

  4. Checking Integer Input: The script then checks whether the start_number and end_number are valid integers. It ensures that they contain only digits (0-9) and no other characters. If either of the numbers is not an integer, the script will display an error message and exit.

  5. Loop to Create Directories: Once the input is validated, the script enters a loop. This loop will run from the start_number to the end_number (both inclusive) to create the desired number of directories. Inside the loop, it constructs the directory name by combining the directory_name and the loop index.

  6. Creating Directories: Inside the loop, for each iteration, the script creates a new directory with the constructed name. For example, if the loop index is 1, it creates a directory named "day1"; if the index is 2, it creates "day2", and so on, up to the specified end_number.

  7. Printing Success Message: After the loop completes, the script prints a success message to indicate that all the directories have been created successfully.

  8. Running the Script: To run the script, you make it executable using the chmod 700 createDirectories.sh command. This gives permission to execute the script. Then, you run the script by providing the directory name and the range of numbers you want to create directories for, like ./createDirectories.sh day 1 10.

๐Ÿ’พ Automated Backup Script

Let's create a script that will do system backups by compressing and archiving a source directory to a target location. It automates data safety, ensuring recovery options in case of data loss or system failures. Users can conveniently back up important files and directories with ease.

#!/bin/bash

# Define source and target directories
src=/home/ubuntu/scripts
trg=/home/ubuntu/backups

# 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 Bash script is designed to create a backup of the files in the "home/ubuntu/scripts" directory. It generates a timestamp to form a unique filename for the backup and saves it in the "home/ubuntu/backups" directory. The user is informed about the backup process with echo statements displaying emojis representing a folder (๐Ÿ“‚) and a clock (๐Ÿ•). The tar command is then used to compress and create the backup archive, and once the backup is complete, the user is notified with a message

๐Ÿ•ฐ๏ธ Understanding Cron and Crontab to Automate Your Backup Script ๐Ÿ”„๐Ÿ“

Cron is like a timekeeping wizard ๐Ÿ”ฎ that helps your computer perform tasks at scheduled intervals, like clockwork โฐ. Crontab (CRON TABle) is its trusty assistant, the one who writes down the schedule for Cron to follow ๐Ÿ—’๏ธ. Together, they automate the backup process, making it hassle-free! ๐Ÿš€

Cron ๐Ÿ•ฐ๏ธ: Cron is a magical ๐Ÿช„ Unix-based utility that runs in the background of your computer, keeping an eye on the clock โŒš. It's like having a personal assistant who can handle repetitive tasks without any reminders!

Crontab ๐Ÿ“…: Crontab is like Cron's planner ๐Ÿ“…. It's a text file where you jot down the to-do list, telling Cron when and how often to perform a specific task. Each line in the Crontab represents a different task and its schedule.

Automating with Cron and Crontab ๐Ÿ”„๐Ÿ—’๏ธ: Now, let's schedule our backup script using Cron and Crontab!

  1. Open the Crontab file by typing crontab -e in the terminal. ๐Ÿ–Š๏ธ

  2. You'll see a text editor where you can add your backup task. Each line in Crontab has a specific format:

    * * * * * command_to_be_executed

    The five * represent the schedule for minute, hour, day of the month, month, and day of the week, respectively. Use numbers to specify the exact time for each.

  3. To run the backup script every day at 3 AM, add this line to Crontab: ๐ŸŒ…

      0 3 * * * /path/to/your/backup_script.sh
    

    Here, 0 represents the minute (0-59) and 3 represents the hour (0-23).

  4. Save the Crontab and exit the editor.

That's it! ๐ŸŽ‰ Now, your backup script will automatically run every day at 3 AM ๐ŸŒŸ, making sure your precious files stay safe and sound.

Cron and Crontab may sound intimidating, but they are your loyal timekeepers, ready to automate repetitive tasks ๐Ÿค–โฐ. With our simple backup script and a touch of Cron magic, you can have peace of mind knowing your files are protected! ๐Ÿ’พ๐Ÿ”’ So go ahead and automate your backup process like a tech pro! ๐Ÿ’ช๐Ÿ‘ฉโ€๐Ÿ’ป

๐Ÿ‘ค User Management in Linux ๐Ÿ‘ค

In a Linux operating system, a user is like a unique character with special powers! ๐Ÿ˜Ž They can work with files and do many other cool things. Each user has a special ID that makes them one of a kind in the OS. ๐Ÿ†”

๐Ÿ” User ID Range: After installing the OS, the first user is super special โ€“ we call them the "root user" ๐Ÿ‘‘ โ€“ and they have the ID 0. Then there are some other special users with IDs from 1 to 999. These users help the system run smoothly. But the real party starts after ID 999! That's where local users come in, and their IDs start from 1000 and go up. ๐ŸŽ‰

๐Ÿ“ Commands for User Info: Now, let's talk about some commands to know more about our users! ๐Ÿ•ต๏ธโ€โ™‚๏ธ

  1. whoami: This command reveals the name of the user who is currently using the system. It's like asking "Who am I?" and getting an answer! ๐Ÿค”

  2. id: When you want to know your special ID, just type this command! It shows both the user and group IDs. ๐Ÿ†”๐Ÿ‘ฅ

  3. cat /etc/passwd: Don't worry; there's no cat involved! ๐Ÿ˜บ This command spills the beans about all the users on the system, including their IDs and other info.

  4. cat /etc/group: Similar to the previous one, but this time, it's all about the groups. It tells you which users are part of each group. ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘

  5. To create a user, use the "useradd" command. For example:

      sudo useradd username
    

    ๐Ÿ—‘๏ธ To delete a user, use the "userdel" command. For example:

      sudo userdel username
    

    โœ๏ธ To modify user properties, such as their password or home directory, use the "usermod" command. For example:

      sudo usermod -d /new/home/directory username
    

    ๐Ÿ“„ To display user information, use the "id" or "finger" command. For example:

      id username
      finger username
    

    Remember to run these commands with administrative privileges using "sudo" to ensure proper user management. ๐Ÿ›ก๏ธ๐Ÿ”‘

๐Ÿ›ก๏ธCreating and Displaying Usernames ๐Ÿ‘€

To create users and display their names, follow these simple steps:

๐Ÿ–ฅ๏ธ Open a terminal or command prompt.

โœจ Run the following command to create the first user:

sudo adduser user1

Fill in the required details as prompted.

๐Ÿ”„ Repeat the previous step to create the second user:

sudo adduser user2

Again, provide the necessary information.

๐Ÿ“‹ To display the usernames, use this command:

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

This command fetches the usernames from the /etc/passwd file.

By learning these user management commands, you can efficiently handle user accounts on Linux systems. ๐Ÿš€๐Ÿ”ง

๐ŸŽ‰ Conclusion ๐ŸŽ‰

๐ŸŽ‰ Congratulations on successfully completing Day 5 of the #90DaysOfDevOps challenge!

๐ŸŒŸ Today's topics included dynamic directories, automated backup scripts, and user management in Linux. You've learned valuable skills to structure data efficiently, ensure data safety with automated backups, and manage users effectively. Keep up the excellent work and stay committed to your DevOps journey. Each day brings new opportunities for growth, so embrace the challenges ahead with enthusiasm. Together, we'll continue this exciting adventure of becoming proficient DevOps professionals! ๐Ÿš€๐Ÿ’ป๐Ÿ”ง๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ“š Happy DevOps-ing! ๐ŸŽ‰๐Ÿš€

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps ๐Ÿš€ by following me on Hashnode, LinkedIn linkedin.com/in/sahil-kamble-40898b208

Thank you for reading! ๐Ÿ™ Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

Did you find this article valuable?

Support Sahil Kamble's blog by becoming a sponsor. Any amount is appreciated!

ย