Shell script password with special chars

5,862

Solution 1

Your password needs to have the special characters escaped. So if your original password is xgT{uic[Is?uJ+it will have to become xgT\{uic\[Is\?uJ\+ (A slash before the special characters).

You might also want to look at something already written to do this such as AutoMySQLBackup, but I'm not sure if you need daily, weekly or monthly backup rotations. However the same rule applies, if the password has special characters those special characters need to be escaped.

Hope that helps!

Solution 2

Yes. It helped to escape special chars:

MPASS=$(printf "%q\n" "$DB_PASS")

But there was still a problem with mysqldump. I finally found out, that mysqldump only worked for me with passwords with special characters when I don't tell mysqldump the host (-h hostname). This seems to be a bug. Or isn't it?

Unfortunately I also have sites where the mysql server is an other host. So this solution isn't perfect.

Share:
5,862

Related videos on Youtube

HHGK
Author by

HHGK

Updated on September 18, 2022

Comments

  • HHGK
    HHGK over 1 year

    I've build a shell mysql backup script which works very well.

    It only has problems with passwords with special chars like this: xgT{uic[Is?uJ+.

    Here is the important part of the script:

    #!/bin/sh
    FILE=mysql-$db.$DATE.sql.gz
    ssh $SUSER@$SHOST "mysqldump -q -u $MUSER -h $MHOST -p$MPASS $db --no-create-db | gzip -9 > $FILE" 2> $ERROR
    

    I've changed it to:

    ssh $SUSER@$SHOST 'mysqldump -q -u $MUSER -h $MHOST -p'\''$MPASS'\'' $db --no-create-db | gzip -9 > $FILE' 2> $ERROR
    

    As you can see I've already tried double quotes. But now I get the error:

    bash: $FILE: ambiguous redirect
    

    I'm getting the password and other info from an external file:

    DB_HOST=000.000.000.000
    DB_DATABASE=dbdame
    DB_USER=db user
    DB_PASS="xgT{uic[Is?uJ+"
    

    Within my script it take the info from the external file:

    Source dbserver.conf
    MUSER=$DB_USER
    MPASS=$DB_PASS
    MHOST=$DB_HOST
    DBS=$DB_DATABASE
    
    • HHGK
      HHGK almost 8 years
      It was: "mysqldump: Got error: 1045: Access denied for user '...'@'...' (using password: YES) when trying to connect
    • Admin
      Admin almost 8 years
      That's exactly the error '...'@'...' or did you mask that? Obviously keep your username and password secret, but did it omit some characters, or the entire string?
    • HHGK
      HHGK almost 8 years
      I've masked it. The original error was with the correct user and address.
    • HHGK
      HHGK almost 8 years
      Could be the problem that I'm getting the password from an external file?:
    • muru
      muru almost 8 years
      If it is mysql, consider using a ~/.my.cnf file in the server: stackoverflow.com/questions/16299603/…
    • HHGK
      HHGK almost 8 years
      That could be a way. But I prefer having all configuration data on my backup target server. So what do I have to change to to be able to transfer the correct password?