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 passwd scp /opt/soft/spring-cloud/member-9076-weixin/logs/2017-05/mealtime-member-9076-all-$(date -d "yesterday" +%Y-%m-%d).tar.gz logbak@備份主機位址:/logbak/124/
#需确認/logbak/124目錄已存在
c、确認ssh是否能登入到遠端伺服器
[root@iZwz95ywngc71vxzcd5yx4Z ~]# ssh logbak@備份主機位址:/logbak/124/
The authenticity of host '備份主機位址 (xxx.xxx.xxx.xxx)' 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 '備份主機位址,xxx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.
logbak@備份主機位址'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