Skip to content

Instantly share code, notes, and snippets.

@sideshowcoder
Last active August 29, 2015 14:07
Show Gist options
  • Save sideshowcoder/a30ab64d8a48d417a072 to your computer and use it in GitHub Desktop.
Save sideshowcoder/a30ab64d8a48d417a072 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Output is a script which installs all currently installed packages from
# homebrew, including those from tabs. Packages no longer available are ignored
# Usage:
# ./backup-homebrew.sh > my-homebrew-backup.sh
#
# warnings about packages not available anymore will be generated
#
# Reinstall the packages:
# $ chmod +x my-homebrew-backup.sh
# $ ./my-homebrew-backup.sh
echo '#!/bin/bash'
echo ''
echo 'failed_items=""'
echo 'function install_package() {'
echo 'echo EXECUTING: brew install $1 $2'
echo 'brew install $1 $2'
echo '[ $? -ne 0 ] && $failed_items="$failed_items $1" # package failed to install.'
echo '}'
brew tap | while read tap; do echo "brew tap $tap"; done
brew list | while read item;
do
if ! [[ "$item" =~ ^Error.* ]]; then
echo "install_package $item '$(brew info $item | /usr/bin/grep 'Built from source with:' | /usr/bin/sed 's/^[ \t]*Built from source with:/ /g; s/\,/ /g')'"
else
>&2 echo "Warning: $item" # write item to stderr
fi
done
echo '[ ! -z $failed_items ] && echo The following items were failed to install: && echo $failed_items'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment