repos-backup /bitbucket-backup.rb

Language Ruby Lines 76
MD5 Hash c238dd5e2fdf7be0b6c3bf9ea2aa7050 Estimated Cost $968 (why?)
Repository https://bitbucket.org/chadmaughan/repos-backup.git View Raw File View Project SPDX
 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
#!/usr/bin/env ruby

# Chad Maughan
# chadmaughan.com
# 2012-09-24
#
# This is a script for cloning or pulling changes from repositories
# on a scheduled basis from http://bitbucket.org using their OAuth API

require 'rubygems'
require 'oauth'
require 'json'
require 'yaml'
require './lib/cli.rb'

cli = BackupCLI.new
cli.parse_options

user = cli.config[:user]
path = cli.config[:path]

if user.nil? || path.nil?
	puts "Incorrect usage, use '--help' (-h) to see required arguments"
	Process.exit
end

url = "https://api.bitbucket.org/1.0"

# make the consumer out of your secret and key
#  you need to set this up on the bitbucket website (under Account -> Integrated Applications)
#
# this file is stored in ~/.repo-backup/.oauth-token 
config = YAML.load_file(ENV['HOME'] + "/.repo-backup/.oauth-token")
consumer_key = config["consumer"]["key"]
consumer_secret = config["consumer"]["secret"]
consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
                               :site => url,
                               :request_token_path => "/oauth/request_token",
                               :authorize_path => "/oauth/authorize",
                               :access_token_path => "/oauth/access_token",
                               :http_method => :get)

# make the access token from your consumer
access_token = OAuth::AccessToken.new(consumer)

# nifty way to debug all HTTP traffic
# 	consumer.http.set_debug_output($stderr)

# make a signed request
response = access_token.get("/user")

# fix the path if it doesn't end with a '/'
path = /\/$/.match(path) ? path : path + "/"

# parse the response
json = JSON.parse(response.body)
puts "Number of Bitbucket repositories to check: #{json['repositories'].size}"
json['repositories'].each do |repo|

	name = repo['name']
	puts name

	dir = path + name
	Dir.chdir(path)

	# see if the directory exists (update or clone)
	if File.directory?(dir)
		Dir.chdir(dir)
		puts "repository exists, updating to: " + dir
		puts system "git pull"
	else
		puts "repository doesn't exit, cloning to: " + dir
		puts system "git clone git@bitbucket.org:#{user}/#{name}.git"
	end
end
Back to Top