Dotfiles backup and restore

A reproducible development environment comes in handy when changing Linux distributions or moving to a new computer. Here is how I automate my laptop's configuration.

The challenge

Part of making the development environment reproducible is having a way to backup and restore its configuration files. The way I do all of that that is quite simple.

I keep all of my config files in a directory named dotfiles, which is checkout out in Git so that I can revert changes if need be. Here are some of the files from dotfiles:

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

I need a way to restore those files, right?

The solution

For that, I have a little script that will make symlinks to where the system will actually look for the configuration files. For instance, ~/dotfiles/.bashrc gets symlinked to ~/.bashrc because that's where Bash looks for this file.

Here's that little helper script:

#!/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.

Simple enough and low maintenance.

Dotfiles backup and restore
Share this