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

haravikk

macrumors 65832
Original poster
May 1, 2005
1,501
21
Okay, so I've written myself a simple little script that looks for structures within volumes, and then syncs files into them as necessary.

The problem with this is that whenever it runs it causes all volumes to wake from sleep while it checks whether the target directory exists. What I'd like to do is instead only perform the check if the volume path hasn't been added a file, and to add volumes to this file if they turn out to be invalid targets.

i.e:
  1. Load cache file into one or more variables
  2. For each volume:
    1. If volume is listed in cache file then skip it.
    2. Else, look to see if it has a valid target folder, if so then perform sync, otherwise add the volume to the cache file.

What is the easiest way to do this? I've worked with lists in shell scripting before, but what I really need is something more like a map where I can take the current volume path and test to see if it is in the cache file (or the contents of the file loaded into a variable).

Also, I've seen that bash4 supports a type of map/dictionary, but I'm not sure if I can use it in a shell script?
 

haravikk

macrumors 65832
Original poster
May 1, 2005
1,501
21
Found a solution!

For those interested here's what I did:

Code:
# Map support functions
function map_put { alias "${1}$2"="$3"; }
function map_get {
    if type -p "${1}$2"
        then
            alias "${1}$2" | awk -F "'" '{ print $2; }';
    fi
}

# Load our .backup cache
CACHE="/tmp/VolumeSync.backup.cache"
if [ -e "$CACHE" ] 
    then
        for line in $(cat "$CACHE")
        do
            map_put "CACHE" "$line" 1
        done
fi

for f in /Volumes/*
do
    HASH=`md5 -q -s "$f"`
    SKIP=`map_get "CACHE" "$HASH"
    if [ -z "$SKIP" ]
        then
            // Work out where we're syncing to
            if [ -e "$file" ]
                then
                    // Sync some stuff
                else
                    echo "$HASH" >> "$CACHE"
            fi
    fi
done
Then it just needs some logic for deciding when to recreate the cache file, nice and easy :)
 

robzr

macrumors member
May 4, 2006
92
18
Portland, OR
Looks like some nice shell script work. Have you looked into rsync? For syncing directory structures, it's hugely powerful.

Rob
 

haravikk

macrumors 65832
Original poster
May 1, 2005
1,501
21
Looks like some nice shell script work. Have you looked into rsync? For syncing directory structures, it's hugely powerful.
That's what I'm using, but I just needed to control which drives it would try to backup to.

Basically it's for things like my 1Password keychain and other portable stuff that I want to keep safe; so whenever a volume is mounted the script looks for a .backup file in the root directory that tells it where to copy files to, and it then uses rsync to move across the differences. Means that whenever my iPod or USB stick are plugged in they automatically get the latest copy of the files.

I've vastly overcomplicated the solution, as I could have just hard coded specific volume names, but I liked the idea of making it automatic; only problem was that without caching excluded volumes it touches every volume connected to my machine whenever a change is detected, which wakes them up if they're sleeping, which was pretty annoying :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.