0

For security purposes, I have been writing a script that daily create dump, and manage them for some daily dumps, and some monthly dumps. I have a weird issue that I can clearly understand. When I run the script above manually ./save_db.sh evrything works. But I did add this in crontab of the same user (which is not root).

cd /home/debian/script && sh save_db.sh

The script is this:

#!/bin/bash

nom_dump="$(date +%H:%M-%d%m%y)"
mysqldump -u debian --all-databases | gzip -c > /home/debian/bdd_dump/journalier/"$nom_dump".sql.gz

sauv_mensuelle="$(date +%d)"
if [ $sauv_mensuelle == "01" ]
then
    cp "$nom_dump".sql.gz /home/debian/bdd_dump/mensuel
    compte_sauv="$(ls /home/debian/bdd_dump/mensuel | wc -l)"
    if (( $compte_sauv > 6 ))
    then
        clean_mensuel="$(ls -t /home/debian/bdd_dump/mensuel/ | tail -1)"
        rm /home/debian/bdd_dump/mensuel/"$clean_mensuel"
    fi
fi

compte_semaine="$(ls /home/debian/bdd_dump/journalier/ | wc -l)"
if (( $compte_semaine > 7 ))
then
    clean_daily="$(ls -t /home/debian/bdd_dump/journalier/ | tail -1)"
    rm /home/debian/bdd_dump/journalier/"$clean_daily"
fi

When crontab runs it, the last part is not working as intended, but I can't know why. Dumps older than 7 days are not being removed

Thanks :)

Antoine Bonal
  • 39
  • 2
  • 7

1 Answers1

-2

The way you are calling this script:

cd /home/debian/script && sh save_db.sh

Is environment dependent. Chmod +x your script and replace the call in crontab with

/path/to/script/my-script-name.sh

You only need one shell process to run, not two, and you don't need to change your working directory to invoke it.

Bret Weinraub
  • 1,943
  • 15
  • 21
  • ok fine, I will do this. I did not realized invoking a second shell could cause this troublles – Antoine Bonal Nov 15 '22 at 07:48
  • 1
    What exactly works also depends on how the script was written; some beginner scripts (incorrectly) require you to run them from the directory where they are saved. But this is not the reason the OP's code is not working, and so not really an answer to this question. – tripleee Nov 15 '22 at 08:14
  • 1
    @BretWeinraub : When the script is invoked with an explicit specification of the shell (sh, bash, zsh, ...), you don't need the x-bit. Actually, the shell is not a problem with respect to the environment, but if you are worrying about the environment, you could use an absolue path: `/usr/bin/bash /home/debian/script/save_db.sh` for instance. – user1934428 Nov 15 '22 at 09:21
  • @user1934428 if you want the script to run the same way, you should invoke it the same way. Leaving it to the invoker is ... ok, yeah, not sure why you would do that. – Bret Weinraub Nov 15 '22 at 17:14
  • Actually, all cron issues are related to the fact that cron's environment doesn't match the test environment. Ink it. – Bret Weinraub Nov 15 '22 at 17:16
  • @AntoineBonal as people are trashing my answer I'm wondering if you've got it fixed :) – Bret Weinraub Nov 15 '22 at 17:19