-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsalesforce-backup.rb
executable file
·233 lines (196 loc) · 5.61 KB
/
salesforce-backup.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/ruby
require 'net/http'
require 'net/https'
require 'rexml/document'
require 'date'
require 'net/smtp'
require 'yaml'
require 'fileutils'
include REXML
class Result
def initialize(xmldoc)
@xmldoc = xmldoc
end
def server_url
@server_url ||= XPath.first(@xmldoc, '//result/serverUrl/text()')
end
def session_id
@session_id ||= XPath.first(@xmldoc, '//result/sessionId/text()')
end
def org_id
@org_id ||= XPath.first(@xmldoc, '//result/userInfo/organizationId/text()')
end
end
class SfError < Exception
attr_accessor :resp
def initialize(resp)
@resp = resp
end
def inspect
puts resp.body
end
alias_method :to_s, :inspect
end
### Helpers ###
def http(host=@sales_force_site, port=443)
h = Net::HTTP.new(host, port)
h.use_ssl = true
h
end
def headers(login)
{
'Cookie' => "oid=#{login.org_id.value}; sid=#{login.session_id.value}",
'X-SFDC-Session' => login.session_id.value
}
end
def file_name(url=nil)
datestamp = Date::today.strftime('%Y-%m-%d')
uid_string = url ? "-#{/.*fileName=(.*)\.ZIP.*/.match(url)[1]}" : ''
"salesforce-#{datestamp}#{uid_string}.ZIP"
end
def progress_percentage(current, total)
((current.to_f/total.to_f)*(100.to_f)).to_i
end
### Salesforce interactions ###
def login
puts "Logging in..."
path = '/services/Soap/u/28.0'
pwd_token_encoded = @sales_force_passwd_and_sec_token.gsub(/&(?!amp;)/,'&')
inital_data = <<-EOF
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<n1:login xmlns:n1="urn:partner.soap.sforce.com">
<n1:username>#{@sales_force_user_name}</n1:username>
<n1:password>#{pwd_token_encoded}</n1:password>
</n1:login>
</env:Body>
</env:Envelope>
EOF
initial_headers = {
'Content-Type' => 'text/xml; charset=UTF-8',
'SOAPAction' => 'login'
}
resp = http('login.salesforce.com').post(path, inital_data, initial_headers)
if resp.code == '200'
xmldoc = Document.new(resp.body)
return Result.new(xmldoc)
else
raise SfError.new(resp)
end
end
def download_index(login)
puts "Downloading index..."
path = '/servlet/servlet.OrgExport'
data = http.post(path, nil, headers(login))
data.body.strip
end
def get_download_size(login, url)
puts "Getting download size..."
data = http.head(url, headers(login))
data['Content-Length'].to_i
end
def download_file(login, url, expected_size)
printing_interval = 10
interval_type = :percentage
last_printed_value = nil
size = 0
fn = file_name(url)
puts "Downloading #{fn}..."
f = open("#{@data_directory}/#{fn}", "wb")
begin
http.request_get(url, headers(login)) do |resp|
resp.read_body do |segment|
f.write(segment)
size = size + segment.size
last_printed_value = print_progress(size, expected_size, printing_interval, last_printed_value, interval_type)
end
puts "\nFinished downloading #{fn}!"
end
ensure
f.close()
end
raise "Size didn't match. Expected: #{expected_size} Actual: #{size}" unless size == expected_size
end
def print_progress(size, expected_size, interval, previous_printed_interval, interval_type=:seconds)
percent_file_complete = ((size.to_f/expected_size.to_f)*(100.to_f)).to_i
case interval_type
when :percentage
previous_printed_interval ||= 0
current_value = percent_file_complete
when :seconds
previous_printed_interval ||= Time.now.to_i
current_value = Time.now.to_i
end
next_interval = previous_printed_interval + interval
if current_value >= next_interval
timestamp = Time.now.strftime('%Y-%m-%d-%H-%M-%S')
puts "#{timestamp}: #{percent_file_complete}% complete (#{size} of #{expected_size})"
return next_interval
end
return previous_printed_interval
end
### Email ###
def email_success(file_name, size)
subject = "Salesforce backup successfully downloaded"
data = "Salesforce backup saved into #{file_name}, size #{size}"
email(subject, data)
end
def email_failure(url, error_msg)
subject = "Salesforce backup download failed"
data = "Failed to download #{url}. #{error_msg}"
email(subject, data)
end
def email(subject, data)
message = <<END
From: Admin <#{@email_address_from}>
To: Admin <#{@email_address_to}>
Subject: #{subject}
#{data}
END
Net::SMTP.start(@smtp_host) do |smtp|
smtp.send_message message, @email_address_from, @email_address_to
end
end
begin
config_file_path = File.join(File.dirname(__FILE__), 'config.yml')
config_hash = YAML.load_file(config_file_path)
config_hash.each { |name, value| instance_variable_set("@#{name}", value) }
result = login
urls = download_index(result).split("\n")
puts " All urls:"
puts urls
puts ''
unless File.directory?(@data_directory)
FileUtils.mkdir_p(@data_directory)
end
urls.each do |url|
fn = file_name(url)
file_path = "#{@data_directory}/#{fn}"
retry_count = 0
begin
puts "Working on: #{url}"
expected_size = get_download_size(result, url)
puts "Expected size: #{expected_size}"
fs = File.size?(file_path)
if fs && fs == expected_size
puts "File #{fn} exists and is the right size. Skipping."
else
download_file(result, url, expected_size)
email_success(file_path, expected_size)
end
rescue Exception => e
if retry_count < 5
retry_count += 1
puts "Error: #{e}"
puts "Retrying (retry_count of 5)..."
retry
else
email_failure(url, e.to_s)
end
end
end
puts "Done!"
end