Liunx下的日志清理shell脚本实战之日志备份
生活随笔
收集整理的這篇文章主要介紹了
Liunx下的日志清理shell脚本实战之日志备份
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Liunx下的日志清理shell腳本實戰
- 一、腳本實現功能以及要求
- 二、shell腳本內容
一、腳本實現功能以及要求
二、shell腳本內容
一、腳本實現功能以及要求
1.日志備份目錄,將日志備份到/mnt/backup/log中,達到即使系統重啟,日志不會丟失的效果。
2.設置備份目錄的最大容量。例如設置500M,超過這個大小,程序將不斷最舊的備份日志刪除,直到可以容納新日志。
3.白名單功能。不重要的日志無需備份,直接刪除,重要日志定期備份。
4.日志文件格式,新鮮的日志以log結尾,且在白名單的重要日志打上時間戳。
5.程序行為,程序每隔10秒掃描一次/var/log 下最新日志,如果超過5M大小,且在白名單中,將其備份,原日志刪除;不在白名單中,直接刪除。檢查備份目錄是否超過了備份目錄的最大容量,如果超過,則會將最老的日志備份刪除。
二、腳本內容
#!/bin/bash ########################################################## #File Name:backup_log.sh #Version:V1.0 #Aurhor:IT #Emali: #Created Time:2021-04-28 11:45:14 #Description: backup log ###########################################################maximum log size alarmrate=500 #the max size file can reach file_max_size=5 #sourece log directory log_ram_dir=/var/log #backup log directory working_dir=/mnt/backup/log #This is the frequency our program runs SLEEPTIME=5#Append YEAR.MONTH.DAY and timestamp to log file { timestamp=$(date +%Y%m%d%H%M%S) RETVAL=$1.$timestamp }#search dir to fetch the oldest log #尋找最舊的日志文件 searchdir () {oldestlog=$(ls -rt|head -n 1 |awk '{print $1}') }#clear old log #刪除工作目錄下的舊日志clear_old_log_under_working_dir () {cd $working_dirwhile true;dologsize=$(du -ms $working_dir |awk '{print $1}')if [ $logsize -gt $alarmrate ];thensearchdirrm -rf $oldestlogelsebreak;fidone }#this is the main process of our log backup activity #主備份程序backuplog_process () {cd $log_ram_dirfor i in * ; dofile_size=$(du -ms $i |awk '{print $1}')case $i inerror.* | mail* | cron* | message* | last* | cpu* | lpr* | dnf* | boot* | hawkey.log )if [ ! -d $workding_dir ];thenmkdir -p $working_dirfiif [ $file_size -gt $file_max_size ];thenfilenameConvert $icp -p $log_ram_dir/$i $working_dir/$RETVELecho "" > $log_ram_dir/$iclear_old_log_under_working_dirfi;;*)if [ $file_size -gt $file_max_size ];thenecho "" > $log_ram_dir/$ifiesac done }#SLEEPTIME=5#while true ; do #backuplog_process #sleep $SLEEPTIME #doneexit 20總結
以上是生活随笔為你收集整理的Liunx下的日志清理shell脚本实战之日志备份的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux的shell脚本函数教程
- 下一篇: Zabbix的客户端安装教程