# ๐Ÿงช Phase 6: Automation > Goal: Real admin skills โ€” scripting, cron, text processing --- ## ๐ŸŽฏ Exercise 1 โ€” Shell Scripting ```bash # 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 ```bash #!/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 ```bash # 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 ```bash # 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: ```bash 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]]