scalingo_backups_manager 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0098acfcaa51bcbcd0ab7675849a739bdf6617f970968c975ea6eaf9d218b7dd'
4
- data.tar.gz: fe08d492ab01f1495b733d5dcbccd1004f1d78ae930ea5db0fb700640f91a969
3
+ metadata.gz: 0c13c0008cbcd861729fbbc5cb43a3b53ed63db136a2b24393b077c2fbb6b50e
4
+ data.tar.gz: d93398d512fe1d0e9fdf964e431fc5c570c3201c5475e51174d3e7f297ef02c8
5
5
  SHA512:
6
- metadata.gz: da90a3650e78a0b00960dbdad25c6493ea475803c31ad7cdb70ea8b86638520c02a65b0d4bbe99272716918eb9f2b73de4a8f79a5dfc276ffe5af51aac1545b1
7
- data.tar.gz: 7ea91ada14e91ac0e4ec23c73eac3ba929ad3d099aee8463d47d0f4feb2af3a7fae4b4b4e2ab8bbe6045d73eb53e19c610871851dbef73f24deb7cfd3b67e94d
6
+ metadata.gz: 26cc222b56f4199eb7a1bd06f1c9e787d53a5f3a2a979836f991fc368369252c53cfeb7cbbd457882e4bce32aa07f09dcf8813a5d73a9007de4e958bfefe94fd
7
+ data.tar.gz: 0577310f2c5cceef264cd75789572577b7c218ff536cdb878a2ab40e6190f9e5d6b4e429587e596e7f6c71b7f1b787f431dbe4c0d7eee6a04c94a795fcd72a3f
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
+
data/Gemfile.lock CHANGED
@@ -3,6 +3,7 @@ PATH
3
3
  specs:
4
4
  scalingo_backups_manager (0.3.0)
5
5
  httparty (~> 0.18)
6
+ net-sftp (~> 3.0.0)
6
7
  scalingo (~> 3.0)
7
8
  thor (~> 1.1)
8
9
 
@@ -33,6 +34,9 @@ GEM
33
34
  multi_json (1.15.0)
34
35
  multi_xml (0.6.0)
35
36
  multipart-post (2.1.1)
37
+ net-sftp (3.0.0)
38
+ net-ssh (>= 5.0.0, < 7.0.0)
39
+ net-ssh (6.1.0)
36
40
  rake (12.3.3)
37
41
  rspec (3.10.0)
38
42
  rspec-core (~> 3.10.0)
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
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
5
 
6
-
7
6
  ## TODO
8
7
 
9
8
  - Mysql
@@ -5,19 +5,20 @@ module ScalingoBackupsManager
5
5
 
6
6
  class Addon
7
7
 
8
- attr_accessor :application, :addon, :config
8
+ attr_accessor :application, :addon, :config, :sftp_config
9
9
 
10
10
  DEFAULT_DATABASE_API_ROOT_URL = "https://db-api.osc-fr1.scalingo.com"
11
11
 
12
- def self.find(app, id, config: {})
12
+ def self.find(app, id, config: {}, sftp_config: {})
13
13
  addon = Configuration.client.addons.find(app.id, id).data
14
- self.new(app, addon, config: config)
14
+ self.new(app, addon, config: config, sftp_config: sftp_config)
15
15
  end
16
16
 
17
- def initialize(app, addon, config: {})
17
+ def initialize(app, addon, config: {}, sftp_config: {})
18
18
  raise "Application must be set" unless app && app.is_a?(ScalingoBackupsManager::Application)
19
19
  @application = app
20
20
  @addon = addon
21
+ @sftp_config = sftp_config
21
22
  @config = config
22
23
  end
23
24
 
@@ -5,6 +5,7 @@ require 'scalingo_backups_manager/addon'
5
5
  require 'scalingo_backups_manager/restore/mongodb'
6
6
  require 'scalingo_backups_manager/restore/postgres'
7
7
  require 'scalingo_backups_manager/restore/mysql'
8
+ require 'scalingo_backups_manager/sftp_tools'
8
9
 
9
10
  module ScalingoBackupsManager
10
11
 
@@ -83,6 +84,7 @@ module ScalingoBackupsManager
83
84
  backup = backups.first
84
85
  download_link = backup.download_link
85
86
  if download_link
87
+ puts "Downloading #{application.name} last backup"
86
88
  path = ("#{addon.config[:path]}" || "backups/#{addon.addon_provider[:id]}") + "/#{Time.now.strftime("%Y%m%d")}.tar.gz"
87
89
  if File.exist?(path)
88
90
  puts "Backup already download, skipping..."
@@ -122,6 +124,46 @@ module ScalingoBackupsManager
122
124
  end
123
125
  end
124
126
 
127
+ desc "upload_to_ftp", "Upload last backup to FTP"
128
+ def upload_to_ftp
129
+ invoke :download, [], application: options[:application], addon: options[:addon]
130
+ configuration = Configuration.new
131
+ configuration.for_each_addons do |application, addon|
132
+ config = addon.sftp_config
133
+ path = ("#{addon.config[:path]}" || "backups/#{addon.addon_provider[:id]}") + "/#{Time.now.strftime("%Y%m%d")}.tar.gz"
134
+ next unless File.exists?(path)
135
+
136
+ sftp = ScalingoBackupsManager::SftpTools.new(config[:auth])
137
+
138
+ folders = [
139
+ config.dig(:auth, :dir),
140
+ config.dig(:dir) || application.name,
141
+ addon.addon_provider[:id]
142
+ ]
143
+
144
+ if config[:retention].blank?
145
+ remote_path = "/" + [folders].delete_if(&:blank?).join("/")
146
+ sftp.mkdir!(remote_path)
147
+ sftp.upload_file(path, remote_path)
148
+ next
149
+ end
150
+
151
+ config[:retention].each do |k, v|
152
+ folders << config.dig(:retention, k, :dir)
153
+ remote_path = "/" + [folders].delete_if(&:blank?).join("/")
154
+ sftp.mkdir!(remote_path)
155
+ case k
156
+ when "daily"
157
+ sftp.upload_file(path, remote_path)
158
+ when "monthly"
159
+ next unless Date.today.day == 1
160
+ sftp.upload_file(path, remote_path)
161
+ end
162
+ end
163
+
164
+ end
165
+ end
166
+
125
167
  end
126
168
 
127
169
  private
@@ -16,7 +16,6 @@ module ScalingoBackupsManager
16
16
  def self.create_file
17
17
  File.open(FILE_NAME, 'w+') do |f|
18
18
  f.flock(File::LOCK_EX)
19
- content = f.read
20
19
  f.rewind
21
20
  f.write({ region: 'osc-fr-1', apps: []}.to_yaml)
22
21
  f.flush
@@ -34,7 +33,7 @@ module ScalingoBackupsManager
34
33
  File.open(FILE_NAME, "w+") do |f|
35
34
  f.flock(File::LOCK_EX)
36
35
  f.truncate(0)
37
- f.write(config.to_yaml)
36
+ f.write(config.to_hash.to_yaml)
38
37
  f.flush
39
38
  end
40
39
  end
@@ -45,7 +44,7 @@ module ScalingoBackupsManager
45
44
  puts "Configuration file does not exist"
46
45
  return
47
46
  end
48
- @config = YAML.load(@file.read) || { apps: [] }
47
+ @config = (YAML.load(@file.read) || { apps: [] }).with_indifferent_access
49
48
  end
50
49
 
51
50
  def add_addon_to_app(application, addon)
@@ -53,16 +52,21 @@ module ScalingoBackupsManager
53
52
  self.class.write_config(@config)
54
53
  end
55
54
 
56
- def for_each_addons(application_uid, addon_uid)
55
+ def for_each_addons(application_uid = nil, addon_uid = nil)
56
+ client_sftp_config = @config[:sftp] || {}
57
+
57
58
  @config[:apps].each do |application_name, application_config|
58
59
  next if application_uid && application_uid != application_name.to_s
59
60
  next unless application_config[:id]
60
61
  application = ScalingoBackupsManager::Application.find(application_config[:id])
61
62
  next unless application_config[:addons] && application_config[:addons].size > 0
63
+
64
+ app_sftp_config = (application_config[:sftp] || {}).reverse_merge(client_sftp_config)
65
+
62
66
  application_config[:addons].each do |addon_name, addon_config|
63
67
  next if addon_uid && addon_uid != addon_name.to_s
64
68
  next unless addon_config[:id]
65
- addon = ScalingoBackupsManager::Addon.find(application, addon_config[:id], config: addon_config)
69
+ addon = ScalingoBackupsManager::Addon.find(application, addon_config[:id], config: addon_config, sftp_config: (addon_config[:sftp] || {}).reverse_merge(app_sftp_config))
66
70
  yield(application, addon) if block_given?
67
71
  end
68
72
  end
@@ -0,0 +1,40 @@
1
+ require 'net/sftp'
2
+
3
+ module ScalingoBackupsManager
4
+ class SftpTools
5
+ attr_accessor :ftp_host
6
+
7
+ def initialize(ftp_host)
8
+ @ftp_host = ftp_host
9
+ end
10
+
11
+ def start
12
+ Net::SFTP.start(@ftp_host[:host], @ftp_host[:user], password: @ftp_host[:password], port: @ftp_host[:port]) do |sftp|
13
+ yield(sftp) if block_given?
14
+ end
15
+ end
16
+
17
+ def mkdir!(path)
18
+ start do |sftp|
19
+ folder_tree = []
20
+ path.split("/").each do |folder_name|
21
+ next if folder_name.blank?
22
+
23
+ folder_tree << folder_name
24
+ begin
25
+ sftp.mkdir!(folder_tree.join("/"))
26
+ rescue
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def upload_file(filepath, remote_dir)
33
+ filename = filepath.split('/').last
34
+ start do |sftp|
35
+ sftp.upload!(filepath, "#{remote_dir}/#{filename}")
36
+ end
37
+ end
38
+ end
39
+
40
+ end
@@ -1,3 +1,3 @@
1
1
  module ScalingoBackupsManager
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.add_dependency "thor", '~> 1.1'
19
19
  spec.add_dependency 'httparty', "~> 0.18"
20
20
  spec.add_dependency 'scalingo', '~> 3.0'
21
+ spec.add_dependency 'net-sftp', '~> 3.0.0'
21
22
 
22
23
  # Specify which files should be added to the gem when it is released.
23
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scalingo_backups_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Clercin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-31 00:00:00.000000000 Z
11
+ date: 2021-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: net-sftp
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
55
69
  description:
56
70
  email:
57
71
  - kevin@9troisquarts.com
@@ -81,6 +95,7 @@ files:
81
95
  - lib/scalingo_backups_manager/restore/mongodb.rb
82
96
  - lib/scalingo_backups_manager/restore/mysql.rb
83
97
  - lib/scalingo_backups_manager/restore/postgres.rb
98
+ - lib/scalingo_backups_manager/sftp_tools.rb
84
99
  - lib/scalingo_backups_manager/version.rb
85
100
  - scalingo_backups_manager.gemspec
86
101
  homepage: https://github.com/9troisquarts/scalingo_backups_manager