Files
Midori-Obsidian/🌸 Midori's Linux Dojo/Phase 6 - Automation/Exercises.md
2026-05-22 01:59:08 +02:00

1.9 KiB

🧪 Phase 6: Automation

Goal: Real admin skills — scripting, cron, text processing


🎯 Exercise 1 — Shell Scripting

# Create a backup script
cat > /root/lab/scripts/backup.sh << 'EOF'
#!/bin/sh
BACKUP_DIR="/root/backups/$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/lab-backup.tar.gz" /root/lab/
echo "Backup saved to $BACKUP_DIR"
EOF

chmod +x /root/lab/scripts/backup.sh
./root/lab/scripts/backup.sh

🎯 Exercise 2 — Variables & Loops

#!/bin/sh
# Variables
NAME="World"
echo "Hello, $NAME!"

# For loop
for i in 1 2 3 4 5; do
    echo "Count: $i"
done

# While loop
COUNT=0
while [ $COUNT -lt 3 ]; do
    echo "Loop $COUNT"
    COUNT=$((COUNT + 1))
done

# Conditionals
if [ -f /root/hello.txt ]; then
    echo "File exists!"
else
    echo "File not found"
fi

🎯 Exercise 3 — Cron Jobs

# Edit crontab
crontab -e

# Add a job that runs every hour:
0 * * * * /root/lab/scripts/backup.sh

# List cron jobs
crontab -l

# Check cron logs
cat /var/log/cron

🎯 Exercise 4 — Text Processing with grep/awk/sed

# grep — search
grep "root" /etc/passwd
grep -r "alpine" /etc/

# awk — column extraction
awk -F: '{print $1, $6}' /etc/passwd
df -h | awk '{print $5, $6}'

# sed — search & replace
sed 's/root/admin/' /etc/passwd > /tmp/test.txt
head -5 /tmp/test.txt

🎯 Final Project

Create a single script that automates the entire setup of both lab VMs:

1. Installs and configures SSH
2. Sets up static IPs
3. Configures firewall rules
4. Installs and starts a web server
5. Sets up NFS share
6. Creates users
7. Schedules backups with cron

Phase 6 Checklist

  • Shell scripts with variables, loops, conditionals
  • Cron jobs for automation
  • Text processing with grep/awk/sed
  • Final project: full automation script

Previous: Phase 5 - Server Stuff