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 # 这个授权的用户名,是给rsync指定的用户名,可以和server当前的系统用户名相同,也可以不同。这里测试用的
是和系统用户名相同的。
[log] # log模块名,可以随意取,但是在做同步指令操作的时候,cms就是目标机器server上的根目录,这里,设定的这个根目录/LogBak 。
path = /LogBak
read only = no
echo "root:rootlogpasswd" && chmod 600 /etc/rsyncd.secrets #写入rsync连接密码,格式为 用户名:密码
/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_private_key_file=/root/yangshuo
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
######## 切割nginx规则 #################
- 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 # 这里要改对应机器nginx 的PID位置
kill -USR1 `cat {{ nginx_pid }}/nginx.pid` # 这里要改对应机器nginx 的PID位置
fi
endscript
}
######## 切割 Tomcat 日志规则 #################
- 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 # 这里要改对应机器 tomcat 的PID位置
kill -USR1 `cat {{ tomcat_pid }}/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}}/*$(date -d "-1 day" +%Y-%m-%d) 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}}/*$(date -d "-1 day" +%Y-%m-%d) 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