LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-25-2018, 12:44 PM   #1
david-campisi
LQ Newbie
 
Registered: Jun 2008
Posts: 9

Rep: Reputation: 0
Unhappy 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?
 
Old 01-25-2018, 01:57 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
/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

Last edited by keefaz; 01-25-2018 at 02:00 PM.
 
Old 01-25-2018, 02:54 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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.
 
1 members found this post helpful.
Old 01-25-2018, 07:21 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
2 members found this post helpful.
Old 01-25-2018, 07:25 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes I forgot about full path, reading local email might be usefull if cron errors go there
 
Old 01-29-2018, 06:49 AM   #6
david-campisi
LQ Newbie
 
Registered: Jun 2008
Posts: 9

Original Poster
Rep: Reputation: 0
The folders exist for $dow /backup/Mon
 
Old 01-29-2018, 08:21 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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.
 
Old 01-30-2018, 06:34 AM   #8
david-campisi
LQ Newbie
 
Registered: Jun 2008
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 01-30-2018, 08:06 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Where is the variable dow defined?
 
Old 01-30-2018, 11:01 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Also, remove the { and } around the variable, it will just add path error
 
Old 01-31-2018, 07:51 AM   #11
david-campisi
LQ Newbie
 
Registered: Jun 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by keefaz View Post
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?
 
Old 01-31-2018, 09:36 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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
 
Old 02-01-2018, 10:28 AM   #13
david-campisi
LQ Newbie
 
Registered: Jun 2008
Posts: 9

Original Poster
Rep: Reputation: 0
#!/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.
 
Old 02-01-2018, 10:58 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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
 
Old 02-01-2018, 11:16 AM   #15
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe set LC_TIME also just in case
Code:
dow=$(LC_TIME=c /bin/date "+%a")
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH working but SCP not working ravishankaradepu Linux - Server 18 06-01-2012 06:34 PM
scp to invoke files stored in environment variable shik28 Linux - Newbie 10 09-16-2011 03:41 AM
[SOLVED] ssh scp key not working to ssh/scp without password anon091 Linux - Newbie 9 08-22-2011 04:28 PM
scp not working lord-fu Fedora 2 08-01-2007 08:27 AM
scp error, TERM env. variable not set? jimbo Linux - Networking 2 11-26-2005 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration