backup_on_the_go 0.1.3

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
+ YTU0NzNlZWUzYWY4OWY5YmUxNzUyMjM3OTA4MzMyOTk3YzIzNzc5NA==
5
+ data.tar.gz: !binary |-
6
+ OGUzOGUwNmQyZDFhYTljNGMwNjc0YzZmNjFhMTI0OGRlZjgyNDQ2Mw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YThkYzQxMWUzNTQzYjUyZjk3MzVmZGFkZjVhOGI4ZjJiZjIyNzIzMzI3NDAy
10
+ Y2RmMjMzNGE0MTdhZGIxNTlkZWMyZGI5ZmRhZmVhNjA3YmM4OTE0ZjhmZjhl
11
+ ZmMzYTBiNmZiNjI4YWQyYThlZGUzMzgwYzljZjdjYzE5YWE3Yzg=
12
+ data.tar.gz: !binary |-
13
+ MDFkMWI5ZjRjZDI5OTI3YjRkY2VlZDhlNmUwYWY1NDg0N2QxNDA5NzhlYjI1
14
+ NmQ2ZTJhOWFkNjZkYjkzMTE5MDJjZjU4Nzk3MWZiOThhYjlhY2QyMjYzM2Nm
15
+ YzJjYzFlYmNlYTViZTFiMzQxZDY4OWQ3ZWVmZTIwYjIzNWQ3YzY=
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2013 Hong Xu
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23
+ POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Backup On The Go
2
+
3
+ [RubyGems][1] | [RDocs][2]
4
+
5
+ Back up (or mirror) your GitHub repositories to BitBucket.
6
+
7
+ ## Install
8
+
9
+ gem install backup_on_the_go
10
+
11
+ ## Run Locally
12
+
13
+ Shortcut:
14
+
15
+ Back up your personal repositories (will back up your repositories to BitBucket
16
+ with a prefix of `backup-on-the-go-`):
17
+
18
+ backup-on-the-go user:your_user_name
19
+
20
+ Back up your personal repositories to a different account on BitBucket:
21
+
22
+ backup-on-the-go github_user:user_name_on_github bitbucket_user:user_name_on_github
23
+
24
+ Back up your organization repositories:
25
+
26
+ backup-on-the-go user:your_user_name github_repos_owner:organization_name bitbucket_repos_owner:team_name
27
+
28
+ For more options, see the parameters of [BackupOnTheGo.backup][3].
29
+
30
+
31
+ ## Run in the Cloud
32
+
33
+ You can deploy to some cloud services to run the backup timely.
34
+
35
+ ### Deploy to Heroku
36
+
37
+ If you are new to [Heroku](http://heroku.com), first go to Heroku website to
38
+ [create an account][heroku_signup], install [Heroku Toolbelt][], and run
39
+ `heroku login` to login.
40
+
41
+ Run the following command to obtain the pre-prepared files for deploying:
42
+
43
+ git clone -b heroku git://github.com/xuhdev/backup-on-the-go.git
44
+ cd backup-on-the-go
45
+ bundle
46
+ heroku create
47
+
48
+ Then edit `backup.rb` to configure your backup. After configuring, you may
49
+ wanna run `foreman start` to test locally. Then:
50
+
51
+ git commit -a -m "My initial backup commit."
52
+ git push heroku heroku:master
53
+
54
+ **NOTE**: Don't push to any public repositories, since your password is there!
55
+
56
+ At last, you need to [scale your dyno formation][] in order to run the backup
57
+ dyno instead of the web dyno:
58
+
59
+ heroku scale web=0 worker=1
60
+
61
+ Done!
62
+
63
+
64
+ [1]: https://rubygems.org/gems/backup_on_the_go
65
+ [2]: http://rubydoc.info/github/xuhdev/backup-on-the-go/master/frames
66
+ [3]: http://rubydoc.info/github/xuhdev/backup-on-the-go/master/BackupOnTheGo.backup
67
+ [heroku_signup]: https://id.heroku.com/signup
68
+ [Heroku Toolbelt]: https://toolbelt.heroku.com/
69
+ [scale your dyno formation]: https://devcenter.heroku.com/articles/scaling
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'backup_on_the_go'
4
+
5
+ if ARGV.length == 0
6
+ puts <<END
7
+ Usage:
8
+
9
+ backup-on-the-go key0:string_value0 key1:string_value1 key2?boolean_value2 ...
10
+
11
+ e.g.
12
+ backup-on-the-go user:username is_prviate?false
13
+
14
+ For the description of keys and values, please see the document of
15
+ BackupOnTheGo.backup
16
+
17
+ END
18
+ exit
19
+ end
20
+
21
+ config = Hash.new
22
+
23
+ ARGV.each do |a|
24
+
25
+ # string value
26
+ key, value = a.split(':', 2)
27
+ if value != nil
28
+ config[key.to_sym] = value
29
+ next
30
+ end
31
+
32
+ # boolean value
33
+ key, value = a.split('?', 2)
34
+ if value != nil
35
+ value.strip!
36
+
37
+ if value == 'true' or value == 'yes' or value == '1'
38
+ config[key.to_sym] = true
39
+ elsif value == 'false' or value == 'no' or value == '0'
40
+ config[key.to_sym] = false
41
+ end
42
+ end
43
+ end
44
+
45
+ BackupOnTheGo.backup config
@@ -0,0 +1,180 @@
1
+ # encoding: utf-8
2
+
3
+ require 'github_api'
4
+ require 'bitbucket_rest_api'
5
+ require 'highline/import'
6
+ require 'tmpdir'
7
+ require 'colorize'
8
+
9
+ # The module of BackupOnTheGo
10
+ module BackupOnTheGo #:nodoc:#
11
+
12
+ DEFAULT_CONFIG = {
13
+ :backup_fork => false,
14
+ :git_cmd => 'git',
15
+ :is_private => true,
16
+ :no_public_fork => true,
17
+ :repo_prefix => 'backup-on-the-go-',
18
+ :verbose => true
19
+ }.freeze
20
+
21
+ # Backup GitHub repositories to BitBucket.
22
+ #
23
+ # = Parameters
24
+ # * <tt>:backup_fork</tt> - Optional boolean - <tt>true</tt> to backup forked repositories, <tt>false</tt> to skip them. Default is <tt>false</tt>.
25
+ # * <tt>:bitbucket_password</tt> - Optional string - The password to access the BitBucket account. If not specified, a prompt will show up to ask for the password.
26
+ # * <tt>:bitbucket_repos_owner</tt> - Optional string - Owner of the backup repositories on BitBucket. The owner could be a team. If not specified, <tt>:bitbucket_user</tt> will be used.
27
+ # * <tt>:bitbucket_user</tt> - *Required* string if <tt>:user</tt> is not specified - The user name on BitBucket. If not specified, <tt>:user</tt> will be used.
28
+ # * <tt>:git_cmd</tt> - Optional string - The git command you want to use. Default is 'git'.
29
+ # * <tt>:github_repos_owner</tt> - Optional string - The owner of the repositories that need to be backed up. The owner could be an organization. If not specified, <tt>:github_user</tt> will be used.
30
+ # * <tt>:github_user</tt> - *Required* string if <tt>:user</tt> is not specified - The user name on GitHub. If not specified, <tt>:user</tt> will be used.
31
+ # * <tt>:is_private</tt> - Optional boolean - <tt>true</tt> to make the backup repositories private, <tt>false</tt> to make them public. Default is <tt>true</tt>.
32
+ # * <tt>:no_public_fork</tt> - Optional boolean - <tt>true</tt> to forbid public fork for the backup repositories, <tt>false</tt> to allow public fork. Default is <tt>true</tt>.
33
+ # * <tt>:repo_prefix</tt> - Optional string - The prefix you wanna prepend to the backup repository names. In this way, if you have a repository with the same name on BitBucket, it won't get flushed. Default is <tt>"backup-on-the-go-"</tt>.
34
+ # * <tt>:user</tt> - *Required* string if <tt>:github_user</tt> and <tt>:bitbucket_user</tt> are not both specified - The user name of GitHub and BitBucket (if they are same for you). If you want to use different user names on GitHub and BitBucket, please specify <tt>:github_user</tt> and <tt>:bitbucket_user</tt> instead.
35
+ # * <tt>:verbose</tt> - Optional boolean - <tt>true</tt> to print additional information and <tt>false</tt> to suppress them. Default is <tt>true</tt>.
36
+ #
37
+ # = Examples
38
+ #
39
+ # # Back up personal repositories
40
+ # BackupOnTheGo.backup :github_user => 'github_user_name',
41
+ # :bitbucket_user => 'bitbucket_user_name',
42
+ # :is_private => false, # make backup repositories public
43
+ # :bitbucket_password => 'bitbucket_password',
44
+ # :repo_prefix => '' # don't need any prefix
45
+ #
46
+ # = Examples
47
+ #
48
+ # # Back up organization repositories
49
+ # BackupOnTheGo.backup :github_user => 'github_user_name',
50
+ # :github_repos_owner => 'organization_name',
51
+ # :bitbucket_user => 'bitbucket_user_name',
52
+ # :bitbucket_repos_owner => 'bitbucket_team_name',
53
+ # :bitbucket_password => 'bitbucket_password',
54
+ # :repo_prefix => 'our-backup'
55
+ #
56
+ def self.backup(configs = {})
57
+
58
+ config = DEFAULT_CONFIG.merge(configs)
59
+
60
+ # either :user or :github_user and :bitbucket_user have to be set
61
+ if config.has_key?(:user)
62
+ config[:github_user] = config[:user] unless config.has_key?(:github_user)
63
+ config[:bitbucket_user] = config[:user] unless config.has_key?(:bitbucket_user)
64
+ end
65
+
66
+ unless config.has_key?(:github_user) and config.has_key?(:bitbucket_user)
67
+ raise 'No user name provided.'
68
+ end
69
+
70
+ unless config.has_key?(:github_repos_owner) # Owner of the github repos. Could be an organization
71
+ config[:github_repos_owner] = config[:github_user]
72
+ end
73
+
74
+ unless config.has_key?(:bitbucket_repos_owner) # Owner of backup repositories. Could be a team.
75
+ config[:bitbucket_repos_owner] = config[:bitbucket_user]
76
+ end
77
+
78
+ # Ask for the password if it is not specified
79
+ unless config.has_key?(:bitbucket_password)
80
+ config[:bitbucket_password] = ask("Enter your BitBucket password: ") { |q| q.echo = false }
81
+ end
82
+
83
+
84
+ gh_repos = Github.repos.list :user => config[:github_repos_owner] # obtain github repos
85
+
86
+ bb = BitBucket.new :login => config[:bitbucket_user], :password => config[:bitbucket_password]
87
+
88
+ backup_repo_names = Array.new
89
+
90
+ bb.repos.list do |repo|
91
+ if repo.owner == config[:bitbucket_repos_owner]
92
+ backup_repo_names.push(repo.slug)
93
+ end
94
+ end
95
+
96
+ gh_repos.each do |repo|
97
+ next if repo.fork && !config[:backup_fork]
98
+
99
+ puts "Backing up #{repo.name}..." if config[:verbose]
100
+
101
+ backup_repo_name = "#{config[:repo_prefix]}#{repo.name}"
102
+
103
+ # Create backup repositories if we don't have them yet
104
+ unless backup_repo_names.include?(backup_repo_name.downcase)
105
+ puts "Creating new repository #{config[:bitbucket_repos_owner]}/#{backup_repo_name}..." if config[:verbose]
106
+ begin
107
+ bb.repos.create :name => backup_repo_name, :owner => config[:bitbucket_repos_owner],
108
+ :scm => 'git', :is_private => config[:is_private], :no_public_fork => config[:no_public_fork]
109
+ rescue
110
+ puts_warning "Creation of repository #{config[:bitbucket_repos_owner]}/#{backup_repo_name} failed."
111
+ end
112
+ end
113
+
114
+ puts "Backing up resources..." if config[:verbose]
115
+
116
+ begin
117
+ bb.repos.edit config[:bitbucket_repos_owner], backup_repo_name,
118
+ :website => repo.homepage,
119
+ :description => repo.description,
120
+ :is_private => config[:is_private]
121
+ rescue
122
+ puts_warning "Failed to update information for #{config[:bitbucket_repos_owner]}/#{backup_repo_name}"
123
+ end
124
+
125
+ Dir.mktmpdir do |dir|
126
+ cmd = "#{config[:git_cmd]} clone --mirror #{repo.git_url} #{dir}/tmp-repo"
127
+ puts "Executing #{cmd}" if config[:verbose]
128
+ unless system(cmd)
129
+ puts_warning "'git clone' failed for #{repo.git_url}\n"
130
+ break
131
+ end
132
+
133
+ # Add bitbucket remote
134
+ cmd = "cd #{dir}/tmp-repo && " +
135
+ "#{config[:git_cmd]} remote add bitbucket 'https://#{config[:bitbucket_user]}:#{config[:bitbucket_password]}@bitbucket.org/#{config[:bitbucket_repos_owner]}/#{backup_repo_name}.git'"
136
+ puts "Executing [#{config[:git_cmd]} remote add bitbucket 'https://#{config[:bitbucket_user]}:your_password@bitbucket.org/#{config[:bitbucket_repos_owner]}/#{backup_repo_name}.git']" if config[:verbose]
137
+ `#{cmd}`
138
+ unless $?.exitstatus
139
+ puts_warning "'git remote add bitbucket ...' failed for #{config[:bitbucket_repos_owner]}/#{backup_repo_name}\n"
140
+ break
141
+ end
142
+
143
+ # obtain the main branch (usually master, just in case)
144
+ cmd = "cd #{dir}/tmp-repo && #{config[:git_cmd]} branch"
145
+ puts "Executing #{cmd}" if config[:verbose]
146
+ branches = `#{cmd}`
147
+ unless $?.exitstatus
148
+ puts_warning "''#{config[:git_cmd]} branch' failed for #{config[:github_repos_owner]}/#{repo.name}"
149
+ break
150
+ end
151
+ main_branch = nil
152
+ branches.each_line do |line|
153
+ # This is the main branch we need
154
+ if line.length >= 1 and line[0] == '*'
155
+ main_branch = line[1..-1].strip
156
+ end
157
+ end
158
+
159
+ cmd = "cd #{dir}/tmp-repo && "
160
+ if main_branch != nil # push bitbucket #{main_branch} first before push --mirror
161
+ cmd += "#{config[:git_cmd]} push bitbucket #{main_branch} && "
162
+ end
163
+ cmd += "#{config[:git_cmd]} push --mirror bitbucket"
164
+ puts "Executing #{cmd}" if config[:verbose]
165
+ `#{cmd}`
166
+ unless $?.exitstatus
167
+ puts_warning "'#{config[:git_cmd]} push' failed for #{config[:bitbucket_repos_owner]}/#{backup_repo_name}\n"
168
+ break
169
+ end
170
+ puts
171
+ end
172
+ end
173
+ end
174
+
175
+ private
176
+
177
+ def self.puts_warning(str)
178
+ puts "[Warning]: #{str}".red
179
+ end
180
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ module BackupOnTheGo #:nodoc:#
4
+ module VERSION
5
+ MAJOR = 0
6
+ MINOR = 1
7
+ TINY = 3
8
+
9
+ STRING = [MAJOR, MINOR, TINY].compact.join('.')
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backup_on_the_go
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Hong Xu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: github_api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.11
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.11
27
+ - !ruby/object:Gem::Dependency
28
+ name: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.16
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.16
41
+ - !ruby/object:Gem::Dependency
42
+ name: bitbucket_rest_api
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.8
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.8
69
+ description: Backup GitHub repositories to BitBucket
70
+ email: hong@topbug.net
71
+ executables:
72
+ - backup-on-the-go
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/backup-on-the-go
77
+ - lib/backup_on_the_go/version.rb
78
+ - lib/backup_on_the_go.rb
79
+ - LICENSE
80
+ - README.md
81
+ homepage: https://github.com/xuhdev/backup-on-the-go#readme
82
+ licenses:
83
+ - BSD
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Backup GitHub repositories to BitBucket
105
+ test_files: []