March 18, 2021

Backing Up User Config Files to Git

Git backup config

Paul Kelly

What is the problem we are trying to solve

I have a bunch of customisations that I make to various things when I am working in a Linux Environment, bash aliases, taskwarrior config etc.

It would be nice to not have to remember what all these changes are each time I setup a new machine. So it would be nice to be able to pull down a copy of the configs I want and use them on a fresh machine to get up and running quickly.

Once I get around to working with Ansible I am sure I will be able to add this process to that as well.

So I want to be able to have a copy of my config files, but not just a normal backup, as I want to be able to see the history of the changes that I make as I will, invariably, mess something up somewhere and need to go back to see what worked and when.

Git is ideal for this as everything is in plain text.

Identify the files you want to keep safe

To start what files are in your user directory that you want to backup?

 ls -a
 .               .npm                        
 ..              .nvm                       
 .bash_aliases   .profile                   
 .bash_history   
 .bash_logout    .ssh                        
 .bashrc         .cache          
 .task                     
 .config         .taskrc                     
 .gem            .taskrc.wingtask           
 .gitconfig      .timewarrior                
 .gnupg          .vscode-server              
 .landscape      .wingtask_certs             
 .local           .yarn
 .motd_shown     .yarnrc

A lot of hidden stuff, but really I want to keep things that I have altered over time, like .bash_aliases, .bashrc, .taskrc. All of the files I have edited that make my experience how I like it, and would want to be able to add quickly to a new machine.

Setup a config directory and git repo

There are multiple ways to do this, but I am going to copy all the files I want to put in the repo into a separate folder and then create links back to where they were in my home folder so everything still works.

The advantage of this is that I don’t have to create a git repo in my home directory which inevitably will get cluttered and require me to to keep adding exceptions to .gitignore. It also means I can create a folder on a new machine, pull the repo down and copy the files I want, sort the symlinks to get up and running quickly.

Firstly we need to create a folder for the backups and then init a new git repo.

 mkdir ~/configs && cd ~/configs
 git init

Now we can copy over the files we want to save and create symlinks back to their original location:

 mv ~/.bash_aliases .
 ln -s ~/configs/.bash_aliases ~/.bash_aliases
 mv ~/.taskrc .
 ln -s ~/configs/.taskrc ~/.taskrc
 ...
 ...

With everything moved check what is to be added to the repo

 git status
 On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .bash_aliases
        .bashrc
        .new_file
        .taskrc

nothing added to commit but untracked files present (use "git add" to track)

Add the files and commit them:

 git add .
 git commit -am "Initial commit of user config files"

Finally add a remote git repo to push the files to and get them uploaded:

 git remote add origin https://github.com/username/linuxuserconfig.git
 git push origin master

Conclusion

By putting your user configuration files under source control it keeps them safe, provides a history in case you want to return the config of an app to a previous state. This can be especially helpful when you are tinkering and screw things up!

With the files uploaded they are available to be taken down onto other computers so your experience can be the same across all your machines.

If you make a change to the config on one machine then just commit and push it so you can then pull it down to all machines. I am sure this could probably be automated with cron and some scripts.