Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pruppert

macrumors 6502
Original poster
Jan 30, 2008
428
81
I have the following shell script. I am trying to use rsync to copy files from /Desktop/Source to /Desktop/Backup. Before it copies, it is supposed to compare the source to the most recent backup in the backup directory. If the files exist in a prior backup, it is not supposed to copy. So, this is basically rsync using the --compare-dest=DIR option.

For some reason, all files get copied to the new backup directory, even if the exact same file existed in a prior backup. I'm guessing I have an error in my script. I'd appreciate if anyone can spot it. Thanks.

A note: My backup directories within /Desktop/Backup are named with the date/time of backup. So, alphabetical sorting is the same and sorting by date modified, which is how the LastBackup value works.

Code:
#!/bin/bash
 
#Get the name of the most recent directory that was saved:
LastBackup=$(ls ~/Desktop/Backup/ | tail -1)

#A new directory name using today's date:
NewBackup=$(date "+%Y_%m_%d__%H_%M_%S")
 
#The source directory:
SRC="Users/myUserName/Desktop/Source/"

#The link destination directory:
LNK="Users/myUserName/Desktop/Backup/$LastBackup"
 
#The rsync options:
OPT="-avh --delete --compare-dest=$LNK"
 
#The target directory:
TRG="Users/myUserName/Desktop/Backup/$NewBackup"

#Execute the backup
rsync $OPT $SRC $TRG
 
Last edited by a moderator:
You seem to be confused about how Posix pathnames work. Or maybe you didn't post your actual code, only an approximation of it, and that approximation has typos.

You have this, for example:
Code:
SRC="Users/myUserName/Desktop/Source/"
This is a relative path. It's relative to the current working directory.

You need an absolute path, otherwise the results will depend on what the working directory is at the time the pathname is evaluated.

This is an absolute path:
Code:
SRC="/Users/myUserName/Desktop/Source/"
The leading '/' is how Posix systems distinguish an absolute pathname from a relative one.

Also note: there is more than one place you have this problem.

http://en.wikipedia.org/wiki/Path_(computing)
http://en.wikipedia.org/wiki/Working_directory

If the posted code isn't your actual exact code, but only an approximation, then maybe you have the pathnames correct. I can only comment on code I've actually seen.


This command:
Code:
rsync $OPT $SRC $TRG
could be (or become) a victim of embedded spaces in SRC or TRG. If either of the expanded variables contains an embedded space character, then it will act as if the variable were two separate args. For example, "an example" would expand to two separate args, "an" and "example".

You need to quote the expansion of the variables, like this:
Code:
rsync $OPT "$SRC" "$TRG"
Note that $OPT actually contains embedded spaces intentionally, so you WANT it to expand to multiple args. Hence, it remains unquoted.


The above are issues with the shell script, not with rsync. You might want to turn on the -x flag to bash so it tells you exactly what commands it executes.

The in-script way to turn on -x is:
Code:
set -x
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.