Skip to content

Instantly share code, notes, and snippets.

@xuhdev
Last active July 17, 2023 12:07
  • Star 37 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save xuhdev/7854010 to your computer and use it in GitHub Desktop.
#!/bin/bash
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
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')'"
done
echo '[ ! -z $failed_items ] && echo The following items were failed to install: && echo $failed_items'
@crcastle
Copy link

This is great. I also use Homebrew Cask and noticed those packages weren't getting picked up in the restore file so I added this to the script before the last echo line:

echo 'function install_cask_package() {'
echo 'echo EXECUTING: brew cask install $1'
echo 'brew cask install $1'
echo '[ $? -ne 0 ] && $failed_items="$failed_items $1"  # package failed to install.'
echo '}'

brew cask list | while read item;
do
  echo "install_cask_package $item"
done

@xuhdev
Copy link
Author

xuhdev commented Aug 22, 2015

@crcastle Thanks! Would you mind me putting the improvements into the blog post and this gist?

@crcastle
Copy link

crcastle commented Apr 5, 2016

@xuhdev really sorry I missed this message from you. Here are the improvements: https://gist.github.com/crcastle/6eb329de90a27b1e2ed7. I run this every 6 hours via cron. I also write the generated restore script to a folder in my Dropbox so that I get no-touch back ups. Good in case my computer dies or is stolen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment