Linux/Unix shell script to clear archived log files

Source: Internet
Author: User

For DEV and UAT environments, databases must be in archive mode, but do not need to be backed up. Therefore, the constant increase in archive archiving logs results in a large amount of disk space consumption. In this case, you can use a shell script to automatically clear these archived logs at regular intervals. This article provides a script to clear archived logs.

1. Clear shell scripts for archiving logs

Robin @ SZDB :~ /Dba_scripts/custom/bin> more remove_arch_dump.sh
#! /Bin/bash
# ------------------------------------------------------------ +
# FileName: remove_arch_dump.sh |
# Desc: |
# Remove old archived log and data pump file |
# Usage: |
#./Remove_arch_dump.sh |
# |
# Authror: Robinson |
# Blog: http://blog.csdn.net/robinson-0612 |
# ------------------------------------------------------------ +

Filename =/etc/oratab

Cat $ filename | while read LINE
Do
Case $ LINE in
\ # *); # Comment-line in oratab
*)
Oracle_SID = 'echo $ LINE | awk-F: '{print $1 }'-'
Echo $ ORACLE_SID

#---------------------------------
# Define archived log directory
#---------------------------------
Dir =/u02/database/$ ORACLE_SID/archive/
Echo $ dir

Filelist = 'LS-t $ dir'
Echo $ filelist

#----------------------------------------------------------
# Start to remove archived log and keep last 2 archived log
#----------------------------------------------------------
Count = 0
For filename in $ filelist
Do
Echo $ filename
Count = $ ($ count + 1 ))
If [$ count-gt 2]; then
Echo $ count
Rm-vrf $ dir $ filename
Fi
Done
Ls-tr $ dir

#--------------------------------
# Define data pump dump directory
#--------------------------------
Dir =/u02/database/$ ORACLE_SID/BNR/dump/
Echo $ dir

Filelist = 'LS-t $ dir'
Echo $ filelist

#---------------------------------------------------------
# Start to remove data pump file and keep last 5 dump file
#---------------------------------------------------------
Count = 0
For filename in $ filelist
Do
Echo $ filename
Count = $ ($ count + 1 ))
If [$ count-gt 5]; then
Echo $ count
Rm-vrf $ dir $ filename
Fi
Done
Ls-tr $ dir

;;
Esac
Done
Exit

2. Script description

A. The script reads the database SID defined in oratab through an external loop to find the archive log path and datapump dump path of the corresponding database.

B. A for loop is used to clear archived logs and keep the last two archived log files.

C. The next for loop is used to clear the dump file exported under the corresponding dump (Oracle datapump) directory under the current SID.

D. The last five files in the dump directory are retained. You can modify the number of recent files that are retained (dump 5 or archive log 2.

E. If you do not need to clear the dump path, comment out the dump part.

F. Deploy it to crontab as needed.

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.