10 Powerful Bash Scripts to Automate System Tasks
This article presents ten advanced Bash script examples that automate common system administration tasks such as backups, resource monitoring, updates, database dumping, FTP uploads, log keyword alerts, report generation, service recovery, temporary file cleanup, and network status checking.
Here are ten advanced Bash script examples, each solving a different problem or performing a distinct task.
1. Backup Directory
#!/bin/bash
SOURCE_DIR="/path/to/source"
BACKUP_DIR="/path/to/backup"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
fi
tar -czf "$BACKUP_DIR/backup_$TIMESTAMP.tar.gz" -C "$SOURCE_DIR" .
echo "Backup of $SOURCE_DIR completed at $BACKUP_DIR/backup_$TIMESTAMP.tar.gz"2. Monitor System Resources
#!/bin/bash
LOG_FILE="/var/log/system_monitor.log"
INTERVAL=5
while true; do
DATE=$(date +"%Y-%m-%d %H:%M:%S")
CPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
MEM=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s", $5}')
echo "$DATE CPU: $CPU, Memory: $MEM, Disk: $DISK" >> $LOG_FILE
sleep $INTERVAL
done3. Automatic System Update
#!/bin/bash
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y autoremove
sudo apt-get -y autoclean
echo "System update and cleanup completed."4. Database Backup
#!/bin/bash
DB_NAME="your_database"
DB_USER="your_username"
DB_PASS="your_password"
BACKUP_DIR="/path/to/backup"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME | gzip > "$BACKUP_DIR/db_backup_$TIMESTAMP.sql.gz"
echo "Database backup completed at $BACKUP_DIR/db_backup_$TIMESTAMP.sql.gz"5. Automatic FTP Upload
#!/bin/bash
FTP_SERVER="ftp.example.com"
FTP_USER="your_username"
FTP_PASS="your_password"
LOCAL_FILE="/path/to/local/file"
REMOTE_DIR="/path/to/remote/dir"
ftp -inv $FTP_SERVER <<EOF
user $FTP_USER $FTP_PASS
cd $REMOTE_DIR
put $LOCAL_FILE
bye
EOF
echo "File uploaded to $FTP_SERVER:$REMOTE_DIR"6. Monitor Log File for Keywords
#!/bin/bash
LOG_FILE="/var/log/syslog"
KEYWORD="ERROR"
EMAIL="[email protected]"
tail -Fn0 $LOG_FILE | while read line; do
echo "$line" | grep "$KEYWORD" &>/dev/null
if [ $? = 0 ]; then
echo "$line" | mail -s "Error found in log file" $EMAIL
fi
done7. Automatic Report Generation
#!/bin/bash
REPORT_DIR="/path/to/report"
REPORT_FILE="$REPORT_DIR/report_$(date +"%Y%m%d").txt"
if [ ! -d "$REPORT_DIR" ]; then
mkdir -p "$REPORT_DIR"
fi
echo "Report generated on $(date)" > $REPORT_FILE
echo "==========================" >> $REPORT_FILE
df -h >> $REPORT_FILE
echo "" >> $REPORT_FILE
free -m >> $REPORT_FILE
echo "" >> $REPORT_FILE
uptime >> $REPORT_FILE
echo "Report saved at $REPORT_FILE"8. Restart Crashed Service
#!/bin/bash
SERVICE_NAME="apache2"
if ! pgrep -x "$SERVICE_NAME" > /dev/null; then
echo "$SERVICE_NAME is not running. Starting $SERVICE_NAME."
systemctl start $SERVICE_NAME
else
echo "$SERVICE_NAME is running."
fi9. Clean Up Temporary Files
#!/bin/bash
TEMP_DIR="/path/to/temp"
DAYS_OLD=7
find $TEMP_DIR -type f -mtime +$DAYS_OLD -exec rm -f {} \;
echo "Old temporary files deleted."10. Network Connection Status Monitoring
#!/bin/bash
HOST="8.8.8.8"
LOG_FILE="/var/log/network_status.log"
INTERVAL=10
while true; do
DATE=$(date +"%Y-%m-%d %H:%M:%S")
if ping -c 1 $HOST &>/dev/null; then
echo "$DATE Network is up" >> $LOG_FILE
else
echo "$DATE Network is down" >> $LOG_FILE
fi
sleep $INTERVAL
doneEach script can be adjusted as needed to suit specific environments and requirements.
Architect's Must-Have
Professional architects sharing high‑quality architecture insights. Covers high‑availability, high‑performance, high‑stability designs, big data, machine learning, Java, system, distributed and AI architectures, plus internet‑driven architectural adjustments and large‑scale practice. Open to idea‑driven, sharing architects for exchange and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
