一个简单的清理 Linux 日志的脚本

2019-04-16 1898点热度 0人点赞

一台服务器运行一段时间后,系统就会产生各种各样的日志,而这些日志其实暴漏了系统的很多信息,这时候就需要定期清理使用痕迹


额外运行一次 history -c 清理自己的命令列表

简单的清理脚本如下

#!/bin/bash

if [ -f "/var/log/boot.log" ]; then cat /dev/null > /var/log/boot.log; fi
if [ -f "/var/log/alternatives.log" ]; then cat /dev/null > /var/log/alternatives.log; fi
if [ -f "/var/log/apport.log" ]; then cat /dev/null > /var/log/apport.log; fi
if [ -f "/var/log/auth.log" ]; then cat /dev/null > /var/log/auth.log; fi
if [ -f "/var/log/bootstrap.log" ]; then cat /dev/null > /var/log/bootstrap.log; fi
if [ -f "/var/log/btmp" ]; then cat /dev/null > /var/log/btmp; fi
if [ -f "/var/log/dmesg" ]; then cat /dev/null > /var/log/dmesg; fi
if [ -f "/var/log/dpkg" ]; then cat /dev/null > /var/log/dpkg; fi
if [ -f "/var/log/fail2ban" ]; then cat /dev/null > /var/log/fail2ban; fi
if [ -f "/var/log/firewalld" ]; then cat /dev/null > /var/log/firewalld; fi
if [ -f "/var/log/kern" ]; then cat /dev/null > /var/log/kern; fi
if [ -f "/var/log/lastlog" ]; then cat /dev/null > /var/log/lastlog; fi
if [ -f "/var/log/syslog" ]; then cat /dev/null > /var/log/syslog; fi
if [ -f "/var/log/wtmp" ]; then cat /dev/null > /var/log/wtmp; fi
if [ -f "~/.bash_history" ]; then cat /dev/null > ~/.bash_history; fi
if [ -f "~/.mysql_history" ]; then cat /dev/null > ~/.mysql_history; fi
if [ -f "~/.viminfo" ]; then cat /dev/null > ~/.viminfo; fi

rm -f /var/log/*.gz
rm -f /var/log/*.1

journalctl --rotate
journalctl --vacuum-time=1s
journalctl --vacuum-size=1B

CentOS 可能需要这个

#!/bin/bash
cat /dev/null > /var/log/boot.log
cat /dev/null > /var/log/btmp
cat /dev/null > /var/log/cron
cat /dev/null > /var/log/dmesg
cat /dev/null > /var/log/firewalld
cat /dev/null > /var/log/grubby
cat /dev/null > /var/log/lastlog
cat /dev/null > /var/log/mail.info
cat /dev/null > /var/log/maillog
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/secure
cat /dev/null > /var/log/spooler
cat /dev/null > /var/log/syslog
cat /dev/null > /var/log/tallylog
cat /dev/null > /var/log/wpa_supplicant.log
cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/yum.log
cat /dev/null > /var/log/tuned/tuned.log
rm -f /var/log/*-20* > /dev/null 2>&1
if [ -d "/var/log/anaconda/" ] ; then
cat /dev/null > /var/log/anaconda/anaconda.log > /dev/null 2>&1
cat /dev/null > /var/log/anaconda/ifcfg.log > /dev/null 2>&1
cat /dev/null > /var/log/anaconda/journal.log > /dev/null 2>&1
cat /dev/null > /var/log/anaconda/packaging.log > /dev/null 2>&1
cat /dev/null > /var/log/anaconda/program.log > /dev/null 2>&1
cat /dev/null > /var/log/anaconda/storage.log > /dev/null 2>&1
cat /dev/null > /var/log/anaconda/syslog > /dev/null 2>&1
cat /dev/null > /var/log/anaconda/X.log > /dev/null 2>&1
fi
if [ -d "/var/log/audit/" ] ; then
rm -f /var/log/audit/audit.log* > /dev/null 2>&1
cat /dev/null > /var/log/audit/audit.log > /dev/null 2>&1
fi
cat /dev/null > ~/.bash_history
cat /dev/null > ~/.mysql_history
cat /dev/null > ~/.viminfo

StarryVoid

Have a good time