Skip to content

Instantly share code, notes, and snippets.

@tomlea
Created January 13, 2009 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomlea/46532 to your computer and use it in GitHub Desktop.
Save tomlea/46532 to your computer and use it in GitHub Desktop.
Command line app to backup all your github repos. You can then use the local path just like a remote one when github goes down.
#!/usr/bin/env ruby
gem 'mbleigh-ruby-github'
require "ruby-github"
unless username = ARGV.first and username =~ /[a-zA-Z0-9]+/
puts "Usage: #{$0} <github_username>"
exit(1)
end
BASE_PATH=File.expand_path("./repos")
Dir.mkdir(BASE_PATH) unless File.directory? BASE_PATH
def git(*args)
system("git", *args)
raise "Command git #{args.join(" ")} Failed" unless $?.success?
end
user_path = "#{BASE_PATH}/#{username}"
Dir.mkdir(user_path) unless File.directory? user_path
Dir.chdir(user_path)
GitHub::API.user(username).repositories.each do |repo|
local_repos = File.expand_path("#{user_path}/#{repo.name}.git")
remote_repos = "git://github.com/#{repo.user}/#{repo.name}.git"
ENV["GIT_DIR"] = "#{repo.name}.git"
git "clone", "-q", "--bare", remote_repos unless File.directory? local_repos
git "fetch", "-q", remote_repos, "HEAD"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment