dump 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.rubocop.yml +53 -0
- data/.rubocop_todo.yml +33 -0
- data/.travis.yml +12 -3
- data/README.markdown +8 -0
- data/dump.gemspec +4 -1
- data/lib/dump.rb +1 -0
- data/lib/dump/capistrano.rb +7 -1
- data/lib/dump/capistrano/v2.rb +331 -0
- data/lib/dump/railtie.rb +1 -0
- data/lib/dump_rake.rb +17 -8
- data/lib/dump_rake/archive_tar_minitar_fix.rb +3 -1
- data/lib/dump_rake/assets.rb +1 -0
- data/lib/dump_rake/continious_timeout.rb +17 -17
- data/lib/dump_rake/dump.rb +27 -22
- data/lib/dump_rake/dump_reader.rb +62 -63
- data/lib/dump_rake/dump_writer.rb +20 -21
- data/lib/dump_rake/env.rb +12 -10
- data/lib/dump_rake/env/filter.rb +3 -0
- data/lib/dump_rake/rails_root.rb +1 -0
- data/lib/dump_rake/table_manipulation.rb +23 -20
- data/lib/generators/assets_config/assets_config_generator.rb +2 -0
- data/lib/tasks/assets.rake +1 -2
- data/lib/tasks/dump.rake +5 -6
- data/recipes/dump.rb +1 -343
- data/script/update_readme +5 -3
- data/spec/cycle_spec.rb +33 -30
- data/spec/dummy_rails_app.rb +42 -0
- data/spec/lib/dump_rake/dump_reader_spec.rb +80 -92
- data/spec/lib/dump_rake/dump_spec.rb +56 -58
- data/spec/lib/dump_rake/dump_writer_spec.rb +42 -43
- data/spec/lib/dump_rake/env/filter_spec.rb +9 -9
- data/spec/lib/dump_rake/env_spec.rb +21 -21
- data/spec/lib/dump_rake/rails_root_spec.rb +7 -7
- data/spec/lib/dump_rake/table_manipulation_spec.rb +57 -63
- data/spec/lib/dump_rake_spec.rb +76 -76
- data/spec/recipes/dump_spec.rb +241 -217
- data/spec/spec_helper.rb +1 -42
- data/spec/tasks/assets_spec.rb +18 -18
- data/spec/tasks/dump_spec.rb +18 -18
- metadata +21 -2
@@ -1,4 +1,5 @@
|
|
1
1
|
class DumpRake
|
2
|
+
# Creating dump
|
2
3
|
class DumpWriter < Dump
|
3
4
|
attr_reader :stream, :config
|
4
5
|
|
@@ -79,21 +80,21 @@ class DumpRake
|
|
79
80
|
|
80
81
|
def write_assets
|
81
82
|
assets = assets_to_dump
|
82
|
-
if assets.
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
83
|
+
return if assets.blank?
|
84
|
+
|
85
|
+
config[:assets] = {}
|
86
|
+
Dir.chdir(DumpRake::RailsRoot) do
|
87
|
+
assets = Dir[*assets].uniq
|
88
|
+
assets.with_progress('Assets') do |asset|
|
89
|
+
paths = Dir[File.join(asset, '**/*')]
|
90
|
+
files = paths.select{ |path| File.file?(path) }
|
91
|
+
config[:assets][asset] = {:total => paths.length, :files => files.length}
|
92
|
+
assets_root_link do |_tmpdir, prefix|
|
93
|
+
paths.with_progress(asset) do |entry|
|
94
|
+
begin
|
95
|
+
Archive::Tar::Minitar.pack_file(File.join(prefix, entry), stream)
|
96
|
+
rescue => e
|
97
|
+
$stderr.puts "Skipped asset due to error #{e}"
|
97
98
|
end
|
98
99
|
end
|
99
100
|
end
|
@@ -108,12 +109,10 @@ class DumpRake
|
|
108
109
|
end
|
109
110
|
|
110
111
|
def assets_to_dump
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
[]
|
116
|
-
end
|
112
|
+
Rake::Task['assets'].invoke
|
113
|
+
DumpRake::Env[:assets].split(DumpRake::Assets::SPLITTER)
|
114
|
+
rescue
|
115
|
+
[]
|
117
116
|
end
|
118
117
|
end
|
119
118
|
end
|
data/lib/dump_rake/env.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require 'dump_rake/env/filter'
|
4
4
|
|
5
5
|
class DumpRake
|
6
|
+
# Working with environment variables
|
6
7
|
module Env
|
7
8
|
DICTIONARY = {
|
8
9
|
:desc => %w[DESC DESCRIPTION],
|
@@ -69,7 +70,7 @@ class DumpRake
|
|
69
70
|
end
|
70
71
|
|
71
72
|
def filter(key, splitter = nil)
|
72
|
-
@filters ||= Hash.new{ |
|
73
|
+
@filters ||= Hash.new{ |h, k| h[k] = Filter.new(*k) }
|
73
74
|
@filters[[self[key], splitter]]
|
74
75
|
end
|
75
76
|
|
@@ -90,7 +91,7 @@ class DumpRake
|
|
90
91
|
:select => [:like, :tags],
|
91
92
|
:assets => [:assets],
|
92
93
|
:restore_options => [:migrate_down, :restore_schema, :restore_tables, :restore_assets],
|
93
|
-
:transfer_options => [:transfer_via]
|
94
|
+
:transfer_options => [:transfer_via],
|
94
95
|
}
|
95
96
|
|
96
97
|
m[:versions] = m[:select] | [:summary]
|
@@ -108,12 +109,13 @@ class DumpRake
|
|
108
109
|
end
|
109
110
|
|
110
111
|
def for_command(command, strings = false)
|
111
|
-
|
112
|
-
|
113
|
-
value = self[variable]
|
114
|
-
|
115
|
-
|
112
|
+
env = {}
|
113
|
+
variable_names_for_command(command).each do |variable|
|
114
|
+
if (value = self[variable])
|
115
|
+
env[strings ? DICTIONARY[variable].first : variable] = value
|
116
|
+
end
|
116
117
|
end
|
118
|
+
env
|
117
119
|
end
|
118
120
|
|
119
121
|
def stringify!(hash)
|
@@ -124,9 +126,9 @@ class DumpRake
|
|
124
126
|
|
125
127
|
def explain_variables_for_command(command)
|
126
128
|
".\n" <<
|
127
|
-
|
128
|
-
|
129
|
-
|
129
|
+
variable_names_for_command(command).map do |variable_name|
|
130
|
+
" #{DICTIONARY[variable_name].join(', ')} — #{EXPLANATIONS[variable_name]}\n"
|
131
|
+
end.join('')
|
130
132
|
end
|
131
133
|
|
132
134
|
private
|
data/lib/dump_rake/env/filter.rb
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
class DumpRake
|
4
4
|
module Env
|
5
|
+
# Filter strings by simple pattern:
|
6
|
+
# 'a,b,c' will pass only 'a', 'b' and 'c'
|
7
|
+
# '-a,b,c' will pass everything except 'a', 'b' and 'c'
|
5
8
|
class Filter
|
6
9
|
attr_reader :invert, :values, :transparent
|
7
10
|
def initialize(s, splitter = nil)
|
data/lib/dump_rake/rails_root.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
class DumpRake
|
2
|
+
# Methods to work with db using ActiveRecord
|
2
3
|
module TableManipulation
|
3
4
|
protected
|
4
5
|
|
@@ -7,36 +8,33 @@ class DumpRake
|
|
7
8
|
end
|
8
9
|
|
9
10
|
def verify_connection
|
10
|
-
|
11
|
+
connection.verify!(0)
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
14
|
def quote_table_name(table)
|
15
|
-
|
15
|
+
connection.quote_table_name(table)
|
16
16
|
end
|
17
17
|
|
18
18
|
def quote_column_name(column)
|
19
|
-
|
19
|
+
connection.quote_column_name(column)
|
20
20
|
end
|
21
21
|
|
22
22
|
def quote_value(value)
|
23
|
-
|
23
|
+
connection.quote(value)
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
26
|
def clear_table(table_sql)
|
28
|
-
|
27
|
+
connection.delete("DELETE FROM #{table_sql}", 'Clearing table')
|
29
28
|
end
|
30
29
|
|
31
30
|
def insert_into_table(table_sql, columns_sql, values_sql)
|
32
31
|
values_sql = values_sql.join(',') if values_sql.is_a?(Array)
|
33
|
-
|
32
|
+
connection.insert("INSERT INTO #{table_sql} #{columns_sql} VALUES #{values_sql}", 'Loading dump')
|
34
33
|
end
|
35
34
|
|
36
35
|
def fix_sequence!(table)
|
37
|
-
|
38
|
-
|
39
|
-
end
|
36
|
+
return unless connection.respond_to?(:reset_pk_sequence!)
|
37
|
+
connection.reset_pk_sequence!(table)
|
40
38
|
end
|
41
39
|
|
42
40
|
def join_for_sql(quoted)
|
@@ -51,9 +49,8 @@ class DumpRake
|
|
51
49
|
join_for_sql(values.map{ |value| quote_value(value) })
|
52
50
|
end
|
53
51
|
|
54
|
-
|
55
52
|
def avaliable_tables
|
56
|
-
|
53
|
+
connection.tables
|
57
54
|
end
|
58
55
|
|
59
56
|
def tables_to_dump
|
@@ -67,7 +64,7 @@ class DumpRake
|
|
67
64
|
end
|
68
65
|
|
69
66
|
def table_row_count(table)
|
70
|
-
|
67
|
+
connection.select_value("SELECT COUNT(*) FROM #{quote_table_name(table)}").to_i
|
71
68
|
end
|
72
69
|
|
73
70
|
CHUNK_SIZE_MIN = 100 unless const_defined?(:CHUNK_SIZE_MIN)
|
@@ -87,7 +84,7 @@ class DumpRake
|
|
87
84
|
end
|
88
85
|
|
89
86
|
def table_columns(table)
|
90
|
-
|
87
|
+
connection.columns(table)
|
91
88
|
end
|
92
89
|
|
93
90
|
def table_has_primary_column?(table)
|
@@ -95,7 +92,7 @@ class DumpRake
|
|
95
92
|
table_columns(table).any?{ |column| column.name == table_primary_key(table) && column.type == :integer }
|
96
93
|
end
|
97
94
|
|
98
|
-
def table_primary_key(
|
95
|
+
def table_primary_key(_table)
|
99
96
|
'id'
|
100
97
|
end
|
101
98
|
|
@@ -105,9 +102,9 @@ class DumpRake
|
|
105
102
|
primary_key = table_primary_key(table)
|
106
103
|
quoted_primary_key = "#{quote_table_name(table)}.#{quote_column_name(primary_key)}"
|
107
104
|
select_where_primary_key =
|
108
|
-
"SELECT * FROM #{quote_table_name(table)}"
|
109
|
-
" WHERE #{quoted_primary_key} %s"
|
110
|
-
" ORDER BY #{quoted_primary_key} ASC"
|
105
|
+
"SELECT * FROM #{quote_table_name(table)}" \
|
106
|
+
" WHERE #{quoted_primary_key} %s" \
|
107
|
+
" ORDER BY #{quoted_primary_key} ASC" \
|
111
108
|
" LIMIT #{chunk_size}"
|
112
109
|
rows = select_all_by_sql(select_where_primary_key % '>= 0')
|
113
110
|
until rows.blank?
|
@@ -125,7 +122,13 @@ class DumpRake
|
|
125
122
|
end
|
126
123
|
|
127
124
|
def select_all_by_sql(sql)
|
128
|
-
|
125
|
+
connection.select_all(sql)
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def connection
|
131
|
+
ActiveRecord::Base.connection
|
129
132
|
end
|
130
133
|
end
|
131
134
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
case
|
2
2
|
when defined?(Rails::Generator::Base)
|
3
|
+
# Generator for rails < 3
|
3
4
|
class AssetsConfigGenerator < Rails::Generator::Base
|
4
5
|
def manifest
|
5
6
|
record do |m|
|
@@ -8,6 +9,7 @@ when defined?(Rails::Generator::Base)
|
|
8
9
|
end
|
9
10
|
end
|
10
11
|
when defined?(Rails::Generators::Base)
|
12
|
+
# Generator for rails >= 3
|
11
13
|
class AssetsConfigGenerator < Rails::Generators::Base
|
12
14
|
def create_assets_config
|
13
15
|
create_file 'config/assets', File.read(File.join(File.dirname(__FILE__), 'templates/assets'))
|
data/lib/tasks/assets.rake
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
$: << File.join(File.dirname(__FILE__), '../../lib')
|
2
1
|
require 'dump_rake'
|
3
2
|
|
4
3
|
task :assets do
|
@@ -6,7 +5,7 @@ task :assets do
|
|
6
5
|
end
|
7
6
|
|
8
7
|
namespace :assets do
|
9
|
-
desc
|
8
|
+
desc 'Delete assets' << DumpRake::Env.explain_variables_for_command(:assets)
|
10
9
|
task :delete => :assets do
|
11
10
|
ENV['ASSETS'].split(':').each do |asset|
|
12
11
|
DumpRake::Assets.glob_asset_children(asset, '*').each do |child|
|
data/lib/tasks/dump.rake
CHANGED
@@ -1,26 +1,25 @@
|
|
1
|
-
$: << File.join(File.dirname(__FILE__), '../../lib')
|
2
1
|
require 'dump_rake'
|
3
2
|
|
4
|
-
desc
|
3
|
+
desc 'Short for dump:create' << DumpRake::Env.explain_variables_for_command(:create)
|
5
4
|
task :dump => 'dump:create'
|
6
5
|
|
7
6
|
namespace :dump do
|
8
|
-
desc
|
7
|
+
desc 'Show avaliable versions' << DumpRake::Env.explain_variables_for_command(:versions)
|
9
8
|
task :versions => :environment do
|
10
9
|
DumpRake.versions(DumpRake::Env.for_command(:versions))
|
11
10
|
end
|
12
11
|
|
13
|
-
desc
|
12
|
+
desc 'Create dump' << DumpRake::Env.explain_variables_for_command(:create)
|
14
13
|
task :create => :environment do
|
15
14
|
DumpRake.create(DumpRake::Env.for_command(:create))
|
16
15
|
end
|
17
16
|
|
18
|
-
desc
|
17
|
+
desc 'Restore dump' << DumpRake::Env.explain_variables_for_command(:restore)
|
19
18
|
task :restore => :environment do
|
20
19
|
DumpRake.restore(DumpRake::Env.for_command(:restore))
|
21
20
|
end
|
22
21
|
|
23
|
-
desc
|
22
|
+
desc 'Cleanup dumps' << DumpRake::Env.explain_variables_for_command(:cleanup)
|
24
23
|
task :cleanup => :environment do
|
25
24
|
DumpRake.cleanup(DumpRake::Env.for_command(:cleanup))
|
26
25
|
end
|
data/recipes/dump.rb
CHANGED
@@ -1,343 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Capistrano::Configuration.instance(:i_need_this!).load do
|
4
|
-
$: << File.join(File.dirname(__FILE__), '../lib')
|
5
|
-
require 'fileutils'
|
6
|
-
require 'shellwords'
|
7
|
-
|
8
|
-
require 'dump_rake/continious_timeout'
|
9
|
-
require 'dump_rake/env'
|
10
|
-
|
11
|
-
begin
|
12
|
-
nil.blank?
|
13
|
-
rescue
|
14
|
-
Object.class_eval do
|
15
|
-
def blank?
|
16
|
-
respond_to?(:empty?) ? empty? : !self
|
17
|
-
end
|
18
|
-
def present?
|
19
|
-
!blank?
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
namespace :dump do
|
25
|
-
def dump_command(command, env = {})
|
26
|
-
rake = env.delete(:rake) || 'rake'
|
27
|
-
|
28
|
-
# stringify_keys! from activesupport
|
29
|
-
DumpRake::Env.stringify!(env)
|
30
|
-
|
31
|
-
env.update(DumpRake::Env.for_command(command, true))
|
32
|
-
|
33
|
-
cmd = %W[-s dump:#{command}]
|
34
|
-
cmd += env.sort.map{ |key, value| "#{key}=#{value}" }
|
35
|
-
"#{rake} #{cmd.shelljoin}"
|
36
|
-
end
|
37
|
-
|
38
|
-
def fetch_rails_env
|
39
|
-
fetch(:rails_env, "production")
|
40
|
-
end
|
41
|
-
|
42
|
-
def got_rsync?
|
43
|
-
`which rsync`
|
44
|
-
$?.success?
|
45
|
-
end
|
46
|
-
|
47
|
-
def do_transfer_via(via, direction, from, to)
|
48
|
-
case via
|
49
|
-
when :rsync
|
50
|
-
if run_local('which rsync').present? && $?.success?
|
51
|
-
execute_on_servers do |servers|
|
52
|
-
commands = servers.map do |server|
|
53
|
-
target = sessions[server]
|
54
|
-
user = target.options[:user] || fetch(:user, nil)
|
55
|
-
host = target.host
|
56
|
-
port = target.options[:port]
|
57
|
-
full_host = "#{"#{user}@" if user.present?}#{host}"
|
58
|
-
|
59
|
-
ssh = port.present? ? "ssh -p #{port}" : 'ssh'
|
60
|
-
cmd = %W[rsync -P -e #{ssh} --timeout=15]
|
61
|
-
case direction
|
62
|
-
when :up
|
63
|
-
cmd << from << "#{full_host}:#{to}"
|
64
|
-
when :down
|
65
|
-
cmd << "#{full_host}:#{from}" << to
|
66
|
-
else
|
67
|
-
raise "Don't know how to transfer in direction #{direction}"
|
68
|
-
end
|
69
|
-
cmd.shelljoin
|
70
|
-
end
|
71
|
-
commands.each do |cmd|
|
72
|
-
logger.info cmd if logger
|
73
|
-
|
74
|
-
3.times do
|
75
|
-
break if system(cmd)
|
76
|
-
break unless [10, 11, 12, 23, 30, 35].include?($?.exitstatus)
|
77
|
-
end
|
78
|
-
raise "rsync returned #{$?.exitstatus}" unless $?.success?
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
when :sftp, :scp
|
83
|
-
ContiniousTimeout.timeout 15 do |thread|
|
84
|
-
transfer(direction, from, to, :via => via) do |channel, path, transfered, total|
|
85
|
-
thread.defer
|
86
|
-
progress = if transfered < total
|
87
|
-
"\e[1m%5.1f%%\e[0m" % (transfered * 100.0 / total)
|
88
|
-
else
|
89
|
-
"100%"
|
90
|
-
end
|
91
|
-
$stderr << "\rTransfering: #{progress}"
|
92
|
-
end
|
93
|
-
end
|
94
|
-
else
|
95
|
-
raise "Unknown transfer method #{via}"
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def do_transfer(direction, from, to)
|
100
|
-
if via = DumpRake::Env[:transfer_via]
|
101
|
-
case via.downcase
|
102
|
-
when 'rsync'
|
103
|
-
do_transfer_via(:rsync, direction, from, to)
|
104
|
-
when 'sftp'
|
105
|
-
do_transfer_via(:sftp, direction, from, to)
|
106
|
-
when 'scp'
|
107
|
-
do_transfer_via(:scp, direction, from, to)
|
108
|
-
else
|
109
|
-
raise "Unknown transfer method #{via}"
|
110
|
-
end
|
111
|
-
else
|
112
|
-
if got_rsync?
|
113
|
-
do_transfer_via(:rsync, direction, from, to)
|
114
|
-
else
|
115
|
-
$stderr.puts "To transfer using rsync — make rsync binary accessible and verify that remote host can work with rsync through ssh"
|
116
|
-
begin
|
117
|
-
do_transfer_via(:sftp, direction, from, to)
|
118
|
-
rescue => e
|
119
|
-
$stderr.puts e
|
120
|
-
do_transfer_via(:scp, direction, from, to)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def with_additional_tags(*tags)
|
127
|
-
tags = [tags, DumpRake::Env[:tags]].flatten.select(&:present?).join(',')
|
128
|
-
DumpRake::Env.with_env(:tags => tags) do
|
129
|
-
yield
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def print_and_return_or_fail
|
134
|
-
out = yield
|
135
|
-
raise 'Failed creating dump' if out.blank?
|
136
|
-
print out
|
137
|
-
out.strip
|
138
|
-
end
|
139
|
-
|
140
|
-
def run_local(cmd)
|
141
|
-
`#{cmd}`
|
142
|
-
end
|
143
|
-
|
144
|
-
def run_remote(cmd)
|
145
|
-
output = ''
|
146
|
-
run(cmd) do |channel, io, data|
|
147
|
-
case io
|
148
|
-
when :out
|
149
|
-
output << data
|
150
|
-
when :err
|
151
|
-
$stderr << data
|
152
|
-
end
|
153
|
-
end
|
154
|
-
output
|
155
|
-
end
|
156
|
-
|
157
|
-
def last_part_of_last_line(out)
|
158
|
-
if line = out.strip.split(/\s*[\n\r]\s*/).last
|
159
|
-
line.split("\t").last
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
def fetch_rake
|
164
|
-
fetch(:rake, nil)
|
165
|
-
end
|
166
|
-
|
167
|
-
def auto_backup?
|
168
|
-
!DumpRake::Env.no?(:backup)
|
169
|
-
end
|
170
|
-
|
171
|
-
namespace :local do
|
172
|
-
desc "Shorthand for dump:local:create" << DumpRake::Env.explain_variables_for_command(:create)
|
173
|
-
task :default, :roles => :db, :only => {:primary => true} do
|
174
|
-
create
|
175
|
-
end
|
176
|
-
|
177
|
-
desc "Create local dump" << DumpRake::Env.explain_variables_for_command(:create)
|
178
|
-
task :create, :roles => :db, :only => {:primary => true} do
|
179
|
-
print_and_return_or_fail do
|
180
|
-
with_additional_tags('local') do
|
181
|
-
run_local(dump_command(:create))
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
desc "Restore local dump" << DumpRake::Env.explain_variables_for_command(:restore)
|
187
|
-
task :restore, :roles => :db, :only => {:primary => true} do
|
188
|
-
run_local(dump_command(:restore))
|
189
|
-
end
|
190
|
-
|
191
|
-
desc "Versions of local dumps" << DumpRake::Env.explain_variables_for_command(:versions)
|
192
|
-
task :versions, :roles => :db, :only => {:primary => true} do
|
193
|
-
print run_local(dump_command(:versions, :show_size => true))
|
194
|
-
end
|
195
|
-
|
196
|
-
desc "Cleanup local dumps" << DumpRake::Env.explain_variables_for_command(:cleanup)
|
197
|
-
task :cleanup, :roles => :db, :only => {:primary => true} do
|
198
|
-
print run_local(dump_command(:cleanup))
|
199
|
-
end
|
200
|
-
|
201
|
-
desc "Upload dump" << DumpRake::Env.explain_variables_for_command(:transfer)
|
202
|
-
task :upload, :roles => :db, :only => {:primary => true} do
|
203
|
-
file = DumpRake::Env.with_env(:summary => nil) do
|
204
|
-
last_part_of_last_line(run_local(dump_command(:versions)))
|
205
|
-
end
|
206
|
-
if file
|
207
|
-
do_transfer :up, "dump/#{file}", "#{current_path}/dump/#{file}"
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
namespace :remote do
|
213
|
-
desc "Shorthand for dump:remote:create" << DumpRake::Env.explain_variables_for_command(:create)
|
214
|
-
task :default, :roles => :db, :only => {:primary => true} do
|
215
|
-
remote.create
|
216
|
-
end
|
217
|
-
|
218
|
-
desc "Create remote dump" << DumpRake::Env.explain_variables_for_command(:create)
|
219
|
-
task :create, :roles => :db, :only => {:primary => true} do
|
220
|
-
print_and_return_or_fail do
|
221
|
-
with_additional_tags('remote') do
|
222
|
-
run_remote("cd #{current_path}; #{dump_command(:create, :rake => fetch_rake, :RAILS_ENV => fetch_rails_env, :PROGRESS_TTY => '+')}")
|
223
|
-
end
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
desc "Restore remote dump" << DumpRake::Env.explain_variables_for_command(:restore)
|
228
|
-
task :restore, :roles => :db, :only => {:primary => true} do
|
229
|
-
run_remote("cd #{current_path}; #{dump_command(:restore, :rake => fetch_rake, :RAILS_ENV => fetch_rails_env, :PROGRESS_TTY => '+')}")
|
230
|
-
end
|
231
|
-
|
232
|
-
desc "Versions of remote dumps" << DumpRake::Env.explain_variables_for_command(:versions)
|
233
|
-
task :versions, :roles => :db, :only => {:primary => true} do
|
234
|
-
print run_remote("cd #{current_path}; #{dump_command(:versions, :rake => fetch_rake, :RAILS_ENV => fetch_rails_env, :PROGRESS_TTY => '+', :show_size => true)}")
|
235
|
-
end
|
236
|
-
|
237
|
-
desc "Cleanup of remote dumps" << DumpRake::Env.explain_variables_for_command(:cleanup)
|
238
|
-
task :cleanup, :roles => :db, :only => {:primary => true} do
|
239
|
-
print run_remote("cd #{current_path}; #{dump_command(:cleanup, :rake => fetch_rake, :RAILS_ENV => fetch_rails_env, :PROGRESS_TTY => '+')}")
|
240
|
-
end
|
241
|
-
|
242
|
-
desc "Download dump" << DumpRake::Env.explain_variables_for_command(:transfer)
|
243
|
-
task :download, :roles => :db, :only => {:primary => true} do
|
244
|
-
file = DumpRake::Env.with_env(:summary => nil) do
|
245
|
-
last_part_of_last_line(run_remote("cd #{current_path}; #{dump_command(:versions, :rake => fetch_rake, :RAILS_ENV => fetch_rails_env, :PROGRESS_TTY => '+')}"))
|
246
|
-
end
|
247
|
-
if file
|
248
|
-
FileUtils.mkpath('dump')
|
249
|
-
do_transfer :down, "#{current_path}/dump/#{file}", "dump/#{file}"
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
desc "Shorthand for dump:local:upload" << DumpRake::Env.explain_variables_for_command(:transfer)
|
255
|
-
task :upload, :roles => :db, :only => {:primary => true} do
|
256
|
-
local.upload
|
257
|
-
end
|
258
|
-
|
259
|
-
desc "Shorthand for dump:remote:download" << DumpRake::Env.explain_variables_for_command(:transfer)
|
260
|
-
task :download, :roles => :db, :only => {:primary => true} do
|
261
|
-
remote:download
|
262
|
-
end
|
263
|
-
|
264
|
-
namespace :mirror do
|
265
|
-
desc "Creates local dump, uploads and restores on remote" << DumpRake::Env.explain_variables_for_command(:mirror)
|
266
|
-
task :up, :roles => :db, :only => {:primary => true} do
|
267
|
-
auto_backup = if auto_backup?
|
268
|
-
with_additional_tags('auto-backup') do
|
269
|
-
remote.create
|
270
|
-
end
|
271
|
-
end
|
272
|
-
if !auto_backup? || auto_backup.present?
|
273
|
-
file = with_additional_tags('mirror') do
|
274
|
-
local.create
|
275
|
-
end
|
276
|
-
if file.present?
|
277
|
-
DumpRake::Env.with_clean_env(:like => file) do
|
278
|
-
local.upload
|
279
|
-
remote.restore
|
280
|
-
end
|
281
|
-
end
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
desc "Creates remote dump, downloads and restores on local" << DumpRake::Env.explain_variables_for_command(:mirror)
|
286
|
-
task :down, :roles => :db, :only => {:primary => true} do
|
287
|
-
auto_backup = if auto_backup?
|
288
|
-
with_additional_tags('auto-backup') do
|
289
|
-
local.create
|
290
|
-
end
|
291
|
-
end
|
292
|
-
if !auto_backup? || auto_backup.present?
|
293
|
-
file = with_additional_tags('mirror') do
|
294
|
-
remote.create
|
295
|
-
end
|
296
|
-
if file.present?
|
297
|
-
DumpRake::Env.with_clean_env(:like => file) do
|
298
|
-
remote.download
|
299
|
-
local.restore
|
300
|
-
end
|
301
|
-
end
|
302
|
-
end
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
namespace :backup do
|
307
|
-
desc "Shorthand for dump:backup:create" << DumpRake::Env.explain_variables_for_command(:backup)
|
308
|
-
task :default, :roles => :db, :only => {:primary => true} do
|
309
|
-
create
|
310
|
-
end
|
311
|
-
|
312
|
-
desc "Creates remote dump and downloads to local (desc defaults to 'backup')" << DumpRake::Env.explain_variables_for_command(:backup)
|
313
|
-
task :create, :roles => :db, :only => {:primary => true} do
|
314
|
-
file = with_additional_tags('backup') do
|
315
|
-
remote.create
|
316
|
-
end
|
317
|
-
if file.present?
|
318
|
-
DumpRake::Env.with_clean_env(:like => file) do
|
319
|
-
remote.download
|
320
|
-
end
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
desc "Uploads dump with backup tag and restores it on remote" << DumpRake::Env.explain_variables_for_command(:backup_restore)
|
325
|
-
task :restore, :roles => :db, :only => {:primary => true} do
|
326
|
-
file = with_additional_tags('backup') do
|
327
|
-
last_part_of_last_line(run_local(dump_command(:versions)))
|
328
|
-
end
|
329
|
-
if file.present?
|
330
|
-
DumpRake::Env.with_clean_env(:like => file) do
|
331
|
-
local.upload
|
332
|
-
remote.restore
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
after 'deploy:update_code' do
|
340
|
-
from, to = %W[#{shared_path}/dump #{release_path}/dump]
|
341
|
-
run "mkdir -p #{from}; rm -rf #{to}; ln -s #{from} #{to}"
|
342
|
-
end
|
343
|
-
end
|
1
|
+
require 'dump/capistrano'
|