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?
bash shell passwords mysql
|
show 7 more comments
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?
bash shell passwords mysql
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 nosshor other additional level of interpretation involved in the script?
– choroba
Nov 12 at 15:40
|
show 7 more comments
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?
bash shell passwords mysql
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
bash shell passwords mysql
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 nosshor other additional level of interpretation involved in the script?
– choroba
Nov 12 at 15:40
|
show 7 more comments
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 nosshor 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
|
show 7 more comments
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
That did not (quite) work either. Also with your version of themysqlpart of the command (without the=between--passwordand$password), I simply end up in themysqlinterface.
– 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
add a comment |
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)
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. Howeverecho pass.txtdoesn't actually echo the contents ofpass.txt... it just echospass.txt;)
– fritzmg
Nov 12 at 14:36
Have you triedcat pass.txt?
– Konrad Botor
Nov 12 at 15:14
yes, should becat pass.txt. My mistakes
– Zhihua Lai
Nov 14 at 10:38
add a comment |
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"
add a comment |
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
That did not (quite) work either. Also with your version of themysqlpart of the command (without the=between--passwordand$password), I simply end up in themysqlinterface.
– 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
add a comment |
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
That did not (quite) work either. Also with your version of themysqlpart of the command (without the=between--passwordand$password), I simply end up in themysqlinterface.
– 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
add a comment |
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
I would do the following:
printf -v password "%q" "$password"
mysql --password=$password
More details on printf:
printf on wiki.bash-hackers.org
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 themysqlpart of the command (without the=between--passwordand$password), I simply end up in themysqlinterface.
– 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
add a comment |
That did not (quite) work either. Also with your version of themysqlpart of the command (without the=between--passwordand$password), I simply end up in themysqlinterface.
– 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
add a comment |
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)
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. Howeverecho pass.txtdoesn't actually echo the contents ofpass.txt... it just echospass.txt;)
– fritzmg
Nov 12 at 14:36
Have you triedcat pass.txt?
– Konrad Botor
Nov 12 at 15:14
yes, should becat pass.txt. My mistakes
– Zhihua Lai
Nov 14 at 10:38
add a comment |
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)
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. Howeverecho pass.txtdoesn't actually echo the contents ofpass.txt... it just echospass.txt;)
– fritzmg
Nov 12 at 14:36
Have you triedcat pass.txt?
– Konrad Botor
Nov 12 at 15:14
yes, should becat pass.txt. My mistakes
– Zhihua Lai
Nov 14 at 10:38
add a comment |
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)
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)
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. Howeverecho pass.txtdoesn't actually echo the contents ofpass.txt... it just echospass.txt;)
– fritzmg
Nov 12 at 14:36
Have you triedcat pass.txt?
– Konrad Botor
Nov 12 at 15:14
yes, should becat pass.txt. My mistakes
– Zhihua Lai
Nov 14 at 10:38
add a comment |
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. Howeverecho pass.txtdoesn't actually echo the contents ofpass.txt... it just echospass.txt;)
– fritzmg
Nov 12 at 14:36
Have you triedcat pass.txt?
– Konrad Botor
Nov 12 at 15:14
yes, should becat 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
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 14 at 12:38
Dave
16515
16515
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
sshor other additional level of interpretation involved in the script?– choroba
Nov 12 at 15:40