Automatically backs up Nginx logs every day in Linux

Source: Internet
Author: User
The Linux scheduled task system backs up data at 00:00 every day. Log storage directory: optwwwlogsnginx file storage directory: optwwwlogsall Log File Format: yyyy-mm-dd.tar.bz2 #! Binbash # poweredbywww.baidu.org.tw # author: kethinc # useage: crontab-e #00 *** shoptww

The Linux scheduled task system backs up data at 00:00 every day.
Log storage directory:/opt/www/logs/nginx
Record Filing storage directory:/opt/www/logs/all
Log File Format: yyyy-mm-Dd.Tar.bz2

#! /Bin/bash
# PowerEdBy www. baIdU.org.tw
# Author: kethiNc
# Useage:Crontab-E
#0 0 *** sh/opt/www/logs/logstore. sh>/dev/null 2> & 1

# The Nginx logs bak
Work_path = "/opt/www/logs"
Logs_path =$ {work_path} "/nginx"
Store_path =$ {work_path} "/all/" $ (Date-D "yesterday" + "% Y ")
Yesterday_logs_path = $ (date-d "yesterday" + "% Y-% m-% d ")
Yesterday_logs _FileZookeeper yesterday_logs_path).tar.bz2

Cd$ Work_path
Mv$ {Logs_path }$ {yesterday_logs_path}
Mkdir-P $ {logs_path}

/Etc/init. d/nginx reload # reload nginx because all the original log files have been moved and the file needs to be generated again to avoid some errors (I think there may be no tests)

Tar-jcvf $ {yesterday_logs_file} $ {yesterday_logs_path}

Mkdir-p $ {store_path}
Mv $ {yesterday_logs_file }$ {store_path}
Rm-Rf $ {yesterday_logs_path}

Usage:
Save the script file/opt/www/logs/logstore. sh
Edit the scheduled task configuration file: crontab-e
Enter 0 0 **** sh/opt/www/logs/logstore. sh>/dev/null 2> & 1 in the last line

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.