日志归集脚本
日志归集需求:归集系统日期前一天的日志,归集到统一目录下,并需要有模块名称和ip标识;
脚本1:
#!/bin/bash echo "===log backup start===" #判断执行用户,为hps用户 if [ $(whoami) != hps ] then echo "execute user must be [hps]!" exit 2 fi #注意月末最后一天日志会在下一个月目录下 ip=`ifconfig | grep inet | head -1 | awk '{print $2}'` month1=`date +'%Y%m'` day1=`date +'%Y%m%d'` day2=`date -d "1 day ago" +'%Y%m%d'` day3=`date -d yesterday +%F` backupDir=/tmp/$ip/$month1/$day2 mkdir -p $backupDir cd /home/hps/logs if [ -d /home/hps/logs/history ];then find /home/hps/logs/history -name "*log*" -type f | grep $day3 | xargs -I {} tar -czf {}.api.$ip.tar.gz find /home/hps/logs/history -name "*gz*" | grep $day3 | xargs -I {} mv {} $backupDir fi echo "===log backup end==="
脚本2:
#!/bin/bash echo "===log backup start===" #判断执行用户,为was用户 if [ $(whoami) != was ] then echo "execute user must be [was]!" exit 2 fi #注意月末最后一天日志会在下一个月目录下 ip=`ifconfig | grep inet | head -1 | awk '{print $2}'` month1=`date +'%Y%m'` day1=`date +'%Y%m%d'` day2=`date -d "1 day ago" +'%Y%m%d'` day3=`date -d yesterday +%F` backupDir=/tmp/$ip/$month1/$day2 mkdir -p $backupDir cd /home/was/logs if [ -d "/home/was/logs/manage" -a -d "/home/was/logs/monitor" ];then #收集管理平台日志 find /home/was/logs/manage -name "*log*" -type f | grep $day2 | xargs -I {} tar -czf {}.manage.$ip.tar.gz find /home/was/logs/manage -name "*gz*" | grep $day2 | xargs -I {} mv {} $backupDir #收集监控平台日志 find /home/was/logs/monitor -name "*log*" -type f | grep $day2 | xargs -I {} tar -czf {}.monitor.$ip.tar.gz find /home/was/logs/monitor -name "*gz*" | grep $day2 | xargs -I {} mv {} $backupDir fi if [ -d "/home/was/logs/front" ];then #收集前台日志 find /home/was/logs/front -name "*log*" -type f | grep $day2 | xargs -I {} tar -czf {}.front.$ip.tar.gz find /home/was/logs/front -name "*gz*" | grep $day2 | xargs -I {} mv {} $backupDir #收集除前台目录外的日志 find /home/was/logs \( -path /home/was/logs/front \) -prune -o -print | grep $day2 | xargs -I {} tar -czf {}.upp.$ip.tar.gz find /home/was/logs \( -path /home/was/logs/front \) -prune -o -print | grep 'tar.gz' | grep $day2 | xargs -I {} mv {} $backupDir fi echo "===log backup end==="
说明:
find排除单个目录,示例:
find /tmp -path /tmp/abc -prune -o -print
find排除多个目录,示例:
find /tmp \( -path /tmp/abc -o -path /tmp/bcd \) -prune -o -print
find /tmp \( -path /tmp/abc -o -path /tmp/bcd -o -path /tmp/cde \) -prune -o -print