Extract directory name

Hello guys.
i have several backup servers which i have to monitor them daily.
now i want an script to use the backup directory and put it in output variable.
my backup files look like this:

[ll -hrt
total 151G
-rw-r----- 1 oracle oinstall  68G Jan 23 00:21 full_ORCL_20150122_7277_1.bck
-rw-r----- 1 oracle oinstall 1.3G Jan 23 00:24 arch_ORCL_20150123_7279_1.bck
-rw-r----- 1 oracle oinstall 444M Jan 24 21:12 level1_ORCL_20150124_7281_1.bck
-rw-r----- 1 oracle oinstall 1.3G Jan 24 21:16 arch_ORCL_20150124_7283_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 24 21:16 c-1229942357-20150124-01
-rw-r----- 1 oracle oinstall 664M Jan 25 21:16 level1_ORCL_20150125_7285_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 25 21:16 c-1229942357-20150125-00
-rw-r----- 1 oracle oinstall 1.2G Jan 25 21:35 arch_ORCL_20150125_7287_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 25 21:35 c-1229942357-20150125-01
-rw-r----- 1 oracle oinstall 870M Jan 26 21:32 level1_ORCL_20150126_7289_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 26 21:32 c-1229942357-20150126-00
-rw-r----- 1 oracle oinstall 1.2G Jan 26 21:42 arch_ORCL_20150126_7291_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 26 21:42 c-1229942357-20150126-01
-rw-r----- 1 oracle oinstall 1.2G Jan 27 21:16 level1_ORCL_20150127_7293_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 27 21:16 c-1229942357-20150127-00
-rw-r----- 1 oracle oinstall 1.2G Jan 27 21:33 arch_ORCL_20150127_7295_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 27 21:33 c-1229942357-20150127-01
-rw-r----- 1 oracle oinstall 1.4G Jan 28 21:31 level1_ORCL_20150128_7297_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 28 21:31 c-1229942357-20150128-00
-rw-r----- 1 oracle oinstall 1.2G Jan 28 21:51 arch_ORCL_20150128_7299_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 28 21:51 c-1229942357-20150128-01
-rw-r----- 1 oracle oinstall  68G Jan 30 07:01 full_ORCL_20150129_7301_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 30 07:02 c-1229942357-20150130-00
-rw-r----- 1 oracle oinstall 1.4G Jan 30 07:05 arch_ORCL_20150130_7303_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 30 07:05 c-1229942357-20150130-01
drwxr-xr-x 2 oracle oinstall  16K Jan 31 21:01 rman
-rw-r----- 1 oracle oinstall 436M Jan 31 21:13 level1_ORCL_20150131_7305_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 31 21:13 c-1229942357-20150131-00
-rw-r----- 1 oracle oinstall 1.4G Jan 31 21:16 arch_ORCL_20150131_7307_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 31 21:17 c-1229942357-20150131-01

as you can see file names contain the date and the type of backup(just level and full are important to me)
now i want return the values like this:
if the backup date belong to today or yesterday and backup type was full return 10
if the backup date belong to today or yesterday and backup was level return 5
and if non of them was true return 0.

i have no idea how can:
1- put the files name in a string.
2- how can extract the last backup date and type?
3- how can compare the date with current day?

sorry for asking questions like this. i just start shell scripting but this one is kind of emergency.
thank you so much.

The methods for determining yesterday's date vary from system to system and shell to shell...

What operating system and shell are you using?
What is your timezone setting?

1 Like

thank you dear Don.
I'm using red hat. but i don't know what kind of shell I'm using!!
my time zone is: IRST.
date output is: Sun Feb 1 10:08:39 IRST 2015
thank you.

What should your output look like?

awk -F"_" -vYstd=$(date +"%Y%m%d" -d"-4 day")\
        '$3>Ystd        {if ($1 ~ "full") printf "10"
                         if ($1 ~ "level") printf "5"
                         sub (/^.* /,"")
                         printf "\t%s\n", $0
                        }
        ' file
10   full_ORCL_20150129_7301_1.bck
     arch_ORCL_20150130_7303_1.bck
5    level1_ORCL_20150131_7305_1.bck
     arch_ORCL_20150131_7307_1.bck

My system doesn't recognize the IRST time zone.

1 Like

Than you dear Don. as always your post was so helpful.
now i should put the [ll -hrt] result into the output.
am i right?

I'm not sure what [ll -hrt] should do. Any command's result, mayhap ls -l , can be piped into that awk cmd:

ls -l | awk ' ... '
1 Like

thank you dear RudiC.
i use your command and make this script but it wouldn't work.
whould you please take a look at it and tell me where i did wrong?

output=$(ls -l $1) 
"output" | awk '-F"_" -vYstd=$(date +"%Y%m%d" -d"-4 day")\
        '$3>Ystd        {if ($1 ~ "full") printf "10"
                         if ($1 ~ "level") printf "5"
                         sub (/^.* /,"")
                         printf "\t%s\n", $0
                        }'

I'm so sorry for asking such a stupid question but i really need to make it work until tomorrow.

Hello Ymir,

Could you please try following code and let us know if this helps.

output=`ls -ltr | awk '{print $NF}' 
for i in ${output[@]}
do
 echo $i | awk -F"_" -vYstd=$(date +"%Y%m%d" -d"-4 day")
                       '$3>Ystd        {if ($1 ~ "full") printf "10"
                           if ($1 ~ "level") printf "5"
                           sub (/^.* /,"")
                           printf "\t%s\n", $0
                     }'
done

Thanks,
R. Singh

1 Like

First, never tell us: "i use your command and make this script but it wouldn't work"! Show us the exact output from running the script that explains to us exactly what didn't work. If you don't show us how it isn't working, it makes it a huge guessing game for all of us (especially those of us who are using a different operating system and shell than you're using).

Second, there is a HUGE difference between running a program and feeding its output to awk as RudiC suggested with:

ls -l | awk ...

and running a program, saving its output in a variable, executing a fixed string, and feeding the results of the command named by that string to awk as your code is doing:

output=$(ls -l $1) 
"output" | awk ...

Why are you storing the output from ls in a variable.

Third: You have an extraneous single quote before the -F option to awk .

And, fourth: I don't see how this script is going to work if you use ls -l instead of ls without the -l option. (With ls -l output, $1 in awk is never going to match Full nor level .)

Please try running your script more like RudiC suggested:

ls "$1" | awk -F"_" -v Ystd=$(date +"%Y%m%d" -d"-4 day") '
        $3>Ystd        {if ($1 ~ "full") printf "10"
                         if ($1 ~ "level") printf "5"
                         sub (/^.* /,"")
                         printf "\t%s\n", $0
                        }'
1 Like

You assigned the ls result to the "output" variable. To get at its contents, the shell needs to expand it: $output , to keep the shell from swallowing redundant spaces etc, quote it: "$output". To pipe it into sth. else, echo it: echo "$output" .
Why don't you pipe ls 's results immediately, as I alluded in my post?

1 Like

Thank you dear Singh.
but your command doesn't work.

dear Don . i use the ssh to get the directory info. so i had to run and put it in a variable.
i use your shell command and it work but not the way i want it.
result:

        arch_ORCL_20150130_7303_1.bck
        arch_ORCL_20150131_7307_1.bck
        arch_ORCL_20150201_7311_1.bck
5       level1_ORCL_20150131_7305_1.bck
5       level1_ORCL_20150201_7309_1.bck

i want just return a value. your shell script return a value for each directory name.
let me put it this way:
i want check the directory for backup status. valid backup lifetime is less than two days. now
if the script return the "10" i found out i have a full backup which is valid.
if the script return the "5" i found out i have a level backup which is valid.
in other wise i dont have a valid backup file.

thank you dear RudiC.
i use the ssh to get the directory info. so i had to run and put it in a variable.

I don't see the necessity to use a variable if you run an ssh command. From what you showed in post #1, there is an entire directory listing to be processed, and you did not specify the output format desired.

Please provide the entire picture of what you need to do so we can provide a fit soulition.

1 Like

Ok. let me explain what exactly i want.
I'm using zenoss to monitor my servers. in the zenoss i cant handle the plain text. i just can use the numerical values. now i want create an alert to warn me if there was no back up files.
so i decided to wrote an script to do this.
now if you guys have better idea please share it with me.
i really appreciate any sort of help.
thank you.

Sorry, I can't imagine the picture from what you post. Wildly guessing: Do you need a script to be run on every server that checks all files and, if a full/level backup found within the last two days, sends a number (10/5) back? How do you intend to send the date parameter if you can handle numbers only? How do you want to store the script?

1 Like

ye exactly.
i don't want to send any date parameters. just a simple yes or no is enough for now.
the script store in my zenoss server and run via ssh(with pki and without password authentication.)
Thank you.

Does this - executed on your servers, assuming you run a recent bash shell - yield what you need:

ls @(full|level)*$(date +%Y%m%d -d-1day)* | { read FN; case "$FN" in full*) echo 10;; level*) echo 5;; *) echo 0;; esac; } 

?

1 Like

thank you.
result:

-bash: syntax error near unexpected token `('

Sorry, failed to mention you need to set the extglob option: shopt -s extglob .

1 Like