#!/bin/bash
set -o nounset
declare -A dic
#This script is used for backup logs to oss!
#Time 2017/11/12
#Author guoyunlong  gongxiaopo
###############Warning#################################
# 脚本放到需迁移日志服务器的路径为:/root/yunwei/     #
# 要先配置此机器root到日志服务器的xitong用户免密      #
# 只需修改:[备份日志所在路径]="保留天数"                        #
# 备份日志:/app/logs/yunwei/qianyi.log                              #
# 脚本可在每天5点之前或者14点以后执行                              #
#######################################################
dic=(
[/app/lcagreemer8080/logs]="7"
[/app/lcagree9901/logs]="30"
)
#注意路径最后不要带'/'


##########################################################################################################################
IP_NEI=`cat /etc/sysconfig/network-scripts/ifcfg-eth0 |grep IPADDR|cut -d = -f2`
if [ -f /etc/sysconfig/network-scripts/ifcfg-eth1 ]
then
        IP_WAI=`cat /etc/sysconfig/network-scripts/ifcfg-eth1 |grep IPADDR|cut -d = -f2`
else
        IP_WAI=no-wai-ip
fi
PATH_OSS=/databackup/log/${IP_NEI}_${IP_WAI}             #得到OSS路径

if [ ! -d /app/logs/yunwei ];then mkdir -pv /app/logs/yunwei;fi
for i in ${!dic[*]};do
    echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}开始备份!" >> /app/logs/yunwei/qianyi.log
    find  $i/  -mtime +${dic[$i]} ! -name "*.gz" ! -name "*.sh" -type f |grep -v ".ssh"| xargs -i gzip {}
    find  $i/  -mtime +${dic[$i]} -name "*.gz" -type f | xargs -i  md5sum {} > /root/yunwei/loglist_src.txt
    if [ ! -s /root/yunwei/loglist_src.txt ];then 
        echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}无需要备份的文件!" >> /app/logs/yunwei/qianyi.log
        continue;fi
    dir=$(awk -F/ '{for(x=2;x<NF;++x) printf "/"$x;printf "\n"}' /root/yunwei/loglist_src.txt|sort|uniq -c|awk '{print $2}')
    for dir_name in ${dir};do
        ssh -n -o StrictHostKeyChecking=no  xitong@10.25.90.142 "mkdir -p ${PATH_OSS}${dir_name}"
    done
    for file_name in  $(awk '{print $2}'  /root/yunwei/loglist_src.txt);do
        scp  -rp ${file_name} xitong@10.25.90.142:${PATH_OSS}${file_name}
    done
    ssh -n xitong@10.25.90.142 "find ${PATH_OSS}${i}/ -type f | xargs -i md5sum {} > $PATH_OSS/loglist_dst.txt"
    scp -rp xitong@10.25.90.142:$PATH_OSS/loglist_dst.txt  /root/yunwei/
    ssh -n xitong@10.25.90.142 "ps aux|grep '/bin/sh -x /databackup/tool/log_tool/upload_log_tool/upload_log.sh'|grep -v grep" > /root/yunwei/tmp
    time=$(date +%H%M)
    if [ ! -s /root/yunwei/tmp ] && [ $time -lt 0500 -o $time -gt 0900 ];then
        while read line;do
            md5=$(echo $line|awk '{print $1}')
            log_file=$(echo    $line|awk '{print $2}')
            grep "$md5" /root/yunwei/loglist_dst.txt
            if [ "$?" == "0" -a "$md5" != "d41d8cd98f00b204e9800998ecf8427e" ];then  
                echo "${log_file}:备份成功" >> /app/logs/yunwei/qianyi.log
                rm -rf "$log_file"
            elif [ "$md5" == "d41d8cd98f00b204e9800998ecf8427e" ];then
                echo "${log_file}:为空文件。" >> /app/logs/yunwei/qianyi.log
                rm -rf "$log_file"
            else
                echo   "${log_file}:备份失败。" >> /app/logs/yunwei/qianyi.log
            fi
        done < /root/yunwei/loglist_src.txt
        echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}备份结束!" >> /app/logs/yunwei/qianyi.log
    else
        echo "${time} 日志服务器upload_log.sh脚本正在运行!"  >> /app/logs/yunwei/qianyi.log
        exit
    fi
done
原版
#!/bin/bash
set -o nounset
declare -A dic
#This script is used for backup logs to oss!
#Time 2017/11/12
#Author guoyunlong  gongxiaopo
###############Warning##########################################
# 脚本放到需迁移日志服务器的路径为:/root/yunwei/              #
# 要先配置此机器root到日志服务器的xitong用户免密               #
# 只需修改dic里的:[备份日志所在路径]="保留天数"                                        #
# 如果需要定时执行可添加如下crontab:                          #
# 0 1 * * * /bin/bash /root/yunwei/log_bak.sh >> /dev/null 2>&1 #
# 脚本的日志:/app/logs/yunwei/qianyi.log                                                            #
# 脚本可在每天5点之前或者9点以后执行                                                         #
################################################################
dic=(
[/app/np2p/lonsyn]="15"
[/app/np2p/loaner_recv_detl]="15"
[/app/np2p/obli_right]="15"
)
#注意路径最后不要带'/'


##########################################################################################################################
IP_NEI=`cat /etc/sysconfig/network-scripts/ifcfg-eth0 |grep IPADDR|cut -d = -f2`
if [ -f /etc/sysconfig/network-scripts/ifcfg-eth1 ]
then
        IP_WAI=`cat /etc/sysconfig/network-scripts/ifcfg-eth1 |grep IPADDR|cut -d = -f2`
else
        IP_WAI=no-wai-ip
fi
PATH_OSS=/databackup/log/${IP_NEI}_${IP_WAI}                    #得到OSS路径

[ ! -d /app/logs/yunwei ] &&  mkdir -pv /app/logs/yunwei
for i in ${!dic[*]};do
        echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}开始备份!" >> /app/logs/yunwei/qianyi.log
    find  $i/  -mtime +${dic[$i]} -name "*[0-9][0-9][0-9][0-9]*" ! -name "*.gz" ! -name "*.sh" ! -name "*.pid" -type f |grep -v ".ssh"|
 xargs -i gzip {}
    find  $i/  -mtime +${dic[$i]} -name "*.gz" -type f > /root/yunwei/loglist_src.txt
        if [ ! -s /root/yunwei/loglist_src.txt ];then
                echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}无需要备份的文件!" >> /app/logs/yunwei/qianyi.log
                continue;fi
        dirs=$(awk -F/ '{for(x=2;x<NF;++x) printf "/"$x;printf "\n"}' /root/yunwei/loglist_src.txt|sort|uniq -c|awk '{print $2}')
        for dir in ${dirs};do
                ssh -n -o StrictHostKeyChecking=no  xitong@10.25.90.142 "mkdir -p ${PATH_OSS}${dir}"
        done
    for file in  $(cat /root/yunwei/loglist_src.txt);do
        scp  -rp ${file} xitong@10.25.90.142:${PATH_OSS}${file}
        if [ $? -eq 0 ];then
            echo "${file}:备份成功" >> /app/logs/yunwei/qianyi.log
            rm -rf ${file}
        else
            echo   "${file}:备份失败。" >> /app/logs/yunwei/qianyi.log
        fi
    done
                echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}备份结束!" >> /app/logs/yunwei/qianyi.log
done
修改后BUG版

 

posted on 2018-08-31 11:36  浮槎北溟  阅读(125)  评论(0编辑  收藏  举报