1

The script

I'm experiencing an issue with this bash script:

dump_all_dbs(){
    $DOMAIN="mysql.domain.com"
    $DBS_FOLDER_BK="$HOME/_backup/"
    $DATA=("'1' 'dbname' 'dbuser' 'dbpassword' 'Yes'" "'2' 'dbname2' 'dbuser2' 'dbpassword2' 'Yes'")

    for i in "${DATA[@]}"; do 

            i=($i)

            # if 'Yes' do it if 'No' skip it 
            CHECK="${i[4]}"

            if [ $CHECK == "'Yes'" ];then

                # Extract the var from DATA
                ID=${i[0]} 
                DBNAME=${i[1]}
                DBUSR=${i[2]}
                DBPSW=${i[3]}

                # Clean from ' "
                ID="$(tr -d "\"\`'" <<<$ID)"
                DBNAME="$(tr -d "\"\`'" <<<$DBNAME)"
                DBUSR="$(tr -d "\"\`'" <<<$DBUSR)"

                # Go to the backup folder
                cd "$DB_FOLDER_BK"

                # Mysqldump
                mysqldump -v -P3306 -h"$DOMAIN" -u"$DBUSR" -p"$DBPSW" "$DBNAME" | gzip -c > "$DBNAME".gz
            else
                # Is 'No' just skip
                continue
            fi 
    done
}

dump_all_dbs_echoed(){
        mysqldump -v -P3306 -hmysql.domain.com -udbusername0 -p'password0' dbname0 | gzip -c > dbname0.gz
        mysqldump -v -P3306 -hmysql.domain.com -udbusername1 -p'password1' dbname1 | gzip -c > dbname1.gz
        mysqldump -v -P3306 -hmysql.domain.com -udbusername2 -p'password2' dbname2 | gzip -c > dbname2.gz
        mysqldump -v -P3306 -hmysql.domain.com -udbusername3 -p'password3' dbname3 | gzip -c > dbname3.gz
}

(NOTE: the password params in the original script was -p not -password, echoed mysqldump with -password'password1'does not work)

Excluding the big security issue in storing in plain text password and usernames.

The question

I would like to ask why if I'm running the script I receive this error:

mysqldump: Got error: 1045: Access denied for user 'dbuser'@'myDSLconnection.domain.com' (using password: YES) when trying to connect

but if I run the single the mysqldump one by one via terminal or "dumps_all_dbs_echoed" everything works perfectly (I did it echoing the params of the mysqldump and copy paste them)

anyone have an idea?

Security Issue

how people treat the issue of store password or hashes? I've tried pgp the file, but it seems not the right way for a chron process.

  • echo out the command it's executing, just to make sure it's ok. Does the password have any characters in that need escaping? – Philᵀᴹ Jun 23 '17 at 11:09
  • Hi Phil! I've already echoed the commands and copy pasted it. they works well. The password variable is stored into the DATA array between two ' (like this 'password') and used as it is in between the params of mysqldump. – entalpia Jun 23 '17 at 11:32
  • This might be just a simple permission issue. Try assigning the dbuser more rights by executing the following: grant all on *.* to 'dbuser'@'%';. Verify the dbuser's permissions by executing the following: select user, host from mysql.user; to verify the user can connect via your $DOMAIN variable. – John aka hot2use Jun 23 '17 at 12:21
  • So, you're saying that $DBPSF value is 'whatever', not just whatever? Note that -password"'whatever'" is not the same as -password'whatever' – mustaccio Jun 23 '17 at 13:03
  • @hot2use tnx for your suggestion. Actually, I can connect from my domain pretty well if I'm running the mysqldumps straight from the shell with all same params collected from $DATA they work fine. The issue appears to be just with the script. It's just 10days I'm playing with bash... I don't know most of it. – entalpia Jun 24 '17 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.