DEV Community

Kanav Gathe
Kanav Gathe

Posted on • Edited on

1

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!

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay