Article summary
I had the unfortunate experience of getting my laptop stolen last summer. Luckily, I was able to restore most of my development environment pretty easily from my backup Git repository. I wanted to share how I set up that repository and how I back up my system package list.
This Hacker News thread is where I initially found this particular backup method. The initial setup is a little weird, but the advantage is that you have a globally accessible Git repository to back up files, without resorting to symlinks or other messy methods.
Setting Up The Git Repository
To set up the repository, you’ll need to enter the following commands in your shell.
git init --bare $HOME/.myconf
alias config='/usr/bin/git --git-dir=$HOME/.myconf/ --work-tree=$HOME'
config config status.showUntrackedFiles no
The idea here is that the Git metadata for the repository hides in the ~/.myconf
directory, but the repository tracks changes in your home directory. The third command configures the repository so that you’re not showing “unstaged changes” for every single file in your home directory. I would suggest storing the alias in a .bashrc
or something similar.
Now that the repository is set up, you can use config
as an alias for Git, referencing your special repository. I’ve shown an example below. Note that the file paths are always relative to your home directory.
config add .vimrc
config commit -m "added .vimrc"
Backing Up System Packages
In addition to the normal files you might want to back up, such as a.bashrc
or a .vimrc
, I like to back up the list of my system packages. I use Homebrew, but the concept should translate to other package managers.
The following command will create a text file with a list of all of the packages and binaries on my machine. To get the most use out of this, I try to install all of my binaries through Homebrew Cask, which also supports installing fonts.
alias backbrew= \
"brew leaves > ~/.config/brew/packages; \
echo '\ncasks:\n'>> ~/.config/brew/packages; \
brew cask list -1 >> ~/.config/brew/packages;"
brew leaves
will first output a list of all of your installed Brew packages without dependencies. This list won’t necessarily be the list of packages you’ve requested to install. For example, coreutils
might disappear from the list if you later install a package that depends on coreutils
.
brew cask list -1
is pretty straightforward; the command outputs a list of all of the binary packages I’ve installed through Cask on my machine. The -1
flag here will format the output to only show one binary per line.
As a finishing touch on my package list backup, I have backbrew
alias set to run twice a day in case I forget to run it manually.
I hope I’ve been able to show you something new to back up your development environment. Hopefully, your next machine transition can be as painless as possible.