MacOS – How to make Rsync copy alias files

aliasapplescriptbackupmacos

System specs: 2011 iMac, OS X Mountain Lion 10.8.4

I have come to understand that Rsync does not handle Mac Alises properly, and have now discovered that hundreds of aliases I've previously copied to my portable drive, are dead and useless because of this.

Is there a way to force Rsync to handle aliases? If the answer is a definitive "no", then how can I recursively batch convert all aliases in a directory to symlinks so that they will be handled by Rsync? I've found references to an Applescript that would apparently do this, but the site hosting the script is long dead & gone.

As a last resort, does anyone know of something other than Rsync that would allow for incremental backups and also handle Aliases?

Best Answer

Rsync can't look into the content of Alias files which would be needed to convert them. So if you want to have your backup be useable as is (without restoring it to the orignal drive) you will have to replace the Aliases with symlinks.

There is a solution for this on MacOSXHints.com. It needs to be run on your original disk (not on the backup), I would also recommend to test it on some sample directories first.

#!/bin/sh
if [ $# -eq 0 ]; then
    echo "Usage: alias2ln alias1 alias2 alias3..."
    echo "  where alias1, alias2, etc are alias files."
    echo "  Each alias file will be converted into a symlink."
fi

while [ $# -gt 0 ]; do
    if [ -f "$1" -a ! -L "$1" ]; then
        item_name=`basename "$1"`
        item_parent=`dirname "$1"`
        item_parent="`cd \"${item_parent}\" 2>/dev/null && pwd || echo \"${item_parent}\"`"
        item_path="${item_parent}/${item_name}"

        linksource=`osascript<<EOS
tell app "Finder"
set theItem to (POSIX file "${item_path}") as alias
if the kind of theItem is "alias" then
get the posix path of (original item of theItem as text)
end if
end tell
EOS`

        if [ $? -eq 0 ]; then
            if [ ! -z "$linksource" ]; then
                rm "$item_path"
                ln -s "${linksource}" "${item_path}"
                echo "\"${1}\" -> \"${linksource}\""
            fi
        fi
    fi
    shift
done

To run

  • save the code above into a text file (e.g. alias2symlink)
  • execute chmod 755 alias2symlink
  • change to the folder where you want to convert the aliases in
  • execute /path/to/alias2symlink alias1 alias2 ...