dotfilesync

Sync dotfiles with Gist

What is dotfilesync?

dotfilesync is a bash script that syncs a list of local files that you define, into a secret gist, that you own.

dotfilesync is very easy to use as it stores your gist credentials into an encrypted Keychain, so you don’t have to provide it each time.

You are not limited to syncing only dotfiles. Although this tool is meant for you to sync local dotfiles, it can also be used to sync any file that can be synced with gist.

This script is inspired by this post on how the author syncs zshrc to their gist.

Features

Config driven

dotfilesync uses a JSON config using a hardcoded path - ${HOME}/.dotfilesync/config.json - to retrieve metadata needed to sync local files to gist.

A sample config shape can look like this:

{
  "githubUser": "snvishna",
  "gistId": "8f1bd18cf47f9d3efb8dc0a88a4e57aa",
  "dotFilePaths": [
    "~/.dotfilesync/config.json",
    "~/.zshrc",
    "~/.bash_profile",
    "~/.ssh/config",
    "~/.scripts/a.sh",
    "~/.scripts/b.sh"
  ]
}

Tools used as Keychain

This tool stores and manage your GitHub Personal Access Token within into an encrypted Keychain. This way your token is safe and encrypted by the Keychain, and you don’t have to provide it each time you run the command to sync files.

For OS X based operating systems, the OSX security command is used.

For Linux based operating systems, the secret-tools command is used to interact with GnomeKeyring. For more info about keyrings used on Linux systems read this article.

Sync multiple files into a single secret gist

All local files that are listed in the config.json file are synced into a single gist. As long as you have a valid config.json file in the ${HOME}/.dotfilesync directory

Auto-generate gist filenames

The file names in gist are automatically created. This script is opinionated on the file names being created. The characters “/” and “~” are replaced with periods (.). Duplicate consecutive periods in the name are also removed.

Prompts before syncing each file

Every file listed in the config.json, whether being pushed to or pulled from gist, is synced only after a confirmation prompt. A sync is performed only after you enter a “y” or a “yes” (case-insensitive). Any other input is ignored from sync.

Creates a backup of the local file contents before overriding

During a fetch operation (syncing local files from gist), a sync is performed, only after creating a backup of the local file. The backup file name is auto-generated based on the current timestamp.

Prerequisites

This script uses jq to parse the config.json on the local filesystem.

OS X

You can run the following command on OS X, if you have Homebrew installed:

   brew install jq

Linux

You can install the required packages on a Debian based distro running the following command:

   apt-get install jq libsecret-tools

Installation

Ensure the prerequisite tools are setup. Installing dotfilesync is easy and a one-time effort:

  1. Start a Zsh shell:

    zsh
    
  2. Fetch the script locally:

  1. Add an entry in zshrc:

    You’ll find the zshrc file in your $HOME directory. Open it with your favorite text editor and add the following alias in there:

     alias dfsync='bash ${HOME}/.dotfilesync/dfsync.sh'
    

    You can now use the dfsync command after you restart the terminal, or source your zsh config.

  2. Create Person Access Token on GitHub:

    You can create a new person access tokens page for running the script on the command line. Follow these instructions on how to create one. Make sure you have the gist scope selected to grant permission on this token.

    Once you generate the personal access token, either copy it on your clipboard, or save it somewhere, since the script will need this to store within the Keychain, before syncing your files in gist.

  3. Run setup:

    Run the command dfsync setup from your terminal. It prompts for your GitHub username and the personal access token. Refer to the Commands section for more details on this command.

  4. You’re done! Enjoy dotfilesync!

Usage

Commands

Uninstall

You can cleanup the script and all resources created by it, using the following instructions: