How to use Git for personal backups

Cover Image

Git for Personal Backups? You Bet!

Okay, you might be thinking, "Git? Isn't that for developers and complex software projects?" Well, yes, it is. But it's also a surprisingly powerful and efficient tool for personal backups, especially for things like configuration files, scripts, and documents.

Why Git for backups? Here's the gist:

  • Version Control: Git keeps track of every change you make. Accidentally delete something? No problem, just revert to a previous version.
  • Efficiency: Git only stores the *differences* between versions, making it very space-efficient.
  • Remote Storage: Services like GitHub, GitLab, and Bitbucket provide free (or very affordable) remote repositories, giving you offsite backups.

Let's Get Started: Backing Up Your Configuration Files

Let's walk through a simple example: backing up your .bashrc file. This works similarly for any text-based configuration file.

  1. Create a Git Repository: Navigate to your home directory and create a new Git repository (a "repo"): git init --bare $HOME/.backup
  2. Set up an Alias: Make backing up and restoring easier with an alias. Add the following to your .bashrc file: alias backup='git --git-dir=$HOME/.backup/ --work-tree=$HOME'
  3. Source your .bashrc: Apply the changes to your current shell with: source ~/.bashrc
  4. Add and Commit: Now, let's add your .bashrc file to the repo and commit it. We'll first ignore everything by default, then specifically track the file we want to track. backup add .bashrc
    backup commit -m "Initial backup of .bashrc"

Congratulations! You've just created your first backup using Git!

Making Changes and More Backups

Let's say you've made some changes to your .bashrc file. Backing up the changes is just as easy:

  1. Modify the File: Make some changes to your .bashrc.
  2. Add and Commit Again: backup add .bashrc
    backup commit -m "Updated .bashrc with new aliases"

Restoring a Previous Version

Uh oh! You messed something up in your .bashrc and want to go back to a previous version. Here's how:

  1. View the Commit History: backup log
  2. Checkout a Specific Commit: Find the commit hash you want to revert to (the long string of characters in the backup log output) and use the following command, replacing <commit-hash> with the actual hash: backup checkout <commit-hash> .bashrc
  3. Copy the File: the previous step checks the file into the 'index', you'll need to manually copy the file to the live directory: cp .bashrc ~/.bashrc
  4. Source the File: reload the bash config to apply your changes source ~/.bashrc

Pushing to a Remote Repository (GitHub, GitLab, etc.)

For true peace of mind, you should push your backups to a remote repository. This protects you against local hardware failures. The process is similar for GitHub, GitLab, or Bitbucket. I'll use GitHub as an example:

  1. Create a Repository on GitHub: Create a new, empty repository on GitHub. Don't initialize it with a README or anything.
  2. Add the Remote: Add the remote repository to your local repo: backup remote add origin git@github.com:<your-username>/<your-repo-name>.git (Replace <your-username> and <your-repo-name> with your GitHub username and repository name.)
  3. Push to the Remote: backup push origin master

Now your backups are safely stored on GitHub! You can periodically push changes to the remote to keep it up-to-date.

What Else Can You Back Up?

You can use this same technique to back up any text-based configuration file or script. Here are a few ideas:

  • .vimrc (or .config/nvim/init.vim for Neovim)
  • .zshrc (if you use Zsh)
  • .ssh/config
  • crontab (using crontab -l > crontab.bak to export, and then backing up crontab.bak)
  • Shell scripts
  • Any other important text files!

Important Note: Be *very* careful about backing up files containing sensitive information like passwords or API keys. Consider using a separate, encrypted repository for such files, or exclude them entirely. Also, this isn't intended to be a replacement for a proper system backup; consider it supplemental insurance for your most important configuration files.

Git might seem a bit intimidating at first, but once you get the hang of it, it's an incredibly useful tool for both developers and anyone who wants to keep their important files safe and sound.