Skip to main content

Command Palette

Search for a command to run...

Shell Scripts commonly used by DevOps Engineers.

Updated
2 min read

Certainly! DevOps engineers often use shell scripts to automate various tasks in their workflow. Here are some examples of shell scripts commonly used by DevOps engineers:

1. Automated Deployment Script:

!/bin/bash

# Simple deployment script to copy files to a server and restart the application

# Variables
APP_DIR="/var/www/myapp"
SERVER="example.com"

# Copy files to the server
scp -r ./dist/* $SERVER:$APP_DIR/

# Restart the application
ssh $SERVER "sudo systemctl restart myapp.service"

2. Backup Script:

#!/bin/bash

# Script to take a backup of a database and files

# Variables
DB_USER="username"
DB_PASS="password"
DB_NAME="mydatabase"
BACKUP_DIR="/backup"

# Backup the database
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/db_backup.sql

# Backup files
tar -czvf $BACKUP_DIR/files_backup.tar.gz /var/www/myapp

3. Log Rotation Script:

#!/bin/bash

# Rotate and archive log files to manage disk space

# Variables
LOG_DIR="/var/log/myapp"
ARCHIVE_DIR="/var/log/archive"

# Rotate logs
find $LOG_DIR -type f -name "*.log" -exec gzip {} \;

# Move rotated logs to archive
find $LOG_DIR -type f -name "*.log.gz" -exec mv {} $ARCHIVE_DIR \;

4. Monitoring Script:

#!/bin/bash

# Simple script to check if a service is running and restart if necessary

# Variables
SERVICE_NAME="myapp"

# Check if the service is running
if pgrep -x "$SERVICE_NAME" >/dev/null; then
    echo "$SERVICE_NAME is running."
else
    echo "$SERVICE_NAME is not running. Restarting..."
    sudo systemctl restart $SERVICE_NAME
fi

5. Environment Configuration Script:

#!/bin/bash

# Script to set up environment variables and dependencies

# Variables
export DB_HOST="localhost"
export DB_USER="username"
export DB_PASS="password"
export APP_ENV="production"

# Install dependencies
npm install

# Run migrations
python manage.py migrate

These scripts cover common scenarios in a DevOps workflow, such as deployment, backup, log management, monitoring, and environment configuration. Remember to customize these scripts based on your specific needs and environment.

More from this blog

Vishal Parit's Blogs

13 posts