Any rsync gurus out there willing to help troubleshoot a shell script?

Discussion in 'OS X Mavericks (10.9)' started by pruppert, Jan 13, 2014.

  1. pruppert, Jan 13, 2014
    Last edited by a moderator: Jan 14, 2014

    pruppert macrumors 6502


    Jan 30, 2008
    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.

    #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:
    #The link destination directory:
    #The rsync options:
    OPT="-avh --delete --compare-dest=$LNK"
    #The target directory:
    #Execute the backup
    rsync $OPT $SRC $TRG
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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:
    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:
    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.

    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:
    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:
    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:
    set -x

Share This Page