• Linux
  • FreeBSD
  • Networking
  • Python
  • AWS
  • WebDev
  • About Us

Linux Shell Scripts for System Administrators

Written by
Linux Leave a Comment

1. File Backup Script:

#!/bin/bash

# Define the backup and source directories
backup_directory="/path/to/backup"
source_directory="/path/to/source"

# Create a timestamped backup of the source directory
timestamp=$(date +%Y%m%d_%H%M%S)
backup_filename="backup_${timestamp}.tar.gz"

# Perform the backup
tar -czf "${backup_directory}/${backup_filename}" "${source_directory}"

2. System Monitoring Script:

#!/bin/bash

# Set the CPU usage threshold
threshold=90

# Check the current CPU usage
cpu_usage=$(top -bn1 | awk '/Cpu\(s\)/ {print int($2)}')

# If the CPU usage exceeds the threshold, trigger an alert
if [ "$cpu_usage" -gt "$threshold" ]; then
    echo "Warning: CPU usage is high at ${cpu_usage}%"
    # Insert alert/notification logic here
fi

3. Log Analyzer Script:

#!/bin/bash

# Define the path to the log file
log_file="/path/to/logfile.log"

# Filter out lines containing "ERROR" and save them to a new file
grep "ERROR" "$log_file" > error_log.txt

# Notify the user that the error log has been created
echo "An error log has been generated."

4. File Encryption/Decryption Script:

#!/bin/bash

# Specify the file to be encrypted
input_file="/path/to/file.txt"

# Encrypt the file using AES-256-CBC encryption
openssl enc -aes-256-cbc -salt -in "$input_file" -out "${input_file}.enc"

# Inform the user that the file has been encrypted
echo "The file has been encrypted: ${input_file}.enc"

5. Automated Software Installation Script:

#!/bin/bash

# Define an array of packages to be installed
package_list=("package1" "package2" "package3")

# Install each package using apt-get
for pkg in "${package_list[@]}"; do
    sudo apt-get install -y "$pkg"
done

# Confirm successful installation of packages
echo "All specified packages have been installed successfully."

6. Network Connectivity Checker Script:

#!/bin/bash

# Define the host to ping
target_host="example.com"

# Test network connectivity by pinging the host
if ping -c 1 "$target_host" > /dev/null 2>&1; then
    echo "Network connection is active."
else
    echo "Network connection is inactive."
fi

7. Website Uptime Checker Script:

#!/bin/bash

# Specify the website URL to check
url="https://example.com"

# Test the accessibility of the website
if curl -s --head --request GET "$url" | grep "200 OK" > /dev/null; then
    echo "The website is accessible."
else
    echo "The website is not accessible."
fi

8. Data Cleanup Script:

#!/bin/bash

# Define the directory to clean up
cleanup_dir="/path/to/cleanup"

# Delete files older than 7 days within the specified directory
find "$cleanup_dir" -type f -mtime +7 -exec rm -f {} \;

# Notify the user that old files have been removed
echo "Files older than 7 days have been deleted."

9. CPU Usage Tracker Script:

#!/bin/bash

# Specify the output file for logging CPU usage
log_file="cpu_usage_log.txt"

# Log the current CPU usage with a timestamp
current_usage=$(top -bn1 | awk '/Cpu\(s\)/ {print int($2)}')
echo "$(date) CPU usage: ${current_usage}%" >> "$log_file"

# Inform the user that CPU usage has been logged
echo "CPU usage has been recorded."

10. System Information Script:

#!/bin/bash

# Define the output file for saving system information
system_info_file="system_info.txt"

# Collect system information and write to the output file
{
    echo "System Information:"
    echo "-------------------"
    echo "Hostname: $(hostname)"
    echo "Operating System: $(uname -a)"
    echo "Memory Usage: $(free -h)"
    echo "Disk Space: $(df -h)"
} > "$system_info_file"

# Notify the user that system information has been saved
echo "System information has been saved to $system_info_file."

11. Disk Space Monitoring Script:

#!/bin/bash

# Define the usage threshold
usage_threshold=90

# Check disk usage for /dev/sda1
current_disk_usage=$(df -h | awk '/\/dev\/sda1/ {print int($5)}')

# Trigger alert if usage exceeds the threshold
if [ "$current_disk_usage" -gt "$usage_threshold" ]; then
    echo "Warning: Disk usage is high at ${current_disk_usage}%"
    # Insert alert/notification logic here
fi

12. Remote Server Backup Script:

#!/bin/bash

# Define the source directory and remote server destination
source_directory="/path/to/source"
destination="user@remoteserver:/path/to/backup"

# Perform the backup using rsync
rsync -avz "$source_directory" "$destination"

# Notify the user that the backup is complete
echo "Backup to remote server completed successfully."

13. Database Backup Script:

#!/bin/bash

# Define the database name and output file with a timestamp
db_name="your_database"
backup_file="database_backup_$(date +%Y%m%d).sql"

# Perform the database backup using mysqldump
mysqldump -u username -ppassword "$db_name" > "$backup_file"

# Notify the user that the backup is complete
echo "Database backup has been created: $backup_file"

14. Directory Synchronization Script:

#!/bin/bash

# Define source and destination directories for synchronization
src_dir="/path/to/source"
dest_dir="/path/to/destination"

# Use rsync to synchronize the directories
rsync -avz "$src_dir" "$dest_dir"

# Confirm that the synchronization is complete
echo "Synchronization of directories completed successfully."

15. System Health Check Script:

#!/bin/bash

# Define the output file for saving system health check results
health_check_file="system_health_check.txt"

# Collect system health information and write to the output file
{
    echo "System Health Check:"
    echo "---------------------"
    echo "Uptime: $(uptime)"
    echo "Load Average: $(awk '{print $1, $2, $3}' /proc/loadavg)"
    echo "Memory Usage: $(free -m)"
} > "$health_check_file"

# Notify the user that the system health check results have been saved
echo "System health check results have been saved to $health_check_file."

16. Service Restart Script:

#!/bin/bash

# Define the service to be restarted
service_to_restart="your_service"

# Restart the specified service using systemctl
sudo systemctl restart "$service_to_restart"

# Inform the user that the service has been restarted
echo "The $service_to_restart service has been restarted successfully."

17. Backup Rotation Script:

#!/bin/bash

# Define the backup directory and maximum number of backups allowed
backup_directory="/path/to/backups"
maximum_backups=5

# Rotate backups by removing the oldest if the number exceeds the maximum allowed
while [ $(ls -1 "$backup_directory" | wc -l) -gt "$maximum_backups" ]; do
    oldest_backup=$(ls -1t "$backup_directory" | tail -n 1)
    rm -r "$backup_directory/$oldest_backup"
done

# Notify the user that backup rotation is complete
echo "Backup rotation has been completed."

© Copyright 2020.TechieNix. All Rights Reserved.