Tuesday, October 4, 2016

Back up Git repositories to Google Drive

I'm trying to come up with a decent backup strategy and I'm almost there.  Figuring out a way to back up git repositories was a little confusing though.  I use GitHub to host repositories that I'm working on locally, and that's an OKish backup, but I don't check every file into Git.  For example, if I'm working on an electronics design I don't really want the manual for the micro-controller to be tracked by version control, but I do want a backup of the manual just in case they change it for some reason.  So for files like this I keep them with all the others and add them to the .gitignore file.  This is great, but they're not backed up anywhere.

Normally I use Google Drive for my backups.  There are other services that are probably better and have desktop syncing apps that are more polished, but I can easily access files from any device and I trust Google not to go broke in 6 months.  So a simple solution to my problem might be to store local repositories in the Google Drive directory.  That may work, but I just don't trust Git and the Google Drive app to get along together.  So what I ended up doing was just copying a backup of the repository to Google Drive.

This works, but if you copy files to the Google Drive directory and overwrite the old versions it wants to re-upload everything even if the files are unchanged.  You could do a copy where only newer files are overwritten but then another problem arises, files that are deleted from the repository remain in the backup taking up space.  That might be ideal in some situations but I want this basically to be a mirror of the current state of the local repository folder.  In reality what I want is a one way sync to the backup location.  Luckily the robocopy command can manage this.

cmd /k robocopy "Repositories To Backup" "Backup Location" /e /purge

By placing the above command in a batch file, anything new in the "Repositories To Backup" directory will be copied to the "Backup Location".  Don't worry about the cmd /k part, it just lets the command window stay open after it runs robocopy.  By default robocopy copies a file if it's changed in any way.  If unchanged, it will just skip the file.  This will prevent Drive from wanting to upload the file again.  The /e option means it will also copy empty subdirectories and the /purge option means that it will delete files from the backup location that don't appear in the source directory.  This keeps the backup location synced to the source location.

I keep all my git repositories in a Projects folder, so I just set the "repositories to backup" to the this folder, so that when I run the batch file it backs up all the repositories at once.  I run the batch script it manually, but you could schedule it to run automatically too.  I know it's not the best solution, but it works for me.

No comments:

Post a Comment