j-cap-recipes 0.0.17 → 0.0.18

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
  SHA1:
3
- metadata.gz: c956d783e3a5cc9334124c40c98d4b6d6d8f0486
4
- data.tar.gz: ff287d34e0858d0d7c4dcbed8fcacb2dc461b763
3
+ metadata.gz: d80bea1e89b26980f0b32a653e182c71e050aff0
4
+ data.tar.gz: a8d06234ff8e44182e449171f26e193ae4d0cfa5
5
5
  SHA512:
6
- metadata.gz: 916166821d2a71988cebb4cd810cc9be36361ffebf238df3f8a0e1bffb3a28e4095ae639f4d78b91de8ada42b9236bb5709480ae1e5b09bd5fed8a6bb2340023
7
- data.tar.gz: 05e3a11711e3988095ef5f43a94da4cc3366b414e96f21914b0006b806c3536a0cc7ac1d653d0e4ef58e3f165a9228dba47d9a3f44694799f929c77d8cc7feef
6
+ metadata.gz: f7ef723ec30da2d92f0dbd01ebcc0c2fb56be446c71e40e6156aee5066329d76eef3d069247fbddf66ba794b6093262585ab07a609039331fdf15d278eecf25c
7
+ data.tar.gz: 713eebf975e826769b0dcc5808133b6f9c3633e76135c67e3832682460255d3e834f878900dd2b6440f53ff86b0c4c9fcf7b2e06f60fa7284b47fa197ad5a5d9
@@ -1,5 +1,9 @@
1
1
  ## Changelog
2
2
 
3
+ ## 0.0.18
4
+
5
+ * Support mysql database for backup
6
+
3
7
  ## 0.0.17
4
8
 
5
9
  * Added ability to define what the env to download or upload for database backups. If it differs from the rails env.
@@ -1,27 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- j-cap-recipes (0.0.17)
4
+ j-cap-recipes (0.0.18)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- capistrano (3.0.0)
9
+ capistrano (3.4.0)
10
10
  i18n
11
11
  rake (>= 10.0.0)
12
- sshkit (>= 0.0.23)
13
- i18n (0.6.5)
14
- net-scp (1.1.2)
12
+ sshkit (~> 1.3)
13
+ colorize (0.7.5)
14
+ i18n (0.7.0)
15
+ net-scp (1.2.1)
15
16
  net-ssh (>= 2.6.5)
16
- net-ssh (2.7.0)
17
- rake (10.1.0)
18
- sshkit (1.1.0)
19
- net-scp
20
- net-ssh
21
- term-ansicolor
22
- term-ansicolor (1.2.2)
23
- tins (~> 0.8)
24
- tins (0.12.0)
17
+ net-ssh (2.9.2)
18
+ rake (10.4.2)
19
+ sshkit (1.7.1)
20
+ colorize (>= 0.7.0)
21
+ net-scp (>= 1.1.2)
22
+ net-ssh (>= 2.8.0)
25
23
 
26
24
  PLATFORMS
27
25
  ruby
@@ -24,6 +24,7 @@ namespace :db do
24
24
  within release_path do
25
25
  with rails_env: fetch(:rails_env) do
26
26
  execute :rake, 'db:backup'
27
+ execute :rake, 'db:cleanup', "ROTATE=#{fetch(:keep_releases)}"
27
28
  end
28
29
  end
29
30
  end
@@ -2,10 +2,10 @@ namespace :db do
2
2
  desc 'PG backup'
3
3
  task backup: [:environment, :load_config] do
4
4
  #stamp the filename
5
- datestamp = Time.now.strftime('%Y-%m-%d_%H-%M-%S')
5
+ dateformat = ENV['date-format'] || '%Y-%m-%d_%H-%M-%S'
6
+ datestamp = Time.now.strftime(dateformat)
6
7
 
7
8
  #create backups folder
8
- backup_dir = ENV['backup-path'] || Rails.root.join('db', 'backups')
9
9
  mkdir_p(backup_dir)
10
10
 
11
11
  config = ActiveRecord::Base.connection_config
@@ -13,13 +13,13 @@ namespace :db do
13
13
  backup_file = File.join(backup_dir, "#{database_name}_#{datestamp}.dump")
14
14
 
15
15
  #dump the backup and zip it up
16
- dump_command = "#{postgres_password(config)} pg_dump #{database_name} -w -F c"
17
- dump_command += postgres_auth_options(config)
18
- dump_command += " > #{backup_file}"
16
+ sh dump_command(config, database_name, backup_file)
19
17
 
20
- sh dump_command
21
-
22
- safe_ln backup_file, File.join(backup_dir, "#{database_name}_latest.dump")
18
+ latest_file_name = File.join(backup_dir, "#{database_name}_latest.dump")
19
+ if File.exist? latest_file_name
20
+ rm latest_file_name
21
+ end
22
+ safe_ln backup_file, latest_file_name
23
23
  end
24
24
 
25
25
  desc 'PG restore from the last backup file'
@@ -33,13 +33,13 @@ namespace :db do
33
33
  execute_task!('db:drop')
34
34
  execute_task!('db:create')
35
35
 
36
- restore_command = "#{postgres_password(config)} pg_restore -d #{database_name} -F c -w #{backup_file}"
37
- restore_command += postgres_auth_options(config)
38
- restore_command += ' -O -c'
39
- sh "#{restore_command} || echo 'done'"
36
+ command = db_restore_command(config, database_name, backup_file)
37
+ sh "#{command} || echo 'done'"
40
38
  end
41
39
 
42
40
  task :kill_postgres_connections => :environment do
41
+ config = ActiveRecord::Base.connection_config
42
+ next if config[:adapter] != "postgresql"
43
43
  db_name = ActiveRecord::Base.connection.current_database
44
44
  pid_column_name = if ActiveRecord::Base.connection.send(:postgresql_version) > 90200
45
45
  'pid'
@@ -61,17 +61,89 @@ namespace :db do
61
61
  end
62
62
  end
63
63
 
64
- end
64
+ desc 'Clean up old dumps'
65
+ task :cleanup do
66
+ dumps = FileList.new(File.join(backup_dir, '*.dump')).exclude(/_latest.dump$/)
67
+
68
+ if keep_versions > 0 && dumps.count >= keep_versions
69
+ puts "Keep #{keep_versions} dumps"
70
+ files = (dumps - dumps.last(keep_versions))
71
+ if files.any?
72
+ files.each do |f|
73
+ rm_r f
74
+ end
75
+ end
76
+ end
65
77
 
66
- def postgres_password(config)
67
- "PGPASSWORD='#{config[:password]}'" if config[:password].present?
68
- end
78
+ end
79
+
80
+ def backup_dir
81
+ @_backup_dir ||= ENV['backup-path'] || Rails.root.join('db', 'backups')
82
+ end
83
+
84
+ def keep_versions
85
+ @_keep_versions ||= ENV['ROTATE'].to_i
86
+ end
87
+
88
+ def postgres_dump_command(config, database_name, backup_file)
89
+ result = "#{postgres_password(config)} pg_dump #{database_name} -w -F c"
90
+ result += postgres_auth_options(config)
91
+ result + " > #{backup_file}"
92
+ end
93
+
94
+ def mysql_dump_command(config, database_name, backup_file)
95
+ result = "mysqldump #{database_name} "
96
+ result += mysql_auth_options(config)
97
+ result + " > #{backup_file}"
98
+ end
99
+
100
+ def dump_command(config, database_name, backup_file)
101
+ case config[:adapter]
102
+ when /mysql/
103
+ mysql_dump_command(config, database_name, backup_file)
104
+ when 'postgresql', 'pg'
105
+ postgres_dump_command(config, database_name, backup_file)
106
+ end
107
+ end
108
+
109
+ def db_restore_command(config, database_name, backup_file)
110
+ case config[:adapter]
111
+ when /mysql/
112
+ mysql_restore_command(config, database_name, backup_file)
113
+ when 'postgresql', 'pg'
114
+ postgres_restore_command(config, database_name, backup_file)
115
+ end
116
+ end
117
+
118
+ def postgres_restore_command(config, database_name, backup_file)
119
+ result = "#{postgres_password(config)} pg_restore -d #{database_name} -F c -w #{backup_file}"
120
+ result += postgres_auth_options(config)
121
+ result + ' -O -c'
122
+ end
123
+
124
+ def mysql_restore_command(config, database_name, backup_file)
125
+ "mysql #{database_name} #{mysql_auth_options(config)} < #{backup_file}"
126
+ end
127
+
128
+ def postgres_password(config)
129
+ "PGPASSWORD='#{config[:password]}'" if config[:password].present?
130
+ end
131
+
132
+ def postgres_auth_options(config)
133
+ command_options = ''
134
+ command_options += " -h #{config[:host]}" if config[:host].present?
135
+ command_options += " -U #{config[:username]}" if config[:username].present?
136
+ command_options
137
+ end
138
+
139
+ def mysql_auth_options(config)
140
+ command_options = ''
141
+ command_options += "--password='#{config[:password]}'" if config[:password].present?
142
+ command_options += " -h #{config[:host]}" if config[:host].present?
143
+ command_options += " -u #{config[:username]}" if config[:username].present?
144
+ command_options
145
+ end
69
146
 
70
- def postgres_auth_options(config)
71
- command_options = ''
72
- command_options += " -h #{config[:hostname]}" if config[:hostname].present?
73
- command_options += " -U #{config[:username]}" if config[:username].present?
74
- command_options
75
147
  end
76
148
 
77
149
  #TODO: Use setting to get S3 credentials
@@ -1,5 +1,5 @@
1
1
  module JCap
2
2
  module Recipes
3
- VERSION = '0.0.17'
3
+ VERSION = '0.0.18'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: j-cap-recipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Nikitochkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-24 00:00:00.000000000 Z
11
+ date: 2015-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  version: '0'
131
131
  requirements: []
132
132
  rubyforge_project:
133
- rubygems_version: 2.4.5
133
+ rubygems_version: 2.4.6
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: Capistrano 3 recipes for nginx, monit, rails log, setup, unicorn