ansible分发日志清理脚本

Posted sseban

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible分发日志清理脚本相关的知识,希望对你有一定的参考价值。

 

日志清理目前覆盖58台服务器如下

此处省略
#!/bin/sh
. /etc/rc.d/init.d/functions
currTime=$(date +"%Y-%m-%d %T")
logpath=/home/local/logs
TarlogPath=/data/home/deploy/output/logs/his/
Host=`hostname`
#find $TarlogPath -type f -mtime  -5 -exec tar -zcfP backup$currTime_.tar.gz "{}" ;
find $TarlogPath -type f -mtime  -5 |xargs tar zcfP backup_$Host.tar.gz
#清理/data/home/deploy/output/logs/his/历史log.gz
find $TarlogPath -type f -mtime +15 -exec rm -rfv {} ;  &>/dev/null
if [ $? -eq 0 ];then
action  "清理日志"  /bin/true
else
action  "清理失败"  /bin/false
fi

 

BS003获取被控制端的备份日志

[root@BS003 backup]# ansible 192.168.16.211 -m fetch -a ‘src=/data/home/deploy/output/logs/backup_MC005.tar.gz  dest=/tmp/backup‘ 

 

BS003删除被控端的备份日志

[root@BS003 log]#  ansible 192.168.16.211 -m file -a ‘path=/data/home/deploy/output/logs/backup_MC005.tar.gz state=absent‘

 

被控端定时清理log

0 2 * * * /bin/sh /server/scripts/log.sh  每天凌晨2点进行清理日志和备份

 

ansible服务端每天3点pull 被控端的tar.gz 每天凌晨4点进行删除控制端的tar.gz

0 3 * * * /bin/bash /server/scripts/cront_pull.sh

0 4 * * * /bin/bash /server/scripts/cront_delete.sh

 

 

批量操作流程实现:

ansible-ploybook

  1. ansible服务端先推送shell 脚本到被控端的/server/scripts目录下和执行权限
  2. 定义被控端定时任务清理(每天凌晨2点备份及清理)
  3. ansible服务端定时拉取被控端备份tar(每天凌晨3点)
  4. ansible 服务端删除被控端备份的tag

 

- hosts: log
  tasks:
    - name: copy file shell 
      copy:
        src: /server/scripts/probject/log.sh 
        dest: /server/scripts
        mode: 0755
    - name: Delete log regularly
      cron:
        minute: 0
        hour: 2
        day: *
        month: * 
        weekday: *
        name: Delete log regularly
        job: /bin/bash /server/scripts/log.sh


从远端拉取tar包到本地
ansible 192.168.16.210 -m fetch -a ‘src=/home/deploy/backup_MC004.tar.gz  dest=/tmp/backup‘

删除远端tar包
ansible 192.168.16.210 -m file -a ‘path=/data/home/deploy/output/logs/backup_MC004.tar.gz  state=absent‘

 

以上是关于ansible分发日志清理脚本的主要内容,如果未能解决你的问题,请参考以下文章

生产环境日志清理脚本

ansible批量拉取远端文件

常用python日期日志获取内容循环的代码片段

日志清理脚本

Linux定时清理磁盘日志文件——Shell脚本

日志清理脚本