Automate Like a Pro: 12 Bash Scripts Every DevOps Engineer Should Know

Zaheer Ahmad
3 min readNov 8, 2024

In DevOps, time is everything. The more you automate, the more you can focus on innovation. From routine server maintenance to cloud integrations, Bash scripts are an invaluable tool for automating your workflows, making your day-to-day work faster, simpler, and error-free.

Here’s a list of 12 Bash scripts every DevOps engineer should know. These scripts cover core operations that can be customized to fit any environment and scaled for larger infrastructures.

1. Automate Server Updates

Purpose: Keep your OS up-to-date without manual work. Schedule automatic updates for better security and stability.

Script:

#!/bin/bash
sudo apt update && sudo apt -y upgrade
echo "Server updates completed on $(date)" >> /var/log/auto_update.log

Pro Tip: Set this up to run weekly with cron jobs and send email alerts for failed updates.

2. Automated MySQL Database Backup and Restore

Purpose: Ensure you always have a recent backup of your databases.

Script:

#!/bin/bash
DATE=$(date +"%Y%m%d")
mysqldump -u username -p'password' database_name > /backups/db_backup_$DATE.sql

Pro Tip: Extend this to automatically transfer backups to AWS S3 or any off-site storage for extra safety.

3. Monitor Disk Usage

Purpose: Get notified when storage levels get critical.

Script:

#!/bin/bash
df -h | awk '$5+0 > 80 {print $0}' >> /var/log/disk_usage.log

Pro Tip: Add alert notifications to know when storage usage hits a critical threshold.

4. Log File Cleanup

Purpose: Free up disk space by purging old log files.

Script:

#!/bin/bash
find /var/log -type f -name "*.log" -mtime +30 -exec rm -f {} \;

Pro Tip: Use this script to delete logs only when disk usage is high, but keep the most recent ones for audits.

5. Bulk User Management

Purpose: Quickly add or manage multiple users with a single script.

Script:

#!/bin/bash
while read user; do
sudo useradd -m $user
done < users.txt

Pro Tip: Integrate this with role-based access controls for efficient, secure user management.

6. Automate Service Restarts

Purpose: Restart critical services automatically if they crash, ensuring uptime.

Script:

#!/bin/bash
service_name="nginx"
if ! systemctl is-active --quiet $service_name; then
systemctl restart $service_name
echo "$service_name restarted on $(date)" >> /var/log/service_restart.log
fi

Pro Tip: Add alert notifications for when a service is restarted.

7. SSH Key Deployment

Purpose: Deploy SSH keys across multiple servers for secure access.

Script:

#!/bin/bash
for server in $(cat server_list.txt); do
ssh-copy-id -i ~/.ssh/id_rsa.pub $server
done

Pro Tip: Use this script to quickly onboard new team members with secure access to development servers.

8. Automated SSL Certificate Renewal

Purpose: Keep your sites secure by automatically renewing SSL certificates.

Script:

#!/bin/bash
certbot renew --quiet
echo "SSL certificate renewed on $(date)" >> /var/log/ssl_renew.log

Pro Tip: Set this up to run monthly, and configure it to alert you if there are any issues during renewal.

9. Server Health Check

Purpose: Monitor server health stats like CPU, memory, and disk usage.

Script:

#!/bin/bash
echo "CPU Load: $(top -bn1 | grep "load average" | awk '{print $10}')"
echo "Memory Usage: $(free -h | grep Mem)"
echo "Disk Usage: $(df -h)"

Pro Tip: Use this to create daily health reports or integrate it with monitoring tools like Grafana.

10. Firewall Configuration

Purpose: Quickly configure basic firewall rules for improved security.

Script:

#!/bin/bash
ufw default deny incoming
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable

Pro Tip: Customize this to restrict access to specific IP addresses or services as needed.

11. S3 Bucket Sync for Off-Site Backups

Purpose: Add an extra layer of data security by syncing files to an S3 bucket.

Script:

#!/bin/bash
aws s3 sync /path/to/files s3://my-bucket

Pro Tip: Set this script to run daily or weekly to automatically back up critical directories.

12. Automate Package Installation

Purpose: Install essential packages on new servers with a single command.

Script:

#!/bin/bash
apt update
apt install -y nginx mysql-server python3

Pro Tip: Use this as a foundation for more extensive provisioning scripts when setting up new environments.

Wrapping Up

These scripts aren’t just time-savers; they’re foundational tools for DevOps engineers looking to automate routine tasks and build a reliable infrastructure. Implement a few of these, adapt them to your environment, and start enjoying the freedom to focus on higher-impact work.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Zaheer Ahmad
Zaheer Ahmad

Written by Zaheer Ahmad

Upwork Top Rated | DevOps Engineer | AWS Certified | Github Action | Docker | Terraform

No responses yet

Write a response