LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2016, 11:19 PM   #1
Archy1
Member
 
Registered: Jan 2014
Distribution: Debian
Posts: 95

Rep: Reputation: 2
rsync exclude arguments held in variable not passing correctly (bourne script)


I have a shell script with hard coded arguments that works:
Code:
#!/bin/sh
DATE=`date "+%Y-%m-%d-%H:%M"`

rsync -avhPHS -x --exclude="/*/.adobe/" \
	--exclude="/*/.cache/" \
	--exclude="/*/.dbus/" \
	--exclude="/*/.gvfs/" \
	--exclude="/*/.macromedia/" \
	--exclude="/*/.thumbnails/" \
	 --link-dest=/mnt/backup/home/current \
 	/home/ /mnt/backup/home/$DATE

rm -f /mnt/backup/home/current
ln -s $DATE /mnt/backup/home/current
sync
To make things easier as my needs change, I set the rsync arguments to easily changeable variables:

Code:
#!/bin/sh
DATE=`date "+%Y-%m-%d-%H:%M"`
OPT="-avhPHS -x"

EXCLUDE="--exclude=\"/*/.adobe/\" --exclude=\"/*/.cache/\" --exclude=\"/*/.dbus/\" --exclude=\"/*/.gvfs/\" --exclude=\"/*/.macromedia/\" --exclude=\"/*/.thumbnails/\""

SOURCE=/home/
DEST="/mnt/backup/home/${DATE}"
LNDEST="--link-dest=/mnt/backup/home/current"

rsync $OPT $EXCLUDE $LNDEST "$SOURCE" "$DEST" ||
       echo "error(s) occurred during sync"

## "#????????????" removes "--link-dest=" from $LNDEST

rm "${LNDEST#????????????}" ||
     echo "Error: ${LNDEST#????????????} can not be removed"

ln -s "$DATE" "${LNDEST#????????????}" ||
     echo "Error: Creating symbolic link \"${LNDEST#????????????}\" to \"$DATE\" failed"

sync
However rsync does not end up excluding anything. If I place fallowing line in the script:
Code:
echo rsync $OPT $EXCLUDE $LNDEST "$SOURCE" "$DEST"
It returns
Code:
rsync -avhPHS -x --exclude="/*/.adobe/" --exclude="/*/.cache/" --exclude="/*/.dbus/" --exclude="/*/.gvfs/" --exclude="/*/.macromedia/" --exclude="/*/.thumbnails/" --link-dest=/mnt/backup/home/current /home/ /mnt/backup/home/2016-07-26-23:09
Just as I would expect, but rsync seems not to be arware of the exclude arguments.

I have also tried the fallowing additional definitions for EXCLUDE:
Code:
EXCLUDE='--exclude="/*/.adobe/" \
--exclude="/*/.cache/" \
--exclude="/*/.dbus/" \
--exclude="/*/.gvfs/" \
--exclude="/*/.macromedia/" \
--exclude="/*/.thumbnails/" \
'

EXCLUDE="--exclude=\"/*/.adobe/\" --exclude=\"/*/.cache/\" --exclude=\"/*/.dbus/\" --exclude=\"/*/.gvfs/\" --exclude=\"/*/.macromedia/\" --exclude=\"/*/.thumbnails/\""

EXCLUDE='--exclude="/*/.adobe/" --exclude="/*/.cache/" --exclude="/*/.dbus/" --exclude="/*/.gvfs/" --exclude="/*/.macromedia/" --exclude="/*/.thumbnails/"'

EXCLUDE='--exclude="*/.adobe/" --exclude="*/.cache/" --exclude="*/.dbus/" --exclude="*/.gvfs/" --exclude="*/.macromedia/" --exclude="*/.thumbnails/"'

EXCLUDE='--exclude="/*/.adobe" --exclude="/*/.cache" --exclude="/*/.dbus" --exclude="/*/.gvfs" --exclude="/*/.macromedia" --exclude="/*/.thumbnails"'

EXCLUDE='--exclude='/*/.adobe' --exclude='/*/.cache' --exclude='/*/.dbus' --exclude='/*/.gvfs' --exclude='/*/.macromedia' --exclude='/*/.thumbnails''

EXCLUDE="--exclude='*/.adobe' --exclude='*/.cache' --exclude='*/.dbus' --exclude='*/.gvfs' --exclude='*/.macromedia' --exclude='*/.thumbnails'"

EXCLUDE="--exclude=\"/*/.adobe/\" \ --exclude=\"/*/.cache/\" \ --exclude=\"/*/.dbus/\" \ --exclude=\"/*/.gvfs/\" \ --exclude=\"/*/.macromedia/\" \ --exclude=\"/*/.thumbnails/\""
None of these have worked.

What did work was the first script without argument variables, and setting EXCLUDE to equal "--exclude-from=/home/USER/scripts/exclude.list". However, I'd like to maintain the exclude list within the script itself instead of a separate file.

This also works so I know the problem is with the EXCLUDE variable and not one of the other variables:
Code:
rsync $OPT --exclude="/*/.adobe/" --exclude="/*/.cache/" --exclude="/*/.dbus/" --exclude="/*/.gvfs/" --exclude="/*/.macromedia/" --exclude="/*/.thumbnails/" $LNDEST "$SOURCE" "$DEST"

Last edited by Archy1; 07-26-2016 at 11:24 PM.
 
Old 07-26-2016, 11:27 PM   #2
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,077

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732
This doesn't answer your question, but with that many excludes, you might find it easier to just create a '.exclude' file and tell rsync to ignore everything listed in it. It is line de-limited.

Code:
--exclude-from=$HOME/.exclude

More to your question, try wrapping your exclude var even moar:

Code:
"${EXCLUDE}"

Either way, I think it's probably better to just use a file.
 
Old 07-26-2016, 11:27 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have you tried putting them in an array?
 
1 members found this post helpful.
Old 07-27-2016, 12:20 AM   #4
Archy1
Member
 
Registered: Jan 2014
Distribution: Debian
Posts: 95

Original Poster
Rep: Reputation: 2
By the way, even if I replace the long exclude list with just "--exclude="/*/.adobe/"", it still doesn't work so I don't think it's a spacing issue. It also doesn't work if I use "--exclude="/USER/.adobe/"" either so the asterisk isn't the problem either.

Having rsync reference ${EXCLUDE} or "${EXCLUDE}" (I believe the latter would cause rsync to treat the exclude list as one argument instead of multiple arguments) didn't work.

Bourne/Almquist/POSIX doesn't support arrays (though I did read this). Of course I could launch the script in bash, though I'd like to run this script on BSD systems (and even then, I could still install bash on those systems but I'd rather just figure out how to make the script work in Bourne.)

I might fall back to:
Code:
EXCLUDE=--exclude-from=/home/USER/scripts/exclude.list
but before that I'd like to try a little longer to make it work in one file.
 
Old 07-28-2016, 10:17 AM   #5
gda
Member
 
Registered: Oct 2015
Posts: 130

Rep: Reputation: 27
Try to change

Code:
EXCLUDE="--exclude=\"/*/.adobe/\" --exclude=\"/*/.cache/\" --exclude=\"/*/.dbus/\" --exclude=\"/*/.gvfs/\" --exclude=\"/*/.macromedia/\" --exclude=\"/*/.thumbnails/\""
in

Code:
EXCLUDE="--exclude=/*/.adobe/ --exclude=/*/.cache/ --exclude=/*/.dbus/ --exclude=/*/.gvfs/ --exclude=/*/.macromedia/ --exclude=/*/.thumbnails/"
 
  


Reply

Tags
exclude, rsync, variable



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
[SOLVED] rsync --exclude-from not working correctly? talkinggoat Linux - Newbie 3 04-19-2016 09:44 PM
passing pointer arguments correctly atlantis43 Programming 3 05-18-2014 06:30 AM
Passing variable arguments to ctypes function of C++ prasanthhs Programming 1 08-09-2012 08:30 PM
Passing arguments to shell script like perl script get-opts somupl86 Linux - Newbie 2 12-02-2010 11:14 PM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 PM.

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