Advanced Linux Shell Scripting for DevOps Engineers with User Management

Farukh KhanFarukh Khan
4 min read

As DevOps engineers, automation is our best friend. Shell scripting, backups, cron jobs, and user management are essential skills that help us streamline day-to-day operations. In this blog, I’ll walk you through practical tasks that will enhance your DevOps expertise while touching on key concepts that simplify complex workflows.

πŸ“ Task 1: Dynamic Directory Creation with Shell Scripts

Automating routine tasks, like creating directories, is a great way to simplify your work. Let’s create a bash script, createDirectories.sh, to dynamically generate a set of directories based on a given range of numbers.

Shell Script to Create Directories:

#!/bin/bash
# Ensure exactly 3 arguments are passed
if [ $# -ne 3 ]; then
    echo "Usage: $0 <directory_name> <start_number> <end_number>"
    exit 1
fi

# Assign input arguments to variables
dir_name=$1
start=$2
end=$3

# Loop to create directories
for (( i=$start; i<=$end; i++ ))
do
    mkdir "${dir_name}${i}"
    echo "Directory ${dir_name}${i} created"
done

How It Works:

  • This script accepts three arguments: a base directory name, a starting number, and an ending number.

  • It uses a for loop to iterate through the given range and create directories with dynamic names.

  • Make the script executable by running:

       chmod +x createDirectories.sh
    

Example Usage:

./createDirectories.sh day 1 90

This command will create directories named day1, day2, day3... up to day90. Similarly, you can create directories with other names:

./createDirectories.sh Movie 20 50

This creates directories from Movie20 to Movie50. A simple yet powerful script that saves you from repetitive manual tasks! βš™οΈ


πŸ”„Task 2: Automating Backups with a Shell Script

Backups are a critical part of maintaining system integrity in DevOps. Creating an automated backup script ensures that your data is regularly saved without manual intervention.

Backup Script:

#!/bin/bash

# Define source and destination directories
src="/path/to/source/"
dest="/path/to/backup/folder/$(date +%Y-%m-%d_%H-%M-%S)"

# Create a backup folder with a timestamp
mkdir -p "$dest"

# Copy all files to the backup directory
cp -r "$src" "$dest"
echo "Backup completed successfully!"

This script takes the contents from a source folder and backs them up into a destination folder with the current timestamp. This way, each backup has a unique identifier, ensuring easy tracking of previous backups. πŸ—‚οΈ

Make the script executable by running:

  •    chmod +x backupScript.sh
    

⏰Task 3: Scheduling Backups with Cron

Now that we’ve got our backup script ready, we can schedule it using cron, a time-based job scheduler in Unix-like operating systems. With cron, you can automate your backups, ensuring they happen at regular intervals without any manual intervention.

Setting Up Cron for Daily Backups:

  • Open the crontab file to edit:

      crontab -e
    

    Add the following line to schedule the backup script to run every day at 2 AM:

      0 2 * * * /path/to/backupScript.sh
    

This ensures your backup script runs automatically at 2 AM each day, reducing the risk of data loss due to human error. πŸ•’

Here's a quick Cron syntax refresher:

* * * * * command_to_execute
β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚ └─── Day of the week (0 - 7) (Sunday = 0 or 7)
β”‚ β”‚ β”‚ └──────── Month (1 - 12)
β”‚ β”‚ └───────────── Day of the month (1 - 31)
β”‚ └────────────────── Hour (0 - 23)
└─────────────────────── Minute (0 - 59)

πŸ‘₯Task 4: Managing Users in Linux

User management is another essential skill in DevOps. A proper understanding of how to add, manage, and remove users ensures that you can control access to your systems effectively.

Adding Users:

Use the following commands to create two users:

sudo useradd -m devops_ninja
sudo useradd -m code_warrior

To verify the users, you can list all the system users with this command:

cat /etc/passwd | grep 'devops_ninja\|code_warrior'

Why User Management Matters:

Managing users in a system is crucial for security, resource allocation, and access control. Whether you’re administering a small team or managing users across multiple systems, these commands ensure you’re in control.

πŸŽ‰ Wrapping Up

Phew! We've covered a lot of ground today, from bash scripting to user management. Remember, practice makes perfect in the world of DevOps. Keep tinkering, keep learning, and most importantly, keep automating!

Don't forget to share your progress on LinkedIn and join the conversation in the #90DaysOfDevOps Challenge. Let's grow together as a community! πŸ’ͺ

Happy DevOps-ing, and until next time, may your pipelines be green and your deployments smooth! πŸš€

0
Subscribe to my newsletter

Read articles from Farukh Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Farukh Khan
Farukh Khan