0

Running next command in terminal is working fine like expected.

/var/www$ sudo mysqldump --defaults-extra-file=/mnt/./.sql/mysqldump.cnf --databases site3 --hex-blob | sudo tee /mnt/site3/20230404_site3.sql

running the same command from within script

-rwx------ 1 root root 1686 apr  4 22:23 BackupDrupal.sh
/var/www$ sudo ./BackupDrupal.sh site3

is ending with next message:

/*!40000 ALTER TABLE `watchdog` ENABLE KEYS */;
UNLOCK TABLES;
mysqldump: Got error: 1049: Unknown database '|' when selecting the database

script:

#!/bin/bash  
#SCRIPT FOR BACKING UP THE DRUPAL FOLDER AND THE DRUPAL DATABASE
#the mysqldump and the whole drupal directory are placed on the  mounted /dev/sda11 parition

#For backing up site3 the script is launched like:
#$ sudo ./Backup_Drupal.sh site3


#define variables
NOW=$(date +"%Y%m%d")
TARGET_DIR="/mnt/$1"
TARGET_FILE=$TARGET_DIR"/"$NOW"_"$1".sql"
SOURCE_DIR="/var/www/"$1
RET=$(mount | grep "/dev/sda11")
ARR=( $RET ) 
#echo $NOW
#echo $TARGET_DIR
#echo $TARGET_FILE
#echo $SOURCE_DIR
#echo $RET
#echo ${ARR[0]}

#check if mounted / if not: mount /dev/sda11
if [ -z "$ARR" ] 
    then 
        echo -e "ARR is empty - /dev/sda11 not mounted ...\n"
        echo -e "Mounting now ... \n"
        mount -t ext4 /dev/sda11 /mnt
fi

#Look for 1st argument
if [ -z "$1" ] 
    then
        echo -e "An argument is needed to run this script. Launch like Backup_Drupal site99 ...\n"
                exit 1  
fi

#check path
if [ -d "$TARGET_DIR" ];
    then
            echo -e "Installing backup in  ${TARGET_DIR} ...\n"
else
        echo -e "Directory ${TARGET_DIR} does not exist ...\n"
            exit 1
fi

#mysqldump database
echo -e "Saving database ...\n"
#SQLDUMP="sudo mysqldump -u root -p --databases $1 --hex-blob | sudo tee $TARGET_FILE;"
SQLDUMP="sudo mysqldump --defaults-extra-file=/mnt/./.sql/mysqldump.cnf --databases $1 --hex-blob | sudo tee $TARGET_FILE"
$SQLDUMP
echo -e "the command used = $SQLDUMP \n"
echo -e "Database saved as $TARGET_FILE...\n"

#save drupal folder in tar.gz format
echo -e "Saving $1 folder in tar.gz format...\n"
TARGET_FILE=$TARGET_DIR"/"$NOW"_"$1".tar.gz"
tar -cpzf $TARGET_FILE $SOURCE_DIR
echo -e "$SOURCE_DIR saved as $TARGET_FILE...\n"
echo -e "All done...\n"

[see:][1] [1]: https://unix.stackexchange.com/questions/741683/permission-denied-using-mounting-point-redirection-in-mysqldump

2 Answers 2

1

mysqldump: Got error: 1049: Unknown database '|' when selecting the database

This makes me think that your input to the command is not being sanitized correctly, perhaps the value of $1 somehow ends up being just the pipe character. You can print the value of the variable in your script to debug this.

I also strongly recommend that you paste your script in https://www.shellcheck.net/ and follow the recommendations, as this will help you write better bash code. It's also available as an addon for most IDEs. I tried it and it points out multiple issues with the code, like (probably) incorrectly placed quotes which interferes with variable expansion.

edit: the other answer from Tom Yan does a better job at explaining the cause of the issue, namely having an entire command including pipes being defined as a variable before being run.

1
  • 1
    Thanks for referring me to shellcheck.net. I improved the script until #$ shellcheck myscript #No issues detected! But the mysqldump error persisted. I did find this simple solution: Stopping using the variable SQLDUMP and using the command directly did the trick : sudo mysqldump --defaults-extra-file=/mnt/./.sql/mysqldump.cnf --databases $1 --hex-blob | sudo tee $TARGET_FILE
    – jwa
    Commented Apr 6, 2023 at 17:37
1

When you do something like:

MEH="..."
$MEH

| in MEH will be used as if it is quoted (i.e., passed to the program as an argument).

You can experiment with e.g. echo duh | cat to see how it works.

If you insist on adopting similar approach, you can try eval:

eval $MEH

(Given the nature of eval itself it probably doesn't matter if you quote $MEH or not.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .