class GithubBackup

Public Class Methods

new(github) click to toggle source
# File lib/backup-github.rb, line 17
def initialize(github)
  @github = github
end

Public Instance Methods

backup_gitrepo( giturl, path ) click to toggle source
# File lib/backup-github.rb, line 29
def backup_gitrepo( giturl, path )
    $logger.info "Backuping #{path.to_path}"
    if path.directory?
      $logger.info "Backuping #{path.to_path} -> UPDATE"
      gritty = Grit::Git.new(path.realpath.to_path)
      gritty.fetch({:timeout=>false})
    else
      $logger.info "Backuping #{path.to_path} -> NEW"
      path.mkpath
      gritty = Grit::Git.new(".")
      gritty.clone({:mirror=>true, :timeout=>false}, giturl, path.realpath.to_path)
    end
end
run( orgname, local_repo ) click to toggle source
# File lib/backup-github.rb, line 43
def run( orgname, local_repo )
  @github.repositories(orgname)[0,3].each do |repo|
    reponame = "#{orgname}/#{repo.name}"
    $logger.info "Backuping #{reponame}"

    repopath = Pathname.new(local_repo) + repo.name
    repopath.mkpath

    $logger.info("Backup Reporsitory")
    backup_gitrepo(repo.ssh_url, repopath + "git-bare" )

    $logger.info("Backup Wiki")
    backup_gitrepo(repo.ssh_url.gsub(".git", ".wiki.git"), repopath + "wiki-bare" )

    # Backup Issues
    if @github.has_issues? reponame
      issuespath = repopath + "issues"
      issuespath.mkpath
      Dir.chdir(issuespath) do
        # After endless problems with Grit, fallback to sytem exec
        `git init` unless File.directory? ".git"
        $logger.info("Fetching issues")
        @github.on_issues(reponame) {|issue| save_issue(issue)}

        $logger.info("Add & Commit issues")
        `git add .`
        `git commit -m backup`
      end
    else
      $logger.info "No issues for #{reponame}"
    end
  end
end
save_issue(issue) click to toggle source
# File lib/backup-github.rb, line 25
def save_issue(issue)
  File.open('%04d.js' % issue.number, "w") { |f| f.write(serialize_issue(issue)) }
end
serialize_issue(issue) click to toggle source
# File lib/backup-github.rb, line 21
def serialize_issue(issue)
  JSON.pretty_generate( issue.content )
end