一、日志切割脚本
#!/bin/bash #This script is used to cut the log log_file=( /usr/local/nginx/logs/log1.log ) for log in ${log_file[*]} do [ -f "$log" ]||{ #echo "$log_file not exist!" continue; } cp $log ${log}_$(date -d "yesterday" +"%F").log [ $? -eq 0 ]&&{ cat /dev/null > $log } done
二、日志压缩
#!/bin/sh #unzip "N" days ago log Date=-1 Log_path=( "/usr/local/nginx/logs/" ) Log_type=( "log1.log_*.log" ) for path in ${Log_path[*]} do if [ -d $path ];then for type in ${Log_type[*]} do find $path -mtime $Date -name "$type" -exec gzip {} \; #find $path -mtime $Date -name "$type" -exec gunzip {} \; done fi done
三、日志归档
#!/bin/bash Scp_tool() #作者:fxl #版本:V2019.08.01.01 #依赖:expect #功能:该函数可免密往多个主机的同一目录同时传输单个或多个文件。 { #需要传输的主机ip Transfer_ips=( 172.27.6.xx ) #需要传输的文件,绝对路径 ,单个或者多个文件均可 Transfer_file=( /usr/local/nginx/logs/log1.log_*.log.gz ) #需要传输的目的主机目录 Transfer_path=/data/log_bak/172.16.8.xx #需要传输的目的主机目录 Transfer_pwd=xxxxxx for node in ${Transfer_ips[*]} do expect -c " set timeout -1 spawn bash -c \"scp ${Transfer_file[@]} [email protected]${node}://$Transfer_path\" expect { \"Are you sure you want to continue connecting (yes/no)?\" {send \"yes\r\"; exp_continue} \"password\" {send \"${Transfer_pwd}\r\"; exp_continue} } expect eof" >/dev/null 2>&1 done } Scp_tool
四、日志删除
#!/bin/sh #Delete "N" days ago log Date=-1 Log_path=( "/usr/local/openresty/nginx/logs/" ) Log_type=( "log1.log_*.log.gz" ) for path in ${Log_path[*]} do if [ -d $path ];then for type in ${Log_type[*]} do find $path -mtime $Date -name "$type" -exec rm -rf {} \; #find $path -name "$type" -exec rm -rf {} \; done fi done
精彩评论