github_snapshot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OGI2N2ZkYjNkY2I0MTZkNjFjYzcxYjMyOWU1NjE1NjU5ZjI3NGEwYQ==
5
+ data.tar.gz: !binary |-
6
+ YTFlNTIzZDBmNTVmOTUyYTQ1NmFiZjdjNjk0YzdjZDBiMmIxMjlkZA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YjMyMzIxOTg4MTI0MDkwYmU3YTUyY2E5MGFiNjI5YTMxZDM0YTcxN2RjNTVh
10
+ MTczZjZlMzkxOGJhNmExNTVmNTU0YWU2MTk1MmQxOTliYTYyZmQ0MDAyMGNm
11
+ MzRmMDM3NWFiOTNhMDcyOTVlOWFhM2JlNWQ0MjkxMWZhMDI0NjA=
12
+ data.tar.gz: !binary |-
13
+ ODZjYWZjY2IzM2I1NDcxOTMyY2RiZjQyYjUxMzE1ODg3YTAyMzViY2Q3ZjEx
14
+ NGY5N2YwNmEwYTc0Mzg0NTJjOTZmMTU0MjEzNTViMDY0MzAxMWQyYmMzOGQ3
15
+ NWFkNjg3Y2QxYzZkMGE4ZGVkYmViOGEwZjgzMjdmZTY5OGQ1ZjY=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ config.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in github_snapshot.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Innvent | Innovation & Venturing
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # GithubSnapshot
2
+
3
+ Snapshoting organization's repositories, including wikis
4
+
5
+ ## Installation
6
+
7
+ `github_snapshot` should be run as a command line tool, so it makes sense installing it globally
8
+
9
+ $ gem install github_snapshot
10
+
11
+ [`s3cmd`](https://github.com/s3tools/s3cmd) should also be installed on the system and properly configured.
12
+
13
+ ## Usage
14
+
15
+ `github_snapshot` expects a `config.yml` file on the folder it is run, here is a sample:
16
+
17
+ ```yaml
18
+ username: <github user with read access to the repositories>
19
+ password: <github password>
20
+ organizations:
21
+ - organization1
22
+ - organization2
23
+ s3bucket: <s3 bucket to store the backups>
24
+ backup_folder: <backup folder were the repos will be cloned to>
25
+ releases_to_keep: <how many releases to keep>
26
+ ```
27
+
28
+ Then, simply run the gem's binary:
29
+
30
+ ```bash
31
+ $ github_snapshot
32
+ ```
33
+
34
+ **You could be prompted to check the authenticity for `github.com` host**. You can either say yes during the first script execution or disable strict host key checking for it:
35
+
36
+ ```bash
37
+ $ echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
38
+ ```
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'github_snapshot'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'github_snapshot'
8
+ end
9
+
10
+ gs = GithubSnapshot::Snapshot.new
11
+ gs.backup
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'github_snapshot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "github_snapshot"
8
+ spec.version = GithubSnapshot::VERSION
9
+ spec.authors = ["Artur Rodrigues" , "Joao Sa"]
10
+ spec.email = ["arturhoo@gmail.com"]
11
+ spec.description = %q{Snapshoting organization's repositories, including wikis}
12
+ spec.summary = %q{Snapshoting organization's repositories}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "awesome_print"
25
+
26
+ spec.add_dependency "github_api", "~> 0.10.2"
27
+ spec.add_dependency "rake"
28
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "repository"
2
+
3
+ module GithubSnapshot
4
+
5
+ class Organization
6
+
7
+ attr_reader :name, :folder, :repos
8
+
9
+ def initialize(name, folder, repos)
10
+ @name = name
11
+ @folder = folder
12
+ @repos = repos
13
+ end
14
+
15
+ def backup
16
+ repos.to_a.each do |repo|
17
+ repository = Repository.new repo, self
18
+ repository.backup
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,84 @@
1
+ require "open3"
2
+ require "rake"
3
+ require "timeout"
4
+ require_relative "utilities"
5
+
6
+ module GithubSnapshot
7
+
8
+ class Repository
9
+
10
+ attr_reader :name, :folder, :wiki_folder, :ssh_url, :wiki_ssh_url,
11
+ :canonical_name, :pushed_at, :gh_has_wiki, :organization
12
+
13
+ def initialize(repo, organization)
14
+ @name = repo['name']
15
+ @folder = "#{organization.folder}/#{@name}-#{GithubSnapshot.time_now}.git"
16
+ @wiki_folder = @folder.gsub('.git', '.wiki.git')
17
+ @ssh_url = repo['ssh_url']
18
+ @wiki_ssh_url = @ssh_url.gsub('.git', '.wiki.git')
19
+ @canonical_name = "#{organization.name}/#{@name}"
20
+ @pushed_at = repo['pushed_at']
21
+ @gh_has_wiki = repo['has_wiki']
22
+ @organization = organization
23
+ end
24
+
25
+ def has_wiki?
26
+ Open3.capture3("git ls-remote #{wiki_ssh_url}")[1].empty? && gh_has_wiki
27
+ end
28
+
29
+ def backup
30
+ GithubSnapshot.logger.info "#{canonical_name} - backing up"
31
+
32
+ # Go to next repo if the repository is empty
33
+ unless pushed_at
34
+ GithubSnapshot.logger.info "#{canonical_name} is empty"
35
+ return nil
36
+ end
37
+
38
+ clone
39
+ clone_wiki if self.has_wiki?
40
+ prune_old_backups
41
+
42
+ GithubSnapshot.logger.info "#{canonical_name} - success"
43
+ end
44
+
45
+ private
46
+
47
+ def clone
48
+ GithubSnapshot.logger.info "#{canonical_name} - cloning"
49
+ begin
50
+ Timeout::timeout (300) {
51
+ GithubSnapshot.exec "#{GithubSnapshot.git_clone_cmd} #{ssh_url} #{folder}"
52
+ }
53
+ rescue Timeout::Error => e
54
+ logger.error "Could not clone #{canonical_name}, timedout"
55
+ end
56
+ GithubSnapshot.exec "tar zcf #{folder}.tar.gz #{folder}"
57
+ GithubSnapshot.exec "rm -rf #{folder}"
58
+ end
59
+
60
+ def clone_wiki
61
+ GithubSnapshot.logger.info "#{canonical_name} - cloning wiki"
62
+ GithubSnapshot.exec "#{GithubSnapshot.git_clone_cmd} #{wiki_ssh_url} #{wiki_folder}"
63
+ GithubSnapshot.exec "tar zcf #{wiki_folder}.tar.gz #{wiki_folder}"
64
+ GithubSnapshot.exec "rm -rf #{wiki_folder}"
65
+ end
66
+
67
+ def prune_old_backups
68
+ GithubSnapshot.logger.info "#{canonical_name} - pruning old backups"
69
+ file_regex = "#{organization.folder}/#{name}*"
70
+ zipped_bkps = FileList[file_regex].exclude(/wiki/)
71
+ zipped_bkps.sort[0..-(GithubSnapshot.releases_to_keep + 1)].each do |file|
72
+ File.delete file
73
+ end if zipped_bkps.length > GithubSnapshot.releases_to_keep
74
+
75
+ wiki_regex = "#{organization.name}/#{name}*wiki*"
76
+ zipped_wikis = FileList[wiki_regex]
77
+ zipped_wikis.sort[0..-(GithubSnapshot.releases_to_keep + 1)].each do |file|
78
+ File.delete file
79
+ end if zipped_wikis.length > GithubSnapshot.releases_to_keep
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,98 @@
1
+ require "github_api"
2
+ require "logger"
3
+ require_relative "utilities"
4
+ require_relative "organization"
5
+
6
+ module GithubSnapshot
7
+ @@logger = Logger.new(STDOUT)
8
+ @@logger.level = Logger::INFO
9
+ @@logger.datetime_format = '%Y-%m-%d %H:%M:%S '
10
+ @@git_clone_cmd = "git clone --quiet --mirror"
11
+ @@time_now = Time.now.getutc.strftime("%Y%m%d%H%M")
12
+ @@releases_to_keep
13
+
14
+ def self.logger
15
+ @@logger
16
+ end
17
+
18
+ def self.git_clone_cmd
19
+ @@git_clone_cmd
20
+ end
21
+
22
+ def self.time_now
23
+ @@time_now
24
+ end
25
+
26
+ def self.releases_to_keep
27
+ @@releases_to_keep
28
+ end
29
+
30
+ def self.releases_to_keep=(releases_to_keep)
31
+ @@releases_to_keep = releases_to_keep
32
+ end
33
+
34
+ def self.exec(cmd)
35
+ Utilities.exec cmd, @@logger
36
+ end
37
+
38
+ class Snapshot
39
+ require "yaml"
40
+
41
+ attr_reader :username, :password, :organizations, :s3_bucket, :backup_folder,
42
+ :releases_to_keep, :git_clone_cmd, :time_now, :github, :logger
43
+
44
+ def initialize(config_file="config.yml")
45
+ config = YAML.load(File.read(config_file))
46
+
47
+ @username = config['username']
48
+ @password = config['password']
49
+ @organizations = config['organizations']
50
+ @s3_bucket = config['s3bucket']
51
+ @backup_folder = config['backup_folder']
52
+ GithubSnapshot.releases_to_keep = config['releases_to_keep']
53
+
54
+ @github = Github.new do |config|
55
+ config.login = username
56
+ config.password = password
57
+ config.auto_pagination = true
58
+ end
59
+ end
60
+
61
+ def backup
62
+ create_backup_folder
63
+ download_from_s3
64
+ backup_orgs
65
+ upload_to_s3
66
+
67
+ total_size = %x[ du -sh #{backup_folder} | cut -f1 ]
68
+ GithubSnapshot.logger.info "backup finished, total size is #{total_size}"
69
+ end
70
+
71
+ private
72
+ def create_backup_folder
73
+ GithubSnapshot.exec "mkdir -p #{backup_folder}"
74
+ end
75
+
76
+ def download_from_s3
77
+ GithubSnapshot.logger.info "downloading fom s3"
78
+ GithubSnapshot.exec "s3cmd sync --delete-removed s3://#{s3_bucket}/ #{backup_folder}/"
79
+ end
80
+
81
+ def backup_orgs
82
+ organizations.each do |org|
83
+ GithubSnapshot.logger.info "#{org} - initializing"
84
+ repos = github.repos.list org: org
85
+ organization = Organization.new org, "#{backup_folder}/#{org}", repos
86
+ organization.backup
87
+ GithubSnapshot.logger.info "#{org} - ending"
88
+ end
89
+ end
90
+
91
+ def upload_to_s3
92
+ GithubSnapshot.logger.info "uploading to s3"
93
+ GithubSnapshot.exec "s3cmd sync --delete-removed #{backup_folder}/ s3://#{s3_bucket}/"
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,12 @@
1
+ module Utilities
2
+ module_function
3
+
4
+ def exec(cmd, logger)
5
+ out, err, status = Open3.capture3 cmd
6
+ if err.empty?
7
+ logger.debug out unless out.empty?
8
+ else
9
+ logger.error(err + " command was: #{cmd}")
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module GithubSnapshot
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "github_snapshot/snapshot"
2
+
3
+ module GithubSnapshot
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github_snapshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur Rodrigues
8
+ - Joao Sa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: pry
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: awesome_print
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: github_api
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.10.2
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: 0.10.2
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: Snapshoting organization's repositories, including wikis
99
+ email:
100
+ - arturhoo@gmail.com
101
+ executables:
102
+ - github_snapshot
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/github_snapshot
112
+ - github_snapshot.gemspec
113
+ - lib/github_snapshot.rb
114
+ - lib/github_snapshot/organization.rb
115
+ - lib/github_snapshot/repository.rb
116
+ - lib/github_snapshot/snapshot.rb
117
+ - lib/github_snapshot/utilities.rb
118
+ - lib/github_snapshot/version.rb
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.7
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Snapshoting organization's repositories
143
+ test_files: []