0

Help,
I am running Ubuntu 23.10 Server and trying to write a script to back up my server. As the script runs it should create an archive and then check for the archive's existence. The relevant code is.

ROOTNAME="/mnt/backup/"\`date +"%Y%m%d"\`

SQLNAME=$ROOTNAME"mysql.sql"

mysqldump -h localhost -u root -pabcd123 --skip-tz-utc -A --add-drop-database --databases > "$SQLNAME" 2> /dev/null

if [ -s $SQLNAME ] then echo `date "+%H:%M:%S~%d/$n/%Y"` " SQL Dumped successfully" >> "$LOGNAME" else echo `date "+%H:%M:%S~%d/$n/%Y"` " ERROR: SQL Dump Not found exiting...." >> "$LOGNAME" exit 1 fi

The file generated by the mysqldump statement is being successfully created. However, the if [ test ]; then statement is not working neither the success nor failure branch of the statement is being followed and I am getting no output to my log file. What am I doing wrong?

Raffa
  • 32,237
  • Are those &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and <BR> charachters I just edited out added by you in the question for styling purpose or you have copied them from your script file and they actually exist there? ... And is the path to your log file defined somewhere in the script i.e. the definition of "$LOGNAME" doesn't appear in your added script snippet. – Raffa Nov 20 '23 at 17:40
  • 1
    ... and why do you escape the backticks for command substitution with a backslash? ... Have you initially copied the script from a website in the browser and pasted to a file then ran it? – Raffa Nov 20 '23 at 17:47
  • 4
    You should avoid ALLCAPS variable names for shell variables and here particularly avoid LOGNAME (which is an environment variable that should contain the user's login name). It's far more likely that $LOGNAME isn't expanding to what you expect than that a simple conditional expression should execute neither branch... – steeldriver Nov 20 '23 at 18:21
  • 1
    ... Please see $LOGNAME – Raffa Nov 20 '23 at 18:24

1 Answers1

1

Running the line in your script -

ROOTNAME="/mnt/backup/"\`date +"%Y%m%d"\` ; echo $ROOTNAME

results in:

+%Y%m%d`: command not found

Perhaps this would be a better way to define your ROOTNAME variable:

ROOTNAME="/mnt/backup/`date '+%Y%m%d'`"

Running:

ROOTNAME="/mnt/backup/`date '+%Y%m%d'`" ; echo $ROOTNAME

results in:

/mnt/backup/20231120

Using bash -x ./yourscript or sh -x ./yourscript is a very useful tool for diagnosing problems and debugging shell scripts. Invoking a bash or sh shell with the -x option causes each shell command in your script to be printed before it is executed and displays the result of each command in your script.

It is best practice to enclose variables in quotes - if [ -s "$SQLNAME" ]

Also, You should avoid using ALLCAPS in variable names, as recommended by steeldriver in This comment Below your question.

Kudos to Raffa for encouraging me to re-evaluate my original answer!

stumblebee
  • 3,547
  • 1
    Although quoting is good practice, I don't think this is the culprit though ... after all the path and filename resulting from the expansion of $SQLNAME appear to be a simple /mnt/backup/3482456mysql.sql so should be parsed right anyway. – Raffa Nov 20 '23 at 18:05
  • @Raffa You were correct! After looking deeper into the OP script, I found a error with ROOTNAME variable and updated my answer. – stumblebee Nov 21 '23 at 03:16
  • 2
    Might be worth noting as well that the usage of $(...) notation for command substitution instead of legacy backticks \...`` is encouraged ... Please see https://github.com/koalaman/shellcheck/wiki/SC2006 – Raffa Nov 21 '23 at 07:47
  • Thank you Guys I have managed to solve this problem now. – James Martin Nov 22 '23 at 10:54