Using Git To Backup Things On Your Mac (OS X/Linux)

This may be useful beyond your OSX workstation, but hasn’t been tested, I’d be surprised if it didn’t work on linux. On windows, the scheduling at least would be different.

I recently forgot to save a copy of my timesheet, a minor mistake, but it prompted me to want to get backups working again for my workstation, something that broke when upgrade to 10.7.x. Previously timemachine had worked well.

I decided the thing I most cared about was my Documents folder, and that git would do the job.

Getting started

The script
This shell script lives in a folder called bin in my home directory, in a file called gitsave.
The only thing that deserves explanation is how it gets changes into the local git repo:

  • The git add . will find files, not yet tracked by git, start tracking them, and add them to the staging area,
  • the -a argument to git commit, will find any files that have been removed, or modified add them to the staging area, before committing the staging area to the local repository.

After creating this file, do a:

If you had several directories you wanted backed up this way, you could just duplicated the final 3 lines, specifying different directories.
Scheduling the backups

I decided to use cron since I was most familiar with it, even though it has been deprecated in OSX since around 10.4, if it is ever removed, I will change how the script is invoked.

run the command crontab, and then type the following

Then hit ^D (Control-D).

This tells cron to run the backup script every minute. This is fine when not many changes occurring, as git doesn’t do much of anything, when no changes are made to the files you are working with.

If you want to run it every hour instead (lets say 3 minutes past the hour), then the following should be typed

Seeing what has changed over time
To see what files have been manipulated over time, run

Restoring files
The way I will do this is to clone the git repository I just made,find the version of the file I want, and copy it back into the Documents folder. SO the method will look a bit like this.

Now, the line-by-line explanation:

  • Change to the top of you home directory,
  • clone the repository,
  • move into the clone of the repository,
  • show the git log
  • The dots represent you looking through the log, to find the specific version of the file you want, and identifying the commit id, (I pretend the commit id is a123z),
  • checkout that file,
  • copy it back into the Documents folder,
  • get out of the clone of Documents,
  • cleanup the clone of the repository,

If you wanted, you could just leave the clone of the Documents repository lying around, and update it, using git pull, next time you wanted to bring back a file.

It is conceivable that the restoration could entirely be done inside the Documents folder itself, but it would have required some different git commands, and with the cron job running every minute, I didn’t want to risk possible side effects due to these things running simultaneously.

Instead of looking at specific commit ids, if you know how far back to go, you could do the following.


Add a Comment

Your email address will not be published. Required fields are marked *