1、确定线上服务器的日志文件名称和路径
2、一台本地服务器能连接公网,创建一个日志账户,设置密码
3、线上服务器要求:
a、确定是否已安装sshpass包
[root@iZwz9ghdadtaey1msor7gnZ sh]# rpm -qa|grep sshpass
sshpass-1.06-1.el7.x86_64
如不存在即安装yum install -y sshpass
b、写脚本传送
Such as:
[root@iZwz9ghdadtaey1msor7gnZ ~]# cat logbak.sh
#!/bin/bash
cd /opt/soft/spring-cloud/member-9076-weixin/logs/2017-05/
tar zcf mealtime-member-9076-all-$(date -d "yesterday" +%Y-%m-%d).tar.gz mealtime-member-all-$(date -d "yesterday" +%Y-%m-%d)-1.log
sshpass -p huagzl1! scp /opt/soft/spring-cloud/member-9076-weixin/logs/2017-05/mealtime-member-9076-all-$(date -d "yesterday" +%Y-%m-%d).tar.gz logbak@fwally.eicp.net:/logbak/124/
#需确认/logbak/124目录已存在
c、确认ssh是否能登录到远程服务器
[root@iZwz95ywngc71vxzcd5yx4Z ~]# ssh logbak@fwally.eicp.net
The authenticity of host ‘fwally.eicp.net (175.0.222.186)‘ can‘t be established.
ECDSA key fingerprint is d1:9e:2e:3f:50:cf:58:3c:fc:49:6d:13:e2:c4:03:95.
Are you sure you want to continue connecting (yes/no)? yes #确认yes
Warning: Permanently added ‘fwally.eicp.net,175.0.222.186‘ (ECDSA) to the list of known hosts.
logbak@fwally.eicp.net‘s password: #输入登录用户的密码即可登录至本地服务器上
Last login: Fri May 12 12:56:08 2017 from 192.168.3.10
-bash-4.2$
4、设置crontab定时任务,执行传送日志到本地的脚本
[root@iZwz9ghdadtaey1msor7gnZ ~]# crontab -l
45 8 * * * /bin/bash /root/logbak.sh
原文地址:http://shaoniana.blog.51cto.com/11471609/1925112