0Pricing
DevOps Bootcamp · Lesson

Automating System Administration

Apply Bash scripting to automate common system administration tasks, such as backups, log rotation, and monitoring.

Automating System Administration is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro to Sysadmin Automation

Welcome! In this lesson, we'll dive into automating common system administration tasks using Bash scripts. This is where your scripting skills truly shine!

Automation helps make system management more efficient, consistent, and less prone to human error. It's a key skill for any developer or IT professional.

Why Automate Sysadmin Tasks?

Automating repetitive tasks offers significant benefits:

  • Efficiency: Tasks run faster and without manual intervention.
  • Consistency: Ensures tasks are performed the same way every time, reducing errors.
  • Reliability: Scripts can be scheduled to run reliably, even when you're not around.
  • Scalability: Easily apply the same operations across many systems.

Common Automation Targets

Many system administration tasks are perfect candidates for automation:

  • Backups: Regularly copying important data.
  • Log Management: Cleaning up old logs or rotating them.
  • System Monitoring: Checking disk space, CPU, memory usage.
  • User Management: Creating or deleting user accounts.
  • Software Updates: Keeping packages up-to-date.

Scripting a Simple Backup

One of the most critical tasks is backing up data. A basic backup script can copy specific files or directories to a safe location, often with a timestamp.

We can use the cp command along with the date command to create unique backup directories.

Run: Daily Data Backup

This script creates a timestamped backup of a 'my_data' directory into a 'backups' folder. Remember to replace /path/to/my_data with a real directory on your system to test it.

#!/bin/bash

SOURCE_DIR="/tmp/my_data"
BACKUP_DIR="/tmp/backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

mkdir -p "$SOURCE_DIR"
echo "Hello from CoddyKit!" > "$SOURCE_DIR/file1.txt"
echo "More data." > "$SOURCE_DIR/file2.txt"

mkdir -p "$BACKUP_DIR"

# Perform the backup
cp -r "$SOURCE_DIR" "$BACKUP_DIR/backup_$TIMESTAMP"

echo "Backup created at: $BACKUP_DIR/backup_$TIMESTAMP"

Automating Log Cleanup

Log files grow quickly and can consume significant disk space. Automating their cleanup or rotation is essential for system health.

We can use the find command to locate old log files and delete them, or move them to an archive.

Run: Old Log Remover

This script deletes log files older than 7 days from a specified directory. Be careful when using rm in real scripts!

#!/bin/bash

LOG_DIR="/tmp/app_logs"

mkdir -p "$LOG_DIR"
touch "$LOG_DIR/today.log"
touch -d "8 days ago" "$LOG_DIR/old_log_1.log"
touch -d "5 days ago" "$LOG_DIR/recent.log"
touch -d "10 days ago" "$LOG_DIR/old_log_2.log"

echo "Logs before cleanup:"
ls -l "$LOG_DIR"

# Find and delete log files older than 7 days
find "$LOG_DIR" -type f -name "*.log" -mtime +7 -delete

echo "\nLogs after cleanup:"
ls -l "$LOG_DIR"

Monitoring Disk Usage

Monitoring system resources like disk space is crucial to prevent outages. A script can periodically check usage and alert you if it exceeds a threshold.

The df command reports filesystem disk space usage. We can parse its output to get just the percentage.

Run: Disk Space Alert

This script checks the disk usage of the root filesystem (/) and prints a warning if it's over 80%. You can modify the THRESHOLD.

#!/bin/bash

THRESHOLD=80

# Get disk usage percentage for root filesystem
USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')

# Check if usage exceeds threshold
if (( USAGE > THRESHOLD )); then
  echo "WARNING: Disk usage on / is at ${USAGE}%!"
else
  echo "Disk usage on / is ${USAGE}%. All good!"
fi

Best Practices for Sysadmin Scripts

When writing automation scripts, consider these best practices:

  • Error Handling: Use set -e to exit on error and add explicit error checks.
  • Logging: Redirect script output to log files for auditing and debugging.
  • Idempotency: Design scripts so running them multiple times has the same effect as running once.
  • Configuration: Use variables or separate config files for easily changeable values.
  • Permissions: Ensure scripts have correct execution permissions (chmod +x).

Automating Sysadmin Check

Which of the following tasks are good candidates for automation using Bash scripts in system administration?

Recap: Automating Sysadmin

Great job! You've learned how Bash scripting can transform system administration tasks.

We covered automating backups, log cleanup, and disk usage monitoring. Remember to apply best practices like error handling and logging to create robust and reliable automation scripts.

These skills are invaluable for maintaining healthy and efficient systems!

Frequently asked questions

Is the “Automating System Administration” lesson free?

Yes — the full text of “Automating System Administration” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Automating System Administration”?

Apply Bash scripting to automate common system administration tasks, such as backups, log rotation, and monitoring. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Automating System Administration” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Scheduling Tasks with Cron
  2. Automating System Administration
  3. Advanced Scripting Project
  4. Automated Log Rotation & Cleanup
← Back to DevOps Bootcamp