Linux Log automatic backup script (log truncation, compression)

Source: Internet
Author: User

Only need to move the log files to the backup directory

Then package the journal compression and delete the log file

Last Rebuild log file

#!/bin/bash
#
Nginx_app=/usr/local/nginx/sbin/nginx
logs_dir=/usr/local/nginx/logs
bak_dir=/ tmp/logbak/
date= ' Date +%y%m%d '
#
#先将日志文件移动到备份目录
cd $logs _dir
echo "Moving logs"
MV *.log $ Bak_dir Sleep
3
#
#重建log文件
echo "rebuild logs"
echo "$nginx _app-s Reopen"
$nginx _app- s reopen
#
#打包log文件, and named
echo "tar log"
CD $bak _dir
tar czf $date by date. tgz *.log
#
Delete temporary files for backup directory
echo "rm logs"
rm-f *.log
echo "Done"

Then add a scheduled task, such as 4:30 A.M. daily, script location in/root/logbak.sh

Crontab-e

4 * * * sh/root/logbak.sh

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

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.