-
Notifications
You must be signed in to change notification settings - Fork 9
/
github2s3.rb
executable file
·156 lines (120 loc) · 3.3 KB
/
github2s3.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/ruby
#############################################################
# Requirements:
# ruby + aws-s3 gem + colorize gem
# git
#
# Author: Akhil Bansal (http://webonrails.com)
#############################################################
#############################################################
# CONFIGURATION SETTINGS: Please change your S3 credentials
# AWS S3 credentials
AWS_ACCESS_KEY_ID = "ACCESS_KEY"
AWS_SECRET_ACCESS_KEY = "SECRET_KEY"
# S3 bucket name to put dumps
S3_BUCKET = "github-backup"
USE_SSL = true
#############################################################
# PLEASE DO NOT EDIT BELOW THIS LINE
#############################################################
require 'rubygems'
require 'fileutils'
require 'aws/s3'
require 'yaml'
require "colorize"
REPOSITORY_FILE = File.dirname(__FILE__) + '/github_repos.yml'
AWS::S3::Base.establish_connection!(
:access_key_id => AWS_ACCESS_KEY_ID,
:secret_access_key => AWS_SECRET_ACCESS_KEY,
:use_ssl => USE_SSL
)
class Bucket < AWS::S3::Bucket
end
class S3Object < AWS::S3::S3Object
end
def clone_and_upload_to_s3(options)
puts "\n\nChecking out #{options[:name]} ...".green
clone_command = "cd #{S3_BUCKET} && git clone --bare #{options[:clone_url]} #{options[:name]}"
puts clone_command.yellow
system(clone_command)
puts "\n Compressing #{options[:name]} ".green
system("cd #{S3_BUCKET} && tar czf #{compressed_filename(options[:name])} #{options[:name]}")
upload_to_s3(compressed_filename(options[:name]))
end
def compressed_filename(str)
str+".tar.gz"
end
def upload_to_s3(filename)
begin
puts "** Uploading #{filename} to S3".green
path = File.join(S3_BUCKET, filename)
S3Object.store(filename, File.read(path), s3bucket)
rescue Exception => e
puts "Could not upload #{filename} to S3".red
puts e.message.red
end
end
def delete_dir_and_sub_dir(dir)
Dir.foreach(dir) do |e|
# Don't bother with . and ..
next if [".",".."].include? e
fullname = dir + File::Separator + e
if FileTest::directory?(fullname)
delete_dir_and_sub_dir(fullname)
else
File.delete(fullname)
end
end
Dir.delete(dir)
end
def ensure_bucket_exists
begin
bucket = Bucket.find(s3bucket)
rescue AWS::S3::NoSuchBucket => e
puts "Bucket '#{s3bucket}' not found."
puts "Creating Bucket '#{s3bucket}'. "
begin
Bucket.create(s3bucket)
puts "Created Bucket '#{s3bucket}'. "
rescue Exception => e
puts e.message
end
end
end
def s3bucket
s3bucket = S3_BUCKET
end
def backup_repos_form_yaml
if File.exist?(REPOSITORY_FILE)
repos = YAML.load_file(REPOSITORY_FILE)
repos.each{|repo| clone_and_upload_to_s3(:name => repo[0], :clone_url => repo[1]['git_clone_url']) }
else
puts "Repository YAML file(./github_repos.yml) file not found".red
end
end
def back_repos_from_arguments
ARGV.each do |arg|
begin
name = arg.split('/').last
clone_and_upload_to_s3(:name => name, :clone_url => arg)
rescue Exception => e
puts e.message.red
end
end
end
def backup_repos
if ARGV.size > 0
back_repos_from_arguments
else
backup_repos_form_yaml
end
end
begin
# create temp dir
Dir.mkdir(S3_BUCKET) rescue nil
ensure_bucket_exists
backup_repos
ensure
# remove temp dir
delete_dir_and_sub_dir(S3_BUCKET)
end