LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SCP with variable not working (https://www.linuxquestions.org/questions/linux-newbie-8/scp-with-variable-not-working-4175622394/)

david-campisi 01-25-2018 12:44 PM

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?

keefaz 01-25-2018 01:57 PM

/backup/$dow is supposed to be a directory? Maybe you have to create it before scp
Code:

ssh backupuser@server mkdir /backup/$dow
scp -p /root/backup/* backupuser@server:/backup/$dow


michaelk 01-25-2018 02:54 PM

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.

AwesomeMachine 01-25-2018 07:21 PM

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.

keefaz 01-25-2018 07:25 PM

Yes I forgot about full path, reading local email might be usefull if cron errors go there

david-campisi 01-29-2018 06:49 AM

The folders exist for $dow /backup/Mon

michaelk 01-29-2018 08:21 AM

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.

david-campisi 01-30-2018 06:34 AM

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.

michaelk 01-30-2018 08:06 AM

Where is the variable dow defined?

keefaz 01-30-2018 11:01 AM

Also, remove the { and } around the variable, it will just add path error

david-campisi 01-31-2018 07:51 AM

Quote:

Originally Posted by keefaz (Post 5813432)
Also, remove the { and } around the variable, it will just add path error

The variable defined is in Backupuser on xx.xx.xx.xx

Should I define it under root profile?

michaelk 01-31-2018 09:36 AM

I would define dow in the script itself.
Code:

#!/bin/bash
set -xv
dow=$(date "+%a")
scp -p /root/backup/* backupuser@xx.xx.xx.xx:/home/backup/$dow


david-campisi 02-01-2018 10:28 AM

#!/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.

michaelk 02-01-2018 10:58 AM

Your cron environment might be different and as previously suggested using the full path should work.
Code:

dow=$(/bin/date "+%a")
/usr/bin/scp -p /root/backup/* backupuser@xx.xx.xx.xx:/home/backup/$dow


keefaz 02-01-2018 11:16 AM

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.