Skip to content

Instantly share code, notes, and snippets.

@tangentsoft
Last active August 29, 2015 14:20
Show Gist options
  • Save tangentsoft/3fd49e667f6f46514f70 to your computer and use it in GitHub Desktop.
Save tangentsoft/3fd49e667f6f46514f70 to your computer and use it in GitHub Desktop.
Lightroom 6 re-compressing backup script
#!/bin/bash
# This script re-compresses an Lr6 zipped lrcat file using xz, which is
# much more efficient. Oddly, it's actually more compatible, since Lr6.0
# uses a variant of Zip that won't even open using built-in OS X utilities.
# Open Lightroom and wait for it to exit. You must then say Cmd-Opt-,
# and tell Lr to "back up catalog when Lightroom next exits", then
# exit. If Lr is already running, this will just wait, so it is not
# important whether you run this script before or after Lr is running.
#
# Don't rewrite this as "if ! time..." because that causes it to use
# /usr/bin/time instead of the builtin, which has harder to read output.
time open -Wa 'Adobe Lightroom'
if [ "$?" != 0 ]
then
echo Lightroom killed!
echo
exit 1
fi
# Make sure the backup volume is mounted.
cbdir=/Volumes/backups/catalogs
if [ ! -d $cbdir ]
then
echo Catalog backup directory $cbdir missing!
echo Did you forget to mount it?
echo
exit 1
fi
# Build zip file names. The "20" prefix on the zip file is meant to avoid
# searching through obviously-wrong subdirectories for *.lrcat.zip. This
# will break in about 85 years, by which time I won't care. :)
zipcat="$(ls ~/tmp/20*/*.lrcat.zip | head -1)"
compcat=$cbdir/$(date +%Y-%m-%d).lrcat.xz
# Did we find the zipped catalog file?
if [ ! -f "$zipcat" ]
then
echo "Could not find zipped LR catalog backup file:"
echo " $zipcat"
echo
exit 1
fi
# Make sure we're not running after a previous crash of this script.
tempcat="$(echo $zipcat | sed -e 's/\.zip$//')"
if [ -e "$tempcat" ]
then
echo "The zipped LR catalog has already been unpacked and not cleaned"
echo "up. Cowardly refusing to stomp on it."
echo
exit 1
fi
# The meat: Unpack the *.lrcat.zip file with The Unarchiver, then
# recompress it with xz(1).
echo 'Backup complete; recompressing it...' &&
open -Wa 'The Unarchiver' "$zipcat" &&
time xz -c < "$tempcat" > "$compcat" &&
echo -n 'Done! ' &&
rm -f "$tempcat" "$zipcat" &&
find ~/tmp -empty -delete &&
echo 'Zipped catalog backup removed.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment