Shell script regular backup clears Running System Log Code

Source: Internet
Author: User

1. Write the backup and clear the old log Shell script:

Copy codeThe Code is as follows :#! /Bin/sh
# Backup eoslog
# Author rhao
# Date 2008-12-27

# Define Environment Variables
EOS_HOME =/home/eos
SAS_HOME =/home/eos/SAS

# Test primary and secondary nodes
If test-d '/home/eos/SAS/sasw.web1'
Then
SAS_LOG_HOME = $ SAS_HOME/sasw.web1/SAS_Domain/bin
Else
SAS_LOG_HOME = $ SAS_HOME/SAS2_web1/SAS_Domain/bin
Fi

# Define and create a log Backup Directory
SAS_LOG_BAK_HOME = $ EOS_HOME/saslog/
Mkdir-p $ SAS_LOG_BAK_HOME

# Switch the directory to the EOS log file directory
Cd $ SAS_LOG_HOME

# Obtain the current system date
TODAY = 'date + % Y % m % d'

# Backing up the current log
Cp nohup. out $ SAS_LOG_BAK_HOME/$ TODAY. log

# Clearing the current log
Touch nohup. null
Cp nohup. null nohup. out-f
Rm nohup. null

# Clear logs seven days ago
For rmfile in 'Find $ SAS_LOG_BAK_HOME-ctime + 7 ';
Do/bin/rm-rf $ rmfile;
Done

2. Add the script to the crontab of the operating system and directly add VI.
Copy codeThe Code is as follows: [eos @ xcfwbz1 SAS] $ crontab-l
59 23 ***/home/eos/SAS/backup_eoslog.sh

The script is executed at every day.

Shell script for clearing logs

Copy codeThe Code is as follows :#! /Bin/bash
LOG_DIR =/var/log
ROOT_UID = 0
LINES = 50
E_XCD = 66
E_NOTROOT = 67
If ["$ UID"-ne "$ ROOT_UID"]
Then
Echo "Must be root to run this script ."
Exit $ E_NOTROOT
Fi
If [-n "$1"]
Then
Lines = $1
Else
Lines = $ LINES
Fi
Cd $ LOG_DIR
If ['pwd '! = "$ LOG_DIR"]

Then
Echo "Can't change to $ LOG_DIR ."
Exit $ E_XCD
Fi
Tail-$ lines messages> mesg. temp
Mv mesg. temp messages
Echo "Logs cleaned up ."
Exit 0

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.