Tuesday, April 2, 2024

13 Essential Shell Scripts for System Administration: Automate Tasks and Boost Productivity

13 Essential Shell Scripts for System Administration: Automate Tasks and Boost Productivity


1. Backup Script

A basic backup script can show how to automate the process of copying files from one location to another, perhaps for a nightly backup.


#!/bin/bash

# Simple backup script

SOURCE="/home/user/Documents"

DESTINATION="/home/user/Backup"

date=$(date '+%Y-%m-%d')

backup_file="backup-$date.tar.gz"

# Create a backup

tar -czvf $DESTINATION/$backup_file $SOURCE

echo "Backup of $SOURCE completed successfully!"

This script uses tar to create a compressed archive of a Documents directory and saves it with a timestamp in a Backup directory. It’s a practical example of using variables, basic commands, and date manipulation.


2. System Update Script

This script can automate the process of updating the system packages on a Linux machine, showcasing the use of system commands within a script.


#!/bin/bash

# Update system script

echo "Updating system packages..."

sudo apt update && sudo apt upgrade -y

echo "System update completed!"

It’s a straightforward script that calls apt (the package manager) to update the list of available packages and then upgrades them. This demonstrates executing system commands and chaining commands with && for efficiency.


3. Directory Cleanup Script

A script to clean up a specific directory by removing files older than a certain number of days can illustrate working with find and command substitution.


#!/bin/bash

# Cleanup script for old files

DIRECTORY="/home/user/tmp"

DAYS=30

# Delete files older than 30 days

find $DIRECTORY -type f -mtime +$DAYS -exec rm -f {} \;

echo "Cleanup of $DIRECTORY completed!"

This example uses find to locate and delete files older than 30 days in a temporary directory, showcasing file system management and the execution of commands on files found by find.


4. Disk Usage Alert Script

A script that checks the disk usage and sends an alert if it exceeds a threshold percentage is useful for monitoring system health.


#!/bin/bash

# Disk usage alert script

THRESHOLD=90

PARTITION="/"

current_usage=$(df "$PARTITION" | grep / | awk '{ print $5 }' | sed 's/%//g')

if [ "$current_usage" -gt "$THRESHOLD" ]; then

  echo "Disk usage on $PARTITION is above $THRESHOLD%. Current usage is $current_usage%."

  # Here you could add a command to send an email or a notification.

fi

This script uses df to check disk usage, processes the output with awk, and checks if the usage exceeds a threshold. It’s a practical example of monitoring and alerts.


5. Log Analysis Script

This script can scan a log file for a specific pattern, such as "error", and output the results into another file. It's useful for monitoring applications or system logs.


#!/bin/bash

# Log analysis script

LOG_FILE="/var/log/application.log"

PATTERN="error"

OUTPUT_FILE="/home/user/error_log_analysis.txt"

grep -i $PATTERN $LOG_FILE > $OUTPUT_FILE

echo "Log analysis completed. Errors stored in $OUTPUT_FILE"

This script uses grep to search for a case-insensitive pattern in a log file, demonstrating file manipulation and pattern matching.


6. Batch Image Resizing Script

A script to batch resize images in a directory showcases working with files and utilizing external tools (imagemagick in this case).


#!/bin/bash

# Batch image resize script

DIRECTORY="/home/user/images"

OUTPUT_DIRECTORY="/home/user/resized_images"

WIDTH=800

mkdir -p $OUTPUT_DIRECTORY

for image in $DIRECTORY/*.jpg; do

  filename=$(basename $image)

  convert $image -resize $WIDTH $OUTPUT_DIRECTORY/$filename

done

echo "All images resized to width $WIDTH pixels."

This script iterates through all .jpg files in a directory, resizes them to a specified width using convert from ImageMagick, and saves them in an output directory. It’s a practical example of batch processing and using external utilities.


7. User Creation Script

This script can be used to create a new user on a system, setting a default password, and forcing a password change on first login. It’s useful for sysadmins.


#!/bin/bash

# User creation script

USERNAME=$1

PASSWORD="defaultPassword"

COMMENT="New user"

if [ $(id -u) -eq 0 ]; then

  useradd -c "$COMMENT" -m -p $(openssl passwd -1 $PASSWORD) $USERNAME

  chage -d 0 $USERNAME

  echo "User $USERNAME has been added to system!"

else

  echo "Only root can add a user to the system."

  exit 2

fi

This script takes a username as an argument, creates a new user with a default password, and forces a password reset upon first login. It highlights argument usage, conditionals, and security practices.


8. Network Check Script

A script that checks if your computer can access the internet by pinging a reliable host, such as Google's public DNS server, and reports back.


#!/bin/bash

# Network check script


TARGET="8.8.8.8"

if ping -c 1 $TARGET &> /dev/null; then

  echo "Network is up."

else

  echo "Network is down."

fi

This simple script uses ping to check network connectivity. It’s an example of network scripting and handling command output.


9. Simple Backup with Logging

This script creates a backup of a directory and logs the operation, demonstrating file manipulation, logging, and date formatting.


#!/bin/bash

# Simple directory backup with logging

SOURCE_DIR="/home/user/my_project"

BACKUP_DIR="/home/user/my_project_backup"

LOG_FILE="/home/user/backup_log.txt"

DATE=$(date '+%Y-%m-%d_%H-%M-%S')

tar -czf $BACKUP_DIR/backup_$DATE.tar.gz $SOURCE_DIR

echo "Backup created for $SOURCE_DIR at $DATE" >> $LOG_FILE

Explain that this script packages the specified source directory into a compressed archive, names it with a timestamp for uniqueness, and logs the operation to a file.


10. Checking Disk Space and Sending an Email

A script that checks for disk space usage and sends an email if it exceeds a certain threshold, showcasing conditional logic and external command integration.


#!/bin/bash

# Check disk space and send email if usage is high

THRESHOLD=90

PARTITION="/"

EMAIL="user@example.com"

USAGE=$(df -h | grep $PARTITION | awk '{ print $5 }' | sed 's/%//')

if [ $USAGE -gt $THRESHOLD ]; then

  echo "Disk usage is above threshold" | mail -s "Disk Space Alert" $EMAIL

fi

This script uses df to get the disk usage percentage, grep to find the specific partition, and awk to extract the usage value. It sends an email alert if the usage exceeds the threshold.


11. Simple Web Server Status Check

A script to check if a web server is running on localhost and restart it if not. This can introduce basic networking commands and service management.


#!/bin/bash

# Check web server status and restart if down

if ! curl -s http://localhost &> /dev/null; then

  echo "Web server down. Attempting restart."

  sudo systemctl restart apache2

else

  echo "Web server is running."

fi

Explain that curl is used to silently check the server's status. If it fails (meaning the server is down), the script attempts to restart the server using systemctl.


12. Monitor File Changes and Notify

A script to monitor changes in a directory and notify the user, useful for understanding file system monitoring and notification mechanisms.


#!/bin/bash

# Monitor directory for changes and notify

DIRECTORY="/home/user/watched_dir"

LOG_FILE="/home/user/directory_changes.log"

inotifywait -m -e create,delete,modify $DIRECTORY &> $LOG_FILE &

echo "Monitoring changes in $DIRECTORY. See $LOG_FILE for logs."

This utilizes inotifywait from the inotify-tools package to monitor specified events (create, delete, modify) in a directory, logging them asynchronously.


13. Batch Convert Markdown to HTML

A script for converting multiple Markdown files in a directory to HTML, illustrating the use of loops, file manipulation, and utilizing external tools.


#!/bin/bash

# Batch convert Markdown files in a directory to HTML

SOURCE_DIR="/home/user/markdown_files"

OUTPUT_DIR="/home/user/html_files"

mkdir -p $OUTPUT_DIR

for file in $SOURCE_DIR/*.md; do

  pandoc "$file" -o "${OUTPUT_DIR}/$(basename "${file%.*}").html"

done

echo "Conversion complete. HTML files are in $OUTPUT_DIR."

Discuss how the script loops through each Markdown file in the source directory, uses pandoc to convert it to HTML, and saves the output in a different directory.




No comments:

Post a Comment