[root@deploy scripts]# cat logs_bak_v2.sh bash
#!/usr/bin/env bash # Description:查找兩天前修改的應用日誌,打包備份以後後刪除 # DATE:2019-06-15 # 加載系統函數 . /etc/init.d/functions # 加載環境變量 export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin #自定義變量 DATE="`date +%F'-'%H-%M`" IP="`ifconfig |awk 'NR==2{print $2}'`" ###須要適當改動 LOG_FILE="/tmp/backup.log" APP_LOGDIR="/opt/payment/logs" ###須要適當改動 BACK_DIR="/opt/payment/HW-backup/logbackup" APP="`ls ${APP_LOGDIR}`" #定義日誌輸出 output_ok_msg () { for msg in $*;do action ${msg} /bin/true done } output_fail_msg () { for msg in $*;do action ${msg} /bin/false done } #應用日誌備份函數 log_back () { for i in $*;do cd ${APP_LOGDIR}/${i} find . -mtime +2 -type f | xargs tar zcvf ${BACK_DIR}/${APP}/${IP}_${i}_${DATE}.tar.gz >/dev/null 2>&1 if [ $? -eq 0 ];then find . -mtime +2 -type f | xargs rm >/dev/null 2>&1 output_ok_msg ${i}日誌備份完成 echo ${DATE}: ${i}日誌備份完成 >>${LOG_FILE} else output_fail_msg ${i}未找到符合條件的應用日誌 echo ${DATE}: ${i}未找到符合條件的應用日誌 >>${LOG_FILE} fi #find . -mtime +1 -type f -exec tar zcvf /opt/back/${IP}_${i}_${DATE}.tar.gz {} \; #find . -mtime +1 -type f -exec cp {} /opt \; #find . -mtime +1 -type f -exec rm {} \; done } log_back ${APP}