Skip to content

Instantly share code, notes, and snippets.

@crcastle
Forked from xuhdev/backup-homebrew.sh
Last active July 17, 2023 12:06
Show Gist options
  • Select an option

  • Save crcastle/6eb329de90a27b1e2ed7 to your computer and use it in GitHub Desktop.

Select an option

Save crcastle/6eb329de90a27b1e2ed7 to your computer and use it in GitHub Desktop.
#!/bin/bash
brew_command=/usr/local/bin/brew
brew_cask_command="$brew_command cask"
echo '#!/bin/bash'
echo ''
echo 'trap ctrl_c INT'
echo 'function ctrl_c() {'
echo 'echo "** Trapped CTRL-C"'
echo 'exit 1'
echo '}'
echo ''
echo 'brew_command=/usr/local/bin/brew'
echo 'brew_cask_command="$brew_command cask"'
echo 'failed_items=""'
echo 'function install_package() {'
echo 'echo EXECUTING: brew install $1 $2'
echo '$brew_command install $1 $2'
echo '[ $? -ne 0 ] && $failed_items="$failed_items $1" # package failed to install.'
echo '}'
echo 'function install_cask_package() {'
echo 'echo EXECUTING: brew cask install $1'
echo '$brew_cask_command install $1'
echo '[ $? -ne 0 ] && $failed_items="$failed_items $1" # package failed to install.'
echo '}'
$brew_command tap | while read tap; do echo "$brew_command tap $tap"; done
$brew_command list | while read item;
do
echo "install_package $item '$($brew_command info $item | /usr/bin/grep 'Built from source with:' | /usr/bin/sed 's/^[ \t]*Built from source with:/ /g; s/\,/ /g')'"
done
$brew_cask_command list | while read item;
do
echo "install_cask_package $item"
done
echo '[ ! -z $failed_items ] && echo The following items were failed to install: && echo $failed_items'
@crcastle
Copy link
Author

crcastle commented Apr 5, 2016

If you use Homebrew, use this script to backup your installed packages -- including cask packages! This makes setup on a new machine super fast.

  1. Copy backup-homebrew.sh to your computer and make it executable. Don't copy restore-homebrew.sh. That's just an example of the restore file that you'll create.
  2. Use crontab -e to schedule the script to run regularly. I run it every 6 hours. See my crontab entry above for an example. It overwrites restore-homebrew.sh every time it runs with a list of all your currently installed homebrew packages.
  3. When you need to restore your homebrew installs, make your generated restore-homebrew.sh executable and execute it. All of your homebrew and homebrew cask packages will be automagically installed!

Bonus: If you use Dropbox, put the script and its output file in a Dropbox folder (see my crontab entry for an example of where I store the files) so you have it backed up should your hard drive die or computer go missing.

@EmilMaric
Copy link

This is great, thanks for sharing! Do you know if Homebrew has some sort of native support for backing up the taps and formulas (or are they planning to add that feature)?

@ghuff07
Copy link

ghuff07 commented Feb 6, 2017

Any guidance on how to modify the script to pull out package options now that the "Built from source with:" has been changed to "Built from source on (date) at (time) with:"?

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