每天定时备份系统日志

文章目录

        • 1.脚本内容
        • 2.定时任务
        • 3.执行脚本备份测试
        • 4.查看执行结果

每天零点定时备份目录,/var/log/messages; /var/log/secure; /var/log/audit/audit.log;

1.脚本内容

liang:~ #cat /scritp/rizhi.sh 
#!/bin/bash
backup(){

#log list
log_messages=/var/log/messages
log_secure=/var/log/secure
log_audit=/var/log/audit/audit.log

#backup dir
dir_backup=/backup/log
dir_date=`date +%F -d "- 1day"`
[ -d ${dir_backup}/${dir_date} ] || mkdir ${dir_backup}/${dir_date}

#backup
cp -a ${log_messages} ${dir_backup}/${dir_date}/
cp -a ${log_secure} ${dir_backup}/${dir_date}/
cp -a ${log_audit} ${dir_backup}/${dir_date}/
}
backup

2.定时任务

liang:/scritp #crontab -l|tail -1
00 00 * * * /bin/bash /scritp/rizhi.sh

3.执行脚本备份测试

liang:~ #date +%F
2019-07-29
liang:~ #bash -x /scritp/rizhi.sh    
+ backup
+ log_messages=/var/log/messages
+ log_secure=/var/log/secure
+ log_audit=/var/log/audit/audit.log
+ dir_backup=/backup/log
++ date +%F -d '- 1day'
+ dir_date=2019-07-28
+ '[' -d /backup/log/2019-07-28 ']'
+ mkdir /backup/log/2019-07-28
+ cp -a /var/log/messages /backup/log/2019-07-28/
+ cp -a /var/log/secure /backup/log/2019-07-28/
+ cp -a /var/log/audit/audit.log /backup/log/2019-07-28/

4.查看执行结果

liang:~ #ll /backup/log/2019-07-28/
total 1752
-rw------- 1 root root 1782685 Jul 29 15:26 audit.log
-rw------- 1 root root    1557 Jul 29 14:46 messages
-rw------- 1 root root    2636 Jul 29 14:39 secure

你可能感兴趣的:(Linux运维,shell脚本练习,shell)