DEV Community

Cover image for How to backup/sync all of your dotfiles​ with Github
Jeff Shomali
Jeff Shomali

Posted on

How to backup/sync all of your dotfiles​ with Github

As a developer, one of the challenges that we are facing every day is to backup or sync up all of our dot(.) files with a new or another environment. There are many dotfiles in my machine such as .alias, .vimrc, .zshrc, .gitconfig etc...

I'm not going to explain how to get a back up of all your files because each one of us has a different environment, so I'll explain how to backup your VS-Code setting. I know there is a VS-Code extension that syncs up your setting with Github. Our goal is to understand how to back up #vscode settings, snippets, extensions, and sync it with Github manually.

Create a Script

  • Create a new Github repository in your Github.com
  • In your local machine go to the desktop and open an empty folder in your terminal
  • Initiate an empty Git repository with $ git init and connect the local repo to newly created repo with $ git remote add git@github.com:yourusername/yourrepo.git

  • Create a shell script and name it whatever you want, like backup.sh and past the following code on that file.

#!/bin/bash


# check to see is git command line installed in this machine
IS_GIT_AVAILABLE="$(git --version)"
if [[ $IS_GIT_AVAILABLE == *"version"* ]]; then
  echo "Git is Available"
else
  echo "Git is not installed"
  exit 1
fi


# copy Vs-Code files
cp  $HOME/Library/Application\ 
Support/Code/User/{keybindings.json,settings.json,spellright.dict} .

# copy snippets folder
cp -r $HOME/Library/Application\ Support/Code/User/snippets .

# copy list of extensions that currently installed
code --list-extensions --show-versions >> ../../vscode/extensions.txt

# copy other dot files 
cp  $HOME/{.zshrc,.vimrc} .



# Check git status
gs="$(git status | grep -i "modified")"
# echo "${gs}"

# If there is a new change
if [[ $gs == *"modified"* ]]; then
  echo "push"
fi


# push to Github
git add -u;
git commit -m "New backup `date +'%Y-%m-%d %H:%M:%S'`";
git push origin master
Enter fullscreen mode Exit fullscreen mode
  • Change the script permission with $ chmod +x backup.sh.
  • Run the script with $ ./backup.sh

If you want to run in background services:

  • crontab -e and add your script path and time you want to run the script for example
$ crontab -e  # this is open the cronjobs table in vim mode use i for insert mode

# this is run the script every minutes
$ 1 * * * * cd /Users/macp15/Projects/dotfiles/scripts/backup && ./backup.sh

# display list of cron jobs
$ crontab -l
Enter fullscreen mode Exit fullscreen mode

Done (Khalas).

Top comments (5)

Collapse
 
bnikanjam profile image
Babak Nikanjam • Edited

Great explanation, very useful and real problem solver. Nice touch on having it run as a background service automatically (periodically). I love these kind of short and neat learning drops. Thanks Jeff.

Collapse
 
msfjarvis profile image
Harsh Shandilya

VS Code has an extension that implements most of this minus the automatic sync (AFAICT) called Syncing that uses Gists instead of a full repo.

Collapse
 
sientelo profile image
Paul Murray

Thank you, this is the easiest dotfiles backup solution I have found. Question - when restoring the dotfiles to a new machine, is it just a matter of basically pulling the remote Git repo to the new machine and then running ./backup.sh ? I did this but my .dotfiles in my new home directory are not updating with the files created in the /dotfiles directory

Collapse
 
notaduck profile image
notaduck

Nice article, but I think you should take a look at stow! It is amazing for managing your dotfiles.

Collapse
 
bgopikrishna profile image
Gopi Krishna

Thanks, Great article.