SCP with variable not working
I have a script to log in to a remote server using scp with a variable. dow Day of week.
scp -p /root/backup/* backupuser@###.###.###.###:/backup/$dow The files do not end up in $dow Mon, Tue, Wed, Thu or Fri This command is in a script file backup.sh which is in crontab /home/backupuser/backup.sh I tried it with scp -p /root/backup/* backupuser@###.###.###.###:`/backup/$dow` back tic scp -p /root/backup/* backupuser@###.###.###.###:`/backup/{$dow} brackets Works from terminal . backup.sh scp -p /root/backup/* backupuser@###.###.###.###:/backup/{$dow} What else can I try? |
/backup/$dow is supposed to be a directory? Maybe you have to create it before scp
Code:
ssh backupuser@server mkdir /backup/$dow |
Welcome to LinuxQuestions.
Just for reference what distribution/version are you running? How did you create the cron job? Is it a system or user cron job? Normally a regular user does not have permission to access the root's home directory (/root). As stated scp does not create directories so I assume that your /root/backup directory contains only one file. |
In the script, the @ might be problematic. So, enclose the entire destination string in double quotes. Also, cron runs a different environment than a user. You might need to use full paths.
|
Yes I forgot about full path, reading local email might be usefull if cron errors go there
|
The folders exist for $dow /backup/Mon
|
Post your entire script.
Are you using ssh keys? Are you running the script as a system or user cron job? As stated a user will not have permission to access /root directory. Using full path to scp could help too. |
backup.sh
#!/bin/bash set -xv scp -p /root/backup/* backupuser@xx.xx.xx.xx:/home/backup/{$dow} Yes, I am using ssh keys. This backup.sh runs as a cron job. |
Where is the variable dow defined?
|
Also, remove the { and } around the variable, it will just add path error
|
Quote:
Should I define it under root profile? |
I would define dow in the script itself.
Code:
#!/bin/bash |
#!/bin/bash
set -xv dow=$(date "+%a") scp -p /root/backup/* backupuser@xx.xx.xx.xx:/home/backup/$dow No directory. I made crontab for each day of week. 00 11 * * 1 scp -p /root/backup/* backupuser@xx.xx.xx.xx:/home/backup/Mon 00 11 * * 2 scp -p /root/backup/* backupuser@xx.xx.xx.xx:/home/backup/Tue This worked. It must be the crontab environment. |
Your cron environment might be different and as previously suggested using the full path should work.
Code:
dow=$(/bin/date "+%a") |
Maybe set LC_TIME also just in case
Code:
dow=$(LC_TIME=c /bin/date "+%a") |
All times are GMT -5. The time now is 11:50 PM. |