Linux 下日志与备份

klogd 内核,专门负责记录内核产生的日志信息
rsyslogd 系统自带的日志管理工具,非内核产生的信息

RSYSLOG is the rocket-fast system for log processing.

/etc/rsyslog.conf 配置文件

facility.priority               action
设备.级别                      动作(绝对路径)      #级别越低越详细
mail.info						/var/log/mail.log		#mail级别为info及以上的信息记录到mail.log文件中	
mail.info						-/var/log/mail.log		#“-”表示异步写入
mail.info						@192.168.1.110          #写到开启远程记录的服务器中
user.!=error      									    #记录与user相关的,不包括error级别的信息
user.!error 											#记录比error级别低的所有信息
user.*;user.!=info    									#user所有信息,不包括info级别的信息
cron.*   												#打印全部定时任务信息
*.info  	 											#任何信息只要比info等级高就打印 
*.emerg                           *   				#发送给所有系统上的用户(系统关键信息)
.none 													#禁止任何消息         
local0 到 local7    									#用户处定义使用

syslog.h下的syslog、openlog等函数的解析和应用
https://www.cnblogs.com/langqi250/archive/2012/11/14/2769544.html

servcie syslog reload 修改配置后重载,不丢失日志写入

设置 vim /etc/sysconfig/syslog 开启服务器远程日志记录

 # -r enables logging from remote machines
SYSLOGD_OPTIONS="-m -r 0"
logger -i -p local6.err test.sh “This is  tom”         	#写入日志测试
[root@ton ~]#tail /var/log/messages
...
Apr 14 13:53:43 ton tom[5086]: test.sh “This is tom”
系统启动及登陆日志
  1. kernel --> 物理终端(/dev/console) --> /var/log/ dmesg
    #cat /var/ log/ dmesg
  2. / sbin/init
    / var/ iod/ megsages #系统标准错误日志信息;非内核产生引导信息;各子系统产生的信息;
dmesg							#启动信息
cat /var/log/dmesg             #查看init脚本运行之前的系统日志
last        		#最近登陆用户的信息(显示的是/var/log/wtmp上二进制文件的内容)
lastlog   			#最后一次登陆信息
lastb    			#登陆错误的用户log  或者  last -f btmp   
strings smtp        #查看二进制的smtp登陆log文件的内容 

tail /var/log/messages
设置控制台信息输出
dmesg -n3  #则只输出emerg,alert,crit信息至终端,不会打印错误信息

0: KERN_EMERG,系统不可用
1: KERN_ALERT,必须立即执行
2: KERN_CRIT,紧急状态
3: KERN_ERR,错误状态
4: KERN_WARNING,警告状态
5: KERN_NOTICE,正常状态且十分重要
6: KERN_INFO,报告
7: KERN_DEBUG,debug-level信息
日志滚动

/etc/logrotate.conf 日志轮询配置可配置指定源码包安装的软件的日志处理
logrotate是由crond运行的,例如/etc/cron.dail/logrotate

# use date as a suffix of the rotated file
 dateext   日期格式命名
 # keep 4 weeks worth of backlogs 保留4个轮替日志
rotate 4

monthly       #每个月轮替一次
create 0664 root utmp #创建新的日志文件 权限 所属用户 所属组
minsize 1M    #日志大小大于1M后才能参与轮替
rotate 1      #保留一个轮替日志文件
missingok   #如果日志文件不存在,继续进行下一个操作,不报错
size  1M       #日志达到1M后进行rotate
notifempty     #若日志为空,则不进行轮替
sharedscripts   #作用域下文件存在至少有一个满足轮替条件的时候,执行一次prerotate脚本和postrotate脚本

logrotate -v /etc/logrotate.conf 记录日志详细
-d: 详细显示指令执行过程,便于排错或了解程序执行的情况
logwatch --print 日志监视工具

备份
  1. 备份分区 ro,umont
  2. 压缩
  3. 校验 md5sum -c
  4. 加密 GnuPG PGP 非对称密钥加密

cp -p 保持原本的属性
cp -P(大写) 保持原本目录结构
cp -u 较目标文件新时拷贝
cp -b a.txt tmp/ 复制时产生备份文件

dump命令
tom.bak.20201003   <-----备份命名时这样比较好

tar   -t 不解包光查看
      -rf 追加文件
	  -uf /path/   更新文件内容 加对应目录或文件

----->|<--  等级0-9 
dump -0uj -f /root/boot.bak.bz2 /boot      先完全备份,并更新备份时间

cat /etc/dumpdates 查看备份记录
dump -1uj -f /root/boot.bak.bz2 /boot     增量备份 
dump -0j -f   只能完全备份,不支持增量备份

dump -W 查看分区备份的信息
-u  记录备份时间到/etc/dumpdates
-j  压缩备份成.bz2格式 
[root@ton ~]#dump -0uj -f /home/tom/tom.bak.bz2 ./tt/
  DUMP: You can't update the dumpdates file when dumping a subdirectory
  DUMP: The ENTIRE dump is aborted.

上述报错,不能使用DUMP来备份子目录,DUMP只在分区上工作,可使用tar备份目录

restore命令
  • -C 比较备份数据和源数据
  • -i 交互模式
  • -t 查看模式
  • -r 还原模式
  • -f 指定文件

restore -tf /etc/somefile.bak  在查看备份文件中的内容
restore -r -f /etc/somefile.bak

[root@ton ~]#restore -i -f /home/tom/tom.bak.bz2 
Dump tape is compressed.
restore > ls
.:
root/

restore > cd /root
restore > ls
./root:
tt/

restore > add tt/       #还原文件夹
restore: ./root: File exists
restore: ./root/tt: File exists
restore > ls
./root:
*tt/

restore > extract        #还原
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
set owner/mode for '.'? [yn] y
restore > q
[root@ton root]#ls -al /root/root/tt/
total 12
drwxr-xr-x  2 root root 4096 Mar  9 20:29 .
dr-xr-x---. 3 root root 4096 Mar  9 20:29 ..
-rwxr-xr-x  1 root root  621 Mar  9 20:29 change.sh

参考https://my.oschina.net/u/4268163/blog/4006391

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值