July 3 2015     

Making backup for each line of code in your project every n minutes in case your code editor decides to revert some file back to its state x hours ago

by archywillhe

I just reinstalled my OS X recently, which not only enabled me to re-experience the joy of owning a new Mac, but also fixed problems like waking up into a partially slumber stage with a black screen and a movable cursor that is practically useless. Since I was going to brew cask install a brand new Sublime Text Editor, I decided to spend a bit more effort customizing it. After some googling and experimentation, these are my settings:

(For the record Source Code Pro can be grabbed here; Spacegray here; and Blackboard here)


This looked like a great environment to code in (I enabled vintage mode too so there are vim magic in it ;3 ) until I learnt that sometimes the text editor would revert some file back to its state x hours ago with no reason at all.

It is a nightmare. (Imagine happily committing your code only to learn all that you’ve written & tested no longer exist in the current dimensions in space-time.)

I still don’t have a clue what is the cause. (I only have a few plug-ins installed. I don’t think the plug-ins have to do with it.) But it has happened quite a few times and I have had my losses. Until I figure out what’s causing the strange phenomenon and get it fixed, I need to start taking precaution.

Here is a simple system that uses watch with the help of git to make backup of every single file in a directory every 90 seconds:

watch can execute a line of code periodically in the terminal. The nice thing is that you can watch the outputs as it does.

Let’s say this is the directory containing projects you want to back up

  1. Make a directory to keep the back up files
  1. Make it into a git repo.
  1. Write a bash script that
  1. Copy everything in the directory to the backup directory
  2. Remove all the .git folders from each project in the backup directory (The .git folder is what that determines a directory to be a git repo: removing it would turn git repo back to a normal directory. In order for commit to work, it is necessary to have every subdirectory as a non git repo. If not, the subdirectories containing .git will be treated as git submodules.)
    [Changes made in a git submodule can’t be committed as changes of the main git repo.]
  3. Git add & commit everything in the backup directory

and save it as script.sh.(You can save it at anywhere you want)

Here is my script.sh.

  1. Simply run this script periodically using watch (You can install it with brew if you don’t have it).

That’s it. Now every 90 seconds, the changes you made in your project (e.g. /Users/a/_/0a-.github.com) would be recorded in the backup directory. You can view changes of the file using git log:

or do it in a GUI Client. (They would show up in the Unsynced tab because all we are doing is just committing and there is no pushing involved.)

such time much machine

The backup directory is going to take more and more disk space as time passes (as long as you remember to watch -n 90 'bash script.sh' before you start to code each time). When you want to clean everything up, simply remove everything (including the .git folder) and do a git init again.

The great thing about using git is that only the changes of the files would be saved. So rather than having 10 backup copies of a project that are slightly different from one another, you’d only have one copy of it, and 9 indications of the different changes you made at different times.

0a.io is handcrafted by Archy archywilhes Will He
vi3w_s0urce (Haskell on Hakyll)