Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Created March 22, 2012 16:15
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save addyosmani/2159263 to your computer and use it in GitHub Desktop.
Save addyosmani/2159263 to your computer and use it in GitHub Desktop.
A little Ruby script for backing up *all* of the repos on your GitHub account with a single command
#gitback 0.1
#usage: sudo ruby gitback.rb
#credits: walter white, minor changes: addy osmani
#!/usr/bin/env ruby
# dependencies
require "yaml"
require "open-uri"
time = Time.new
# feel free to edit this as you please
backupDirectory = "/backups/github.com/#{time.year}.#{time.month}.#{time.day}"
# your github username
username = "addyosmani"
#repositories =
# .map{|r| %Q[#{r[:name]}] }
#FileUtils.mkdir_p #{backupDirectory}
YAML.load(open("http://github.com/api/v2/yaml/repos/show/#{username}"))['repositories'].map{|repository|
puts "discovered repository: #{repository[:name]} ... backing up ..."
#exec
system "git clone git@github.com:#{username}/#{repository[:name]}.git #{backupDirectory}/#{repository[:name]}"
}

and if you just want a quick way to list all your repos this might be useful:

#replace 'addyosmani' with your username:
ruby -ryaml -ropen-uri -e "puts YAML.load(open('http://github.com/api/v2/yaml/repos/show/addyosmani'))['repositories'].map {|r| %Q[* **#{r[:name]}** (#{r[:description]}) is at <#{r[:url]}/>] }"

In this post I'm going to present some solutions for easily backing up all of the GitHub repositories for a specific account.

##Discussion

We live in a great time for open-source development and the availability of free plans for services like GitHub and Google Code have undeniably had an implicit affect on our ability to push projects out as a community.

One side-effect of this that with each new commit we make, we have an increasing reliance on these services having both perfect uptime and always being available.

So..crazy thought of the day: what if GitHub wasn't available for a few hours? Or even more pragmatically, what if you were
concerned about your cloud-hosted repositories simply dissapearing.

Now, I might not be a typical user.

I probably have somewhere in the region of 80 odd GitHub repos and almost no single system at work or at home has a copy of all of these on them. I do have recent local copies of some, but certainly not everything.

I was reminded about this the other day and thought it would be useful useful to share some solutions for backing up an account with just a few keystrokes at the terminal.

##Solutions

The first solution is a simple Ruby script I'm calling 'GitBack'. With it, you simply specify your GitHub username and it then maps through all the repositories on your account, locally cloning them using the GitHub API. By default, it includes timestamps in your local directory name, however you can easily opt for something simpler if you prefer.

#gitback 0.1
#credits: Walter White, Updates: Addy Osmani
#!/usr/bin/env ruby

# dependencies
require "yaml"
require "open-uri"

# your github username
username = "addyosmani"

time = Time.new
# feel free to comment out the option you don't wish to use.
backupDirectory = "/backups/github/#{time.year}.#{time.month}.#{time.day}"
#or simply: backupDirectory = "/backups/github/"

#repositories =
# .map{|r| %Q[#{r[:name]}] }

#FileUtils.mkdir_p #{backupDirectory}

YAML.load(open("http://github.com/api/v2/yaml/repos/show/#{username}"))['repositories'].map{|repository|

    puts "discovered repository: #{repository[:name]} ... backing up ..."
    #exec
    system "git clone git@github.com:#{username}/#{repository[:name]}.git #{backupDirectory}/#{repository[:name]}"
}

This can then be used just like this:

sudo ruby gitback.rb

Most of the credit for this solution goes to Walter White. I only made a few changes which I thought helped improve the overall readability of the code, so props to him! \o/

sudo ruby gitback.rb

If you prefer to simply specify the GitHub username and location to backup your repositories at the terminal-level instead, there's a Ruby Gem available by @ddollar that lets you achieve something very similar called GitHub-Backup. (If you need to install Ruby Gems, this will help).

To install GitHub-Backup, simply run:

$ gem install github-backup

and it can then be used as follows:

$ github-backup addyosmani /storage/backups

##Conclusions

Backing up all your repositories might seem to some like an exercise in overkill, but I found the above particularly useful for (if nothing else) getting all my repositories local before I set off for a flight or trip somewhere.

It is sensible to have some level of backup redundancy if you have concerns about files in the cloud not always being available, but I hope these solutions are useful to someone out there!.

@addyosmani
Copy link
Author

And here is a RubyGem that also does the same. https://github.com/ddollar/github-backup

#install
$ gem install github-backup

#to use:
$ github-backup addyosmani /storage/backups

@NLDoA
Copy link

NLDoA commented Aug 9, 2014

Sounds amazing! I really want to backup all of my organizations repos, and I am going to try this out soon! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment