0% found this document useful (0 votes)
2 views

Mastering Linux Automation

The document provides essential shell scripts for automating various Linux tasks, including backup, system updates, user account management, disk usage monitoring, service monitoring, log rotation, and server health checks. Each script is accompanied by a brief explanation of its purpose and functionality. Additionally, instructions for scheduling these scripts using cron are included.

Uploaded by

karan209205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Mastering Linux Automation

The document provides essential shell scripts for automating various Linux tasks, including backup, system updates, user account management, disk usage monitoring, service monitoring, log rotation, and server health checks. Each script is accompanied by a brief explanation of its purpose and functionality. Additionally, instructions for scheduling these scripts using cron are included.

Uploaded by

karan209205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Md. Ikbal Hossain Mobile: 01950758712 E-mail: ikbalbd1052@gmail.

com

Mastering Linux Automation: Simplify Your Workflow with These Essential Shell Scripts.

1. Backup Automation Script

Automating backups is critical for data safety and disaster recovery.


Script: auto_backup.sh

#!/bin/bash

# Variables
SOURCE_DIR="/path/to/source"
BACKUP_DIR="/path/to/backup"
LOG_FILE="/var/log/backup.log"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")

# Create a backup
echo "[$DATE] Starting backup..." >> "$LOG_FILE"
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" "$SOURCE_DIR" 2>>"$LOG_FILE"

if [ $? -eq 0 ]; then
echo "[$DATE] Backup completed successfully." >> "$LOG_FILE"
else
echo "[$DATE] Backup failed!" >> "$LOG_FILE"
fi

2. System Update Script

Keep all packages up to date to maintain security and performance.


Script: auto_update.sh

#!/bin/bash

LOG_FILE="/var/log/system_update.log"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")

echo "[$DATE] Starting system update..." >> "$LOG_FILE"

# Update package lists and upgrade


sudo apt update && sudo apt upgrade -y >> "$LOG_FILE" 2>&1

if [ $? -eq 0 ]; then
echo "[$DATE] System update completed successfully." >> "$LOG_FILE"
else
echo "[$DATE] System update failed!" >> "$LOG_FILE"
fi

Page 1 of 4
Md. Ikbal Hossain Mobile: 01950758712 E-mail: [email protected]

3. User Account Management Script

Easily create and manage user accounts.


Script: user_management.sh

#!/bin/bash

# Add user
add_user() {
read -p "Enter username: " username
sudo adduser "$username"
echo "User $username added."
}

# Delete user
delete_user() {
read -p "Enter username to delete: " username
sudo deluser "$username"
echo "User $username deleted."
}

echo "1. Add User"


echo "2. Delete User"
read -p "Choose an option: " choice

case $choice in
1)
add_user
;;
2)
delete_user
;;
*)
echo "Invalid option!"
;;
esac

4. Disk Usage Monitoring Script

Monitor disk space and send alerts if it exceeds a threshold.


Script: disk_monitor.sh

#!/bin/bash

THRESHOLD=80
LOG_FILE="/var/log/disk_usage.log"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")

echo "[$DATE] Checking disk usage..." >> "$LOG_FILE"

df -H | awk '{ print $5 " " $1 }' | while read output; do


usage=$(echo "$output" | awk '{print $1}' | sed 's/%//')

Page 2 of 4
Md. Ikbal Hossain Mobile: 01950758712 E-mail: [email protected]

filesystem=$(echo "$output" | awk '{print $2}')

if [ "$usage" -ge "$THRESHOLD" ]; then


echo "[$DATE] WARNING: $filesystem usage is at ${usage}%!" >> "$LOG_FILE"
fi
done

5. Service Monitor and Restart Script

Ensure essential services remain active.


Script: service_monitor.sh

#!/bin/bash

SERVICE_NAME="apache2"
LOG_FILE="/var/log/service_monitor.log"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")

echo "[$DATE] Checking $SERVICE_NAME service..." >> "$LOG_FILE"

if systemctl is-active --quiet "$SERVICE_NAME"; then


echo "[$DATE] $SERVICE_NAME is running." >> "$LOG_FILE"
else
echo "[$DATE] $SERVICE_NAME is not running. Restarting..." >> "$LOG_FILE"
systemctl restart "$SERVICE_NAME"

if [ $? -eq 0 ]; then
echo "[$DATE] $SERVICE_NAME restarted successfully." >> "$LOG_FILE"
else
echo "[$DATE] Failed to restart $SERVICE_NAME." >> "$LOG_FILE"
fi
fi

6. Log Rotation Script

Automatically manage and rotate logs to save space.


Script: log_rotation.sh

#!/bin/bash

LOG_DIR="/var/log/myapp"
MAX_SIZE=10485760 # 10 MB
DATE=$(date +"%Y-%m-%d_%H-%M-%S")

for file in "$LOG_DIR"/*.log; do


if [ -f "$file" ]; then
size=$(stat -c%s "$file")
if [ "$size" -ge "$MAX_SIZE" ]; then
mv "$file" "${file}_$DATE"
gzip "${file}_$DATE"
echo "Rotated $file to ${file}_$DATE.gz"

Page 3 of 4
Md. Ikbal Hossain Mobile: 01950758712 E-mail: [email protected]

fi
fi
done

7. Automated Server Health Check Script

Consolidate health metrics like CPU, memory, and disk usage.


Script: health_check.sh

#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H-%M-%S")
LOG_FILE="/var/log/server_health.log"

echo "[$DATE] Server Health Check:" >> "$LOG_FILE"


echo "CPU Load: $(uptime | awk -F'load average:' '{ print $2 }')" >> "$LOG_FILE"
echo "Memory Usage: $(free -h | grep Mem | awk '{print $3 "/" $2}')" >> "$LOG_FILE"
echo "Disk Usage: $(df -h / | awk 'NR==2 {print $5}')" >> "$LOG_FILE"
echo "-------------------------------" >> "$LOG_FILE"

8. Scheduled Job Automation (Cron Setup)

To schedule these scripts:

1. Edit crontab:

crontab -e

2. Add entries like:

0 2 * * * /path/to/auto_backup.sh
0 3 * * * /path/to/auto_update.sh

Page 4 of 4

You might also like