├── github_repos.yml ├── README ├── LICENSE └── github2s3.rb /github_repos.yml: -------------------------------------------------------------------------------- 1 | state_select: 2 | git_clone_url: "git@github.com:bansalakhil/state_select.git" 3 | 4 | another_repo: 5 | git_clone_url: "git@github.com:bansalakhil/another_repository.git" 6 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This ruby script that takes backup of github repository to Amazon S3. 2 | 3 | For more details read here: http://vinsol.com/blog/2011/05/16/github2s3-backup-github-repositories-to-amazon-s3/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Akhil Bansal 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /github2s3.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | 3 | ############################################################# 4 | # Requirements: 5 | # ruby + aws-s3 gem + colorize gem 6 | # git 7 | # 8 | # Author: Akhil Bansal (http://webonrails.com) 9 | ############################################################# 10 | 11 | 12 | ############################################################# 13 | # CONFIGURATION SETTINGS: Please change your S3 credentials 14 | 15 | # AWS S3 credentials 16 | 17 | 18 | AWS_ACCESS_KEY_ID = "ACCESS_KEY" 19 | AWS_SECRET_ACCESS_KEY = "SECRET_KEY" 20 | 21 | # S3 bucket name to put dumps 22 | S3_BUCKET = "github-backup" 23 | 24 | USE_SSL = true 25 | 26 | 27 | 28 | ############################################################# 29 | # PLEASE DO NOT EDIT BELOW THIS LINE 30 | ############################################################# 31 | 32 | 33 | require 'rubygems' 34 | require 'fileutils' 35 | require 'aws/s3' 36 | require 'yaml' 37 | require "colorize" 38 | 39 | REPOSITORY_FILE = File.dirname(__FILE__) + '/github_repos.yml' 40 | 41 | AWS::S3::Base.establish_connection!( 42 | :access_key_id => AWS_ACCESS_KEY_ID, 43 | :secret_access_key => AWS_SECRET_ACCESS_KEY, 44 | :use_ssl => USE_SSL 45 | 46 | ) 47 | 48 | class Bucket < AWS::S3::Bucket 49 | end 50 | 51 | class S3Object < AWS::S3::S3Object 52 | end 53 | 54 | def clone_and_upload_to_s3(options) 55 | puts "\n\nChecking out #{options[:name]} ...".green 56 | clone_command = "cd #{S3_BUCKET} && git clone --bare #{options[:clone_url]} #{options[:name]}" 57 | puts clone_command.yellow 58 | system(clone_command) 59 | puts "\n Compressing #{options[:name]} ".green 60 | system("cd #{S3_BUCKET} && tar czf #{compressed_filename(options[:name])} #{options[:name]}") 61 | 62 | upload_to_s3(compressed_filename(options[:name])) 63 | 64 | end 65 | 66 | def compressed_filename(str) 67 | str+".tar.gz" 68 | end 69 | 70 | def upload_to_s3(filename) 71 | begin 72 | puts "** Uploading #{filename} to S3".green 73 | path = File.join(S3_BUCKET, filename) 74 | S3Object.store(filename, File.read(path), s3bucket) 75 | rescue Exception => e 76 | puts "Could not upload #{filename} to S3".red 77 | puts e.message.red 78 | end 79 | end 80 | 81 | def delete_dir_and_sub_dir(dir) 82 | Dir.foreach(dir) do |e| 83 | # Don't bother with . and .. 84 | next if [".",".."].include? e 85 | fullname = dir + File::Separator + e 86 | if FileTest::directory?(fullname) 87 | delete_dir_and_sub_dir(fullname) 88 | else 89 | File.delete(fullname) 90 | end 91 | end 92 | Dir.delete(dir) 93 | end 94 | 95 | def ensure_bucket_exists 96 | begin 97 | bucket = Bucket.find(s3bucket) 98 | rescue AWS::S3::NoSuchBucket => e 99 | puts "Bucket '#{s3bucket}' not found." 100 | puts "Creating Bucket '#{s3bucket}'. " 101 | 102 | begin 103 | Bucket.create(s3bucket) 104 | puts "Created Bucket '#{s3bucket}'. " 105 | rescue Exception => e 106 | puts e.message 107 | end 108 | end 109 | 110 | end 111 | 112 | def s3bucket 113 | s3bucket = S3_BUCKET 114 | end 115 | 116 | 117 | def backup_repos_form_yaml 118 | if File.exist?(REPOSITORY_FILE) 119 | repos = YAML.load_file(REPOSITORY_FILE) 120 | repos.each{|repo| clone_and_upload_to_s3(:name => repo[0], :clone_url => repo[1]['git_clone_url']) } 121 | else 122 | puts "Repository YAML file(./github_repos.yml) file not found".red 123 | end 124 | end 125 | 126 | def back_repos_from_arguments 127 | ARGV.each do |arg| 128 | begin 129 | name = arg.split('/').last 130 | clone_and_upload_to_s3(:name => name, :clone_url => arg) 131 | rescue Exception => e 132 | puts e.message.red 133 | end 134 | end 135 | end 136 | 137 | 138 | def backup_repos 139 | if ARGV.size > 0 140 | back_repos_from_arguments 141 | else 142 | backup_repos_form_yaml 143 | end 144 | end 145 | 146 | 147 | begin 148 | # create temp dir 149 | Dir.mkdir(S3_BUCKET) rescue nil 150 | ensure_bucket_exists 151 | backup_repos 152 | ensure 153 | # remove temp dir 154 | delete_dir_and_sub_dir(S3_BUCKET) 155 | end 156 | 157 | --------------------------------------------------------------------------------