日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Liunx下的日志清理shell脚本实战之日志备份

發布時間:2025/4/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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脚本实战之日志备份的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。