app-tools 1.16.2 → 1.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/mongo-backup +111 -58
- data/lib/app_tools/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b1349b6e56f652dd2e10565c9fe956cde148239
|
4
|
+
data.tar.gz: 0f08c79203bcca5b91a5b66866478a8af38eea88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c3669c778ab3acc2ac98c2e88a927f44c38d3976b23b3a8b46b5ea1dd1e42be8fd493983ceb26d30b03c54f4a404c24e9a0b3f21a2bc4c4ebf3440c7a665b58
|
7
|
+
data.tar.gz: 7fe90f20aa1972ba86f5882d6d9b2fc8ce01ec4a9ed5a846e5fb61fb1cd0416a64a9871de7d6fbec67cf75d84cdf13a2db3e1d518c471458279964b9f2e1d9d2
|
data/bin/mongo-backup
CHANGED
@@ -6,6 +6,7 @@ require 'app_tools/version'
|
|
6
6
|
require 'fileutils'
|
7
7
|
require 'mongo'
|
8
8
|
require 'tmpdir'
|
9
|
+
require 'slack-ruby-client'
|
9
10
|
|
10
11
|
module AppTools
|
11
12
|
module MongoBackup
|
@@ -29,6 +30,8 @@ module AppTools
|
|
29
30
|
bucket_name = options[:bucket]
|
30
31
|
max_backups = options[:max].to_i
|
31
32
|
mongo_path = options[:mongo]
|
33
|
+
slack_api_token = options[:api]
|
34
|
+
slack_channel = options[:channel]
|
32
35
|
|
33
36
|
if profile.nil?
|
34
37
|
exit_now! "Must specify AWS profile"
|
@@ -47,6 +50,10 @@ module AppTools
|
|
47
50
|
exit_now! "Must specify a database to backup!"
|
48
51
|
end
|
49
52
|
|
53
|
+
if slack_api_token.nil? or slack_channel.nil?
|
54
|
+
puts "WARNING: No Slack API and/or channel token given, no notification will be sent"
|
55
|
+
end
|
56
|
+
|
50
57
|
mongo_uri = Mongo::URI.new(mongo_path)
|
51
58
|
database = mongo_uri.database
|
52
59
|
user = mongo_uri.credentials[:user]
|
@@ -58,67 +65,111 @@ module AppTools
|
|
58
65
|
scutil_tool = find_tool('scutil', ['/usr/sbin'])
|
59
66
|
tar_tool = find_tool('tar', ['/usr/bin'])
|
60
67
|
|
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
|
-
|
68
|
+
backup_status = :failed
|
69
|
+
|
70
|
+
Dir.mktmpdir do |dump_dir_name|
|
71
|
+
begin
|
72
|
+
local_host_name = `#{scutil_tool} --get LocalHostName`.strip
|
73
|
+
|
74
|
+
puts "Backing up mongodb://#{mongo_server}/#{database} from #{local_host_name}"
|
75
|
+
|
76
|
+
if !(user.nil? and password.nil?)
|
77
|
+
`#{mongodump_tool} -h #{mongo_server} -u #{user} -p #{password} -d #{database} -o #{dump_dir_name}`
|
78
|
+
else
|
79
|
+
`#{mongodump_tool} -h #{mongo_server} -d #{database} -o #{dump_dir_name}`
|
80
|
+
end
|
81
|
+
|
82
|
+
if $? != 0
|
83
|
+
raise "Unable to create dump of mongodb://#{mongo_server}/#{database}"
|
84
|
+
end
|
85
|
+
|
86
|
+
date_time = DateTime.now.strftime("%Y%m%d-%H%M%SZ")
|
87
|
+
|
88
|
+
backup_filename = "#{local_host_name}-#{database}-#{date_time}.tar.gz"
|
89
|
+
|
90
|
+
`cd #{dump_dir_name}; #{tar_tool} -czvf #{backup_filename} #{database}/*`
|
91
|
+
|
92
|
+
if $? != 0
|
93
|
+
raise "Unable to create tar zip file '#{backup_filename}'"
|
94
|
+
end
|
95
|
+
|
96
|
+
`cd #{dump_dir_name}; #{aws_tool} s3 cp #{backup_filename} s3://#{bucket_name}/ --profile #{profile}`
|
97
|
+
|
98
|
+
if $? != 0
|
99
|
+
raise "Unable to upload '#{backup_filename}' to s3://#{bucket_name}/"
|
100
|
+
end
|
101
|
+
|
102
|
+
# Reduce backups to desired maximum
|
103
|
+
backup_filenames = `#{aws_tool} s3 ls s3://#{bucket_name} --profile #{profile}`.split('\n')
|
104
|
+
|
105
|
+
if $? != 0
|
106
|
+
raise "Unable to get list of existing backups from s3://#{bucket_name}"
|
107
|
+
end
|
108
|
+
|
109
|
+
# backup_filenames = %w(
|
110
|
+
# 'host-database-20160624-002055Z.tar.gz',
|
111
|
+
# 'host-database-20160620-002055Z.tar.gz',
|
112
|
+
# 'host-database-20160621-002055Z.tar.gz',
|
113
|
+
# 'host-database-20160708-002055Z.tar.gz',
|
114
|
+
# 'host-database-20160601-002055Z.tar.gz.save',
|
115
|
+
# )
|
116
|
+
backup_filenames.sort!
|
117
|
+
backup_filenames.delete_if { |f| f.match(/save/) }
|
118
|
+
num_backups_to_delete = backup_filenames.count - max_backups
|
119
|
+
|
120
|
+
if num_backups_to_delete > 0
|
121
|
+
(0...num_backups_to_delete).each { |i|
|
122
|
+
`#{aws_tool} s3 rm s3://#{bucket_name}/#{backup_filenames[i]} --profile #{profile}`
|
123
|
+
|
124
|
+
if $? != 0
|
125
|
+
puts "WARNING: Unable to delete s3://#{bucket_name}/#{backup_filenames[i]}"
|
126
|
+
end
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
backup_status = :succeeded
|
131
|
+
ensure
|
132
|
+
Slack.configure do |config|
|
133
|
+
config.token = slack_api_token
|
134
|
+
end
|
135
|
+
|
136
|
+
unless slack_api_token.nil? or slack_channel.nil?
|
137
|
+
if backup_status == :succeeded
|
138
|
+
message = "MongoDB backup complete"
|
139
|
+
attachments = [
|
140
|
+
{
|
141
|
+
"color": "good",
|
142
|
+
"title": "MongoDB Backup Succeeded",
|
143
|
+
"text": "A backup of database mongodb://#{mongo_server}/#{database} was made from #{local_host_name} to s3://#{bucket_name}/#{backup_filename}",
|
144
|
+
"ts": Time.now.utc.to_i
|
145
|
+
}
|
146
|
+
]
|
147
|
+
else
|
148
|
+
message = "MongoDB backup failed"
|
149
|
+
attachments = [
|
150
|
+
{
|
151
|
+
"color": "danger",
|
152
|
+
"title": "MongoDB Backup Failed",
|
153
|
+
"text": "A backup of database mongodb://#{mongo_server}/#{database} from #{local_host_name} failed.",
|
154
|
+
"ts": Time.now.utc.to_i
|
155
|
+
}
|
156
|
+
]
|
157
|
+
end
|
158
|
+
begin
|
159
|
+
slack = Slack::Web::Client.new
|
160
|
+
slack.chat_postMessage(channel: slack_channel, text: message, attachments: attachments, as_user: true)
|
161
|
+
rescue
|
162
|
+
puts "Unable to send Slack message to #{slack_channel}"
|
116
163
|
end
|
117
|
-
|
164
|
+
end
|
118
165
|
end
|
119
|
-
|
166
|
+
end
|
120
167
|
|
121
|
-
|
168
|
+
if backup_status == :succeeded
|
169
|
+
puts "Backup was successful"
|
170
|
+
else
|
171
|
+
puts "Backup failed"
|
172
|
+
end
|
122
173
|
end
|
123
174
|
|
124
175
|
description 'mongo-backup - Perform backup for a MongoDB database'
|
@@ -128,6 +179,8 @@ module AppTools
|
|
128
179
|
on("-b", "--bucket BUCKET_NAME", "AWS bucket")
|
129
180
|
on("-n", "--max MAX_BACKUPS", "Maximum number of backups to keep")
|
130
181
|
on("-m", "--mongo MONGO_URI", "Full URI of MongoDB to backup")
|
182
|
+
on("-t", "--api SLACK_API_TOKEN", "Slack API token")
|
183
|
+
on("-c", "--channel SLACK_CHANNEL", "Slack channel")
|
131
184
|
|
132
185
|
use_log_level_option :toggle_debug_on_signal => 'USR1'
|
133
186
|
|
data/lib/app_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: app-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.17.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Lyon-Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '2.3'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: slack-ruby-client
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.7'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.7'
|
139
153
|
description: |
|
140
154
|
Generate IPA files with correct Swift and symbol files for uploading to iTunesConnect.
|
141
155
|
Resign IPA files with correct certificate and provisioning profiles for distribution. Upload IPA files to iTunesConnect.
|