12 Essential Bash Scripts for DevOps Automation
This article presents twelve practical Bash scripts that automate common DevOps tasks such as system updates, disk monitoring, backups, log rotation, SSH key setup, MySQL dumping, Docker cleanup, Kubernetes pod checks, SSL certificate monitoring, Git pulling, user management, and service health verification.
By integrating these scripts into daily operations, you can save time and ensure your infrastructure stays secure and efficient.
1. Automating System Updates
Regularly updating system packages is critical for maintaining security and performance. This script automates the update and upgrade process.
#!/bin/bash
# Update and upgrade system packages
echo "Starting system update..."
sudo apt update && sudo apt upgrade -y
echo "System update completed."2. Disk Usage Monitoring Script
Monitoring disk usage prevents systems from running out of space, which can cause major disruptions.
#!/bin/bash
# Check disk usage and send alert if usage exceeds 80%
THRESHOLD=80
df -h | awk '{ if($5+0 > THRESHOLD) print $0; }' | while read output; do
echo "Disk usage alert: $output"
done3. Automated Backup Script
This script creates automated backups of important directories, ensuring data is always protected.
#!/bin/bash
# Backup a directory and store it in a backup folder with a timestamp
SOURCE="/path/to/important/data"
DEST="/path/to/backup/location"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
tar -czvf $DEST/backup_$TIMESTAMP.tar.gz $SOURCE
echo "Backup completed: $DEST/backup_$TIMESTAMP.tar.gz"4. Log Rotation Script
Logs can grow large and take up valuable disk space. This script rotates and compresses log files.
#!/bin/bash
# Rotate and compress logs
LOG_FILE="/path/to/logfile.log"
BACKUP_DIR="/path/to/log/backup"
TIMESTAMP=$(date +"%Y%m%d")
mv $LOG_FILE $BACKUP_DIR/log_$TIMESTAMP.log
gzip $BACKUP_DIR/log_$TIMESTAMP.log
touch $LOG_FILE
echo "Log rotation completed."5. Automated SSH Key Setup
This script simplifies the process of setting up SSH keys for remote login, improving security and automation.
#!/bin/bash
# Generate SSH key and copy to remote server
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -q -N ""
ssh-copy-id user@remote_server
echo "SSH key setup completed."6. Automated MySQL Database Backup
Backups are critical for database management. This script automates the process of dumping MySQL databases.
#!/bin/bash
# MySQL database backup
DB_NAME="my_database"
USER="db_user"
PASSWORD="db_pass"
BACKUP_DIR="/path/to/backup"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
mysqldump -u $USER -p$PASSWORD $DB_NAME > $BACKUP_DIR/${DB_NAME}_$TIMESTAMP.sql
echo "Database backup completed: $BACKUP_DIR/${DB_NAME}_$TIMESTAMP.sql"7. Automated Docker Cleanup
Docker containers, images, and volumes can accumulate over time, consuming disk space. This script automates cleanup.
#!/bin/bash
# Docker container and image cleanup
docker system prune -af
docker volume prune -f
echo "Docker cleanup completed."8. Kubernetes Pod Status Check
This script monitors Kubernetes pods to ensure your application is running as expected.
#!/bin/bash
# Check Kubernetes pod status
NAMESPACE="default"
kubectl get pods -n $NAMESPACE9. SSL Certificate Expiry Checker
SSL certificates need to be renewed periodically. This script checks when your SSL certificate expires and alerts you in advance.
#!/bin/bash
# Check SSL certificate expiration
DOMAIN="example.com"
EXPIRY_DATE=$(echo | openssl s_client -servername $DOMAIN -connect $DOMAIN:443 2>/dev/null | openssl x509 -noout -dates | grep notAfter | cut -d= -f2)
DAYS_LEFT=$(( ( $(date -d "$EXPIRY_DATE" +%s) - $(date +%s) ) / 86400 ))
echo "SSL certificate for $DOMAIN expires in $DAYS_LEFT days."10. Git Auto Pull Script
For automated deployment processes, this script ensures the latest code from the repository is pulled to the server.
#!/bin/bash
# Auto pull the latest code from the Git repository
REPO_PATH="/path/to/repo"
BRANCH="main"
cd $REPO_PATH
git pull origin $BRANCH
echo "Code pulled from $BRANCH branch."11. User Account Management Script
Managing user accounts is a common task for DevOps engineers. This script automates adding users to the system.
#!/bin/bash
# Add a new user
USERNAME=$1
sudo useradd -m $USERNAME
sudo passwd $USERNAME
sudo usermod -aG sudo $USERNAME
echo "User $USERNAME added and granted sudo privileges."12. Service Status Checker
Monitoring the status of critical services is essential for system reliability. This script automates service health checks.
#!/bin/bash
# Check the status of a specific service
SERVICE=$1
systemctl is-active --quiet $SERVICE && echo "$SERVICE is running" || echo "$SERVICE is not running"DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.