Shell

Shell 脚本部分实例:SVN 完整备份、Zabbix 监控用户密码过期、构建本地 YUM 以及上篇文章中有读者的需求(负载高时,查出占用比较高的进程脚本并存储或推送通知);

SVN 完整备份

通过 hotcopy 进行 SVN 完整备份,备份保留 7 天。

  1. #!/bin/bash
  2. # Filename : svn_backup_repos.sh
  3. # Date : 2020/12/14
  4. # Author : Fcant
  5. # Crontab : 59 23 * * * /bin/bash $BASE_PATH/svn_backup_repos.sh >/dev/null 2>&1
  6. # Notes : 将脚本加入crontab中,每天定时执行
  7. # Description: SVN完全备份
  8. set -e
  9. SRC_PATH="/opt/svndata"
  10. DST_PATH="/data/svnbackup"
  11. LOG_FILE="$DST_PATH/logs/svn_backup.log"
  12. SVN_BACKUP_C="/bin/svnadmin hotcopy"
  13. SVN_LOOK_C="/bin/svnlook youngest"
  14. TODAY=$(date +'%F')
  15. cd $SRC_PATH
  16. ALL_REPOS=$(find ./ -maxdepth 1 -type d ! -name 'httpd' -a ! -name 'bak' | tr -d './')
  17. # 创建备份目录,备份脚本日志目录
  18. test -d $DST_PATH || mkdir -p $DST_PATH
  19. test -d $DST_PATH/logs || mkdir $DST_PATH/logs
  20. test -d $DST_PATH/$TODAY || mkdir $DST_PATH/$TODAY
  21. # 备份repos文件
  22. for repo in $ALL_REPOS
  23. do
  24. $SVN_BACKUP_C $SRC_PATH/$repo $DST_PATH/$TODAY/$repo
  25. # 判断备份是否完成
  26. if $SVN_LOOK_C $DST_PATH/$TODAY/$repo;then
  27. echo "$TODAY: $repo Backup Success" >> $LOG_FILE
  28. else
  29. echo "$TODAY: $repo Backup Fail" >> $LOG_FILE
  30. fi
  31. done
  32. # # 备份用户密码文件和权限文件
  33. cp -p authz access.conf $DST_PATH/$TODAY
  34. # 日志文件转储
  35. mv $LOG_FILE $LOG_FILE-$TODAY
  36. # 删除七天前的备份
  37. seven_days_ago=$(date -d "7 days ago" +'%F')
  38. rm -rf $DST_PATH/$seven_days_ago

zabbix 监控用户密码过期

用于 Zabbix 监控 Linux 系统用户(shell 为 /bin/bash 和 /bin/sh)密码过期,密码有效期剩余 7 天触发加自动发现用户。

  1. #!/bin/bash
  2. diskarray=(`awk -F':' '$NF ~ /\/bin\/bash/||/\/bin\/sh/{print $1}' /etc/passwd`)
  3. length=${#diskarray[@]}
  4. printf "{\n"
  5. printf '\t'"\"data\":["
  6. for ((i=0;i<$length;i++))
  7. do
  8. printf '\n\t\t{'
  9. printf "\"{#USER_NAME}\":\"${diskarray[$i]}\"}"
  10. if [ $i -lt $[$length-1] ];then
  11. printf ','
  12. fi
  13. done
  14. printf "\n\t]\n"
  15. printf "}\n"
  16. 检查用户密码过期
  17. #!/bin/bash
  18. export LANG=en_US.UTF-8
  19. SEVEN_DAYS_AGO=$(date -d '-7 day' +'%s')
  20. user="$1"
  21. # 将Sep 09, 2018格式的时间转换成unix时间
  22. expires_date=$(sudo chage -l $user | awk -F':' '/Password expires/{print $NF}' | sed -n 's/^ //p')
  23. if [[ "$expires_date" != "never" ]];then
  24. expires_date=$(date -d "$expires_date" +'%s')
  25. if [ "$expires_date" -le "$SEVEN_DAYS_AGO" ];then
  26. echo "1"
  27. else
  28. echo "0"
  29. fi
  30. else
  31. echo "0"
  32. fi

构建本地YUM

通过 rsync 的方式同步 yum,通过 Nginx 只做 http yum 站点;

但是 centos6 的镜像最近都不能用了,国内貌似都禁用了,如果找到合适的自行更换地址。

  1. #!/bin/bash
  2. # 更新yum镜像
  3. RsyncCommand="rsync -rvutH -P --delete --delete-after --delay-updates --bwlimit=1000"
  4. DIR="/app/yumData"
  5. LogDir="$DIR/logs"
  6. Centos6Base="$DIR/Centos6/x86_64/Base"
  7. Centos7Base="$DIR/Centos7/x86_64/Base"
  8. Centos6Epel="$DIR/Centos6/x86_64/Epel"
  9. Centos7Epel="$DIR/Centos7/x86_64/Epel"
  10. Centos6Salt="$DIR/Centos6/x86_64/Salt"
  11. Centos7Salt="$DIR/Centos7/x86_64/Salt"
  12. Centos6Update="$DIR/Centos6/x86_64/Update"
  13. Centos7Update="$DIR/Centos7/x86_64/Update"
  14. Centos6Docker="$DIR/Centos6/x86_64/Docker"
  15. Centos7Docker="$DIR/Centos7/x86_64/Docker"
  16. Centos6Mysql5_7="$DIR/Centos6/x86_64/Mysql/Mysql5.7"
  17. Centos7Mysql5_7="$DIR/Centos7/x86_64/Mysql/Mysql5.7"
  18. Centos6Mysql8_0="$DIR/Centos6/x86_64/Mysql/Mysql8.0"
  19. Centos7Mysql8_0="$DIR/Centos7/x86_64/Mysql/Mysql8.0"
  20. MirrorDomain="rsync://rsync.mirrors.ustc.edu.cn"
  21. # 目录不存在就创建
  22. check_dir(){
  23. for dir in $*
  24. do
  25. test -d $dir || mkdir -p $dir
  26. done
  27. }
  28. # 检查rsync同步结果
  29. check_rsync_status(){
  30. if [ $? -eq 0 ];then
  31. echo "rsync success" >> $1
  32. else
  33. echo "rsync fail" >> $1
  34. fi
  35. }
  36. check_dir $DIR $LogDir $Centos6Base $Centos7Base $Centos6Epel $Centos7Epel $Centos6Salt $Centos7Salt $Centos6Update $Centos7Update $Centos6Docker $Centos7Docker $Centos6Mysql5_7 $Centos7Mysql5_7 $Centos6Mysql8_0 $Centos7Mysql8_0
  37. # Base yumrepo
  38. #$RsyncCommand "$MirrorDomain"/repo/centos/6/os/x86_64/ $Centos6Base >> "$LogDir/centos6Base.log" 2>&1
  39. # check_rsync_status "$LogDir/centos6Base.log"
  40. $RsyncCommand "$MirrorDomain"/repo/centos/7/os/x86_64/ $Centos7Base >> "$LogDir/centos7Base.log" 2>&1
  41. check_rsync_status "$LogDir/centos7Base.log"
  42. # Epel yumrepo
  43. # $RsyncCommand "$MirrorDomain"/repo/epel/6/x86_64/ $Centos6Epel >> "$LogDir/centos6Epel.log" 2>&1
  44. # check_rsync_status "$LogDir/centos6Epel.log"
  45. $RsyncCommand "$MirrorDomain"/repo/epel/7/x86_64/ $Centos7Epel >> "$LogDir/centos7Epel.log" 2>&1
  46. check_rsync_status "$LogDir/centos7Epel.log"
  47. # SaltStack yumrepo
  48. # $RsyncCommand "$MirrorDomain"/repo/salt/yum/redhat/6/x86_64/ $Centos6Salt >> "$LogDir/centos6Salt.log" 2>&1
  49. # ln -s $Centos6Salt/archive/$(ls $Centos6Salt/archive | tail -1) $Centos6Salt/latest
  50. # check_rsync_status "$LogDir/centos6Salt.log"
  51. $RsyncComman "$MirrorDomain"/repo/salt/yum/redhat/7/x86_64/ $Centos7Salt >> "$LogDir/centos7Salt.log" 2>&1
  52. check_rsync_status "$LogDir/centos7Salt.log"
  53. # ln -s $Centos7Salt/archive/$(ls $Centos7Salt/archive | tail -1) $Centos7Salt/latest
  54. # Docker yumrepo
  55. $RsyncCommand "$MirrorDomain"/repo/docker-ce/linux/centos/7/x86_64/stable/ $Centos7Docker >> "$LogDir/centos7Docker.log" 2>&1
  56. check_rsync_status "$LogDir/centos7Docker.log"
  57. # centos update yumrepo
  58. # $RsyncCommand "$MirrorDomain"/repo/centos/6/updates/x86_64/ $Centos6Update >> "$LogDir/centos6Update.log" 2>&1
  59. # check_rsync_status "$LogDir/centos6Update.log"
  60. $RsyncCommand "$MirrorDomain"/repo/centos/7/updates/x86_64/ $Centos7Update >> "$LogDir/centos7Update.log" 2>&1
  61. check_rsync_status "$LogDir/centos7Update.log"
  62. # mysql 5.7 yumrepo
  63. # $RsyncCommand "$MirrorDomain"/repo/mysql-repo/yum/mysql-5.7-community/el/6/x86_64/ "$Centos6Mysql5_7" >> "$LogDir/centos6Mysql5.7.log" 2>&1
  64. # check_rsync_status "$LogDir/centos6Mysql5.7.log"
  65. $RsyncCommand "$MirrorDomain"/repo/mysql-repo/yum/mysql-5.7-community/el/7/x86_64/ "$Centos7Mysql5_7" >> "$LogDir/centos7Mysql5.7.log" 2>&1
  66. check_rsync_status "$LogDir/centos7Mysql5.7.log"
  67. # mysql 8.0 yumrepo
  68. # $RsyncCommand "$MirrorDomain"/repo/mysql-repo/yum/mysql-8.0-community/el/6/x86_64/ "$Centos6Mysql8_0" >> "$LogDir/centos6Mysql8.0.log" 2>&1
  69. # check_rsync_status "$LogDir/centos6Mysql8.0.log"
  70. $RsyncCommand "$MirrorDomain"/repo/mysql-repo/yum/mysql-8.0-community/el/7/x86_64/ "$Centos7Mysql8_0" >> "$LogDir/centos7Mysql8.0.log" 2>&1
  71. check_rsync_status "$LogDir/centos7Mysql8.0.log"

负载高时,查出占用比较高的进程脚本并存储或推送通知

  1. #!/bin/bash
  2. # 物理cpu个数
  3. physical_cpu_count=$(egrep 'physical id' /proc/cpuinfo | sort | uniq | wc -l)
  4. # 单个物理cpu核数
  5. physical_cpu_cores=$(egrep 'cpu cores' /proc/cpuinfo | uniq | awk '{print $NF}')
  6. # 总核数
  7. total_cpu_cores=$((physical_cpu_count*physical_cpu_cores))
  8. # 分别是一分钟、五分钟、十五分钟负载的阈值,其中有一项超过阈值才会触发
  9. one_min_load_threshold="$total_cpu_cores"
  10. five_min_load_threshold=$(awk 'BEGIN {print '"$total_cpu_cores"' * "0.8"}')
  11. fifteen_min_load_threshold=$(awk 'BEGIN {print '"$total_cpu_cores"' * "0.7"}')
  12. # 分别是分钟、五分钟、十五分钟负载平均值
  13. one_min_load=$(uptime | awk '{print $(NF-2)}' | tr -d ',')
  14. five_min_load=$(uptime | awk '{print $(NF-1)}' | tr -d ',')
  15. fifteen_min_load=$(uptime | awk '{print $NF}' | tr -d ',')
  16. # 获取当前cpu 内存 磁盘io信息,并写入日志文件
  17. # 如果需要发送消息或者调用其他,请自行编写函数即可
  18. get_info(){
  19. log_dir="cpu_high_script_log"
  20. test -d "$log_dir" || mkdir "$log_dir"
  21. ps -eo user,pid,%cpu,stat,time,command --sort -%cpu | head -10 > "$log_dir"/cpu_top10.log
  22. ps -eo user,pid,%mem,rss,vsz,stat,time,command --sort -%mem | head -10 > "$log_dir"/mem_top10.log
  23. iostat -dx 1 10 > "$log_dir"/disk_io_10.log
  24. }
  25. export -f get_info
  26. echo "$one_min_load $one_min_load_threshold $five_min_load $five_min_load_threshold $fifteen_min_load $fifteen_min_load_threshold" | \
  27. awk '{ if ($1>=$2 || $3>=$4 || $5>=$6) system("get_info") }'