Dotfiles backup and restore

As software engineers, we spent time improving our tools, which leads to increased output and better software. It is important to backup this work so that it doesn’t go to waste.

Backing up configuration files

As software engineers, customizing our development environments can lead to big boosts in productivity and fewer bugs.

All the work that goes into tweaking your Neovim, Sublime or VSCode configuration pays for itself once you get intelligent code analysis, automatic refactorings, proper syntax highlighting and fast keyboard shortcuts.

Backing up this configuration is just as important as backing up any other files on your system. Usually, this configuration comes in the form of so-called “dotfiles”, which are not put in your Dropbox or Google Drive folder. And dotfiles might now be accounted for in backup software focused on the needs of the general population.

So what can we do to keep a safe backup of our configuration files ?

The most common way of doing that is keeping them in Git, a software that allows us to track changes in files and send them to a safe server so we can restore the later.

Here are some of the files from dotfiles I currently have stored in Git:

.asoundrc
.bashrc
.config
.gitconfig
.local
.npmrc
.profile
.ssh
.Xdefaults
.xinitrc

Restoring our configuration

If our hard drive ever breaks down, or our computer gets stolen, we need a way to restore the files we have previously saved in Git.

After running a git clone of the dotfiles’ Git repository, I run this little script that will make symlinks to where the system will actually look for the configuration files:

#!/bin/bash

cd dotfiles
for f in `find . -type f | sed 's#^./##'`; do
    # create the directory in which to put the config file's symlink
    echo -n mkdir $HOME/`dirname $f`
    read
    mkdir -p $HOME/`dirname $f`

    # create the symlink
    echo -n ln -sf $HOME/dotfiles/$f $HOME/$f
    read
    ln -sf $HOME/dotfiles/$f $HOME/$f
done
cd ..

When I run this script, it shows me what it is about to do, asks for confirmation and then creates the symlink where it should be.

For instance, ~/dotfiles/.bashrc gets symlinked to ~/.bashrc. Then, when Bash looks for its configuration, it will to ~/.bashrc which will point to ~/dotfiles/.bashrc, at which point Bash will be able to read the custom configuration I have been improving over the last few years.