scalingo_backups_manager 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0098acfcaa51bcbcd0ab7675849a739bdf6617f970968c975ea6eaf9d218b7dd'
4
+ data.tar.gz: fe08d492ab01f1495b733d5dcbccd1004f1d78ae930ea5db0fb700640f91a969
5
+ SHA512:
6
+ metadata.gz: da90a3650e78a0b00960dbdad25c6493ea475803c31ad7cdb70ea8b86638520c02a65b0d4bbe99272716918eb9f2b73de4a8f79a5dfc276ffe5af51aac1545b1
7
+ data.tar.gz: 7ea91ada14e91ac0e4ec23c73eac3ba929ad3d099aee8463d47d0f4feb2af3a7fae4b4b4e2ab8bbe6045d73eb53e19c610871851dbef73f24deb7cfd3b67e94d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ scalingo-backups-config.yml
13
+ /backups/
14
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kevin@9troisquarts.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in scalingo_backups_manager.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ scalingo_backups_manager (0.3.0)
5
+ httparty (~> 0.18)
6
+ scalingo (~> 3.0)
7
+ thor (~> 1.1)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (6.1.3.2)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ zeitwerk (~> 2.3)
18
+ concurrent-ruby (1.1.8)
19
+ diff-lcs (1.4.4)
20
+ faraday (1.0.1)
21
+ multipart-post (>= 1.2, < 3)
22
+ faraday_middleware (1.0.0)
23
+ faraday (~> 1.0)
24
+ httparty (0.18.1)
25
+ mime-types (~> 3.0)
26
+ multi_xml (>= 0.5.2)
27
+ i18n (1.8.10)
28
+ concurrent-ruby (~> 1.0)
29
+ mime-types (3.3.1)
30
+ mime-types-data (~> 3.2015)
31
+ mime-types-data (3.2021.0225)
32
+ minitest (5.14.4)
33
+ multi_json (1.15.0)
34
+ multi_xml (0.6.0)
35
+ multipart-post (2.1.1)
36
+ rake (12.3.3)
37
+ rspec (3.10.0)
38
+ rspec-core (~> 3.10.0)
39
+ rspec-expectations (~> 3.10.0)
40
+ rspec-mocks (~> 3.10.0)
41
+ rspec-core (3.10.1)
42
+ rspec-support (~> 3.10.0)
43
+ rspec-expectations (3.10.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-mocks (3.10.2)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.10.0)
49
+ rspec-support (3.10.2)
50
+ scalingo (3.0.0)
51
+ activesupport (>= 5, < 7)
52
+ faraday (~> 1.0.1)
53
+ faraday_middleware (~> 1.0.0)
54
+ multi_json (~> 1.0, >= 1.0.3)
55
+ thor (1.1.0)
56
+ tzinfo (2.0.4)
57
+ concurrent-ruby (~> 1.0)
58
+ zeitwerk (2.4.2)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ rake (~> 12.0)
65
+ rspec (~> 3.0)
66
+ scalingo_backups_manager!
67
+
68
+ BUNDLED WITH
69
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Kevin Clercin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # THIS GEM IS A WORK IN PROGRESS
2
+ # Scalingo backups manager
3
+
4
+ This gem allow to download backups of multiple scalingo applications and addons in order to be restore in local database or be send to an SFTP server
5
+
6
+
7
+ ## TODO
8
+
9
+ - Mysql
10
+ - Postgresql
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile (not hosted on Rubygems):
15
+
16
+ ```ruby
17
+ gem 'scalingo_backups_manager', git: "https://github.com/9troisquarts/scalingo_backups_manager"
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install scalingo_backups_manager
27
+
28
+ ## Usage
29
+
30
+ In order to use this gem, you need to define an environment variable named SCALINGO_API_TOKEN which can be created on your [scalingo profile](https://dashboard-prev.osc-fr1.scalingo.com/profile)
31
+
32
+ `bundle exec scalingo_backups_manager`
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/scalingo_backups_manager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/scalingo_backups_manager/blob/master/CODE_OF_CONDUCT.md).
43
+
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the ScalingoBackupsManager project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/scalingo_backups_manager/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "scalingo_backups_manager"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "scalingo_backups_manager"
5
+
6
+ ScalingoBackupsManager::Cli.start(ARGV)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require "scalingo_backups_manager/version"
2
+ require 'scalingo_backups_manager/cli'
3
+ module ScalingoBackupsManager
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,64 @@
1
+ require 'scalingo_backups_manager/configuration'
2
+ require 'scalingo_backups_manager/backup'
3
+
4
+ module ScalingoBackupsManager
5
+
6
+ class Addon
7
+
8
+ attr_accessor :application, :addon, :config
9
+
10
+ DEFAULT_DATABASE_API_ROOT_URL = "https://db-api.osc-fr1.scalingo.com"
11
+
12
+ def self.find(app, id, config: {})
13
+ addon = Configuration.client.addons.find(app.id, id).data
14
+ self.new(app, addon, config: config)
15
+ end
16
+
17
+ def initialize(app, addon, config: {})
18
+ raise "Application must be set" unless app && app.is_a?(ScalingoBackupsManager::Application)
19
+ @application = app
20
+ @addon = addon
21
+ @config = config
22
+ end
23
+
24
+ def database_api_url(options: {})
25
+ return @database_api_url if @database_api_url
26
+ @database_api_url = "#{options[:database_api_root_url] || DEFAULT_DATABASE_API_ROOT_URL}/api/databases/#{id}"
27
+ end
28
+
29
+ def client(options: {})
30
+ return @client if @client
31
+ scalingo = Scalingo::Client.new
32
+ scalingo.authenticate_with(access_token: ENV["SCALINGO_API_TOKEN"])
33
+ response = scalingo.addons.token(application.id, id).data
34
+ if response.try(:[], :token)
35
+ bearer_token = response[:token]
36
+ else
37
+ raise "An error occured during addon authentication"
38
+ end
39
+
40
+ addon_cli_config = Scalingo::Client.new
41
+ addon_cli_config.token = bearer_token
42
+ @client = Scalingo::API::Client.new(database_api_url(options), scalingo: addon_cli_config)
43
+ end
44
+
45
+
46
+ [:id, :resource_id, :addon_provider].each do |attr_name|
47
+ define_method attr_name do
48
+ addon[attr_name]
49
+ end
50
+ end
51
+
52
+ def backups
53
+ response = client.authenticated_connection.get("#{database_api_url}/backups").body
54
+ return [] unless response[:database_backups] && response[:database_backups].size > 0
55
+ bcks = []
56
+ response[:database_backups].each do |backup|
57
+ bcks.push ScalingoBackupsManager::Backup.new(self, backup[:id])
58
+ end
59
+ bcks
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,43 @@
1
+ require 'scalingo_backups_manager/configuration'
2
+
3
+ module ScalingoBackupsManager
4
+ class Application
5
+
6
+ class << self
7
+ def client
8
+ Configuration.client
9
+ end
10
+
11
+ def all
12
+ client.apps.all&.data || []
13
+ end
14
+
15
+ def find(id)
16
+ app = client.apps.find(id)&.data
17
+ self.new(app)
18
+ end
19
+
20
+ end
21
+
22
+ attr_accessor :application
23
+
24
+ def initialize(app)
25
+ @application = app
26
+ end
27
+
28
+ [:id, :name].each do |attr_name|
29
+ define_method attr_name do
30
+ application[attr_name]
31
+ end
32
+ end
33
+
34
+ def client
35
+ self.class.client
36
+ end
37
+
38
+ def addons
39
+ self.class.client.addons.for(application[:id])&.data || []
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ module ScalingoBackupsManager
2
+
3
+ class Backup
4
+ attr_accessor :addon, :id
5
+
6
+ def initialize(add, id)
7
+ @addon = add
8
+ @id = id
9
+ end
10
+
11
+ def download_link
12
+ response = addon.client.authenticated_connection.get("#{addon.database_api_url}/backups/#{id}/archive").body
13
+ return nil unless response[:download_url]
14
+ response[:download_url]
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,129 @@
1
+ require 'thor'
2
+ require 'scalingo_backups_manager/configuration'
3
+ require 'scalingo_backups_manager/application'
4
+ require 'scalingo_backups_manager/addon'
5
+ require 'scalingo_backups_manager/restore/mongodb'
6
+ require 'scalingo_backups_manager/restore/postgres'
7
+ require 'scalingo_backups_manager/restore/mysql'
8
+
9
+ module ScalingoBackupsManager
10
+
11
+ DATABASE_PROVIDER_IDS = %w(mongodb postgresql mysql)
12
+ class Cli < Thor
13
+ desc "install", "It will guide you in the configuration process"
14
+ method_options all: :boolean
15
+ def install
16
+ all = options[:all]
17
+ unless Configuration.file_exists?
18
+ puts "Configuration file not found"
19
+ puts "Creating file..."
20
+ Configuration.create_file
21
+ end
22
+ unless ENV["SCALINGO_API_TOKEN"]
23
+ puts "The environment variable SCALINGO_API_TOKEN is not set, Exiting..."
24
+ return
25
+ end
26
+ configuration = Configuration.new
27
+ applications = ScalingoBackupsManager::Application.all
28
+ if applications.empty?
29
+ puts "You do not have access to any scalingo application"
30
+ return
31
+ end
32
+ if all
33
+ puts "Fetching scalingo app"
34
+ applications.each do |app|
35
+ application = ScalingoBackupsManager::Application.find(app[:id])
36
+ application.addons.each do |addon|
37
+ addon = ScalingoBackupsManager::Addon.find(application, addon[:id])
38
+ configuration.add_addon_to_app(application, addon) if addon.addon_provider[:id] && DATABASE_PROVIDER_IDS.include?(addon.addon_provider[:id])
39
+ end
40
+ end
41
+ else
42
+ application = nil
43
+ while application.nil?
44
+ applications.each_with_index do |application, index|
45
+ puts "#{index + 1} - #{application[:name]}"
46
+ end
47
+ application_choice = ask("Select an application :").to_i
48
+ application = ScalingoBackupsManager::Application.find(applications[application_choice - 1][:id]) if application_choice > 0 && applications[application_choice - 1]
49
+ end
50
+
51
+ addons = application.addons
52
+ if addons.empty?
53
+ puts "This application have no addons"
54
+ return
55
+ end
56
+ addon = nil
57
+ while addon.nil?
58
+ p "#### Selecting #{application.name} addon ####"
59
+ addons.each_with_index do |addon, index|
60
+ puts "#{index + 1} - #{addon[:addon_provider][:name]} #{addon[:plan][:display_name]}"
61
+ end
62
+ addon_choice = ask("Select addon :").to_i
63
+ addon = ScalingoBackupsManager::Addon.find(application, addons[addon_choice - 1][:id]) if addon_choice > 0 && addons[addon_choice - 1]
64
+ end
65
+ configuration.add_addon_to_app(application, addon)
66
+ end
67
+ end
68
+
69
+ desc "download", "Download last backup all of application in configuration"
70
+ method_options application: :string, addon: :string
71
+ def download
72
+ searched_application = options[:application]
73
+ searched_addon = options[:addon]
74
+ configuration = Configuration.new
75
+ unless configuration
76
+ puts "No configuration found, invoking install"
77
+ invoke :install
78
+ end
79
+
80
+ configuration.for_each_addons(searched_application, searched_addon) do |application, addon|
81
+ backups = addon.backups
82
+ next unless backups.size > 0
83
+ backup = backups.first
84
+ download_link = backup.download_link
85
+ if download_link
86
+ path = ("#{addon.config[:path]}" || "backups/#{addon.addon_provider[:id]}") + "/#{Time.now.strftime("%Y%m%d")}.tar.gz"
87
+ if File.exist?(path)
88
+ puts "Backup already download, skipping..."
89
+ else
90
+ system "curl #{download_link} -o #{path} --create-dirs"
91
+ end
92
+ else
93
+ puts "No download link found for #{addon.addon_provider[:id]}, Skipping..."
94
+ end
95
+ end
96
+ end
97
+
98
+ desc "restore", "Restore application backup to database"
99
+ method_option :application, type: :string, aliases: "-A", desc: "Application of addons to restore"
100
+ method_option :addon, :type => :string, aliases: "-a", desc: "Addon to restore"
101
+ method_option :port, type: :string, aliases: "-p", desc: "Port of database"
102
+ method_option :host, type: :string, aliases: "-h", desc: "Host of your database server, useful when you are running your database in docker"
103
+ method_option :remote_database, type: :string, aliases: "-rdb", desc: "Name of remote database to restore"
104
+ method_option :database, type: :string, aliases: "-db", desc: "Name of local database, default is the database set in your database.yml/mongoid.yml"
105
+ method_option :skip_backup_delete, type: :boolean, aliases: "-skip-rm", desc: "Skip the deletion of folder after restore is complete"
106
+ def restore
107
+ invoke :download, [], application: options[:application], addon: options[:addon]
108
+ configuration = Configuration.new
109
+ configuration.for_each_addons(options[:application], options[:addon]) do |application, addon|
110
+ path = ("#{addon.config[:path]}" || "backups/#{addon.addon_provider[:id]}") + "/#{Time.now.strftime("%Y%m%d")}.tar.gz"
111
+ opts = { port: options[:port], host: options[:host], remote_database_name: options[:remote_database], local_database_name: options[:database], skip_rm: options[:skip_backup_delete] }
112
+ case addon.addon_provider[:id]
113
+ when 'mongodb'
114
+ ScalingoBackupsManager::Restore::Mongodb.restore(path, opts)
115
+ when 'postgresql'
116
+ ScalingoBackupsManager::Restore::Postgres.restore(path, opts)
117
+ when 'mysql'
118
+ ScalingoBackupsManager::Restore::Mysql.restore(path, opts)
119
+ else
120
+ puts "Restore of #{addon.addon_provider[:id]} is not handle yet"
121
+ end
122
+ end
123
+ end
124
+
125
+ end
126
+
127
+ private
128
+
129
+ end
@@ -0,0 +1,74 @@
1
+ require 'scalingo'
2
+ require 'yaml'
3
+ require 'scalingo_backups_manager/application'
4
+ require 'scalingo_backups_manager/addon'
5
+
6
+ module ScalingoBackupsManager
7
+
8
+ class Configuration
9
+ attr_accessor :config
10
+ FILE_NAME = "scalingo-backups-config.yml"
11
+
12
+ def self.file_exists?
13
+ File.exist?(FILE_NAME)
14
+ end
15
+
16
+ def self.create_file
17
+ File.open(FILE_NAME, 'w+') do |f|
18
+ f.flock(File::LOCK_EX)
19
+ content = f.read
20
+ f.rewind
21
+ f.write({ region: 'osc-fr-1', apps: []}.to_yaml)
22
+ f.flush
23
+ end
24
+ end
25
+
26
+ def self.client
27
+ return @client if @client
28
+ @client = Scalingo::Client.new
29
+ @client.authenticate_with(access_token: ENV["SCALINGO_API_TOKEN"])
30
+ @client
31
+ end
32
+
33
+ def self.write_config(config)
34
+ File.open(FILE_NAME, "w+") do |f|
35
+ f.flock(File::LOCK_EX)
36
+ f.truncate(0)
37
+ f.write(config.to_yaml)
38
+ f.flush
39
+ end
40
+ end
41
+
42
+ def initialize
43
+ @file = File.open(FILE_NAME)
44
+ unless @file
45
+ puts "Configuration file does not exist"
46
+ return
47
+ end
48
+ @config = YAML.load(@file.read) || { apps: [] }
49
+ end
50
+
51
+ def add_addon_to_app(application, addon)
52
+ @config.deep_merge!({ apps: { "#{application.name}": { id: application.id, addons: { "#{addon.addon_provider[:id]}": { id: addon.id, path: "backups/#{application.name}/#{addon.addon_provider[:id]}/" } } } } })
53
+ self.class.write_config(@config)
54
+ end
55
+
56
+ def for_each_addons(application_uid, addon_uid)
57
+ @config[:apps].each do |application_name, application_config|
58
+ next if application_uid && application_uid != application_name.to_s
59
+ next unless application_config[:id]
60
+ application = ScalingoBackupsManager::Application.find(application_config[:id])
61
+ next unless application_config[:addons] && application_config[:addons].size > 0
62
+ application_config[:addons].each do |addon_name, addon_config|
63
+ next if addon_uid && addon_uid != addon_name.to_s
64
+ next unless addon_config[:id]
65
+ addon = ScalingoBackupsManager::Addon.find(application, addon_config[:id], config: addon_config)
66
+ yield(application, addon) if block_given?
67
+ end
68
+ end
69
+ end
70
+
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,66 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ module ScalingoBackupsManager
4
+ module Restore
5
+ class Mongodb
6
+
7
+ def self.restore(filename, options = {})
8
+ opts = options.reverse_merge({
9
+ database_config_file: 'mongoid.yml',
10
+ env: 'development',
11
+ host: nil,
12
+ remote_database_name: nil,
13
+ local_database_name: nil,
14
+ skip_rm: false
15
+ })
16
+ destination_path = filename.split("/")
17
+ backup_name = destination_path.pop.gsub(".tar.gz", "")
18
+ destination_path = destination_path.join("/") + backup_name + "/"
19
+ p destination_path
20
+ if Dir.exist?(destination_path)
21
+ puts "Unzipped backup is already present, skipping..."
22
+ else
23
+ Dir.mkdir(destination_path) unless Dir.exist?(destination_path)
24
+ untar_cmd = "tar zxvf #{filename} -C #{destination_path}"
25
+ system untar_cmd
26
+ end
27
+
28
+ rails_db_config = YAML.load(ERB.new(File.read("config/#{opts[:database_config_file]}")).result)[opts[:env]]["clients"]["default"]
29
+ config = {
30
+ host: rails_db_config["hosts"].first,
31
+ database: rails_db_config["database"],
32
+ password: rails_db_config["options"]["password"],
33
+ user: rails_db_config["options"]["user"],
34
+ auth_source: rails_db_config["options"]["auth_source"]
35
+ }
36
+
37
+ restore_cmd = "/usr/bin/env mongorestore --drop -h #{opts[:host] || config[:host]}"
38
+ if opts[:local_database_name].present?
39
+ restore_cmd << " --db #{opts[:local_database_name]}"
40
+ end
41
+
42
+ if opts[:remote_database_name].present?
43
+ restore_cmd << " --dir \"#{destination_path}/#{opts[:remote_database_name]}\""
44
+ else
45
+ restore_cmd << " --dir \"#{destination_path}/\""
46
+ end
47
+
48
+ if config[:auth_source].present?
49
+ restore_cmd << " --authenticationDatabase #{config[:auth_source]}"
50
+ end
51
+ if config[:user].present?
52
+ restore_cmd << " -u #{config[:user]}"
53
+ if config[:password].present?
54
+ restore_cmd << " --password"
55
+ restore_cmd << " #{config[:password]}"
56
+ end
57
+ end
58
+
59
+ puts "*** Restoring backup to Mongodb database ***"
60
+ system(restore_cmd)
61
+ FileUtils.rm_r destination_path unless opts[:skip_rm]
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,71 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ module ScalingoBackupsManager
4
+ module Restore
5
+ class Mysql
6
+
7
+ def self.restore(filename, options = {})
8
+ opts = options.reverse_merge({
9
+ database_config_file: 'database.yml',
10
+ env: 'development',
11
+ host: nil,
12
+ remote_database_name: nil,
13
+ local_database_name: nil,
14
+ skip_rm: false,
15
+ port: nil,
16
+ })
17
+ destination_path = filename.split("/")
18
+ backup_name = destination_path.pop.gsub(".tar.gz", "")
19
+ destination_path = destination_path.join("/") + backup_name + "/"
20
+ if Dir.exist?(destination_path)
21
+ puts "Unzipped backup is already present, skipping..."
22
+ else
23
+ unless Dir.exist?(destination_path)
24
+ p "Creating destination folder..."
25
+ Dir.mkdir(destination_path)
26
+ end
27
+ untar_cmd = "tar zxvf #{filename} -C #{destination_path}"
28
+ p "Untar file with command #{untar_cmd}"
29
+ system untar_cmd
30
+ end
31
+
32
+ rails_db_config = YAML.load(ERB.new(File.read("config/#{opts[:database_config_file]}")).result)[opts[:env]]
33
+ config = {
34
+ host: rails_db_config["host"],
35
+ database: rails_db_config["database"],
36
+ password: rails_db_config["password"],
37
+ user: rails_db_config["username"],
38
+ }
39
+
40
+ restore_cmd = "/usr/bin/env mysql -h #{opts[:host] || config[:host]}"
41
+
42
+ if config[:user].present?
43
+ restore_cmd << " -u #{config[:user]}"
44
+ if config[:password].present?
45
+ restore_cmd << " --password="
46
+ restore_cmd << "#{config[:password]}"
47
+ end
48
+ end
49
+
50
+ if config[:port].present? || opts[:port].present?
51
+ restore_cmd << " -P #{opts[:port] || config[:port] || 5432}"
52
+ end
53
+
54
+ restore_cmd << " #{config[:database]}"
55
+
56
+ file_path = Dir["#{destination_path}*.sql"]
57
+ if file_path.empty?
58
+ puts "*** No SQL file found in tar ***"
59
+ return
60
+ end
61
+ restore_cmd << " < #{file_path.first}"
62
+
63
+ puts "*** Restoring backup to mysql database ***"
64
+ puts "Command: #{restore_cmd}"
65
+ system(restore_cmd)
66
+ #FileUtils.rm_r destination_path unless opts[:skip_rm]
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,60 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+ module ScalingoBackupsManager
4
+ module Restore
5
+ class Postgres
6
+
7
+ def self.restore(filename, options = {})
8
+ opts = options.reverse_merge({
9
+ database_config_file: 'database.yml',
10
+ env: 'development',
11
+ host: nil,
12
+ remote_database_name: nil,
13
+ local_database_name: nil,
14
+ skip_rm: false,
15
+ port: nil,
16
+ })
17
+ destination_path = filename.split("/")
18
+ backup_name = destination_path.pop.gsub(".tar.gz", "")
19
+ destination_path = destination_path.join("/") + backup_name + "/"
20
+ p destination_path
21
+ if Dir.exist?(destination_path)
22
+ puts "Unzipped backup is already present, skipping..."
23
+ else
24
+ Dir.mkdir(destination_path) unless Dir.exist?(destination_path)
25
+ untar_cmd = "tar zxvf #{filename} -C #{destination_path}"
26
+ system untar_cmd
27
+ end
28
+
29
+ rails_db_config = YAML.load(ERB.new(File.read("config/#{opts[:database_config_file]}")).result)[opts[:env]]
30
+ config = {
31
+ host: rails_db_config["host"],
32
+ database: rails_db_config["database"],
33
+ password: rails_db_config["password"],
34
+ user: rails_db_config["user"],
35
+ }
36
+
37
+ restore_cmd = "/usr/bin/env psql #{config[:database]} -h #{opts[:host] || config[:host]}"
38
+
39
+ if config[:user].present?
40
+ restore_cmd << " --u #{config[:user]}"
41
+ if config[:password].present?
42
+ restore_cmd << " --password"
43
+ restore_cmd << " #{config[:password]}"
44
+ end
45
+ end
46
+
47
+ if config[:port].present?
48
+ restore_cmd << " -p #{opts[:port] || config[:port] || 5432}"
49
+ end
50
+
51
+ restore_cmd << " < #{destination_path}"
52
+
53
+ puts "*** Restoring backup to Postgres database ***"
54
+ system(restore_cmd)
55
+ FileUtils.rm_r destination_path unless opts[:skip_rm]
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module ScalingoBackupsManager
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/scalingo_backups_manager/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "scalingo_backups_manager"
5
+ spec.version = ScalingoBackupsManager::VERSION
6
+ spec.authors = ["Kevin Clercin"]
7
+ spec.email = ["kevin@9troisquarts.com"]
8
+
9
+ spec.summary = %q{Gem allowing to download backups from scalingo}
10
+ spec.homepage = "https://github.com/9troisquarts/scalingo_backups_manager"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/9troisquarts/scalingo_backups_manager"
16
+ spec.metadata["changelog_uri"] = "https://github.com/9troisquarts/scalingo_backups_manager/CHANGELOG.md"
17
+
18
+ spec.add_dependency "thor", '~> 1.1'
19
+ spec.add_dependency 'httparty', "~> 0.18"
20
+ spec.add_dependency 'scalingo', '~> 3.0'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "bin"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.executables << "scalingo_backups_manager"
30
+ spec.require_paths = ["lib"]
31
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scalingo_backups_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Clercin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.18'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.18'
41
+ - !ruby/object:Gem::Dependency
42
+ name: scalingo
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - kevin@9troisquarts.com
58
+ executables:
59
+ - scalingo_backups_manager
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/scalingo_backups_manager
74
+ - bin/setup
75
+ - lib/scalingo_backups_manager.rb
76
+ - lib/scalingo_backups_manager/addon.rb
77
+ - lib/scalingo_backups_manager/application.rb
78
+ - lib/scalingo_backups_manager/backup.rb
79
+ - lib/scalingo_backups_manager/cli.rb
80
+ - lib/scalingo_backups_manager/configuration.rb
81
+ - lib/scalingo_backups_manager/restore/mongodb.rb
82
+ - lib/scalingo_backups_manager/restore/mysql.rb
83
+ - lib/scalingo_backups_manager/restore/postgres.rb
84
+ - lib/scalingo_backups_manager/version.rb
85
+ - scalingo_backups_manager.gemspec
86
+ homepage: https://github.com/9troisquarts/scalingo_backups_manager
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: https://github.com/9troisquarts/scalingo_backups_manager
91
+ source_code_uri: https://github.com/9troisquarts/scalingo_backups_manager
92
+ changelog_uri: https://github.com/9troisquarts/scalingo_backups_manager/CHANGELOG.md
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.3.0
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Gem allowing to download backups from scalingo
112
+ test_files: []