Linux伺服器日誌備份到本地

小周啊發表於2019-05-11

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


相關文章