Skip to content

Instantly share code, notes, and snippets.

@softr8
Created September 4, 2015 18:23
Show Gist options
  • Save softr8/be3a08309dadaf947012 to your computer and use it in GitHub Desktop.
Save softr8/be3a08309dadaf947012 to your computer and use it in GitHub Desktop.
Backup github repos locally
require 'github_api'
require 'git'
client = Github.new oauth_token: 'TOKEN', org: 'ORG_NAME'
BASE_DIR ||= '../repos'
i = 1
while true
repos = client.repos.list(page: i)
break if repos.size == 0
repos.each do |repo|
type = repo.private ? "private" : "public"
repo_path = File.join BASE_DIR, type, repo.name
unless Dir.exists?(repo_path)
puts "Cloning repo #{repo.name}..."
Git.clone repo.ssh_url, repo_path
puts "...Done"
end
g = Git.open(repo_path)
puts "Fetching new changes of #{repo.name}"
puts g.pull
puts "...Done"
end
i += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment