*ฅ^•ﻌ•^ฅ* ✨✨  HWisnu's blog  ✨✨ о ฅ^•ﻌ•^ฅ

dotfiles backup

Introduction

Managing dotfiles is essential for using Linux effectively, especially when working with multiple programs that require specific configurations.

Chezmoi

Chezmoi is a comprehensive tool for managing dotfiles configurations. However, I recently had a negative experience with it, which I attribute mostly to my own mistake. By sharing my story, I hope to help others avoid making the same error.

The "chezmoi update" Command: A Cautionary Tale

It's been several months since I last used chezmoi, and as a result, I've become a bit rusty on how to use it properly. Despite my best efforts to recall the process, I found myself struggling to remember the correct steps. However, I did manage to recall one crucial aspect: the importance of backing up my configuration files. To refresh my memory and get started again, I decided to visit the official chezmoi website, which I hoped would provide me with the necessary guidance and instructions to get back up to speed.

After a short read of the relevant commands, I was sure I needed to apply the 'chezmoi update' command in order to update my chezmoi remote Github repo with the latest config (local repo). Then a text-editor came up and there's a diff like when you compare different versions in Github.

Unfamiliar with the output, I selected "apply-all," which proved to be a grave mistake. Chezmoi pulled my outdated configuration from the remote repository and overwrote my local files, including my .zshrc and .bashrc files.

The quest to recover components of .zshrc from memory

I got several hundreds lines of code in .zshrc so you can imagine my frustration when I opened the file and found only < 50 lines of code! Through some magickery and lots of prayers, I managed to recover my aliases and functions.

With the .zshrc got overwritten, the only hope I got was on the existing terminal session, which was still on the last .zshrc config. Here are some terminal commands that helped me get some of my .zshrc config back:

alias >> ~/.zshrc.recover
printenv >> ~/.zshrc.recover
functions >> ~/.zshrc.recover

What the commands do was take the alias, environment variables and functions from memory and then dump them to a file named .zshrc.recover

Again, I need to underline this disaster was due to me not using chezmoi for several months and totally forgot the correct commands. So 95% of the blame can be attributed to myself and 5% to chezmoi (LOL). Why chezmoi got 5% of the blame? I feel they could've done better and provide a local backup before overwritting local files with remote files!

Solution and workflow

Feeling uncomfortable needing to adjust my workflow in accordance to Chezmoi's documentation, I decided to make my own process. Chezmoi workflow requires user to make adjustment to the config files inside chezmoi's folder, not directly in the programs folder or the config file.

But what if a program got updated and automatically made adjustments to its config? The workflow offered by Chezmoi would have missed this. Which is why the need of a custom process.

This article here got a nice graph near the end on Chezmoi's workflow. Please take a look at the graph and read the explanation to understand why that is potentially problematic if the changes in config are not initialized by the user but by the programs itself.

An example of this if we use powerlevel10k and we run the p10k config command --> post finishing the process, powerlevel10k will automatically update the config changes on its original file.

My workflow in steps:

  1. Create a script "chezmoi_add.sh" where I put all the config files I want to be added into chezmoi.
chezmoi add ~/.bashrc
chezmoi add ~/.zshrc
chezmoi add ~/.config/kitty
chezmoi add ~/.config/tmux
chezmoi add ~/.config/yazi
chezmoi add ~/.config/nvim
  1. Add the chezmoi_add.sh script into my startup script which I run everytime I reboot the system.
sleep 1
cd "$(dirname "$0")"
./chezmoi_add.sh
notify-send "CHEZMOI ADD" "dotfiles backup completed."
  1. Once all the config dotfiles are added to chezmoi, let's go to chezmoi directory using this command:
chezmoi cd
  1. Last step, do the usual git add - commit - push
git add .
git commit -m "your commit message here"
git push

Voila! With the custom process above, I managed to backup dotfiles config while preserving my usual workflow. Like hell I would be willingly adjust my workflow only for Chezmoi!

#backup #chezmoi #config #dotfiles #linux