shell实现备份日志

现网问题不方便直接定位,可以将对应模块的日志进入备份,实现如下,初学阶段,欢迎各位大佬疯狂踩踏

#!/bin/bash
read -p "请输入备份日志的存放路径:" backup_log_path
if [ ! -d "$backup_log_path" ]; then
        mkdir -p $backup_log_path
fi
# 备份DNS日志
function DNSLOGS() {
	cat /usr/local/xxxx*.log |egrep "$dur_day" >$backup_log_path/backup_xxxx.log
}
# 备份IPAM+DHCP日志
function IPDHLOGS() {
	cp /usr/local/cccc*.log |egrep "$dur_day" >$backup_log_path/backup_cccc.log
}
# 备份所有日志
function ALLLOGS() {
	cp /usr/local/*.log /$backup_log_path/
}
# 压缩日志
function TARLOGS() {
	echo "######开始备份日志######"
	ALLLOGS
	log_file_num=$(ls $backup_log_path/*|wc -l)
	nohup tar -zcvf $backup_log_path/AllLogs.tar.gz $backup_log_path/*.log --remove-files > $backup_log_path/../tarfile.txt 2>&1 &
	while true
		do
			tar_log_file_num=$(cat $backup_log_path/../tarfile.txt|wc -l)
			if [ $tar_log_file_num -eq $log_file_num ];then
				echo "######备份日志完成######"
				break
			else
				echo -ne "$tar_log_file_num|$log_file_num\r"
				sleep 0.1
			fi
		done
	echo 请前往$backup_log_path目录下获取日志文件
}

# 获取日志时间区间
function TIMES {
	$(> day.txt)
	read -p "需要备份最近几天的日志?:" day
	for((i=0;i<$day;i++));  
		do   
			echo -n `date -d "$i day ago" +%Y-%m-%d`\| >> day.txt
		done
	dur_day=`sed -i 's/.$//' day.txt && cat day.txt`
}
# 清理临时文件
function CLEAR {
	rm -rf $backup_log_path/../nohup.out $backup_log_path/../tarfile.txt
}
while true; do
    select input in dns_log dhcp_log ddi_log all_log quit; do
        case $input in
             dns_log)
					TIMES
					DNSLOGS
                     ;;
            dhcp_log)
                     echo "dhcp_log in /root"
                     ;;
            ddi_log)
                     echo "ddi_log in /etc"
                     ;;
			all_log)
					ALLLOGS
					CLEAR
					;;
               quit)
                     exit 0
                     ;;
               *)
                    echo "---------------------------------------"
                    echo "Please enter the number." 
                    echo "---------------------------------------"
                    break
                    ;;
        esac
    done
done

你可能感兴趣的:(Shell)