10 Essential Bash Scripts to Boost DevOps Efficiency
Learn how to streamline your DevOps workflow with 10 practical Bash scripts for automation, monitoring, and system management.
Join the DZone community and get the full member experience.
Join For FreeAutomation is a major aspect of the DevOps workflow, enhancing efficiency, and Bash script is one of the oldest and powerful tools for achieving this automation. Bash scripts help engineers and system admins to eliminate mundane workflow, repetitive tasks, and reduce errors across multiple environments. With its simplicity and adaptability in many Unix-based systems, the Bash script is used in day-to-day operations without the overhead of complex automation tooling.
In this article, you will learn 10 essential Bash scripts that can boost your DevOps productivity. These range from automating simple CI/CD DevOps workflow, backups, and Docker container management to monitoring system health and environment provisioning.
1. Simple CI/CD Script
You can use this Bash script to automate a basic CI/CD pipeline for a Node.js application.
#!/bin/bash
# Variables
REPO_DIR="your_github_repo_path"
GIT_BRANCH="main"
echo "Starting deployment..."
# Navigate to project directory
cd $REPO_DIR || exit 1
# Pull latest code
git fetch origin $GIT_BRANCH
git reset --hard origin/$GIT_BRANCH
# Install dependencies
npm install --omit=dev
# Build the project (if applicable)
npm run build
# Restart the service (choose one based on setup)
pm2 restart node-app || sudo systemctl restart node-app
echo "Deployment complete."
This script pulls the latest changes from your GitHub repository, installs dependencies, builds the Node.js application, and restarts the service. The script can be used in both staging and production environments, especially if your project is lightweight.
2. Automated Database Backup Script
You can use a Bash script to create automated backups for both MySQL and PostgreSQL databases and export them with a timestamp as .sql or .backup files.
For example, you can use this Bash script to automate your MySQL backups.
#!/bin/bash
# Variables
DB_USER="your_db_username"
DB_PASS="your_mysql_password"
DB_NAME="your_database_name"
BACKUP_DIR="/var/backups/mysql"
TIMESTAMP=$(date +"%F-%T")
mkdir -p $BACKUP_DIR
# Backup command
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > "$BACKUP_DIR/${DB_NAME}_$TIMESTAMP.sql"
echo "MySQL backup completed: $BACKUP_DIR/${DB_NAME}_$TIMESTAMP.sql"
This script will connect to your MySQL database server and dump backups with a timestamped file to your specified backup directory (in this example, the directory is /var/backups/mysql).
Additionally, to back up your PostgreSQL database, you can use the following script.
#!/bin/bash
# Variables
DB_USER="your_db_username"
DB_NAME="your_database_name"
BACKUP_DIR="/var/backups/postgresql"
TIMESTAMP=$(date +"%F-%T")
mkdir -p $BACKUP_DIR
# Backup command
DB_PASSWORD="your_postgres_password" pg_dump -U $DB_USER -F c -b -v -f "$BACKUP_DIR/${DB_NAME}_$TIMESTAMP.backup" $DB_NAME
echo "PostgreSQL backup completed: $BACKUP_DIR/${DB_NAME}_$TIMESTAMP.backup"
Similarly, this script connects to the PostgreSQL database server and dumps backups with a timestamped file to a specified backup directory (in this example, the directory is /var/backups/postgresql).
3. Automated WordPress Site File Backup
For teams managing multiple WordPress instances on a single server, you can use an automated Bash script to help prevent data loss from plugin updates or file corruption. Backing up the WordPress files is important because the wp-content directory consists of important files such as themes, plugins, and media. You can use the script below to back up your WordPress site files.
#!/bin/bash
# Variables
SITE_DIR="/var/www/html/your-wordpress-site"
BACKUP_DIR="/var/backups/wordpress"
TIMESTAMP=$(date +"%F-%T")
mkdir -p $BACKUP_DIR
# Create compressed archive of WordPress site
tar -czf "$BACKUP_DIR/wp-site-backup-$TIMESTAMP.tar.gz" -C "$SITE_DIR" .
echo "WordPress site backup completed: $BACKUP_DIR/wp-site-backup-$TIMESTAMP.tar.gz"
This script compresses the WordPress site directory and saves it as a timestamped archive. You can pair this script with a database backup script (depending on the database used for your WordPress site) for full recovery.
4. Monitoring Disk Space
You can use this Bash script to check and monitor disk usage in the root partition of your machine.
#!/bin/bash
# Configurable variables
WARNING_LIMIT=80 # You can use your own limit
ALERT_EMAIL="[email protected]" # This can be changed to your email
# Extract current usage percentage (without % symbol)
USAGE=$(df -h / | awk 'NR==2 { gsub("%",""); print $5 }')
# Compare usage to warning limit
if [ "$USAGE" -ge "$WARNING_LIMIT" ]; then
HOSTNAME=$(hostname)
MESSAGE="Warning: Disk usage on $HOSTNAME has reached ${USAGE}%."
echo "$MESSAGE" | mail -s "Disk Usage Alert on $HOSTNAME" "$ALERT_EMAIL"
fi
This script triggers an email notification when a threshold is exceeded. You can schedule this script to run hourly, weekly, or even daily with your system's cron.
5. Automated Docker Container Cleanup
You can use a repeatable Bash script to automate your container cleanup.
#!/bash/bin
# Cleanup: remove dangling images, stopped containers, and unused networks
echo "Cleaning up unused Docker resources..."
docker container prune -f
docker image prune -f
docker network prune -f
echo "Docker cleanup completed."
This script deletes images, stops containers, and removes unused networks.
6. Automated User Account Creation
When you're working on multiple servers and managing teams, manually creating users can be time-consuming and error-prone. You can use a Bash script to automate the user creation.
#!/bin/bash
# Path to the file containing one username per line
USER_FILE="path-to-your_userlist"
# Set a default password for all new accounts
DEFAULT_PASS="changeme"
# Create each user from the list
while read -r USERNAME || [[ -n "$USERNAME" ]]; do
if id "$USERNAME" &>/dev/null; then
echo "User $USERNAME already exists. Skipping..."
else
useradd -m "$USERNAME"
echo "$USERNAME:$DEFAULT_PASS" | chpasswd
echo "User $USERNAME created successfully with the default password."
fi
done < "$USER_FILE"
This script automatically creates all the users it reads from the userlist file.
7. SSL Certificate Renewal
If you use Let's Encrypt for your production service running on HTTPS, you can use the Bash script below to automate your certificate renewal with certbot.
#!/bin/bash
WEB_SERVER = "your-web-server" # for example, nginx.
# Renew all Let's Encrypt certificates
echo "Checking and renewing SSL certificates..."
certbot renew --quiet --agree-tos
# Reload web server after renewal (for example, Apache or Nginx)
echo "Reloading web server to apply new certificates..."
systemctl reload $WEB_SERVER # Replace WEB_SERVER with your websever currently in use
echo "SSL certificate renewal process completed."
This script checks for certificates that are near expiration and renews them.
8. Kubernetes Node Resource Checker
You can use this Bash script to monitor the resource availability of your nodes. Bash script and kubectl can be combined to create an automated script for CPU and memory availability across all your cluster nodes.
#!/bin/bash
# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
echo "kubectl not found. Please install and configure it before running this script."
exit 1
fi
echo "Fetching Kubernetes node resource usage..."
# Fetch node metrics (CPU and memory usage)
kubectl top nodes | awk '
BEGIN {
printf "%-30s %-15s %-15s\n", "Node", "CPU Usage", "Memory Usage"
print "---------------------------------------------------------------"
}
NR>1 {
printf "%-30s %-15s %-15s\n", $1, $2, $3
}'
This script first checks if kubectl is installed. Then it uses the kubectl top nodes to retrieve your cluster nodes' resource usage data and format it in a readable table.
9. Automated Log Rotation and Archival
Log rotation is a practice that prevents logs from filling up your disk space, especially in a production environment. You can use this Bash script to automate this process and maintain server health and retain log history.
#!/bin/bash
# Directory containing log files
LOG_DIR="your-target-log-path"
ARCHIVE_DIR="/var/backups/logs"
TIMESTAMP=$(date +"%F-%T")
# Create archive directory if it doesn't exist
mkdir -p "$ARCHIVE_DIR"
# Rotate and compress logs
for LOG_FILE in "$LOG_DIR"/*.log; do
if [ -f "$LOG_FILE" ]; then
BASENAME=$(basename "$LOG_FILE")
cp "$LOG_FILE" "$ARCHIVE_DIR/${BASENAME%.*}-$TIMESTAMP.log"
cat /dev/null > "$LOG_FILE"
gzip "$ARCHIVE_DIR/${BASENAME%.*}-$TIMESTAMP.log"
echo "Rotated and archived $BASENAME"
fi
done
This script copies your log file from the target directory to a specified archive with a timestamp. Then it deletes the initial log file and finally compresses the archived file to save space.
10. System Update Script
You can use a Bash script to automate your system updates and clean up unused dependencies in a Unix system.
#!/bin/bash
# Update package index
echo "Updating package lists..."
sudo apt update -y
# Upgrade installed packages
echo "Upgrading packages..."
sudo apt upgrade -y
# Remove unnecessary packages
echo "Removing unused packages..."
sudo apt autoremove -y
# Clean up cached files
echo "Cleaning up..."
sudo apt clean
echo "System update and cleanup completed successfully."
This script ensures your system is up to date with the latest packages and security fixes.
Wrapping Up
Bash script is still very relevant in DevOps workflows and automation. These Bash scripts represent just the tip of the iceberg of the many ways you can automate routine DevOps tasks and workflows. With the use of a Bash script, you can reduce manual efforts, save time, and reduce human errors across multiple environments. Whether you're building a simple CI/CD pipeline, doing backups, monitoring, or managing servers, Bash scripting remains a practical approach to your day-to-day DevOps operations.
Opinions expressed by DZone contributors are their own.
Comments