Why I can't pass a parameter from cron to shell script [on hold]
up vote
2
down vote
favorite
With this cron script I am trying to get Rsync to work
*/1 * * * * /root/backup.sh `date +today/%M`
And a shell script as the one below
#!/bin/bash -x
PATH=/bin:/usr/bin:/sbin:/usr/sbin
REMOTE="REMOTEADDRESS"
RSYNC=/usr/bin/rsync
Comment: This works
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:backups/
Comment: This fails
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:$1/
I am unable to understand why the passed parameter does not get passed correctly to the script. In the log everything looks as it should.
UPDATE
The reason for the cron to pass the parameter and not have it in the script is to have a backup schedule that allows to have :
- a backup each hour overwritten each day
- a backup each day overwritten each month
- a persistent Backup each month
The error when passing the parameter is that RSync can not mkdir on the remote server. It can with any static line. The log shows the correct directory passed when using the current cron.
debian shell cron
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as off-topic by Isaac, RalfFriedl, G-Man, Stephen Harris, Archemar 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Isaac, RalfFriedl, Stephen Harris, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 16 more comments
up vote
2
down vote
favorite
With this cron script I am trying to get Rsync to work
*/1 * * * * /root/backup.sh `date +today/%M`
And a shell script as the one below
#!/bin/bash -x
PATH=/bin:/usr/bin:/sbin:/usr/sbin
REMOTE="REMOTEADDRESS"
RSYNC=/usr/bin/rsync
Comment: This works
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:backups/
Comment: This fails
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:$1/
I am unable to understand why the passed parameter does not get passed correctly to the script. In the log everything looks as it should.
UPDATE
The reason for the cron to pass the parameter and not have it in the script is to have a backup schedule that allows to have :
- a backup each hour overwritten each day
- a backup each day overwritten each month
- a persistent Backup each month
The error when passing the parameter is that RSync can not mkdir on the remote server. It can with any static line. The log shows the correct directory passed when using the current cron.
debian shell cron
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as off-topic by Isaac, RalfFriedl, G-Man, Stephen Harris, Archemar 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Isaac, RalfFriedl, Stephen Harris, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.
1
What is the error message of the failed rsync command?
– rudimeier
Nov 30 at 18:24
Not really related, but why do you set the path and use an absolute path torsync?
– Kusalananda
Nov 30 at 19:47
The error from rsync is that I can't mkdir on the remote server. It can with the static command. I have been testing how to create a backup schedule where it backups every hour overwriting each day and then backing up each day overwriting each month and lastly have a persistent Backup each month. So I can't hardcode the path because it needs to vary with 3 cron scripts for the hour, day and month.
– Scorekaj22
Nov 30 at 20:38
So it's a permission problem unrelated to cron.
– Kusalananda
Nov 30 at 20:46
How can it be that? The cron runs it perfectly with the static line? So why should the permission be different run it with a parameter?
– Scorekaj22
Nov 30 at 20:48
|
show 16 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
With this cron script I am trying to get Rsync to work
*/1 * * * * /root/backup.sh `date +today/%M`
And a shell script as the one below
#!/bin/bash -x
PATH=/bin:/usr/bin:/sbin:/usr/sbin
REMOTE="REMOTEADDRESS"
RSYNC=/usr/bin/rsync
Comment: This works
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:backups/
Comment: This fails
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:$1/
I am unable to understand why the passed parameter does not get passed correctly to the script. In the log everything looks as it should.
UPDATE
The reason for the cron to pass the parameter and not have it in the script is to have a backup schedule that allows to have :
- a backup each hour overwritten each day
- a backup each day overwritten each month
- a persistent Backup each month
The error when passing the parameter is that RSync can not mkdir on the remote server. It can with any static line. The log shows the correct directory passed when using the current cron.
debian shell cron
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
With this cron script I am trying to get Rsync to work
*/1 * * * * /root/backup.sh `date +today/%M`
And a shell script as the one below
#!/bin/bash -x
PATH=/bin:/usr/bin:/sbin:/usr/sbin
REMOTE="REMOTEADDRESS"
RSYNC=/usr/bin/rsync
Comment: This works
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:backups/
Comment: This fails
$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:$1/
I am unable to understand why the passed parameter does not get passed correctly to the script. In the log everything looks as it should.
UPDATE
The reason for the cron to pass the parameter and not have it in the script is to have a backup schedule that allows to have :
- a backup each hour overwritten each day
- a backup each day overwritten each month
- a persistent Backup each month
The error when passing the parameter is that RSync can not mkdir on the remote server. It can with any static line. The log shows the correct directory passed when using the current cron.
debian shell cron
debian shell cron
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 30 at 21:17
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 30 at 18:02
Scorekaj22
134
134
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Scorekaj22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as off-topic by Isaac, RalfFriedl, G-Man, Stephen Harris, Archemar 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Isaac, RalfFriedl, Stephen Harris, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Isaac, RalfFriedl, G-Man, Stephen Harris, Archemar 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Isaac, RalfFriedl, Stephen Harris, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.
1
What is the error message of the failed rsync command?
– rudimeier
Nov 30 at 18:24
Not really related, but why do you set the path and use an absolute path torsync?
– Kusalananda
Nov 30 at 19:47
The error from rsync is that I can't mkdir on the remote server. It can with the static command. I have been testing how to create a backup schedule where it backups every hour overwriting each day and then backing up each day overwriting each month and lastly have a persistent Backup each month. So I can't hardcode the path because it needs to vary with 3 cron scripts for the hour, day and month.
– Scorekaj22
Nov 30 at 20:38
So it's a permission problem unrelated to cron.
– Kusalananda
Nov 30 at 20:46
How can it be that? The cron runs it perfectly with the static line? So why should the permission be different run it with a parameter?
– Scorekaj22
Nov 30 at 20:48
|
show 16 more comments
1
What is the error message of the failed rsync command?
– rudimeier
Nov 30 at 18:24
Not really related, but why do you set the path and use an absolute path torsync?
– Kusalananda
Nov 30 at 19:47
The error from rsync is that I can't mkdir on the remote server. It can with the static command. I have been testing how to create a backup schedule where it backups every hour overwriting each day and then backing up each day overwriting each month and lastly have a persistent Backup each month. So I can't hardcode the path because it needs to vary with 3 cron scripts for the hour, day and month.
– Scorekaj22
Nov 30 at 20:38
So it's a permission problem unrelated to cron.
– Kusalananda
Nov 30 at 20:46
How can it be that? The cron runs it perfectly with the static line? So why should the permission be different run it with a parameter?
– Scorekaj22
Nov 30 at 20:48
1
1
What is the error message of the failed rsync command?
– rudimeier
Nov 30 at 18:24
What is the error message of the failed rsync command?
– rudimeier
Nov 30 at 18:24
Not really related, but why do you set the path and use an absolute path to
rsync?– Kusalananda
Nov 30 at 19:47
Not really related, but why do you set the path and use an absolute path to
rsync?– Kusalananda
Nov 30 at 19:47
The error from rsync is that I can't mkdir on the remote server. It can with the static command. I have been testing how to create a backup schedule where it backups every hour overwriting each day and then backing up each day overwriting each month and lastly have a persistent Backup each month. So I can't hardcode the path because it needs to vary with 3 cron scripts for the hour, day and month.
– Scorekaj22
Nov 30 at 20:38
The error from rsync is that I can't mkdir on the remote server. It can with the static command. I have been testing how to create a backup schedule where it backups every hour overwriting each day and then backing up each day overwriting each month and lastly have a persistent Backup each month. So I can't hardcode the path because it needs to vary with 3 cron scripts for the hour, day and month.
– Scorekaj22
Nov 30 at 20:38
So it's a permission problem unrelated to cron.
– Kusalananda
Nov 30 at 20:46
So it's a permission problem unrelated to cron.
– Kusalananda
Nov 30 at 20:46
How can it be that? The cron runs it perfectly with the static line? So why should the permission be different run it with a parameter?
– Scorekaj22
Nov 30 at 20:48
How can it be that? The cron runs it perfectly with the static line? So why should the permission be different run it with a parameter?
– Scorekaj22
Nov 30 at 20:48
|
show 16 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You must ensure that the directory where you are trying to copy to (the dest/dir) exists in the remote computer:
rsync -aqz ./ user@host:dest/dir
At minimum, all the directories (except the last one) should already exist.
That is the same behavior as mkdir: mkdir will only create the last directory (by default, the -p option can change that).
That is: rsync will fail unless /home/user/dest/dir exists on the remote computer.
That is with relative directories.
The same also happens with absolute directories (ones that start with an /):
rsync -aqz ./ user@host:/home/user/dest/dir
The exact same rule but the directory is not assumed to be at the user home directory but could be anywhere (the user must have permissions to write to it, of course).
In additional testing I found that if rsync is going to use a directory like:
newdir/testdir
At least the directory newdir must exist. That is exactly the same as mkdir does. A mkdir newdir/testdir will fail if there is no newdir dir.
I tested it with the same script as you report:
- using with a fixed dir,
- a given dir on a parameter,
- a given dir on the script
$1parameter - all the above repeated with and without cron.
In all cases, missing the initial directory of a two parts directory gets rsync to fail.
1
Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you!
– Scorekaj22
2 days ago
You are welcome, glad that you found a good solution. … I must make a note that you should quote$REMOTE:$1/as"$REMOTE:$1/"to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-)
– Isaac
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You must ensure that the directory where you are trying to copy to (the dest/dir) exists in the remote computer:
rsync -aqz ./ user@host:dest/dir
At minimum, all the directories (except the last one) should already exist.
That is the same behavior as mkdir: mkdir will only create the last directory (by default, the -p option can change that).
That is: rsync will fail unless /home/user/dest/dir exists on the remote computer.
That is with relative directories.
The same also happens with absolute directories (ones that start with an /):
rsync -aqz ./ user@host:/home/user/dest/dir
The exact same rule but the directory is not assumed to be at the user home directory but could be anywhere (the user must have permissions to write to it, of course).
In additional testing I found that if rsync is going to use a directory like:
newdir/testdir
At least the directory newdir must exist. That is exactly the same as mkdir does. A mkdir newdir/testdir will fail if there is no newdir dir.
I tested it with the same script as you report:
- using with a fixed dir,
- a given dir on a parameter,
- a given dir on the script
$1parameter - all the above repeated with and without cron.
In all cases, missing the initial directory of a two parts directory gets rsync to fail.
1
Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you!
– Scorekaj22
2 days ago
You are welcome, glad that you found a good solution. … I must make a note that you should quote$REMOTE:$1/as"$REMOTE:$1/"to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-)
– Isaac
2 days ago
add a comment |
up vote
0
down vote
accepted
You must ensure that the directory where you are trying to copy to (the dest/dir) exists in the remote computer:
rsync -aqz ./ user@host:dest/dir
At minimum, all the directories (except the last one) should already exist.
That is the same behavior as mkdir: mkdir will only create the last directory (by default, the -p option can change that).
That is: rsync will fail unless /home/user/dest/dir exists on the remote computer.
That is with relative directories.
The same also happens with absolute directories (ones that start with an /):
rsync -aqz ./ user@host:/home/user/dest/dir
The exact same rule but the directory is not assumed to be at the user home directory but could be anywhere (the user must have permissions to write to it, of course).
In additional testing I found that if rsync is going to use a directory like:
newdir/testdir
At least the directory newdir must exist. That is exactly the same as mkdir does. A mkdir newdir/testdir will fail if there is no newdir dir.
I tested it with the same script as you report:
- using with a fixed dir,
- a given dir on a parameter,
- a given dir on the script
$1parameter - all the above repeated with and without cron.
In all cases, missing the initial directory of a two parts directory gets rsync to fail.
1
Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you!
– Scorekaj22
2 days ago
You are welcome, glad that you found a good solution. … I must make a note that you should quote$REMOTE:$1/as"$REMOTE:$1/"to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-)
– Isaac
2 days ago
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You must ensure that the directory where you are trying to copy to (the dest/dir) exists in the remote computer:
rsync -aqz ./ user@host:dest/dir
At minimum, all the directories (except the last one) should already exist.
That is the same behavior as mkdir: mkdir will only create the last directory (by default, the -p option can change that).
That is: rsync will fail unless /home/user/dest/dir exists on the remote computer.
That is with relative directories.
The same also happens with absolute directories (ones that start with an /):
rsync -aqz ./ user@host:/home/user/dest/dir
The exact same rule but the directory is not assumed to be at the user home directory but could be anywhere (the user must have permissions to write to it, of course).
In additional testing I found that if rsync is going to use a directory like:
newdir/testdir
At least the directory newdir must exist. That is exactly the same as mkdir does. A mkdir newdir/testdir will fail if there is no newdir dir.
I tested it with the same script as you report:
- using with a fixed dir,
- a given dir on a parameter,
- a given dir on the script
$1parameter - all the above repeated with and without cron.
In all cases, missing the initial directory of a two parts directory gets rsync to fail.
You must ensure that the directory where you are trying to copy to (the dest/dir) exists in the remote computer:
rsync -aqz ./ user@host:dest/dir
At minimum, all the directories (except the last one) should already exist.
That is the same behavior as mkdir: mkdir will only create the last directory (by default, the -p option can change that).
That is: rsync will fail unless /home/user/dest/dir exists on the remote computer.
That is with relative directories.
The same also happens with absolute directories (ones that start with an /):
rsync -aqz ./ user@host:/home/user/dest/dir
The exact same rule but the directory is not assumed to be at the user home directory but could be anywhere (the user must have permissions to write to it, of course).
In additional testing I found that if rsync is going to use a directory like:
newdir/testdir
At least the directory newdir must exist. That is exactly the same as mkdir does. A mkdir newdir/testdir will fail if there is no newdir dir.
I tested it with the same script as you report:
- using with a fixed dir,
- a given dir on a parameter,
- a given dir on the script
$1parameter - all the above repeated with and without cron.
In all cases, missing the initial directory of a two parts directory gets rsync to fail.
edited 2 days ago
answered 2 days ago
Isaac
10.2k11446
10.2k11446
1
Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you!
– Scorekaj22
2 days ago
You are welcome, glad that you found a good solution. … I must make a note that you should quote$REMOTE:$1/as"$REMOTE:$1/"to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-)
– Isaac
2 days ago
add a comment |
1
Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you!
– Scorekaj22
2 days ago
You are welcome, glad that you found a good solution. … I must make a note that you should quote$REMOTE:$1/as"$REMOTE:$1/"to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-)
– Isaac
2 days ago
1
1
Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you!
– Scorekaj22
2 days ago
Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you!
– Scorekaj22
2 days ago
You are welcome, glad that you found a good solution. … I must make a note that you should quote
$REMOTE:$1/ as "$REMOTE:$1/" to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-)– Isaac
2 days ago
You are welcome, glad that you found a good solution. … I must make a note that you should quote
$REMOTE:$1/ as "$REMOTE:$1/" to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-)– Isaac
2 days ago
add a comment |
1
What is the error message of the failed rsync command?
– rudimeier
Nov 30 at 18:24
Not really related, but why do you set the path and use an absolute path to
rsync?– Kusalananda
Nov 30 at 19:47
The error from rsync is that I can't mkdir on the remote server. It can with the static command. I have been testing how to create a backup schedule where it backups every hour overwriting each day and then backing up each day overwriting each month and lastly have a persistent Backup each month. So I can't hardcode the path because it needs to vary with 3 cron scripts for the hour, day and month.
– Scorekaj22
Nov 30 at 20:38
So it's a permission problem unrelated to cron.
– Kusalananda
Nov 30 at 20:46
How can it be that? The cron runs it perfectly with the static line? So why should the permission be different run it with a parameter?
– Scorekaj22
Nov 30 at 20:48