Skip to main content

Command Palette

Search for a command to run...

Advanced Linux Shell Scripting for DevOps Engineers with User management

Updated
2 min readView as Markdown
Advanced Linux Shell Scripting for DevOps Engineers with User management

In the fast-paced world of DevOps, ensuring data integrity, streamlining processes, and managing user access are critical aspects of maintaining a stable and efficient environment. In this blog post, we'll delve into three essential tasks that every DevOps engineer should be familiar with creating directories using scripts, implementing automated backups, and managing user access.

Creating Directories Using a Script

Purpose and Importance

Creating directories manually can be time-consuming and error-prone. Automation reduces the likelihood of human errors and enhances productivity, which are key principles of DevOps.

The Script

The following bash script, createDirectories.sh, automates the creation of multiple directories with dynamic names:

#!/bin/bash

directoriesname=$1
start=$2
end=$3

for ((n=start; n<=end; n++));
do
   mkdir "${directoriesname}${n}"
done

echo "Directories created: ${directoriesname}{${start}..${end}}"

How to Run

Execute the script with three arguments: directory name, start number, and end number

./createDirectories.sh day 1 90

Backup Script

Purpose and Importance

Backups are the backbone of disaster recovery plans in DevOps. They ensure data continuity and safeguard against data loss.

The Script

The bash script backup_script.sh creates compressed backups of a source directory:

bashCopy code#!/bin/bash

source_dir="/path/to/your/source/directory"
backup_dir="/path/to/backup/directory"

timestamp=$(date +%Y%m%d%H%M%S)
backup_file="backup_$timestamp.tar.gz"

mkdir -p "$backup_dir"
tar -czf "$backup_dir/$backup_file" -C "$source_dir" .

echo "Backup completed: $backup_dir/$backup_file"

How to Run

Run the script to create backups:

./backup_script.sh

Automating Backup Using Cron

Purpose and Importance

Automation is a core DevOps principle. Scheduling tasks like backups using cron jobs reduces manual intervention and ensures consistent execution.

Setting Up Cron Job

Open the crontab editor:

bashCopy codecrontab -e

Add a line to run the backup script daily at a specific time (e.g., 3 AM):

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

Creating Users and Displaying Usernames

Purpose and Importance

User management is crucial for maintaining security and access control in DevOps environments.

Commands

To create users and display their usernames:

bashCopy codesudo useradd user1
sudo useradd user2
echo "Usernames: user1 user2"

Conclusion

In the dynamic world of DevOps, mastering backup strategies, automation techniques, and user management is non-negotiable. These tasks ensure data resilience, streamline processes and enhance security. By automating mundane tasks, you free up valuable time for innovation and problem-solving. Understanding these fundamental tasks equips DevOps engineers with the tools they need to succeed in a complex IT landscape.


More from this blog

DevOps Hustlers

56 posts