scalingo_databases_rake_tasks 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +39 -0
- data/Gemfile +4 -0
- data/README.md +65 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/scalingo_databases_rake_tasks.rb +18 -0
- data/lib/scalingo_databases_rake_tasks/railtie.rb +12 -0
- data/lib/scalingo_databases_rake_tasks/tasks/common.rake +93 -0
- data/lib/scalingo_databases_rake_tasks/tasks/mongodb.rake +106 -0
- data/lib/scalingo_databases_rake_tasks/tasks/mysql.rake +87 -0
- data/lib/scalingo_databases_rake_tasks/tasks/postgresql.rake +90 -0
- data/lib/scalingo_databases_rake_tasks/version.rb +3 -0
- data/scalingo_databases_rake_tasks.gemspec +23 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9375caa0de1e6e6f5f2a7c0c2ac4d0a5a09a6357
|
4
|
+
data.tar.gz: 6dc2b750f6ddb3c52d8af993eb71c3aa57cfc12d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4eabd1086dffe6a0afe83076e204debf67af3d2ed3e410dbc6d790ffc41345ec6765f996660d18b89d3ac350f89036355476f149aa61c36b39b0890531bd6787
|
7
|
+
data.tar.gz: f6b6d9ac88615b1a2130b5221a7a15c21f0dcf5bd1289e6e908c1975fda66891f437a5160aea337bdc66d4873d46a82c5d664d9874df0440001dffbd85521910
|
data/.gitignore
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Created by https://www.gitignore.io/api/ruby
|
2
|
+
|
3
|
+
### Ruby ###
|
4
|
+
*.gem
|
5
|
+
*.rbc
|
6
|
+
/.config
|
7
|
+
/coverage/
|
8
|
+
/InstalledFiles
|
9
|
+
/pkg/
|
10
|
+
/spec/reports/
|
11
|
+
/spec/examples.txt
|
12
|
+
/test/tmp/
|
13
|
+
/test/version_tmp/
|
14
|
+
/tmp/
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
|
21
|
+
## Documentation cache and generated files:
|
22
|
+
/.yardoc/
|
23
|
+
/_yardoc/
|
24
|
+
/doc/
|
25
|
+
/rdoc/
|
26
|
+
|
27
|
+
## Environment normalization:
|
28
|
+
/.bundle/
|
29
|
+
/vendor/bundle
|
30
|
+
/lib/bundler/man/
|
31
|
+
|
32
|
+
# for a library or gem, you might want to ignore these files since the code is
|
33
|
+
# intended to run in multiple environments; otherwise, check them in:
|
34
|
+
Gemfile.lock
|
35
|
+
.ruby-version
|
36
|
+
.ruby-gemset
|
37
|
+
|
38
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
39
|
+
.rvmrc
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# ScalingoDatabasesRakeTasks
|
2
|
+
|
3
|
+
A gem providing rake tasks such as **backup** and **restore** for database manipulations on [Scalingo](https://scalingo.com/).
|
4
|
+
|
5
|
+
Currently supported databases:
|
6
|
+
- MongoDB
|
7
|
+
- MySQL
|
8
|
+
- PostgreSQL
|
9
|
+
|
10
|
+
Available tasks for **each** database:
|
11
|
+
- `backup_local`: make a backup of local database
|
12
|
+
- `backup_remote`: make a backup of remote Scalingo database
|
13
|
+
- `restore_local`: restore a local database with an archive
|
14
|
+
- `restore_remote`: restore a remote Scalingo database with an archive
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
For remote operations you will have to set your shell's environment variable `APP` as your app name on Scalingo. The variable `DB_ENV_NAME` is optional, by default it will be the one generated when you provisionned the database addon. For example, if your database is a MongoDB then the variable will be `SCALINGO_MONGO_URL`.
|
19
|
+
|
20
|
+
### Commands
|
21
|
+
|
22
|
+
To see the complete list of tasks: `rake -T scalingo`
|
23
|
+
|
24
|
+
Example of commands for MongoDB:
|
25
|
+
- `rake scalingo:mongodb:backup_local`
|
26
|
+
- `rake scalingo:mongodb:backup_remote`
|
27
|
+
- `rake scalingo:mongodb:restore_local`
|
28
|
+
- `rake scalingo:mongodb:restore_remote`
|
29
|
+
|
30
|
+
### Backup and Restore
|
31
|
+
|
32
|
+
Backups are stored under the `tmp` folder of your project, the database type is part of the archive name (e.g. scalingo_mongodb_dump.tar.gz).
|
33
|
+
|
34
|
+
To restore from a specific archive, you'll have to give it the default archive name and put it inside of `tmp` folder before running the rake command.
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
gem 'scalingo_databases_rake_tasks'
|
42
|
+
```
|
43
|
+
|
44
|
+
And then execute:
|
45
|
+
|
46
|
+
$ bundle
|
47
|
+
|
48
|
+
Or install it yourself as:
|
49
|
+
|
50
|
+
$ gem install scalingo_databases_rake_tasks
|
51
|
+
|
52
|
+
For Rails apps nothing to do.
|
53
|
+
|
54
|
+
For other apps, add `require "scalingo_databases_rake_tasks"` to your Rakefile.
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
59
|
+
|
60
|
+
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).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Scalingo/scalingo_databases_rake_tasks.
|
65
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "scalingo_databases_rake_tasks"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "scalingo_databases_rake_tasks/version"
|
2
|
+
require "rake"
|
3
|
+
|
4
|
+
module ScalingoDbTasks
|
5
|
+
include Rake::DSL if defined? Rake::DSL
|
6
|
+
def self.install_tasks
|
7
|
+
tasks = ["common", "mongodb", "mysql", "postgresql"]
|
8
|
+
tasks.each { |type| load("scalingo_databases_rake_tasks/tasks/#{type}.rake") }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# If we are in a Rails app use Railtie to load tasks.
|
13
|
+
# Otherwise the user has to add `require "scalingo_databases_rake_tasks"` in Rakefile
|
14
|
+
if defined?(Rails)
|
15
|
+
require "scalingo_databases_rake_tasks/railtie"
|
16
|
+
else
|
17
|
+
ScalingoDbTasks::install_tasks
|
18
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
namespace :scalingo do
|
5
|
+
private
|
6
|
+
|
7
|
+
def confirm_remote(database)
|
8
|
+
print("You're about to restore your remote database #{database}.\nData will be destroyed. (y/N) ")
|
9
|
+
input = STDIN.gets.strip.downcase
|
10
|
+
if input != 'y'
|
11
|
+
abort
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def archive_name name = "dump"
|
16
|
+
"#{Rails.root}/tmp/#{name}.tar.gz"
|
17
|
+
end
|
18
|
+
|
19
|
+
def make_tmp_dir
|
20
|
+
FileUtils.mkdir_p 'tmp'
|
21
|
+
end
|
22
|
+
|
23
|
+
def remote_credentials app, variable
|
24
|
+
output = `scalingo -a #{app} env | grep "^#{variable}=" | cut -d '=' -f2 | tr -d '\n'`
|
25
|
+
uri = URI(output.strip)
|
26
|
+
if uri.to_s.blank?
|
27
|
+
raise VariableError
|
28
|
+
else
|
29
|
+
[uri.path[1..-1], uri.user, uri.password]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def start_scalingo_tunnel app, variable
|
34
|
+
cmd = "scalingo -a #{app} db-tunnel -p 27717 #{variable}"
|
35
|
+
puts "*** Executing #{cmd}"
|
36
|
+
i, o, thr = Open3::pipeline_rw cmd
|
37
|
+
|
38
|
+
while true
|
39
|
+
read_line(o) do |line|
|
40
|
+
# puts line # debug
|
41
|
+
if line.include?("Encrypted")
|
42
|
+
abort "*** Your SSH key is encrypted. This gem is only compatible with SSH agents or unencrypted keys."
|
43
|
+
end
|
44
|
+
|
45
|
+
if line.include?("address already in use")
|
46
|
+
abort "*** Address 127.0.0.1:27717 is already in use."
|
47
|
+
end
|
48
|
+
|
49
|
+
if line.include?("'127.0.0.1:27717'")
|
50
|
+
return thr[0].pid
|
51
|
+
elsif line.include?("'127.0.0.1:")
|
52
|
+
abort "*** Address 127.0.0.1:27717 is already in use."
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def read_line out
|
59
|
+
line = ""
|
60
|
+
while line == ""
|
61
|
+
sleep 0.2
|
62
|
+
begin
|
63
|
+
line = out.read_nonblock(200)
|
64
|
+
yield(line)
|
65
|
+
rescue EOFError, IO::EAGAINWaitReadable ; end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def open_tunnel default_env_name
|
70
|
+
app = ENV["APP"]
|
71
|
+
variable = ENV["DB_ENV_NAME"] || default_env_name
|
72
|
+
unless app
|
73
|
+
abort 'ENV["APP"] missing.'
|
74
|
+
end
|
75
|
+
unless variable
|
76
|
+
abort 'ENV["DB_ENV_NAME"] missing.'
|
77
|
+
end
|
78
|
+
|
79
|
+
database, user, password = remote_credentials(app, variable)
|
80
|
+
pid = start_scalingo_tunnel(app, variable)
|
81
|
+
at_exit do
|
82
|
+
Process.kill("INT", pid) if pid != -1
|
83
|
+
puts '*** Tunnel closed'
|
84
|
+
end
|
85
|
+
puts '*** Tunnel opened'
|
86
|
+
yield(database, user, password)
|
87
|
+
end
|
88
|
+
|
89
|
+
class VariableError < StandardError
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
namespace :scalingo do
|
2
|
+
namespace :mongodb do
|
3
|
+
desc "Backup local MongoDB database"
|
4
|
+
task :backup_local => :environment do
|
5
|
+
database, user, password, host = ScalingoMongoDB.local_credentials ENV['FILE']
|
6
|
+
ScalingoMongoDB.backup(database, user, password, host)
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Backup remote Scalingo MongoDB database"
|
10
|
+
task :backup_remote => :environment do
|
11
|
+
open_tunnel("SCALINGO_MONGO_URL") do |database, user, password|
|
12
|
+
ScalingoMongoDB.backup(database, user, password, "127.0.0.1:27717")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Restore local MongoDB database using a Scalingo backup"
|
17
|
+
task :restore_local => :environment do
|
18
|
+
database, user, password, host = ScalingoMongoDB.local_credentials ENV['FILE']
|
19
|
+
ScalingoMongoDB.restore(database, user, password, host)
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Restore remote Scalingo MongoDB database using local backup"
|
23
|
+
task :restore_remote => :environment do
|
24
|
+
open_tunnel("SCALINGO_MONGO_URL") do |database, user, password|
|
25
|
+
confirm_remote(database)
|
26
|
+
ScalingoMongoDB.restore(database, user, password, "127.0.0.1:27717")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
module ScalingoMongoDB
|
33
|
+
DUMP_NAME = "scalingo_mongodb_dump"
|
34
|
+
DUMP_PATH = Dir.tmpdir() + "/#{DUMP_NAME}"
|
35
|
+
|
36
|
+
def self.local_credentials(filename)
|
37
|
+
filename ||= "mongoid"
|
38
|
+
result = File.read "#{Rails.root}/config/#{filename}.yml"
|
39
|
+
config_file = YAML::load(ERB.new(result).result)
|
40
|
+
|
41
|
+
if config_file[Rails.env]['sessions']['default']['uri']
|
42
|
+
require 'uri'
|
43
|
+
uri = URI.parse config_file[Rails.env]['sessions']['default']['uri']
|
44
|
+
|
45
|
+
return [
|
46
|
+
uri.path[1..-1],
|
47
|
+
uri.user,
|
48
|
+
uri.password,
|
49
|
+
(uri.host || "127.0.0.1")
|
50
|
+
]
|
51
|
+
else
|
52
|
+
return [
|
53
|
+
config_file[Rails.env]['sessions']['default']['database'],
|
54
|
+
config_file[Rails.env]['sessions']['default']['username'],
|
55
|
+
config_file[Rails.env]['sessions']['default']['password'],
|
56
|
+
config_file[Rails.env]['sessions']['default']['hosts'].first || "127.0.0.1"
|
57
|
+
]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.backup database, user, password, host
|
62
|
+
cmd = "rm -rf #{DUMP_PATH} 2>/dev/null && /usr/bin/env mongodump -h #{host} -d #{database} -o #{DUMP_PATH}"
|
63
|
+
if user.blank?
|
64
|
+
output = cmd
|
65
|
+
else
|
66
|
+
cmd << " -u #{user}"
|
67
|
+
if password.blank?
|
68
|
+
output = cmd
|
69
|
+
else
|
70
|
+
cmd << " --password"
|
71
|
+
output = "#{cmd} ... [password filtered]"
|
72
|
+
cmd << " #{password}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
[cmd, output].each do |command|
|
77
|
+
command << " && tar czfh #{archive_name DUMP_NAME} -C #{Dir.tmpdir()} #{DUMP_NAME}"
|
78
|
+
end
|
79
|
+
|
80
|
+
puts "*** Executing #{output}"
|
81
|
+
make_tmp_dir
|
82
|
+
system(cmd)
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.restore database, user, password, host
|
86
|
+
cmd = "rm -rf #{DUMP_PATH}/ 2>/dev/null && tar xvzf #{archive_name DUMP_NAME} -C #{Dir.tmpdir()}"
|
87
|
+
cmd << " && /usr/bin/env mongorestore --drop -h #{host} -d #{database} --dir #{DUMP_PATH}/*"
|
88
|
+
if user.blank?
|
89
|
+
output = cmd
|
90
|
+
else
|
91
|
+
cmd << " -u #{user}"
|
92
|
+
if password.blank?
|
93
|
+
output = cmd
|
94
|
+
else
|
95
|
+
cmd << " --password"
|
96
|
+
output = "#{cmd} ... [password filtered]"
|
97
|
+
cmd << " #{password}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
puts "*** Executing #{output}"
|
102
|
+
system(cmd)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
namespace :scalingo do
|
2
|
+
namespace :mysql do
|
3
|
+
desc "Backup local MySQL database"
|
4
|
+
task :backup_local => :environment do
|
5
|
+
database, user, password, host, port = ScalingoMySQL.local_credentials
|
6
|
+
ScalingoMySQL.backup(database, user, password, host, port)
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Backup remote Scalingo MySQL database"
|
10
|
+
task :backup_remote => :environment do
|
11
|
+
open_tunnel("SCALINGO_MYSQL_URL") do |database, user, password|
|
12
|
+
ScalingoMySQL.backup(database, user, password, "127.0.0.1", 27717)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Restore local MySQL database using a Scalingo backup"
|
17
|
+
task :restore_local => :environment do
|
18
|
+
database, user, password, host, port = ScalingoMySQL.local_credentials
|
19
|
+
ScalingoMySQL.restore(database, user, password, host, port)
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Restore remote Scalingo MySQL database using local backup"
|
23
|
+
task :restore_remote => :environment do
|
24
|
+
open_tunnel("SCALINGO_MYSQL_URL") do |database, user, password|
|
25
|
+
confirm_remote(database)
|
26
|
+
ScalingoMySQL.restore(database, user, password, "127.0.0.1", 27717)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
module ScalingoMySQL
|
33
|
+
DUMP_NAME = "scalingo_mysql_dump.sql"
|
34
|
+
DUMP_PATH = Dir.tmpdir() + "/#{DUMP_NAME}"
|
35
|
+
|
36
|
+
def self.local_credentials
|
37
|
+
config = ActiveRecord::Base.configurations[Rails.env]
|
38
|
+
return [
|
39
|
+
config['database'],
|
40
|
+
config['username'],
|
41
|
+
config['password'],
|
42
|
+
config['host'] || "127.0.0.1",
|
43
|
+
config['port'] || "3306",
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.backup database, user, password, host, port
|
48
|
+
base_cmd = "/usr/bin/env mysqldump --add-drop-table --create-options --disable-keys --extended-insert --single-transaction --quick --set-charset -h #{host} -P #{port} -u #{user}"
|
49
|
+
cmd = ""
|
50
|
+
cmd << base_cmd
|
51
|
+
public_cmd = ""
|
52
|
+
public_cmd << base_cmd
|
53
|
+
|
54
|
+
unless password.nil?
|
55
|
+
cmd << " -p'#{password}'"
|
56
|
+
public_cmd << " -p [password filtered]"
|
57
|
+
end
|
58
|
+
tar_cmd = " #{database} > #{DUMP_PATH} && tar cvzf #{archive_name DUMP_NAME} -C #{Dir.tmpdir()} #{DUMP_NAME}"
|
59
|
+
cmd << tar_cmd
|
60
|
+
public_cmd << tar_cmd
|
61
|
+
|
62
|
+
puts "*** Executing #{public_cmd}"
|
63
|
+
make_tmp_dir
|
64
|
+
system(cmd)
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.restore database, user, password, host, port
|
68
|
+
base_cmd = "/usr/bin/env tar xvzOf #{archive_name DUMP_NAME} | /usr/bin/env mysql -h #{host} -P #{port} -u #{user}"
|
69
|
+
cmd = ""
|
70
|
+
cmd << base_cmd
|
71
|
+
public_cmd = ""
|
72
|
+
public_cmd << base_cmd
|
73
|
+
|
74
|
+
unless password.nil?
|
75
|
+
cmd << " -p'#{password}'"
|
76
|
+
public_cmd << " -p [password filtered]"
|
77
|
+
end
|
78
|
+
cmd << " #{database}"
|
79
|
+
public_cmd << " #{database}"
|
80
|
+
|
81
|
+
puts "*** Executing #{public_cmd}"
|
82
|
+
system(cmd)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
namespace :scalingo do
|
2
|
+
namespace :postgresql do
|
3
|
+
desc "Backup local PostgreSQL database"
|
4
|
+
task :backup_local => :environment do
|
5
|
+
database, user, password, host, port = ScalingoPostgreSQL.local_credentials
|
6
|
+
ScalingoPostgreSQL.backup(database, user, password, host, port)
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Backup remote Scalingo PostgreSQL database"
|
10
|
+
task :backup_remote => :environment do
|
11
|
+
open_tunnel("SCALINGO_POSTGRESQL_URL") do |database, user, password|
|
12
|
+
ScalingoPostgreSQL.backup(database, user, password, "127.0.0.1", "27717")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Restore local PostgreSQL database using a Scalingo backup"
|
17
|
+
task :restore_local => :environment do
|
18
|
+
database, user, password, host, port = ScalingoPostgreSQL.local_credentials
|
19
|
+
ScalingoPostgreSQL.restore(database, user, password, host, port)
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Restore remote Scalingo PostgreSQL database using local backup"
|
23
|
+
task :restore_remote => :environment do
|
24
|
+
open_tunnel("SCALINGO_POSTGRESQL_URL") do |database, user, password|
|
25
|
+
confirm_remote(database)
|
26
|
+
ScalingoPostgreSQL.restore(database, user, password, "127.0.0.1", "27717")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
module ScalingoPostgreSQL
|
33
|
+
DUMP_NAME = "scalingo_postgresql_dump"
|
34
|
+
DUMP_PATH = Dir.tmpdir() + "/#{DUMP_NAME}"
|
35
|
+
|
36
|
+
def self.local_credentials
|
37
|
+
config = ActiveRecord::Base.configurations[Rails.env]
|
38
|
+
return [
|
39
|
+
config['database'],
|
40
|
+
config['username'],
|
41
|
+
config['password'],
|
42
|
+
config['host'] || "127.0.0.1",
|
43
|
+
config['port'] || "5432",
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.backup database, user, password, host, port
|
48
|
+
user_cmd = ""
|
49
|
+
password_cmd = ""
|
50
|
+
if not user.blank?
|
51
|
+
user_cmd = " -U #{user}"
|
52
|
+
if not password.blank?
|
53
|
+
password_cmd = "PGPASSWORD=#{password}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
base_cmd = "pg_dump -O -n public --format=c #{user_cmd} -h #{host} -p #{port} -d #{database}"
|
57
|
+
output = "rm -rf #{DUMP_PATH} 2>/dev/null && /usr/bin/env PGPASSWORD=[FILTERED] #{base_cmd}"
|
58
|
+
cmd = "rm -rf #{DUMP_PATH} 2>/dev/null && /usr/bin/env #{password_cmd} #{base_cmd}"
|
59
|
+
|
60
|
+
[cmd, output].each do |command|
|
61
|
+
command << " > #{DUMP_PATH} && tar cvzf #{archive_name DUMP_NAME} -C #{Dir.tmpdir()} #{DUMP_NAME}"
|
62
|
+
end
|
63
|
+
|
64
|
+
puts "*** Executing #{output}"
|
65
|
+
make_tmp_dir
|
66
|
+
system(cmd)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.restore database, user, password, host, port
|
70
|
+
user_cmd = ""
|
71
|
+
password_cmd = ""
|
72
|
+
if not user.blank?
|
73
|
+
user_cmd = " -U #{user}"
|
74
|
+
if not password.blank?
|
75
|
+
password_cmd = "PGPASSWORD=#{password}"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
base_cmd = "tar xvzOf #{archive_name DUMP_NAME} | "
|
80
|
+
pg_cmd = "pg_restore -O -n public --clean --if-exists #{user_cmd} -h #{host} -p #{port} -d #{database}"
|
81
|
+
output = "#{base_cmd} PGPASSWORD=[FILTERED] #{pg_cmd}"
|
82
|
+
cmd = "#{base_cmd} #{password_cmd} #{pg_cmd}"
|
83
|
+
|
84
|
+
puts "*** Executing #{output}"
|
85
|
+
system(cmd)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'scalingo_databases_rake_tasks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "scalingo_databases_rake_tasks"
|
8
|
+
spec.version = ScalingoDbTasks::VERSION
|
9
|
+
spec.authors = ["Scalingo"]
|
10
|
+
spec.email = ["hello@scalingo.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Perform database related tasks on Scalingo.}
|
13
|
+
spec.description = %q{Perform database related tasks on Scalingo.}
|
14
|
+
spec.homepage = "https://github.com/Scalingo/scalingo_databases_rake_tasks-gem"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scalingo_databases_rake_tasks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scalingo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Perform database related tasks on Scalingo.
|
42
|
+
email:
|
43
|
+
- hello@scalingo.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- bin/console
|
53
|
+
- bin/setup
|
54
|
+
- lib/scalingo_databases_rake_tasks.rb
|
55
|
+
- lib/scalingo_databases_rake_tasks/railtie.rb
|
56
|
+
- lib/scalingo_databases_rake_tasks/tasks/common.rake
|
57
|
+
- lib/scalingo_databases_rake_tasks/tasks/mongodb.rake
|
58
|
+
- lib/scalingo_databases_rake_tasks/tasks/mysql.rake
|
59
|
+
- lib/scalingo_databases_rake_tasks/tasks/postgresql.rake
|
60
|
+
- lib/scalingo_databases_rake_tasks/version.rb
|
61
|
+
- scalingo_databases_rake_tasks.gemspec
|
62
|
+
homepage: https://github.com/Scalingo/scalingo_databases_rake_tasks-gem
|
63
|
+
licenses: []
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.4.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Perform database related tasks on Scalingo.
|
85
|
+
test_files: []
|