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
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
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 -
4. User Management
#!/bin/bash
# Create users
sudo useradd -m user1
sudo useradd -m user2
# Display created users
grep "^user[12]" /etc/passwd
Script Features 🔧
-
Directory Creation
- Dynamic directory naming
- Range-based creation
- Error handling
-
Backup System
- Timestamp-based backups
- Full directory copying
- Automatic organization
-
Automation
- Crontab integration
- Scheduled execution
- Regular backups
-
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!