Solved Trying to combine variables/constants

jontheil

Member


Messages: 75

Dear all
I am trying to make a simple script to making rsync incremental backups from my FreeBSD server (10.1-Release) to a FreeNAS box. Should create new subdirs named by the current date on the NAS box and run from cron.
To make it look clean, I will store the ssh connection parameters, the basic backup path and the current date in variables. And then run the relevant rsync command using these variables.
But I can't figure out how to combine/concenate the variables.
I have looked almost anywhere on the net and in the manual sh(1) without finding the right solution.
Lines from my not-working script:
Code:
#!/bin/sh -xv
SSHCONNECT="ssh -p 9851 root@192.168.1.51"
SSHPATH="/mnt/naspool/nasstorage/backup/mflserver4/"
DATE="`date +%Y-%m-%d`"
*NEWDIR="$DATE$SSHPATH"
echo "$NEWDIR"
The echo output is obviously wrong - missing the date part:
Code:
/mnt/naspool/nasstorage/backup/mflserver4/
I have very limited experience in scripting so I guess the solution is straightforward.

Any help appreciated

Regards,
Jon
 

wblock@

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 3,638
Messages: 13,850

Don't quote the backticks, and you probably want the date at the end of the path:
Code:
SSHCONNECT="ssh -p 9851 root@192.168.1.51"
SSHPATH="/mnt/naspool/nasstorage/backup/mflserver4/"
DATE=`date +%Y-%m-%d`
NEWDIR="$SSHPATH$DATE"
echo "$NEWDIR"
There should probably be a slash after the date.

If you want to be scrupulously correct, the variables can be referred to like ${DATE}. This can make it both more and less clear at the same time:
Code:
SSHCONNECT="ssh -p 9851 root@192.168.1.51"
SSHPATH="/mnt/naspool/nasstorage/backup/mflserver4/"
DATE=`date +%Y-%m-%d`
NEWDIR="${SSHPATH}${DATE}"
echo "${NEWDIR}"
 

DutchDaemon

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 2,786
Messages: 11,279

You might as well get with the time and replace
Code:
DATE=`date +%Y-%m-%d`
with
Code:
DATE=$( date +%Y-%m-%d )
 
OP
OP
jontheil

jontheil

Member


Messages: 75

Thanks for the quick reply (now replies).
Actually, I had an echo statement for the $DATE before, and it showed up all right. But now I have tried to implement your other proposals:
Code:
SSHCONNECT="ssh -p 9851 root@192.168.1.51"
SSHPATH="/mnt/naspool/nasstorage/backup/mflserver4/"
DATE=`date +%Y-%m-%d`
NEWDIR="${SSHPATH}${DATE}/"
echo "SSHCONNECT = ${SSHCONNECT}"
echo "SSHPATH = ${SSHPATH}"
echo "DATE = ${DATE}"
echo "NEWDIR = ${NEWDIR}"
Yet the output is not as intended:
Code:
SSHCONNECT = ssh -p 9851 root@192.168.1.51
SSHPATH = /mnt/naspool/nasstorage/backup/mflserver4/
DATE = 2014-12-29
/014-12-29mnt/naspool/nasstorage/backup/mflserver4/
I have tried to reverse the tokens in the NEWDIR statement - with even worse result. Another strange thing: The last echo statement misses the NEWDIR = part. Not that it means anything here, but I wonder if I made a simple typo somewhere.
 

wblock@

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 3,638
Messages: 13,850

It works here:
Code:
SSHCONNECT = ssh -p 9851 root@192.168.1.51
SSHPATH = /mnt/naspool/nasstorage/backup/mflserver4/
DATE = 2014-12-28
NEWDIR = /mnt/naspool/nasstorage/backup/mflserver4/2014-12-28/
 
OP
OP
jontheil

jontheil

Member


Messages: 75

Well, thanks again for testing.
In fact, the script seems to behave okay here to. That is, if I redirect the output to a text file. So something must be wrong with my environment. Most of the time, I use Putty as a SSH terminal. Could it be a wrong setting here? Or a wrong setting in the environment on the server itself?
It seems to be shown correctly form webmins Command shell, but show wrong on my IPMI client.
Is is very pleasant to know that the script actually works, so I can help me making a more safe system.
At the same time, I don't like the fact that I cannot always trust the output on my screen.
When I upgraded from Release-10.0 to Release 10.1, I tried to do it right. But of course, something can have gone wrong there.
I realize that my "new" problem is very far from the original topic. So I should not ask for advise for this problem in the current thread. Though I would be happy for pointing my in the right direction.

Best regards and thanks again!
;)
 
Top