DEV Community

Kanav Gathe
Kanav Gathe

Posted on

Day 5/90: Advanced Shell Scripting for DevOps Engineers 🛠️ #90DaysOfDevOps

Day 5: Advanced Shell Scripting for DevOps Engineers 🚀

Hello DevOps enthusiasts! 👋 Welcome to Day 5 of the #90DaysOfDevOps challenge. Today, we're diving into advanced shell scripting with directory creation, backup automation, and user management.

Task Solutions 💻

1. Directory Creation Script

#!/bin/bash

# Function to create multiple directories
create_directories() {
    local dirname=$1
    local start=$2
    local end=$3

    for ((i=start; i<=end; i++)); do
        mkdir -p "${dirname}${i}"
    done
    echo "Created directories from ${dirname}${start} to ${dirname}${end}"
}

# Example usage:
# ./createDirectories.sh day 1 90
# Creates day1 through day90
Enter fullscreen mode Exit fullscreen mode

2. Backup Script Implementation

#!/bin/bash

# Function to create backup
backup_workspace() {
    local source_dir=$1
    local backup_dir="backup_$(date +%Y%m%d_%H%M%S)"

    mkdir -p "$backup_dir"
    cp -r "$source_dir"/* "$backup_dir"
    echo "Backup created in: $backup_dir"
}

# Example usage:
# ./backup.sh /path/to/source
Enter fullscreen mode Exit fullscreen mode

3. Crontab for Automated Backup

# Add daily backup at midnight
0 0 * * * /path/to/backup_script.sh

# Check existing crontab
crontab -l

# Add new cron job
(crontab -l 2>/dev/null; echo "0 0 * * * /path/to/backup_script.sh") | crontab -
Enter fullscreen mode Exit fullscreen mode

4. User Management

#!/bin/bash

# Create users
sudo useradd -m user1
sudo useradd -m user2

# Display created users
grep "^user[12]" /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Script Features 🔧

  1. Directory Creation

    • Dynamic directory naming
    • Range-based creation
    • Error handling
  2. Backup System

    • Timestamp-based backups
    • Full directory copying
    • Automatic organization
  3. Automation

    • Crontab integration
    • Scheduled execution
    • Regular backups
  4. User Management

    • User creation
    • System integration
    • Verification steps

Key Takeaways 💡

  • Shell scripts can automate complex tasks
  • Proper timestamp usage is crucial for backups
  • Crontab enables scheduled automation
  • User management requires proper permissions

Bash #DevOps #Automation #Linux #90DaysOfDevOps


This is Day 5 of my #90DaysOfDevOps journey. Keep automating and improving!

Top comments (0)