The shell automatically downloads the nginx log file to the local

#### Recently I encountered a situation that the server did not respond, and nginx did not write logs. After investigation, it was found that the log files were too large and the disk was full. In the past, scripts were automatically uploaded to OSS, but now the log needs to be downloaded. To the local, so I wrote a script in the shell, then put it in the crontab, and downloaded the log every morning.

Shell script
```
#!/bin/bash

HOST="115.114.113.112"
LOG_PATH="/usr/local/nginx /logs/"
LOCAL_LOG_PATH="/home/admin/logs/$HOST/"

# Synchronize all dated log files locally, and then delete remote files
# Log file naming: access_2016-12-28.log
LOGS=$( ssh root@$HOST ls $LOG_PATH | grep _)
LOGS=${LOGS// / }

function log() {
    echo `date "+%Y-%m-%d %H:%M:%S"` $1
}

for log in $LOGS
do
    REMOTE_LOG_FILE="$LOG_PATH$log"
    LOCAL_LOG_FILE="$LOCAL_LOG_PATH$log"
    log " Ready to download from $HOST >> $REMOTE_LOG_FILE"
    log "Check local $LOCAL_LOG_FILE exists"
    DOWNLOAD=1
    if [ -f $LOCAL_LOG_FILE ]; then
        log "Local file already exists, and check the md5 value of the file"
        REMOTE_FILE_MD5=$(ssh root@$HOST md5sum $REMOTE_LOG_FILE | awk '{print $1}')
        LOCAL_FILE_MD5=$(md5sum $LOCAL_LOG_FILE | awk '{print $1}')
        # log "$REMOTE_FILE_MD5 == $LOCAL_FILE_MD5"
        if [ "$REMOTE_FILE_MD5"x = "$LOCAL_FILE_MD5"x ]; then
            DOWNLOAD=0
            log "The file content is the same, skip the current file"
        else
            log "The file content is different, to download the file"     fi
        fi


    if [ $DOWNLOAD = 1 ]; then
        log "Download $REMOTE_LOG_FILE from $HOST"
        scp root@$HOST:$REMOTE_LOG_FILE $LOCAL_LOG_PATH
    fi

    log "Delete $REMOTE_LOG_FILE from $HOST"
    ssh root@$HOST rm $REMOTE_LOG_FILE
done
```

Give the script execution permission

```
$ chmod +x sync_115.114.113.112.sh
```

Add it to crontab, and execute it every morning at one minute, because the log file will be generated at 0 minutes on the server, so wait a minute and then download it, basically No problem
```
1 0 * * * /home/admin/logs/sync_115.114.113.112.sh
```

It should be noted here that you need to upload the ssh public key to the server in advance, so that you can log in without a password and execute the command

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361383&siteId=291194637