Problems with a variable in a backup script

Hi,

what I want to do is to backup a database every day so that the first six days a week it is stored in a dir named 1 - 6. The last day it should be stored in a dir name woche_1 - woche_2 and once a month (15th) it should be stored in a dir named as the month. So I did this:


day=`date +%u`
date=`date +%d`
month=`date +%b`

if  $day -lt 7 ]
then
    dir=$day
else
    ((week=$date/7))

        if  $week -le 1 ]
        then
            dir="woche_1"
        elsif  $week -le 2 ]
            dir="woche_2"
        elsif  $week -le 3 ]
            dir="woche_3"
        elsif  $week -le 4 ]
            dir="woche_4"
        elsif  $week -le 5 ]
            dir="woche_5"
        fi
fi
if  $date -eq 15 ]
then
    dir=$month
fi

The daily and monthly updates are working fine. Only the weekly don’t. They are stored in the main dir of the backup server. I log the result and it seems that the variable $dir is empty in that case. What’s wrong with that?

Thanks

Erik

You might read the bash man page for the syntax of the if statement:

if list; then list;  elif list; then list; ] ...  else list; ] fi

So firstly, it’s spelled elif. Secondly, you need a then after the elif. And thirdly you really should use a case statement here, much more succinct.

Could have guessed I had my post ready and see ken_yap already replied :wink:

BTW, this is what I use to create folders for backups:


`date +%Y`/`date +%d-%m-%Y`

This way the whole year is in a folder, in each there are folders for each day.

You only needed to call date once:

`date +%Y/%d-%m-%Y`

Hi,

thanx for the reply.

I read a bit but not the man and I found it like this. What confuses me is that I wrote that script about three years ago for the old server and it worked. I find lines like this in the log:


Zielverzeichnis ist: woche_3

which means target folder is: woche_3. And the file was placed in the correct folder. Now I moved it to the new server and it didn’t work. I’m really confused. :wink:

Ok, case statement could be better but I don’t like them. :wink:

BTW: Folders are not created with this but selected which means the folders exist on the backup server.

Thanx again. Will have to wait until sunday to check if it worked correctly.

Bye

Erik

All these answers assume that erikro is writing a bash, sh or ksh script. Which he did not say at all (but it may be very likely that he wants to use bash).

But while is script contains many contructs that are no bash constructs at all and he tells that part of it works, I wonder if he really does not use another language.

Can you please explain erikro?

(()) is bash, however if elif else fi have been in there since Bourne shell days.

I’m actually surprised if he didn’t see any errors; it said elseif command not found here.

Same here.

and ksh.

Maybe he made a mix of different languages.

Hmm, misspelled elif, missing then. Maybe he showed it to us after he had made some random “improvements”.

“Is this the original script that used to work?”

“Yes, yes, of course… er, no, I erm, made some changes a few minutes ago”

lol!

Hi,

Should be a bash skript. You are talking about many constructs which are wrong? Well, I’m not a good bash script programmer. Mostly I use Perl. So what else is wrong? The problem itself seems to be solved.

Bye

Erik

Hi,

Well, as I said the original script worked at least three years and I passed it without any changes to the new server. That’s what’s confusing me.

Bye

Erik

???

Thanks for sharing! Buy Paul Smith bag and Paul Smith men’s shoes in Paul Smith Shop. Paul Smith, famous brand in UK. Colorful strips are is logo. The simple but useful, fashionable design is your best match. Nowadays, it has many stylish products, including Paul Smith’s shoes, Paul Smith bags-shirts, Wallets,Ties,Scarves,Cuffinks, Glasses,Belts, Bath Towels, Money Clips,Key Purses and Key Rings! Paul Smith, you deserved it!