database copy shell script including password with special characters











up vote
0
down vote

favorite












I am trying to create a shell script for a mysqldump (and mysql) operation where the password is stored directly in the script. The password contains special characters which need to be handled accordingly. However, I cannot figure out a combination that actually works.



For example, the following works when executed directly on the console:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password


The password contains a $ which is then escaped with $. However, the exact same command does not work when put into a shell_script.sh. When I execute the script, MySQL complains



Access denied for user 'the_user'@'localhost'


So far I have tried the following within the script:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'


None of these seem to escape the password properly. Out of these, only the first one works properly when used directly on the shell.



What am I missing? How do I have to specify the password, so these commands also work from within a shell script?










share|improve this question
























  • Is the actual problematic character a dollar sign?
    – choroba
    Nov 12 at 14:20










  • The actual password also contains other special characters, like %, =, ?, # for example. However, out of these I only need to escape the $ - at least directly on the shell. The % is at the beginning of the password, if that makes any difference (which it might?).
    – fritzmg
    Nov 12 at 14:25












  • % isn't special at all. ? might cause problems, though, as it might try to match. Whitespace could also be a problem. Backslashing every problematic character should work, though.
    – choroba
    Nov 12 at 15:20










  • Still no luck. I have backslashed every character outside of [a-zA-Z0-9], and still the command does not work when executed via a script. It still only works when executed directly on the command line.
    – fritzmg
    Nov 12 at 15:24










  • Are you sure there's no ssh or other additional level of interpretation involved in the script?
    – choroba
    Nov 12 at 15:40















up vote
0
down vote

favorite












I am trying to create a shell script for a mysqldump (and mysql) operation where the password is stored directly in the script. The password contains special characters which need to be handled accordingly. However, I cannot figure out a combination that actually works.



For example, the following works when executed directly on the console:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password


The password contains a $ which is then escaped with $. However, the exact same command does not work when put into a shell_script.sh. When I execute the script, MySQL complains



Access denied for user 'the_user'@'localhost'


So far I have tried the following within the script:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'


None of these seem to escape the password properly. Out of these, only the first one works properly when used directly on the shell.



What am I missing? How do I have to specify the password, so these commands also work from within a shell script?










share|improve this question
























  • Is the actual problematic character a dollar sign?
    – choroba
    Nov 12 at 14:20










  • The actual password also contains other special characters, like %, =, ?, # for example. However, out of these I only need to escape the $ - at least directly on the shell. The % is at the beginning of the password, if that makes any difference (which it might?).
    – fritzmg
    Nov 12 at 14:25












  • % isn't special at all. ? might cause problems, though, as it might try to match. Whitespace could also be a problem. Backslashing every problematic character should work, though.
    – choroba
    Nov 12 at 15:20










  • Still no luck. I have backslashed every character outside of [a-zA-Z0-9], and still the command does not work when executed via a script. It still only works when executed directly on the command line.
    – fritzmg
    Nov 12 at 15:24










  • Are you sure there's no ssh or other additional level of interpretation involved in the script?
    – choroba
    Nov 12 at 15:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to create a shell script for a mysqldump (and mysql) operation where the password is stored directly in the script. The password contains special characters which need to be handled accordingly. However, I cannot figure out a combination that actually works.



For example, the following works when executed directly on the console:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password


The password contains a $ which is then escaped with $. However, the exact same command does not work when put into a shell_script.sh. When I execute the script, MySQL complains



Access denied for user 'the_user'@'localhost'


So far I have tried the following within the script:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'


None of these seem to escape the password properly. Out of these, only the first one works properly when used directly on the shell.



What am I missing? How do I have to specify the password, so these commands also work from within a shell script?










share|improve this question















I am trying to create a shell script for a mysqldump (and mysql) operation where the password is stored directly in the script. The password contains special characters which need to be handled accordingly. However, I cannot figure out a combination that actually works.



For example, the following works when executed directly on the console:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password


The password contains a $ which is then escaped with $. However, the exact same command does not work when put into a shell_script.sh. When I execute the script, MySQL complains



Access denied for user 'the_user'@'localhost'


So far I have tried the following within the script:



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the$password | mysql -u the_user target_db --password=the$password

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'

mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"

mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'


None of these seem to escape the password properly. Out of these, only the first one works properly when used directly on the shell.



What am I missing? How do I have to specify the password, so these commands also work from within a shell script?







bash shell passwords mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:28

























asked Nov 12 at 14:14









fritzmg

23129




23129












  • Is the actual problematic character a dollar sign?
    – choroba
    Nov 12 at 14:20










  • The actual password also contains other special characters, like %, =, ?, # for example. However, out of these I only need to escape the $ - at least directly on the shell. The % is at the beginning of the password, if that makes any difference (which it might?).
    – fritzmg
    Nov 12 at 14:25












  • % isn't special at all. ? might cause problems, though, as it might try to match. Whitespace could also be a problem. Backslashing every problematic character should work, though.
    – choroba
    Nov 12 at 15:20










  • Still no luck. I have backslashed every character outside of [a-zA-Z0-9], and still the command does not work when executed via a script. It still only works when executed directly on the command line.
    – fritzmg
    Nov 12 at 15:24










  • Are you sure there's no ssh or other additional level of interpretation involved in the script?
    – choroba
    Nov 12 at 15:40


















  • Is the actual problematic character a dollar sign?
    – choroba
    Nov 12 at 14:20










  • The actual password also contains other special characters, like %, =, ?, # for example. However, out of these I only need to escape the $ - at least directly on the shell. The % is at the beginning of the password, if that makes any difference (which it might?).
    – fritzmg
    Nov 12 at 14:25












  • % isn't special at all. ? might cause problems, though, as it might try to match. Whitespace could also be a problem. Backslashing every problematic character should work, though.
    – choroba
    Nov 12 at 15:20










  • Still no luck. I have backslashed every character outside of [a-zA-Z0-9], and still the command does not work when executed via a script. It still only works when executed directly on the command line.
    – fritzmg
    Nov 12 at 15:24










  • Are you sure there's no ssh or other additional level of interpretation involved in the script?
    – choroba
    Nov 12 at 15:40
















Is the actual problematic character a dollar sign?
– choroba
Nov 12 at 14:20




Is the actual problematic character a dollar sign?
– choroba
Nov 12 at 14:20












The actual password also contains other special characters, like %, =, ?, # for example. However, out of these I only need to escape the $ - at least directly on the shell. The % is at the beginning of the password, if that makes any difference (which it might?).
– fritzmg
Nov 12 at 14:25






The actual password also contains other special characters, like %, =, ?, # for example. However, out of these I only need to escape the $ - at least directly on the shell. The % is at the beginning of the password, if that makes any difference (which it might?).
– fritzmg
Nov 12 at 14:25














% isn't special at all. ? might cause problems, though, as it might try to match. Whitespace could also be a problem. Backslashing every problematic character should work, though.
– choroba
Nov 12 at 15:20




% isn't special at all. ? might cause problems, though, as it might try to match. Whitespace could also be a problem. Backslashing every problematic character should work, though.
– choroba
Nov 12 at 15:20












Still no luck. I have backslashed every character outside of [a-zA-Z0-9], and still the command does not work when executed via a script. It still only works when executed directly on the command line.
– fritzmg
Nov 12 at 15:24




Still no luck. I have backslashed every character outside of [a-zA-Z0-9], and still the command does not work when executed via a script. It still only works when executed directly on the command line.
– fritzmg
Nov 12 at 15:24












Are you sure there's no ssh or other additional level of interpretation involved in the script?
– choroba
Nov 12 at 15:40




Are you sure there's no ssh or other additional level of interpretation involved in the script?
– choroba
Nov 12 at 15:40










3 Answers
3






active

oldest

votes

















up vote
0
down vote













I would do the following:



printf -v password "%q" "$password"
mysql --password=$password


More details on printf:
printf on wiki.bash-hackers.org






share|improve this answer























  • That did not (quite) work either. Also with your version of the mysql part of the command (without the = between --password and $password), I simply end up in the mysql interface.
    – fritzmg
    Nov 12 at 15:09










  • Actually the lack of = was a typo. Thanks for pointing out.
    – Konrad Botor
    Nov 12 at 15:13










  • Yeah, I suspected as much. However, it still does not work :(
    – fritzmg
    Nov 12 at 15:21


















up vote
0
down vote













Try to put the password (without New Line and Without Escape) into a textfile e.g. pass.txt and then use the following



mysqldump -u the_user --hex-blob --add-drop-table source_db --password=$(cat pass.txt) | mysql -u the_user target_db --password=$(cat pass.txt)





share|improve this answer























  • That did not work. I am assuming by "without NR" you mean without escaping the characters?
    – fritzmg
    Nov 12 at 14:32










  • without escaping and without the new lines.
    – Zhihua Lai
    Nov 12 at 14:34










  • Yeah, that's what I did. However echo pass.txt doesn't actually echo the contents of pass.txt... it just echos pass.txt ;)
    – fritzmg
    Nov 12 at 14:36










  • Have you tried cat pass.txt?
    – Konrad Botor
    Nov 12 at 15:14










  • yes, should be cat pass.txt. My mistakes
    – Zhihua Lai
    Nov 14 at 10:38




















up vote
0
down vote













Add the password to the SQL ini file in a separate [mysqldump] section. If you need it for the MYSQL call too add it to the [mysql] section of the ini file also.



[mysqldump]
password="the$password"

[mysql]
password="the$password"





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1374726%2fdatabase-copy-shell-script-including-password-with-special-characters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I would do the following:



    printf -v password "%q" "$password"
    mysql --password=$password


    More details on printf:
    printf on wiki.bash-hackers.org






    share|improve this answer























    • That did not (quite) work either. Also with your version of the mysql part of the command (without the = between --password and $password), I simply end up in the mysql interface.
      – fritzmg
      Nov 12 at 15:09










    • Actually the lack of = was a typo. Thanks for pointing out.
      – Konrad Botor
      Nov 12 at 15:13










    • Yeah, I suspected as much. However, it still does not work :(
      – fritzmg
      Nov 12 at 15:21















    up vote
    0
    down vote













    I would do the following:



    printf -v password "%q" "$password"
    mysql --password=$password


    More details on printf:
    printf on wiki.bash-hackers.org






    share|improve this answer























    • That did not (quite) work either. Also with your version of the mysql part of the command (without the = between --password and $password), I simply end up in the mysql interface.
      – fritzmg
      Nov 12 at 15:09










    • Actually the lack of = was a typo. Thanks for pointing out.
      – Konrad Botor
      Nov 12 at 15:13










    • Yeah, I suspected as much. However, it still does not work :(
      – fritzmg
      Nov 12 at 15:21













    up vote
    0
    down vote










    up vote
    0
    down vote









    I would do the following:



    printf -v password "%q" "$password"
    mysql --password=$password


    More details on printf:
    printf on wiki.bash-hackers.org






    share|improve this answer














    I would do the following:



    printf -v password "%q" "$password"
    mysql --password=$password


    More details on printf:
    printf on wiki.bash-hackers.org







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 15:12

























    answered Nov 12 at 14:23









    Konrad Botor

    2947




    2947












    • That did not (quite) work either. Also with your version of the mysql part of the command (without the = between --password and $password), I simply end up in the mysql interface.
      – fritzmg
      Nov 12 at 15:09










    • Actually the lack of = was a typo. Thanks for pointing out.
      – Konrad Botor
      Nov 12 at 15:13










    • Yeah, I suspected as much. However, it still does not work :(
      – fritzmg
      Nov 12 at 15:21


















    • That did not (quite) work either. Also with your version of the mysql part of the command (without the = between --password and $password), I simply end up in the mysql interface.
      – fritzmg
      Nov 12 at 15:09










    • Actually the lack of = was a typo. Thanks for pointing out.
      – Konrad Botor
      Nov 12 at 15:13










    • Yeah, I suspected as much. However, it still does not work :(
      – fritzmg
      Nov 12 at 15:21
















    That did not (quite) work either. Also with your version of the mysql part of the command (without the = between --password and $password), I simply end up in the mysql interface.
    – fritzmg
    Nov 12 at 15:09




    That did not (quite) work either. Also with your version of the mysql part of the command (without the = between --password and $password), I simply end up in the mysql interface.
    – fritzmg
    Nov 12 at 15:09












    Actually the lack of = was a typo. Thanks for pointing out.
    – Konrad Botor
    Nov 12 at 15:13




    Actually the lack of = was a typo. Thanks for pointing out.
    – Konrad Botor
    Nov 12 at 15:13












    Yeah, I suspected as much. However, it still does not work :(
    – fritzmg
    Nov 12 at 15:21




    Yeah, I suspected as much. However, it still does not work :(
    – fritzmg
    Nov 12 at 15:21












    up vote
    0
    down vote













    Try to put the password (without New Line and Without Escape) into a textfile e.g. pass.txt and then use the following



    mysqldump -u the_user --hex-blob --add-drop-table source_db --password=$(cat pass.txt) | mysql -u the_user target_db --password=$(cat pass.txt)





    share|improve this answer























    • That did not work. I am assuming by "without NR" you mean without escaping the characters?
      – fritzmg
      Nov 12 at 14:32










    • without escaping and without the new lines.
      – Zhihua Lai
      Nov 12 at 14:34










    • Yeah, that's what I did. However echo pass.txt doesn't actually echo the contents of pass.txt... it just echos pass.txt ;)
      – fritzmg
      Nov 12 at 14:36










    • Have you tried cat pass.txt?
      – Konrad Botor
      Nov 12 at 15:14










    • yes, should be cat pass.txt. My mistakes
      – Zhihua Lai
      Nov 14 at 10:38

















    up vote
    0
    down vote













    Try to put the password (without New Line and Without Escape) into a textfile e.g. pass.txt and then use the following



    mysqldump -u the_user --hex-blob --add-drop-table source_db --password=$(cat pass.txt) | mysql -u the_user target_db --password=$(cat pass.txt)





    share|improve this answer























    • That did not work. I am assuming by "without NR" you mean without escaping the characters?
      – fritzmg
      Nov 12 at 14:32










    • without escaping and without the new lines.
      – Zhihua Lai
      Nov 12 at 14:34










    • Yeah, that's what I did. However echo pass.txt doesn't actually echo the contents of pass.txt... it just echos pass.txt ;)
      – fritzmg
      Nov 12 at 14:36










    • Have you tried cat pass.txt?
      – Konrad Botor
      Nov 12 at 15:14










    • yes, should be cat pass.txt. My mistakes
      – Zhihua Lai
      Nov 14 at 10:38















    up vote
    0
    down vote










    up vote
    0
    down vote









    Try to put the password (without New Line and Without Escape) into a textfile e.g. pass.txt and then use the following



    mysqldump -u the_user --hex-blob --add-drop-table source_db --password=$(cat pass.txt) | mysql -u the_user target_db --password=$(cat pass.txt)





    share|improve this answer














    Try to put the password (without New Line and Without Escape) into a textfile e.g. pass.txt and then use the following



    mysqldump -u the_user --hex-blob --add-drop-table source_db --password=$(cat pass.txt) | mysql -u the_user target_db --password=$(cat pass.txt)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 at 10:38

























    answered Nov 12 at 14:20









    Zhihua Lai

    1188




    1188












    • That did not work. I am assuming by "without NR" you mean without escaping the characters?
      – fritzmg
      Nov 12 at 14:32










    • without escaping and without the new lines.
      – Zhihua Lai
      Nov 12 at 14:34










    • Yeah, that's what I did. However echo pass.txt doesn't actually echo the contents of pass.txt... it just echos pass.txt ;)
      – fritzmg
      Nov 12 at 14:36










    • Have you tried cat pass.txt?
      – Konrad Botor
      Nov 12 at 15:14










    • yes, should be cat pass.txt. My mistakes
      – Zhihua Lai
      Nov 14 at 10:38




















    • That did not work. I am assuming by "without NR" you mean without escaping the characters?
      – fritzmg
      Nov 12 at 14:32










    • without escaping and without the new lines.
      – Zhihua Lai
      Nov 12 at 14:34










    • Yeah, that's what I did. However echo pass.txt doesn't actually echo the contents of pass.txt... it just echos pass.txt ;)
      – fritzmg
      Nov 12 at 14:36










    • Have you tried cat pass.txt?
      – Konrad Botor
      Nov 12 at 15:14










    • yes, should be cat pass.txt. My mistakes
      – Zhihua Lai
      Nov 14 at 10:38


















    That did not work. I am assuming by "without NR" you mean without escaping the characters?
    – fritzmg
    Nov 12 at 14:32




    That did not work. I am assuming by "without NR" you mean without escaping the characters?
    – fritzmg
    Nov 12 at 14:32












    without escaping and without the new lines.
    – Zhihua Lai
    Nov 12 at 14:34




    without escaping and without the new lines.
    – Zhihua Lai
    Nov 12 at 14:34












    Yeah, that's what I did. However echo pass.txt doesn't actually echo the contents of pass.txt... it just echos pass.txt ;)
    – fritzmg
    Nov 12 at 14:36




    Yeah, that's what I did. However echo pass.txt doesn't actually echo the contents of pass.txt... it just echos pass.txt ;)
    – fritzmg
    Nov 12 at 14:36












    Have you tried cat pass.txt?
    – Konrad Botor
    Nov 12 at 15:14




    Have you tried cat pass.txt?
    – Konrad Botor
    Nov 12 at 15:14












    yes, should be cat pass.txt. My mistakes
    – Zhihua Lai
    Nov 14 at 10:38






    yes, should be cat pass.txt. My mistakes
    – Zhihua Lai
    Nov 14 at 10:38












    up vote
    0
    down vote













    Add the password to the SQL ini file in a separate [mysqldump] section. If you need it for the MYSQL call too add it to the [mysql] section of the ini file also.



    [mysqldump]
    password="the$password"

    [mysql]
    password="the$password"





    share|improve this answer

























      up vote
      0
      down vote













      Add the password to the SQL ini file in a separate [mysqldump] section. If you need it for the MYSQL call too add it to the [mysql] section of the ini file also.



      [mysqldump]
      password="the$password"

      [mysql]
      password="the$password"





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Add the password to the SQL ini file in a separate [mysqldump] section. If you need it for the MYSQL call too add it to the [mysql] section of the ini file also.



        [mysqldump]
        password="the$password"

        [mysql]
        password="the$password"





        share|improve this answer












        Add the password to the SQL ini file in a separate [mysqldump] section. If you need it for the MYSQL call too add it to the [mysql] section of the ini file also.



        [mysqldump]
        password="the$password"

        [mysql]
        password="the$password"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 at 12:38









        Dave

        16515




        16515






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1374726%2fdatabase-copy-shell-script-including-password-with-special-characters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            Brian Clough

            Cáceres