Rsync设置
Rsync服务端
vim /etc/rsyncd.conf
uid = root
gid = root
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
secrets file = /etc/rsyncd.secrets
auth users = root
是和系统用户名相同的。
[log]
path = /LogBak
read only = no
echo "root:rootlogpasswd" && chmod 600 /etc/rsyncd.secrets
/usr/bin/rsync --daemon && echo "/usr/bin/rsync --daemon" >> /etc/rc.Local && chmod +x /etc/rc.local
Rsync客户端
echo "export RSYNC_PASSWORD=rootlogpasswd" >> /etc/profile && source /etc/profile
传输文件命令
rsync 文件路径 root@192.168.10.27::log
示例: 传输文件,并生成本机客户端ip地址为文件路径存储
rsync -avz /var/log/* root@192.168.10.27::log/$(ifconfig $( ip route show | awk '/default/ { print $5 }' ) |sed -n 2p |awk -F ' ' '{print$2}' |sed 's/.*://')/
Ansible设置
host文件
在host文件里直接设置日志路径等其他变量,待会直接在yaml文件中调用
cat host
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
[zzfwzyk]
192.168.1.128 ansible_host=192.168.1.128 nginx_log_dir=/var/web/nginx/logs nginx_pid=/var/web/nginx/logs tomcat_log_dir= tomcat_pid=
Yaml文件
cat log.yaml
---
- hosts: "{{ host }}"
gather_facts: No
tasks:
- name: add time_log
blockinfile:
path: /etc/bashrc
marker_begin: "start logvariate"
marker_end: "end logvariate"
insertafter: "End of file"
state: present
block: |
export RSYNC_PASSWORD=rootlogpasswd
export TIME_LOG=$(date -d "-1 day" +%Y-%m-%d)
export NGINX_LOG_DIR={{ nginx_log_dir }}
export TOMCAT_LOG_DIR={{ tomcat_log_dir }}
- name: source profile
shell: |
source /etc/bashrc
args:
executable: /bin/bash
- name: create nginxlog rule
shell: touch /etc/logrotate.d/nginxlog
args:
executable: /bin/bash
- name: nginxlog incise rule
blockinfile:
path: /etc/logrotate.d/nginxlog
marker_begin: "start nginxlog"
marker_end: "end nginxlog"
insertafter: "End of file"
state: present
block: |
{{ nginx_log_dir }}/*.log {
daily
rotate 10
missingok
nocompress
nodelaycompress
copytruncate
dateext
dateformat -%Y-%m-%d
dateyesterday
postrotate
if [ -f {{ nginx_pid }}/nginx.pid ];then
kill -USR1 `cat {{ nginx_pid }}/nginx.pid`
fi
endscript
}
- name: create tomcatlog rule
shell: touch /etc/logrotate.d/tomcatlog
args:
executable: /bin/bash
- name: tomcatlog incise rule
blockinfile:
path: /etc/logrotate.d/tomcatlog
marker_begin: "start tomcatlog"
marker_end: "end tomcatlog"
insertafter: "End of file"
state: present
block: |
{{ tomcat_log_dir }}/*.log {
daily
rotate 10
missingok
nocompress
nodelaycompress
copytruncate
dateext
dateformat -%Y-%m-%d
dateyesterday
postrotate
if [ -f {{ tomcat_pid }}/tomcat.pid ];then
kill -USR1 `cat {{ tomcat_pid }}/tomcat.pid`
fi
endscript
}
- name: nginxlog incise rule
blockinfile:
path: /var/spool/cron/root
marker_begin: "start cronloh"
marker_end: "end cronlog"
insertafter: "End of file"
state: present
block: |
echo "00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginxlog" >> /var/spool/cron/root
echo "05 00 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcatlog" >> /var/spool/cron/root
echo "10 00 * * * rsync -avz $NGINX_LOG_DIR/*$TIME_LOG root@192.168.10.27::log/$(ifconfig $( ip route show | awk '/default/ { print $5 }' ) |sed -n 2p |awk -F ' ' '{print$2}' |sed 's/.*://')/" >> /var/spool/cron/root
echo "20 00 * * * rsync -avz $TOMCAT_LOG_DIR/*$TIME_LOG root@192.168.10.27::log/$(ifconfig $( ip route show | awk '/default/ { print $5 }' ) |sed -n 2p |awk -F ' ' '{print$2}' |sed 's/.*://')/" >> /var/spool/cron/root
|