標籤:shell
指令碼1
#!/bin/bashDATE=$(date +%Y%m%d%H%M)NAME=$(echo $1 | awk -F‘,‘ ‘{print NF}‘)LOG_PATH="$2"if [ $# -ne 2 ];then echo "USAGE: sh $0 log_name1,log_name2 log_path" exitfifor NUM in `seq 1 $NAME`do LOG_NAME=$(echo $1 | cut -d ‘,‘ -f $NUM) for i in {1..6} do ansible -m synchronize -a "mode=pull src=${LOG_PATH}${LOG_NAME} dest=/mnt/dx${i}${LOG_NAME}" dx${i}sock2 done for i in 1 2 3 4 do ansible -m synchronize -a "mode=pull src=${LOG_PATH}${LOG_NAME} dest=/mnt/lt${i}${LOG_NAME}" lt${i}sock2 done for i in 179 yy 360 9377 do ansible -m synchronize -a "mode=pull src=${LOG_PATH}${LOG_NAME} dest=/mnt/${i}${LOG_NAME}" ${i}sock2 done tar zcvf $DATE-${LOG_NAME}.tar.gz *${LOG_NAME} && rm -rf /mnt/*${LOG_NAME}doneNUMBER=$(ls *.tar.gz | wc -l)if [ $NUMBER -gt 1 ];then TAR_NAME=$(echo ${LOG_NAME} | awk -F‘.‘ ‘{print $1}‘) tar zcvf ${DATE}-all-${TAR_NAME}.tar.gz *.tar.gz rm -rf /mnt/${DATE}-${TAR_NAME}*.tar.gzfi
指令碼2
#!/bin/bashDATE=$(date +%Y%m%d%H%M)#NAME=$(echo $1 | awk -F‘,‘ ‘{print NF}‘)SHU="$1"NAME="$2"LOG_PATH="$3"if [ $# -ne 3 ];then echo "USAGE: sh $0 number log_name log_path" exitfifor NUM in `seq 1 $SHU`do for i in {1..6} do LOG_NAME=$(ansible -m shell -a "cd ${LOG_PATH};ls -l -t ${NAME}.* | awk ‘{print $9}‘ | head -${NUM} | tail -1" dx${i}sock2 | tail -1 | awk ‘{print $9}‘) ansible -m synchronize -a "mode=pull src=${LOG_PATH}${LOG_NAME} dest=/mnt/dx${i}${LOG_NAME}" dx${i}sock2 done for i in 1 2 3 4 do LOG_NAME=$(ansible -m shell -a "cd ${LOG_PATH};ls -l -t ${NAME}.* | awk ‘{print $9}‘ | head -${NUM} | tail -1" lt${i}sock2 | tail -1 | awk ‘{print $9}‘) ansible -m synchronize -a "mode=pull src=${LOG_PATH}${LOG_NAME} dest=/mnt/lt${i}${LOG_NAME}" lt${i}sock2 done for i in 179 yy 360 9377 do ansible -m synchronize -a "mode=pull src=${LOG_PATH}${LOG_NAME} dest=/mnt/${i}${LOG_NAME}" ${i}sock2 donedonetar zcvf $DATE-${NAME}-$NUM.tar.gz *${NAME}* && rm -rf /mnt/{*.log,*.log.*}
本文出自 “YHT的營運筆記” 部落格,請務必保留此出處http://yht1990.blog.51cto.com/9014030/1926727
用shell編寫批量打包日誌指令碼