gh_backup 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +18 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/bin/backup +5 -0
- data/gh_backup.gemspec +22 -0
- data/lib/gh_backup/version.rb +3 -0
- data/lib/gh_backup.rb +139 -0
- data/screenshots/preview.png +0 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 owain lewis
|
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,25 @@
|
|
1
|
+
# GhBackup
|
2
|
+
|
3
|
+
A simple script that backs up all the github repos for an organization
|
4
|
+
|
5
|
+

|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install locally with
|
10
|
+
|
11
|
+
$ gem install gh_backup
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Backups are placed in a /backups folder in the current directory
|
16
|
+
|
17
|
+
backup all <org> <username> <password>
|
18
|
+
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
1. Fork it
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/backup
ADDED
data/gh_backup.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gh_backup/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "gh_backup"
|
8
|
+
gem.version = GhBackup::VERSION
|
9
|
+
gem.authors = ["owain lewis"]
|
10
|
+
gem.email = ["owain@owainlewis.com"]
|
11
|
+
gem.description = %q{ Backups up github repos }
|
12
|
+
gem.summary = %q{ Gem that backups up repos for an organization }
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency "thor"
|
21
|
+
gem.add_runtime_dependency "rake"
|
22
|
+
end
|
data/lib/gh_backup.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require "gh_backup/version"
|
2
|
+
require 'yaml'
|
3
|
+
require 'json'
|
4
|
+
require "thor"
|
5
|
+
|
6
|
+
module GhBackup
|
7
|
+
|
8
|
+
class Base
|
9
|
+
|
10
|
+
def initialize(color=true)
|
11
|
+
@color = color
|
12
|
+
end
|
13
|
+
|
14
|
+
# Create a string path to a github repo with auth info
|
15
|
+
#
|
16
|
+
def repo_url(username, password, account, repo)
|
17
|
+
"https://#{username}:#{password}@github.com/#{account}/#{repo}.git"
|
18
|
+
end
|
19
|
+
|
20
|
+
# Zip up a directory in the backups folder
|
21
|
+
def zipf(name, dir)
|
22
|
+
system "tar zcvf #{name}.tar.gz backups/#{dir}"
|
23
|
+
end
|
24
|
+
|
25
|
+
# Colorize a string for the terminal
|
26
|
+
def colorize(str, code)
|
27
|
+
if @color
|
28
|
+
"\e[#{code}m#{str}\e[0m"
|
29
|
+
else
|
30
|
+
str
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def make_dir(dir)
|
35
|
+
unless Dir.exists?(dir)
|
36
|
+
Dir.mkdir(dir)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns current date as a string
|
41
|
+
def format_current_date
|
42
|
+
Time.now.strftime("%m%d%Y")
|
43
|
+
end
|
44
|
+
|
45
|
+
# Clone a single repository and put it in the backups dir
|
46
|
+
# repos are saved under a folder with the current date i.e 02012014 etc
|
47
|
+
def clone_repo(username, password, account, repo)
|
48
|
+
now = format_current_date
|
49
|
+
make_dir("./backups")
|
50
|
+
make_dir("./backups/#{now}")
|
51
|
+
path = "./backups/#{now}/#{repo}"
|
52
|
+
if Dir.exists?(path)
|
53
|
+
puts colorize("Dir already exists. Skipping clone", 31)
|
54
|
+
else
|
55
|
+
system "git clone #{repo_url(username, password, account, repo)} #{path}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Run all backups
|
60
|
+
def backup(f, account, user, pass)
|
61
|
+
repos = YAML.load_file(f).map(&:strip)
|
62
|
+
repo_count = repos.length
|
63
|
+
repos.each_with_index do |repo, idx|
|
64
|
+
puts colorize("\n#{idx+1} of #{repo_count}: Cloning #{repo}", 32)
|
65
|
+
clone_repo(user, pass, account, repo)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Extracts the repo name from a github URL
|
70
|
+
# i.e https://github.com/boxuk/wedge.js.git
|
71
|
+
def split_repo_name(repo_path)
|
72
|
+
parts = repo_path.split(/\//)
|
73
|
+
parts.last.split(/.git/).first
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns an array of all the repos for an organization
|
77
|
+
def list_org_repos(org, user, password, limit=50)
|
78
|
+
auth = "#{user}:#{password}"
|
79
|
+
path = "https://api.github.com/orgs/#{org}/repos?per_page=#{limit}"
|
80
|
+
repos = `curl -u #{auth} #{path}`
|
81
|
+
JSON.parse(repos).map do |repo|
|
82
|
+
repo["clone_url"]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# You can download the repos directly without having to save to YAML
|
87
|
+
def clone_org_repos(org, user, password, limit=50)
|
88
|
+
all_repos = list_org_repos(org, user, password, limit)
|
89
|
+
repo_count = all_repos.length
|
90
|
+
all_repos.each_with_index.map do |repo, i|
|
91
|
+
repository = split_repo_name(repo)
|
92
|
+
puts colorize("\n#{i+1} of #{repo_count}: Cloning #{repo}", 32)
|
93
|
+
clone_repo(user, password, org, repository)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# ===========================
|
99
|
+
#
|
100
|
+
# Backup CLI
|
101
|
+
#
|
102
|
+
# ===========================
|
103
|
+
|
104
|
+
class CLI < Thor
|
105
|
+
|
106
|
+
def initialize(*args)
|
107
|
+
@backup = Base.new
|
108
|
+
super
|
109
|
+
end
|
110
|
+
|
111
|
+
desc "user", "Backup user repos"
|
112
|
+
method_option :username, type: :string, aliases: '-u', required: true
|
113
|
+
method_option :password, type: :string, aliases: '-p', required: true
|
114
|
+
def user
|
115
|
+
puts options.inspect
|
116
|
+
puts options.username
|
117
|
+
end
|
118
|
+
|
119
|
+
desc "clone", "Clone a single repo"
|
120
|
+
def clone(account, user, pass, repo)
|
121
|
+
@backup.clone_repo(user, pass, account, repo)
|
122
|
+
end
|
123
|
+
|
124
|
+
desc "yml", "Backup all repos listed in repos.yml"
|
125
|
+
def yml(account, user, pass)
|
126
|
+
@backup.backup(account, user, pass)
|
127
|
+
end
|
128
|
+
|
129
|
+
desc "all", "Backup all repos for an org"
|
130
|
+
def all(org, user, pass)
|
131
|
+
@backup.clone_org_repos(org, user, pass)
|
132
|
+
end
|
133
|
+
|
134
|
+
desc "list", "List all repos for an organization"
|
135
|
+
def list(account, user, pass)
|
136
|
+
puts @backup.list_org_repos(account, user, pass)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gh_backup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- owain lewis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: ! ' Backups up github repos '
|
47
|
+
email:
|
48
|
+
- owain@owainlewis.com
|
49
|
+
executables:
|
50
|
+
- backup
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- Gemfile.lock
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- bin/backup
|
61
|
+
- gh_backup.gemspec
|
62
|
+
- lib/gh_backup.rb
|
63
|
+
- lib/gh_backup/version.rb
|
64
|
+
- screenshots/preview.png
|
65
|
+
homepage: ''
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
hash: -2439856035684088366
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: -2439856035684088366
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.23
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Gem that backups up repos for an organization
|
95
|
+
test_files: []
|