天天看點

日志管理

日志管理

1.日志備份,以/var/log/messages為例

将本機/var/log/messages備份到174的/tmp下

# rsync -av /var/log/messages [email protected]:/tmp

#-a, --archive 歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性

# -v, --verbose 詳細模式輸出 

2.日志打包

# tar -cf log_file.tar /var/log/messages #-c建立包;-f包名稱

3.日志清理

保留指定日志的後3行,其餘全部删除

#!/bin/bash

#Usage:clear logfile,save last 3 lines.

#Author:chengyanli

#Date:2016/07/27

logdir=/var/log/ #the logdir

cd ${logdir}

filelines=`cat boot.log|wc -l` #the total lines of the logfile

dellines=$[${filelines}-3] #the lines you want to del

if [ ${dellines} -ge 1 ]; then #judge the dellines bigger than 1 or not

sed -i "1,${dellines}d" boot.log #del the line:1-dellines,save the last 3 lines

fi

4.日志完整性檢查,使用md5

服務端:10.1.16.173

# md5sum tallylog > tallmd5

# mkdir check2

# mv tallylog check2/

# mv tallmd5 check2/

# scp -r check2/ 10.1.16.174:

用戶端:10.1.16.174

# cd check2/ #一定要進入此目錄

# md5sum -c tallmd5 #将校驗碼檔案與源檔案進行比對,比對上了,成功

日志管理

修改tallylog,再進行校驗,出錯

日志管理

也可以指明檔案名

日志管理

5.日志提取并上傳到ftp的ftp目錄中

awk '{if($0~"Installed")print}' yum.log #提取yum.log中包含Installed關鍵字的行

tail -n 3 boot.log #檢視檔案後3行内容

head -n 3 boot.log #檢視檔案前3行内容

uniq -c #去重并輸出重複的個數

sort #排序

日志管理
日志管理
日志管理

繼續閱讀