
#!/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################################# # The path to the required migration log server is:/root/yunwei/ # # We need to configure this machine root to Xitong users of the log server.It only needs to be modified: [backup log path]="Number of days retained" # # Backup log:/app/logs/yunwei/qianyi.log # # The script can be executed before or after 5 o'clock every day, 14 days later.It is very popular in the world.""DIC=( [/app/lcagreemer8080/logs]="7" [/app/lcagree9901/logs]="30" ) #Take care not to take the path at last.'/' ########################################################################################################################## 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} #Get the OSS pathif [ ! -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}Start backup!" >> /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}There is no need to backup files!" >> /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}:Backup success" >> /app/logs/yunwei/qianyi.log rm -rf "$log_file" elif [ "$md5" == "d41d8cd98f00b204e9800998ecf8427e" ];then echo "${log_file}:Empty file." >> /app/logs/yunwei/qianyi.log rm -rf "$log_file" else echo "${log_file}:Backup failed." >> /app/logs/yunwei/qianyi.log fi done < /root/yunwei/loglist_src.txt echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}End of backup!" >> /app/logs/yunwei/qianyi.log else echo "${time} The log server upload_log.sh script is running!" >> /app/logs/yunwei/qianyi.log exit fi done
Original edition

#!/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########################################## # The path to the required migration log server is:/root/yunwei/ # # We need to configure this machine root to Xitong users of the log server.It only needs to modify the path in DIC: [backup log path]="Number of days retained" # # If you need regular execution, you can add the following crontab:#Be0 1 * * * /bin/bash /root/yunwei/log_bak.sh >> /dev/null 2>&1 # # Script log:/app/logs/yunwei/qianyi.log # # Scripts can be executed before or after 5 o'clock every dayIt is very popular.It is very popular.DIC=( [/app/np2p/lonsyn]="15" [/app/np2p/loaner_recv_detl]="15" [/app/np2p/obli_right]="15" ) #Take care not to take the path at last.'/' ########################################################################################################################## 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} #Get the OSS path[! -d /app/logs/yunwei ] && mkdir -pv /app/logs/yunwei for i in ${!dic[*]};do echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}Start backup!" >> /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}There is no need to backup files!" >> /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}:Backup success" >> /app/logs/yunwei/qianyi.log rm -rf ${file} else echo "${file}:Backup failed." >> /app/logs/yunwei/qianyi.log fi done echo "`date +"%Y-%m-%d %H:%M:%S"` ${i}End of backup!" >> /app/logs/yunwei/qianyi.log done
Revised version BUG