DEV Community

Cover image for Backup Your Dotfiles With Version Control
Felix Swinkels
Felix Swinkels

Posted on

Backup Your Dotfiles With Version Control

I came across a dotfiles repository on GitHub the other day and it instantly made me realize I've been backing up mine wrong! Let’s set up our own GitHub repo and start doing it the right way, today.

Dotfiles are text-based configuration files for Unix tools. By backing them up to GitHub we make sure all our favorite configurations, hacks, and tweaks don’t get lost in case of a system crash.

Let’s go!

Create a folder called dotfiles and copy the files we want to back up.

Create the folder

mkdir dotfiles
Enter fullscreen mode Exit fullscreen mode

Copy the files you want to backup

cp ~/.<filename> ~/dotfiles/.<filename>
Enter fullscreen mode Exit fullscreen mode

For example, I’ve added my .zshrc and .vimrc files

cp ~/.zshrc ~/dotfiles/.zshrc
cp ~/.vimrc ~/dotfiles/.vimrc
Enter fullscreen mode Exit fullscreen mode

First commit

Create a new repository called dotfiles on GitHub or use the GitHub CLI tool.

Initialize and add the files

cd dotfiles
git init
git add .
Enter fullscreen mode Exit fullscreen mode

Commit and push

git commit -m “first commit”
git push origin master
Enter fullscreen mode Exit fullscreen mode

Automate it!

Let’s create a simple bash script and add it to crontabs to run automatically.

create the script

echo -e "#!/bin/bash/" >> backup_dotfiles.sh
Enter fullscreen mode Exit fullscreen mode

Open the file in your favorite editor and add the following

# create a timestamp alias for the commit message
timestamp() {
  date +"%d-%m-%Y @ %T"
}

# files to backup
cp ~/.zshrc ~/dotfiles/.zshrc
cp ~/.vimrc ~/dotfiles/.vimrc

# pull & push
if [[ `git status --porcelain` ]]; then
    git pull origin master
    git add .
    git commit -m "Update: $(timestamp)"
    git push origin master
fi
Enter fullscreen mode Exit fullscreen mode

As you can see I’ve added the .zshrc and .vimrc files again. You can add any (config)files you want here!

To add the job to cron, type

crontab -e
Enter fullscreen mode Exit fullscreen mode

You can use crontab.guru to generate how frequent you want the backup script to run. Let’s do every Monday at 10:00 by adding the following line to the cron file and save it

0 10 * * 1 bash backup_dotfiles.sh
Enter fullscreen mode Exit fullscreen mode

That’s it!

We’ve created a GitHub repo to back up dotfiles to, and automated the process with a bash script that runs periodically with cron. As always, drop your feedback in the comments! ✌️

Top comments (1)

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

Awesome 🔥, I encountered the same thing a while back, so spent an afternoon writing about it.

How to Build Your Own Linux Dotfiles Manager from Scratch

Also, open for contributions

GitHub logo Bhupesh-V / dotman

dotman is a simple, elegant & easy to use dotfiles manager 🖖🏽

build status badge License: MIT platform: linux and macos bash love Website Status dotman Twitter: bhupeshimself



✨ Learn how I made d○tman from scratch ✨


Demo 🔥

dotman-demo

🌠 Features

  • Single file manager (Portable)
  • No config files for dotman (No .dotrc 🤦)
  • No useless arguments (single command 😎)
  • Easy to use
  • Extendable , Available as a Template
  • Fewer Dependencies
    • Git
    • Bash>=3

Wait! it's not written in a fancy language

And it doesn't have to be. Why?

  • Your focus should be on your dot files & scripts rather than on a dotfiles manager or how to use it.(Wait I forgot what was the command to push files? Is it dt push or dt --push)
  • Creating a overly-complex solution for something simple should not be the goal.

💠 Installation

via curl

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Bhupesh-v/dotman/master/tools/install.sh)"
Enter fullscreen mode Exit fullscreen mode

via wget 📥

sh -c "$(wget -O- https://raw.githubusercontent.com/Bhupesh-v/dotman/master/tools/install.sh)"
Enter fullscreen mode Exit fullscreen mode

via httpie 🥧

sh -c "$(http --download https://raw.githubusercontent.com/Bhupesh-v/dotman/master/tools/install.sh)
Enter fullscreen mode Exit fullscreen mode