Friday, May 15, 2015

Litle Bit of Shell Script


I've written posts about Python and Python is in fact a great programming language. But, recently I needed solve a situation in which Shell Script was more indicated:

Thanks to the excellent forum about Script :

http://www.unix.com/

Instead of write a whole new post, I'm gonna put here the post I put there. The conclusion, let´s say:


"I'm sharing my code here. Sure it is not elegant but it is doing exactly what I need. So, it is useful !
I have a server that makes backup each 7 day in a FTP server. I want to keep only 3 files and compacted.
The first time that this code will be executed, let´s say 7 days from here, it is expected that will have one file on the FTP server. Then, the script will only compact it.

Seven day after and the script will run again. This time, we will have two files : A new backup file from the server and a .gz file created last time.
I want to keep the .gz, since it has not 20 days yet and I want it compacted.

Later on, there will be situations which we will have a new backup that needs to be compacted, .gz files that need to be kept and .gz file older than 20 days that needs to be deleted.

The FTP should be managed like this way:

backup files coming every 7 days.
Only new backup files being compacted 
files .gz newer than 20 days keeps untouched
files .gz older than 20 days being deleted

By deleting file older than 20 days, allows to me at least 3 backup files on the FTP server. This is more than necessary for my needs.
Another point is that this FTP server receives files from other servers as well. Then it is necessary to verify which file I need to handle exactly"

First, I coming to the forum asking for help. I was trying to accomplish the same task but I was using a different approach and an inadequate one.
 I was using "for" and I was trying to get the variable generated from:

 files=($(find . -type f -name 't*' -mtime +"$days"))

if [ $? = "0"]

As explained by someone on the forum:

"The $?  will change after every command, so within the loop you can't depend on it. On top, the second if [[ $? ... ]] 's result is unpredictable at all. To do several tests on the result of a single command, assign its $?  to a variable and test against this."

 This is true and I got stuck trying to solve it.

Then, someone else proposed to create a temporary with the age of 20 days and use it as comparison. It works very well. Maybe I will never get to that by myself.

It was very interesting Challenge!!!

#!/bin/bash

# Create a temp reference file:

touch -d -20days "/tmp/20dayref.$$"


cd /srv/ftp

find . -type f  -name 't*' -print | while read file

//backup file starts with 't'

do

    if [[ ("$file" -nt "/tmp/20dayref.$$") && ( "$file" =~ \.txt$) ]]

//I am using .txt as example. It must reflect your backup extension.

    then

 #file is younger --append to archive


    echo "File": $file " is newer  than 20 days"
    echo "Compacting...."
    NOW=$(date +"%m-%d-%Y")
    FILE="test-Backup.$NOW.tar.gz.$$"
    tar -cz -f "$FILE" "$file"
    echo "Deleting ...": $file
    rm $file


elif [[ "$file" -ot "/tmp/20dayref.$$" ]]

then


 #file is old delete it

    echo "File": $file "is older than 20 days"
    echo $file
    echo "Deleted  permanently...."
    rm $file

else

echo "Job successfully completed!!!!"

    fi

done

No comments:

Post a Comment