Why does this shell script fail in bash, but the commands work in SSH?
up vote
0
down vote
favorite
What I'm trying to write is a shell script that will back up a website and all the MySQL DBs that support it. I found examples all over the 'net, but when I run the script in bash in a jailed SSH session, it fails. If I type the commands straight into the SSH session, they work fine.
#!/bin/sh
DB1="first_db"
THEDATE=$(date +"%Y-%m-%d")
THEUSER=user
THEDBPW=password
mysqldump -u $THEDBUSER -p$THEDBPW $DB1 > dbbackup_$DB1_$THEDATE.sql
tar -czf sitebackup_website_$THEDATE.tar /home/website/public_html
gzip sitebackup_website_$THEDATE.tar
This script lives on the web server, so for testing purposes, I SSH into the server, cd
into the directory, and run bash backup.sh
. mysqldump
isn't connecting, seeming to fail to pass the password in (it returns an error reporting 'using password: NO'). Then the tar command fails, reporting that it can't stat /home/website/public_html
with a 'no such file or directory'. tar
then reports it is exiting with error status due to previous errors.
What am I missing here?
shell ssh tar
add a comment |
up vote
0
down vote
favorite
What I'm trying to write is a shell script that will back up a website and all the MySQL DBs that support it. I found examples all over the 'net, but when I run the script in bash in a jailed SSH session, it fails. If I type the commands straight into the SSH session, they work fine.
#!/bin/sh
DB1="first_db"
THEDATE=$(date +"%Y-%m-%d")
THEUSER=user
THEDBPW=password
mysqldump -u $THEDBUSER -p$THEDBPW $DB1 > dbbackup_$DB1_$THEDATE.sql
tar -czf sitebackup_website_$THEDATE.tar /home/website/public_html
gzip sitebackup_website_$THEDATE.tar
This script lives on the web server, so for testing purposes, I SSH into the server, cd
into the directory, and run bash backup.sh
. mysqldump
isn't connecting, seeming to fail to pass the password in (it returns an error reporting 'using password: NO'). Then the tar command fails, reporting that it can't stat /home/website/public_html
with a 'no such file or directory'. tar
then reports it is exiting with error status due to previous errors.
What am I missing here?
shell ssh tar
1
Can you explain what "a jailed SSH session" means. Are you runningssh foo chroot /bar bash backup.sh
? Does it work without thechroot
?
– Patrick
May 9 '14 at 12:45
Do you still have problems if you quote your variables? Use"$var"
or"${var}"
– terdon♦
May 9 '14 at 13:58
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
What I'm trying to write is a shell script that will back up a website and all the MySQL DBs that support it. I found examples all over the 'net, but when I run the script in bash in a jailed SSH session, it fails. If I type the commands straight into the SSH session, they work fine.
#!/bin/sh
DB1="first_db"
THEDATE=$(date +"%Y-%m-%d")
THEUSER=user
THEDBPW=password
mysqldump -u $THEDBUSER -p$THEDBPW $DB1 > dbbackup_$DB1_$THEDATE.sql
tar -czf sitebackup_website_$THEDATE.tar /home/website/public_html
gzip sitebackup_website_$THEDATE.tar
This script lives on the web server, so for testing purposes, I SSH into the server, cd
into the directory, and run bash backup.sh
. mysqldump
isn't connecting, seeming to fail to pass the password in (it returns an error reporting 'using password: NO'). Then the tar command fails, reporting that it can't stat /home/website/public_html
with a 'no such file or directory'. tar
then reports it is exiting with error status due to previous errors.
What am I missing here?
shell ssh tar
What I'm trying to write is a shell script that will back up a website and all the MySQL DBs that support it. I found examples all over the 'net, but when I run the script in bash in a jailed SSH session, it fails. If I type the commands straight into the SSH session, they work fine.
#!/bin/sh
DB1="first_db"
THEDATE=$(date +"%Y-%m-%d")
THEUSER=user
THEDBPW=password
mysqldump -u $THEDBUSER -p$THEDBPW $DB1 > dbbackup_$DB1_$THEDATE.sql
tar -czf sitebackup_website_$THEDATE.tar /home/website/public_html
gzip sitebackup_website_$THEDATE.tar
This script lives on the web server, so for testing purposes, I SSH into the server, cd
into the directory, and run bash backup.sh
. mysqldump
isn't connecting, seeming to fail to pass the password in (it returns an error reporting 'using password: NO'). Then the tar command fails, reporting that it can't stat /home/website/public_html
with a 'no such file or directory'. tar
then reports it is exiting with error status due to previous errors.
What am I missing here?
shell ssh tar
shell ssh tar
edited Dec 7 at 23:34
Rui F Ribeiro
38.7k1479128
38.7k1479128
asked May 9 '14 at 9:11
Bendustries
33
33
1
Can you explain what "a jailed SSH session" means. Are you runningssh foo chroot /bar bash backup.sh
? Does it work without thechroot
?
– Patrick
May 9 '14 at 12:45
Do you still have problems if you quote your variables? Use"$var"
or"${var}"
– terdon♦
May 9 '14 at 13:58
add a comment |
1
Can you explain what "a jailed SSH session" means. Are you runningssh foo chroot /bar bash backup.sh
? Does it work without thechroot
?
– Patrick
May 9 '14 at 12:45
Do you still have problems if you quote your variables? Use"$var"
or"${var}"
– terdon♦
May 9 '14 at 13:58
1
1
Can you explain what "a jailed SSH session" means. Are you running
ssh foo chroot /bar bash backup.sh
? Does it work without the chroot
?– Patrick
May 9 '14 at 12:45
Can you explain what "a jailed SSH session" means. Are you running
ssh foo chroot /bar bash backup.sh
? Does it work without the chroot
?– Patrick
May 9 '14 at 12:45
Do you still have problems if you quote your variables? Use
"$var"
or "${var}"
– terdon♦
May 9 '14 at 13:58
Do you still have problems if you quote your variables? Use
"$var"
or "${var}"
– terdon♦
May 9 '14 at 13:58
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
Check the script for Windows line endings by logging in on the server and running
cat -v /path/to/script
If the line ends with ^M
, that is the problem.
You can fix a file with broken line endings by running
dos2unix /path/to/script
If dos2unix doesn't exist on the server, you can instead use sed, like this:
sed -i 's/r$//' /path/to/script
This was exactly it. Once I randos2unix
on the script, it worked just fine. Thanks!
– Bendustries
May 11 '14 at 4:56
add a comment |
up vote
1
down vote
You have assigned the username to variable THEUSER
, but your mysqldump
command is using the variable THEDBUSER
.
2
That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
– Simkill
May 9 '14 at 9:32
Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again.
– Bendustries
May 11 '14 at 5:00
add a comment |
up vote
0
down vote
tardir=$PWD ; cd /home/website/public_html &&
tar -czf "$tardir"/sitebackup_complete-office_"$THEDATE.tar"
or just:
tar -C/home/website/public_html -czf
sitebackup_complete-office_"$THEDATE.tar"
In general many tars
will fail to work at all with absolute paths unless explicitly instructed to do so.
By default, GNU tar drops a leading
/
on input or output,and complains about file names containing a..
component. This option
turns off this behavior.
It's entirely possible that ssh's
commands are resolving paths beforehand depending on command delivery and quoting. The same is relevant to the expansion of the variables in your pathnames.
add a comment |
up vote
0
down vote
If I understand you description of the problem correctly, the key difference seems to be the jailed SSH session - processes running inside the jailed session cannot see outside of their jail directory (often their home). That's why you don't experience problems in non-jailed session. Yo might also have a look at the wikipedia Jail entry for more details.
It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
– Jenny D
May 9 '14 at 12:19
You're right, Jenny D - all SSH is jailed for me.
– Bendustries
May 11 '14 at 4:59
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
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%2funix.stackexchange.com%2fquestions%2f128637%2fwhy-does-this-shell-script-fail-in-bash-but-the-commands-work-in-ssh%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Check the script for Windows line endings by logging in on the server and running
cat -v /path/to/script
If the line ends with ^M
, that is the problem.
You can fix a file with broken line endings by running
dos2unix /path/to/script
If dos2unix doesn't exist on the server, you can instead use sed, like this:
sed -i 's/r$//' /path/to/script
This was exactly it. Once I randos2unix
on the script, it worked just fine. Thanks!
– Bendustries
May 11 '14 at 4:56
add a comment |
up vote
2
down vote
accepted
Check the script for Windows line endings by logging in on the server and running
cat -v /path/to/script
If the line ends with ^M
, that is the problem.
You can fix a file with broken line endings by running
dos2unix /path/to/script
If dos2unix doesn't exist on the server, you can instead use sed, like this:
sed -i 's/r$//' /path/to/script
This was exactly it. Once I randos2unix
on the script, it worked just fine. Thanks!
– Bendustries
May 11 '14 at 4:56
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Check the script for Windows line endings by logging in on the server and running
cat -v /path/to/script
If the line ends with ^M
, that is the problem.
You can fix a file with broken line endings by running
dos2unix /path/to/script
If dos2unix doesn't exist on the server, you can instead use sed, like this:
sed -i 's/r$//' /path/to/script
Check the script for Windows line endings by logging in on the server and running
cat -v /path/to/script
If the line ends with ^M
, that is the problem.
You can fix a file with broken line endings by running
dos2unix /path/to/script
If dos2unix doesn't exist on the server, you can instead use sed, like this:
sed -i 's/r$//' /path/to/script
answered May 9 '14 at 10:07
Jenny D
10.5k22745
10.5k22745
This was exactly it. Once I randos2unix
on the script, it worked just fine. Thanks!
– Bendustries
May 11 '14 at 4:56
add a comment |
This was exactly it. Once I randos2unix
on the script, it worked just fine. Thanks!
– Bendustries
May 11 '14 at 4:56
This was exactly it. Once I ran
dos2unix
on the script, it worked just fine. Thanks!– Bendustries
May 11 '14 at 4:56
This was exactly it. Once I ran
dos2unix
on the script, it worked just fine. Thanks!– Bendustries
May 11 '14 at 4:56
add a comment |
up vote
1
down vote
You have assigned the username to variable THEUSER
, but your mysqldump
command is using the variable THEDBUSER
.
2
That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
– Simkill
May 9 '14 at 9:32
Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again.
– Bendustries
May 11 '14 at 5:00
add a comment |
up vote
1
down vote
You have assigned the username to variable THEUSER
, but your mysqldump
command is using the variable THEDBUSER
.
2
That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
– Simkill
May 9 '14 at 9:32
Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again.
– Bendustries
May 11 '14 at 5:00
add a comment |
up vote
1
down vote
up vote
1
down vote
You have assigned the username to variable THEUSER
, but your mysqldump
command is using the variable THEDBUSER
.
You have assigned the username to variable THEUSER
, but your mysqldump
command is using the variable THEDBUSER
.
edited May 9 '14 at 10:02
peterph
23k24457
23k24457
answered May 9 '14 at 9:22
user67231
191
191
2
That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
– Simkill
May 9 '14 at 9:32
Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again.
– Bendustries
May 11 '14 at 5:00
add a comment |
2
That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
– Simkill
May 9 '14 at 9:32
Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again.
– Bendustries
May 11 '14 at 5:00
2
2
That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
– Simkill
May 9 '14 at 9:32
That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
– Simkill
May 9 '14 at 9:32
Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again.
– Bendustries
May 11 '14 at 5:00
Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again.
– Bendustries
May 11 '14 at 5:00
add a comment |
up vote
0
down vote
tardir=$PWD ; cd /home/website/public_html &&
tar -czf "$tardir"/sitebackup_complete-office_"$THEDATE.tar"
or just:
tar -C/home/website/public_html -czf
sitebackup_complete-office_"$THEDATE.tar"
In general many tars
will fail to work at all with absolute paths unless explicitly instructed to do so.
By default, GNU tar drops a leading
/
on input or output,and complains about file names containing a..
component. This option
turns off this behavior.
It's entirely possible that ssh's
commands are resolving paths beforehand depending on command delivery and quoting. The same is relevant to the expansion of the variables in your pathnames.
add a comment |
up vote
0
down vote
tardir=$PWD ; cd /home/website/public_html &&
tar -czf "$tardir"/sitebackup_complete-office_"$THEDATE.tar"
or just:
tar -C/home/website/public_html -czf
sitebackup_complete-office_"$THEDATE.tar"
In general many tars
will fail to work at all with absolute paths unless explicitly instructed to do so.
By default, GNU tar drops a leading
/
on input or output,and complains about file names containing a..
component. This option
turns off this behavior.
It's entirely possible that ssh's
commands are resolving paths beforehand depending on command delivery and quoting. The same is relevant to the expansion of the variables in your pathnames.
add a comment |
up vote
0
down vote
up vote
0
down vote
tardir=$PWD ; cd /home/website/public_html &&
tar -czf "$tardir"/sitebackup_complete-office_"$THEDATE.tar"
or just:
tar -C/home/website/public_html -czf
sitebackup_complete-office_"$THEDATE.tar"
In general many tars
will fail to work at all with absolute paths unless explicitly instructed to do so.
By default, GNU tar drops a leading
/
on input or output,and complains about file names containing a..
component. This option
turns off this behavior.
It's entirely possible that ssh's
commands are resolving paths beforehand depending on command delivery and quoting. The same is relevant to the expansion of the variables in your pathnames.
tardir=$PWD ; cd /home/website/public_html &&
tar -czf "$tardir"/sitebackup_complete-office_"$THEDATE.tar"
or just:
tar -C/home/website/public_html -czf
sitebackup_complete-office_"$THEDATE.tar"
In general many tars
will fail to work at all with absolute paths unless explicitly instructed to do so.
By default, GNU tar drops a leading
/
on input or output,and complains about file names containing a..
component. This option
turns off this behavior.
It's entirely possible that ssh's
commands are resolving paths beforehand depending on command delivery and quoting. The same is relevant to the expansion of the variables in your pathnames.
edited May 9 '14 at 10:03
answered May 9 '14 at 9:54
mikeserv
45.3k567153
45.3k567153
add a comment |
add a comment |
up vote
0
down vote
If I understand you description of the problem correctly, the key difference seems to be the jailed SSH session - processes running inside the jailed session cannot see outside of their jail directory (often their home). That's why you don't experience problems in non-jailed session. Yo might also have a look at the wikipedia Jail entry for more details.
It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
– Jenny D
May 9 '14 at 12:19
You're right, Jenny D - all SSH is jailed for me.
– Bendustries
May 11 '14 at 4:59
add a comment |
up vote
0
down vote
If I understand you description of the problem correctly, the key difference seems to be the jailed SSH session - processes running inside the jailed session cannot see outside of their jail directory (often their home). That's why you don't experience problems in non-jailed session. Yo might also have a look at the wikipedia Jail entry for more details.
It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
– Jenny D
May 9 '14 at 12:19
You're right, Jenny D - all SSH is jailed for me.
– Bendustries
May 11 '14 at 4:59
add a comment |
up vote
0
down vote
up vote
0
down vote
If I understand you description of the problem correctly, the key difference seems to be the jailed SSH session - processes running inside the jailed session cannot see outside of their jail directory (often their home). That's why you don't experience problems in non-jailed session. Yo might also have a look at the wikipedia Jail entry for more details.
If I understand you description of the problem correctly, the key difference seems to be the jailed SSH session - processes running inside the jailed session cannot see outside of their jail directory (often their home). That's why you don't experience problems in non-jailed session. Yo might also have a look at the wikipedia Jail entry for more details.
answered May 9 '14 at 10:11
peterph
23k24457
23k24457
It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
– Jenny D
May 9 '14 at 12:19
You're right, Jenny D - all SSH is jailed for me.
– Bendustries
May 11 '14 at 4:59
add a comment |
It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
– Jenny D
May 9 '14 at 12:19
You're right, Jenny D - all SSH is jailed for me.
– Bendustries
May 11 '14 at 4:59
It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
– Jenny D
May 9 '14 at 12:19
It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
– Jenny D
May 9 '14 at 12:19
You're right, Jenny D - all SSH is jailed for me.
– Bendustries
May 11 '14 at 4:59
You're right, Jenny D - all SSH is jailed for me.
– Bendustries
May 11 '14 at 4:59
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2funix.stackexchange.com%2fquestions%2f128637%2fwhy-does-this-shell-script-fail-in-bash-but-the-commands-work-in-ssh%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
1
Can you explain what "a jailed SSH session" means. Are you running
ssh foo chroot /bar bash backup.sh
? Does it work without thechroot
?– Patrick
May 9 '14 at 12:45
Do you still have problems if you quote your variables? Use
"$var"
or"${var}"
– terdon♦
May 9 '14 at 13:58