rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 8:00 pm

I've been able to create a local mount with Dougie's help.

No I have been trying to automate a backup of RPi folders to the mounted Win Folder.

All is woring very well with static folder names, but I would like to have the RSYNC command use Month and Day in the folder names.

Here is what works:
rsync -a /home/pi/Projects /mnt/ThinkCentre/RPi/trialRPi_BU/Saturday

The folder Projects and it's content is being copied to the correct mounted folder trialRPi_BU and adds folder Saturday if it is not already present.

Now, I have been reading multiple examples using date, so, I made a bash script with the commands:
DATE=`date +%m.%d`
rsync -av /home/pi/Projects /mnt/ThinkCentre/RPi/trialRPi_BU/$DATE

This now copies origin folder Projects and content, but, adds the following to the destination folder trilRPi_BU:
folder name: DATE
and inside folder DATE, copies the appropriate folder Projects and it's content.

Why is DATE not recognized as a variable to be interpreted?

User avatar
DougieLawson
Posts: 37670
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 8:49 pm

Run your script with bash -x /home/pi/rsync.sh # or whatever its name is. That shows how each variable expands and should show what's going wrong.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 9:11 pm

This is the output of the bash -x (as you can see, I added /usr/bin/date to see if it was a date retrical problem)
=============
+ DAY0=''\''/usr/bin/date'\''/'
+ DAY1='/usr/bin/date -I -d "1 day ago"/'
+ SRC=/home/pi/Projects
+ TRG='/mnt/ThinkCentre/RPi/TrialRPi_BU/'\''/usr/bin/date'\''/'
+ OPT=-avhr
+ rsync -avhr /home/pi/Projects '/mnt/ThinkCentre/RPi/TrialRPi_BU/'\''/usr/bin/date'\''/'
sending incremental file list
rsync: mkdir "/mnt/ThinkCentre/RPi/TrialRPi_BU/'/usr/bin/date'" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(674) [Receiver=3.1.1]

Dutch_Master
Posts: 362
Joined: Sat Jul 27, 2013 11:36 am

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 9:21 pm

If you want the Bash shell to expand a command including another command, use back-ticks. These are usually on the same key as the tilde ~.

example:

Code: Select all

#! /bin/bash
#
echo "Today is" `date`
HTH!

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 9:26 pm

I made another small script, entered exactly as above echo "today is" 'date'
and received the following: Today is date

User avatar
rpdom
Posts: 16369
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 9:46 pm

Dutch_Master wrote:If you want the Bash shell to expand a command including another command, use back-ticks. These are usually on the same key as the tilde ~.
Not on my keyboard, they aren't. (UK keyboard layout) ;-)

Also, the use of back ticks in bash is deprecated. They can get messy and confusing. The current recommendation is to use the $() construct for the command, which can have multiple levels.

Your example, but using $()

Code: Select all

#! /bin/bash
#
echo "Today is" $(date)
although I would code that as

Code: Select all

#! /bin/bash
#
echo "Today is $(date)"

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 9:52 pm

Getting somewhere

So, echo "Today is" $(date) - TY!

Then I tried to get it into a variable:
noww= $(date +'%Y')

This gave the result: line 6: 2016: command not found

User avatar
rpdom
Posts: 16369
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 9:56 pm

Get rid of the space after the =
It needs to be

Code: Select all

noww=$(date +'%Y')
not

Code: Select all

noww= $(date +'%Y')

User avatar
DougieLawson
Posts: 37670
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 9:56 pm

There's no spaces on assignment in bash. T'is a real PITA but you'll get used to it.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 10:03 pm

I retract, it worked!

using:
BUyear=$(date +'%Y')
rsync -av //target /destination/$BUyear

worked

Thank you all again!!

Dutch_Master
Posts: 362
Joined: Sat Jul 27, 2013 11:36 am

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 10:23 pm

rfeyer wrote:I made another small script, entered exactly as above echo "today is" 'date'
and received the following: Today is date
Because you used single quotes, not back-ticks.

@rpdom: I wasn't aware that back-ticks are now depreciated. They still work on mine.

Code: Select all

bash --version
GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu)
(trunkated)

User avatar
rpdom
Posts: 16369
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RSYNC with DAY and MONTH in destination folder

Sat Nov 19, 2016 10:35 pm

Dutch_Master wrote:@rpdom: I wasn't aware that back-ticks are now depreciated. They still work on mine.

Code: Select all

bash --version
GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu)
(trunkated)
I didn't say they didn't work, just that they are deprecated (the use of them is discouraged).

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: RSYNC with DAY and MONTH in destination folder

Sun Nov 20, 2016 6:17 pm

Sorry to say - after all your help I am still stuck.
Below are the bash file and error:

#!/bin/bash
toPath="/mnt/ThinkCentre/RPi/TrialRPi_BU/$(date +'%B')/$(date +'%d')"
#execute
rsync -rh /home/pi/Projects/ $toPath

error:

rsync: mkdir "/mnt/ThinkCentre/RPi/TrialRPi_BU/November/20" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(674) [Receiver=3.1.1]

User avatar
rpdom
Posts: 16369
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RSYNC with DAY and MONTH in destination folder

Sun Nov 20, 2016 6:35 pm

rfeyer wrote:Sorry to say - after all your help I am still stuck.
Below are the bash file and error:

#!/bin/bash
toPath="/mnt/ThinkCentre/RPi/TrialRPi_BU/$(date +'%B')/$(date +'%d')"
#execute
rsync -rh /home/pi/Projects/ $toPath

error:

rsync: mkdir "/mnt/ThinkCentre/RPi/TrialRPi_BU/November/20" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(674) [Receiver=3.1.1]
That's because the destination directory doesn't exist.

Try this

Code: Select all

#!/bin/bash
toPath="/mnt/ThinkCentre/RPi/TrialRPi_BU/$(date +'%B')/$(date +'%d')"

# Make sure destination directory exists
mkdir -p $toPath

#execute 
rsync -rh /home/pi/Projects/ $toPath
Oh, just as an aside, you could just use one date command in that line by including the / in the date format string

Code: Select all

toPath="/mnt/ThinkCentre/RPi/TrialRPi_BU/$(date +'%B/%d')"
or even

Code: Select all

toPath="$(date +'/mnt/ThinkCentre/RPi/TrialRPi_BU/%B/%d')"
By just using the one date command you avoid the possibilities of strange issues if the script is run at certain times.
For example, if the script runs very, very close to midnight on 30th November. The first date command would return "November", but the second command will run a fraction of a second later and the clock could have ticked past midnight and return "01" as it is now 1st December. A very rare occurrence, but it could happen.

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: RSYNC with DAY and MONTH in destination folder

Sun Nov 20, 2016 6:59 pm

TY for the reply - tried it, but still does not work.
I am pasting below the code I used - first with the variables - even with mkdir $toPath it still refused.
Then, as you can see inside code, I used almost the same path in the toPath variable, without the dates, and it worked.

Also, I thought rsync would create paths which are not existent?

#!/bin/bash

#toPath=/mnt/ThinkCentre/RPi/TrialRPi_BU/$(date +'%B')/$(date +'%d')
#toPath="$(date +'/mnt/ThinkCentre/RPi/TrialRPi_BU/%B/%d')"
mkdir -p $toPath

toPath = "/mnt/ThinkCentre/RPi/TrialRPi_BU"

#execute
rsync -rh /home/pi/Projects/ $toPath

rfeyer
Posts: 102
Joined: Sun Nov 23, 2014 9:25 pm

Re: RSYNC with DAY and MONTH in destination folder

Sun Nov 20, 2016 7:04 pm

OK - I rebooted RPi and the below worked!!!!
TY all again, not sure how I will get these variables straight!
So, though, I still thought rsync would create the folders?

#!/bin/bash

toPath="$(date +'/mnt/ThinkCentre/RPi/TrialRPi_BU/%B/%d')"
mkdir -p $toPath
toPath = "/mnt/ThinkCentre/RPi/TrialRPi_BU"

#execute
rsync -rh /home/pi/Projects/ $toPath

Return to “Beginners”