Linux实用脚本
自动实时同步数据 使用INOTIFY+RSYNC自动实时同步数据 inotify_rsyncs.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /data/ --exclude=Temp" RSYNC_CMD1="rsync -avz /data/ --exclude-from=/etc/rc.d/init.d/exclude.txt harry@10.11.7.68:/data/ --delete" RSYNC_CMD2="rsync -avz /data/ --exclude-from=/etc/rc.d/init.d/exclude.txt harry@10.11.7.68:/data/ --delete" $INOTIFY_CMD | while read DIRECTORY EVENT FILEdo if [ $(pgrep rsync | wc -l) -le 0 ] ; then $RSYNC_CMD1 &&$RSYNC_CMD2 >> rsync.log fi done
MYSQL自动备份 MYSQL自动备份以及删除备份脚本 db_backup.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #!/bin/bash dbback (){db_user="ma_prd" db_passwd="<password>" db_path="/data/bakmysql" db_file="backuprecord" db_date=`date +%Y%m%d_%H:%M:%S` [ -d $db_path ] || exit 2 mysqldump -u$db_user -p$db_passwd --single-transaction ma | gzip > $db_path /${db_date} _ma.sql.gz REVAL=$? if [ $REVAL -eq 0 ] then echo "$db_date ma db is backups successful" >>$db_path /$db_file else echo "$db_date ma db is backups failed" >>$db_path /$db_file fi } delbak (){local db_path="/data/bakmysql" find $db_path -type f -name "*ma*.gz" -mtime +7 -exec rm -rf {} \; } dbback delbak
文件自动备份 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #!/bin/bash source_files=( "/usr/local/nginx/conf/nginx.conf" "/usr/local/nginx/conf/tcp.stream" "/usr/local/src/check_nginx_pid.sh" "/home/wwwroot/default/it/" ) backup_dir="/home/backup" retention_days=90 mkdir -p $backup_dir for source_file in "${source_files[@]} " ; do backup_file=$(basename $source_file ).$(date +%Y%m%d%H%M%S) cp -r $source_file $backup_dir /$backup_file echo "备份成功: $backup_dir /$backup_file " done find $backup_dir -type f -mtime +$retention_days -exec rm {} \; echo "备份完成"
检测网站可用性 使用curl检测网站可用性脚本 web_check_with_curl.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #!/bin/bash red='\e[0;31m' RED='\e[1;31m' green='\e[0;32m' GREEN='\e[1;32m' blue='\e[0;34m' BLUE='\e[1;34m' cyan='\e[0;36m' CYAN='\e[1;36m' NC='\e[0m' date =`date +%Y-%m-%d' ' %H:%M:%S` ua="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36" pass_count=0 fail_count=0 urls=( "http://www.xxx.com" ) function request (){ status=$(curl -sk -o /dev/null --retry 1 --connect-timeout 1 -w '%{http_code}' --user-agent "$ua " $1 ) if [ $status -eq '200' -o $status -eq '301' \ -o $status -eq '302' ]; then echo -e "[${GREEN} Passed ${NC} ] => $1 " ((pass_count ++)) else echo -e "[${RED} Failed ${NC} ] => $1 " ((fail_count ++)) fi } function main (){ echo "Start checking ..." for ((i=0;i<${#urls[*]} ;i++)) do request ${urls[i]} ; done echo -e "======================== Summary ======================== " echo -e "Total: ${cyan} $((pass_count + fail_count) )${NC} Passed: ${green} ${pass_count} ${NC} Failed: ${red} ${fail_count} ${NC} Time: $date " } main $*
检测软件是否运行 利用脚本定时监控本地、远端数据库服务端或Web服务是否运行正常,例如:负载高、cup高、连接数满了。参考 建议通过专业的监控软件实施监控,比如Prometheus,zabbix等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 netstat -lntup|grep 3306|wc -l ss -lntup|grep 3306|wc -l lsof -i:3306|wc -l echo -e "\n" |telnet baidu.com 80|grep Connected|wc namp www.baidu.com -p 80|grep open|wc -l if [ "`netstat -lnt|grep 3306|awk -F " [ :]+" '{print $4 }'`" = "3306" ] then echo "MySQL is Running." else echo "MySQL is Stopped." systemctl start mysqld echo "MySQL is Starting......" fi [root@devqiu ~] MySQL is Stopped. MySQL is Starting...... [root@devqiu ~] MySQL is Running.
封禁异常IP地址 检测并封禁异常IP地址的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #!/bin/bash DATE=$(date +%d/%b/%Y:%H:%M) LOG_FILE="/usr/local/nginx/logs/access.log" BANNED_IP_LOG="/usr/local/nginx/logs/banned_ip.log" ABNORMAL_IP=$(tail -n 10000 "$LOG_FILE " | grep "$DATE " | awk '{a[$1]++}END{for(i in a) if(a[i]>10) print i}' ) declare -a IP_LISTfor IP in $ABNORMAL_IP ; do if ! iptables -vnL | grep -q "$IP " ; then iptables -I INPUT -s "$IP " -j DROP echo "$(date +'%F_%T') $IP " >> "$BANNED_IP_LOG " IP_LIST+=("$IP " ) fi done if [ ${#IP_LIST[@]} -gt 0 ]; then echo "以下IP地址已被封禁:" printf "%s\n" "${IP_LIST[@]} " else echo "没有需要封禁的IP地址。" fi
网卡实时流量 查看网卡实时流量脚本 bash interface_moniter.sh eth0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #!/bin/bash NIC=${1:-lo} echo -e " In ------ Out" while true ; do OLD_IN=$(awk '$0~"' $NIC '"{print $2}' /proc/net/dev) OLD_OUT=$(awk '$0~"' $NIC '"{print $10}' /proc/net/dev) sleep 1 NEW_IN=$(awk '$0~"' $NIC '"{print $2}' /proc/net/dev) NEW_OUT=$(awk '$0~"' $NIC '"{print $10}' /proc/net/dev) IN=$(printf "%.1f%s" "$((($NEW_IN-$OLD_IN) /1024))" "KB/s" ) OUT=$(printf "%.1f%s" "$((($NEW_OUT-$OLD_OUT) /1024))" "KB/s" ) echo "$IN $OUT " sleep 1 done
日志分析脚本 访问日志分析脚本 bash log_analyze.sh access.log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!/bin/bash LOG_FILE=$1 echo "统计访问最多的10个IP" awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}' $LOG_FILE | sort -k2 -nr | head -10 echo "----------------------" echo "统计时间段访问最多的IP" awk '$4>="[01/Dec/2018:13:20:25" && $4<="[27/Nov/2018:16:20:49"{a[$1]++}END{for(v in a)print v,a[v]}' $LOG_FILE | sort -k2 -nr | head -10 echo "----------------------" echo "统计访问最多的10个页面" awk '{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>10)print v,a[v]}}' $LOG_FILE | sort -k2 -nr echo "----------------------" echo "统计访问页面状态码数量" awk '{a[$7" "$9]++}END{for(v in a){if(a[v]>5)print v,a[v]}}' $LOG_FILE
服务器运行分析 Linux实时信息&状态监控脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #!/bin/bash hostname=$(hostname) ip_address=$(hostname -I | awk '{print $1}' ) os=$(lsb_release -ds) kernel=$(uname -r) uptime =$(uptime -p)while true ; do cpu_model=$(cat /proc/cpuinfo | grep "model name" | head -n1 | awk -F': ' '{print $2}' ) cpu_cores=$(cat /proc/cpuinfo | grep "model name" | wc -l) memory_total=$(free -h | awk 'NR==2{print $2}' ) memory_used=$(free -h | awk 'NR==2{print $3}' ) memory_free=$(free -h | awk 'NR==2{print $4}' ) memory_available=$(free -h | awk 'NR==2{print $7}' ) disk_total=$(df -h --output=size / | awk 'NR==2{print $1}' ) disk_used=$(df -h --output=used / | awk 'NR==2{print $1}' ) disk_free=$(df -h --output=avail / | awk 'NR==2{print $1}' ) cpu_usage=$(top -b -n 1 | grep "%Cpu(s):" | awk '{printf "%.2f%%", 100-$8}' ) clear echo "服务器信息:" echo "主机名:$hostname " echo "IP地址:$ip_address " echo "操作系统:$os " echo "内核版本:$kernel " echo "运行时间:$uptime " echo "--------------------------------------" echo "CPU信息:" echo "型号:$cpu_model " echo "核心数:$cpu_cores " echo "CPU使用率:$cpu_usage " echo "--------------------------------------" echo "内存信息:" echo "总量:$memory_total " echo "已使用:$memory_used " echo "可用:$memory_available " echo "--------------------------------------" echo "磁盘信息:" echo "总量:$disk_total " echo "已使用:$disk_used " echo "可用:$disk_free " sleep 3 done
运行结果