Mabuhay

Hello world! This is it. I've always wanted to blog. I don't want no fame but just to let myself heard. No! Just to express myself. So, I don't really care if someone believes in what I'm going to write here nor if ever someone gets interested reading it. My blogs may be a novel-like, a one-liner, it doesn't matter. Still, I'm willing to listen to your views, as long as it justifies mine... Well, enjoy your stay, and I hope you'll learn something new because I just did and sharing it with you.. Welcome!
Showing posts with label purging script. Show all posts
Showing posts with label purging script. Show all posts

Thursday, January 29, 2009

Scripting 101: Purging Files

Ei-lo! 'Zup? I'm beginning to enjoy this I guess: scripting. Unexpected, I got a ping from a former colleague, asking from a noobs [read: me] regarding scripting. Without blinking, I accepted the challenge. I was asked to check on the script she made for purging files. After checking on the requirements, given, and asking for advise from a scripting guru [Morsgang Freeman], below are the revised forms. Btw, the first one I made really sucks [I hope these won't :(]. As of writing, these are not tested, not 'til tomorrow when she receives the email/script.

1. First is to create a file, say SUBDIR.txt with the ff. contents:


MERGE/REJECTS/ARCHIVE

MERGE/REJECTS/ARCHIVERAW

MERGE/FILTERED/ARCHIVE

MERGE/FILTERED/ARCHIVERAW

INSERT/REJECTS/INSERTED

INSERT/FILTERED/INSERTED

FTP/FTP_ARCHIVE


2. I made two variations of the script:
a. Version 1

#!/bin/bash

export LOGDIR=/ramsys_data/bmcps/ramsys/
export DIRLIST="ALTL AMA5 CAMA FREE NRTL SEGR UTSS"

for XXX in DIRLIST
do
cd $LOGDIR
if [ "$PWD" = "$LOGDIR" ]
then
for PURDIR in `cat SUBDIR.txt`
do
# A log file can be added here [good practice] to list the files to be deleted...
find $XXX/$PURDIR -type f -name "*" -mtime +5 -exec rm {} \;
done
else
echo "Check $USER\'s permissions; cannot change to $CURDIR."
# You can add a mailer here for notification...
exit 1
fi
done


b. Version 2

#!/bin/bash

LOGDIR=/ramsys_data/bmcps/ramsys/
DIRLIST="ALTL AMA5 CAMA FREE NRTL SEGR UTSS"

cd $LOGDIR || {
echo "Check the permissions; can't change to $CURDIR."
# You can add a mailer here for notification...
exit 1
}

for XXX in DIRLIST
do
for PURDIR in `cat SUBDIR.txt`
do
# A log file can be added here [good practice] to list the files to be deleted...
find $XXX/$PURDIR -type f -name "*" -mtime +5 -exec rm {} \;
done
done


If this will be run in cron, e.g. everyday at 0100H, we can make it as:

0 1 * * * ( scriptname_here.sh > /if_wanted/can/store/to/logfile 2>&1 ) &

Or throw it to /dev/null. Whatever suits you.

World Clock