leipreachan 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,16 +1,22 @@
1
+ v1.1.1
2
+ [*] Fix empty file name while restore
3
+ [+] Allow list with param DATE via Capistrano
4
+ [+] Add check for file and folder exist
5
+ [+] Add check for system dependencies
6
+
1
7
  v1.1.0
2
8
  [+] Allow restore any backup via Capistarano
3
9
 
4
10
  v1.0.3
11
+ [*] Fix postgresql backup
5
12
  [+] English doc
6
13
  [+] Capistrano 2 support
7
14
  [+] Allow restore lasts backup via Capistarano
8
15
  [+] Drop tables before restore
9
- [*] Fix postgresql backup
10
16
 
11
17
  v1.0.2
12
18
  [*] Update gemspec
13
19
  [*] Decrease depend gem versions
14
20
 
15
21
  v1.0.0:
16
- [+] Initial version
22
+ [+] Initial version
@@ -9,18 +9,35 @@ Capistrano::Configuration.instance(:must_exist).load do
9
9
 
10
10
  desc "List of backups"
11
11
  task :list, role: :app do
12
- run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:list DIR=#{fetch(:folder)}"
12
+ set :date, ENV['DATE'].nil? ? '' : "DATE=#{ENV['DATE']}"
13
+ run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:list DIR=#{fetch(:folder)} #{fetch(:date)}"
13
14
  end
14
15
 
15
16
  desc "Restore database"
16
17
  task :restore, role: :app do
18
+ run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:list DIR=#{fetch(:folder)}"
19
+ date = Capistrano::CLI.ui.ask('Please specify date (default: latest one)', nil)
20
+ puts "Your choice: #{date.empty? ? 'Latest one' : date.strip}"
21
+
22
+ unless date.empty?
23
+ run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:list DIR=#{fetch(:folder)} DATE=#{date.strip}"
24
+ end
25
+
26
+ filename = Capistrano::CLI.ui.ask('Please specify filename for restore (default: latest one)', nil)
27
+ puts "Your choice: #{filename.empty? ? 'Latest one' : filename.strip}"
28
+
17
29
  puts "="*80
18
- puts " WARNING!!! YOUR CURRENT DATABASE DATA WILL BE LOST!!!"
19
- puts " Think twice before enter 'YES' and push the Enter button"
30
+ puts "WARNING!!! YOUR CURRENT DATABASE DATA WILL BE LOST!!!"
31
+ puts "Think twice before enter 'YES' and push the Enter button"
20
32
  puts "="*80
21
- answer = Capistrano::CLI.ui.ask " \"YES\" for restore last backup of the database: '"
22
- if answer == 'YES' then
23
- run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:restorelast DIR=#{fetch(:folder)}"
33
+
34
+ answercheck = Capistrano::CLI.ui.ask('"YES" for restore last backup of the database: ', nil)
35
+ if answercheck == 'YES'
36
+ filename = filename.empty? ? '' : "FILE=#{filename.strip}"
37
+ date = date.empty? ? '' : "DATE=#{date.strip}"
38
+ run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:restorefile DIR=#{fetch(:folder)} #{date} #{filename}"
39
+ else
40
+ puts "Incorrect Answer!!!"
24
41
  end
25
42
  end
26
43
  end
@@ -16,7 +16,8 @@ namespace :deploy do
16
16
  on roles(:app) do
17
17
  within current_path do
18
18
  with rails_env: fetch(:rails_env) do
19
- execute :bundle, :exec, :rake, "leipreachan:list DIR=#{fetch(:folder)}"
19
+ set :date, ENV['DATE'].nil? ? '' : "DATE=#{ENV['DATE']}"
20
+ execute :bundle, :exec, :rake, "leipreachan:list DIR=#{fetch(:folder)} #{fetch(:date)}"
20
21
  end
21
22
  end
22
23
  end
@@ -44,7 +45,8 @@ namespace :deploy do
44
45
 
45
46
  set :answercheck, ask('"YES" for restore last backup of the database: ', 'No')
46
47
  if fetch(:answercheck) == 'YES'
47
- execute :bundle, :exec, :rake, "leipreachan:restorefile DIR=#{fetch(:folder)} DATE=#{fetch(:date)} FILE=#{fetch(:filename)}"
48
+ filename = filename.nil? ? '' : "FILE=#{filename.strip}"
49
+ execute :bundle, :exec, :rake, "leipreachan:restorefile DIR=#{fetch(:folder)} DATE=#{fetch(:date)} #{fetch(:filename)}"
48
50
  end
49
51
  end
50
52
  end
@@ -1,5 +1,9 @@
1
1
  module Leipreachan
2
2
  class Backuper < DBBackup
3
+ def system_check_list
4
+ %w(gzip zcat mysql mysqldump)
5
+ end
6
+
3
7
  def user
4
8
  @user ||= db_config['username'].present? ? "-u#{db_config['username']}" : ""
5
9
  end
@@ -17,7 +21,7 @@ module Leipreachan
17
21
  end
18
22
 
19
23
  def dbrestore! file
20
- system("zcat < #{backup_base_on(backup_folder)}/#{file} | mysql -h #{host} #{user} #{password}#{db_config['database']}")
24
+ system("zcat < #{file} | mysql -h #{host} #{user} #{password}#{db_config['database']}")
21
25
  end
22
26
 
23
27
  private
@@ -1,5 +1,9 @@
1
1
  module Leipreachan
2
2
  class Backuper < DBBackup
3
+ def system_check_list
4
+ %w(gzip zcat psql pg_dump)
5
+ end
6
+
3
7
  def user
4
8
  @user ||= db_config['user'].present? ? "-U #{db_config['user']}" : ""
5
9
  end
@@ -20,15 +24,13 @@ module Leipreachan
20
24
  puts "Will be restored -> #{file}"
21
25
  puts ""
22
26
  drop_tables!
23
- system("zcat < #{backup_base_on(backup_folder)}/#{file} | #{password}psql -h #{host} #{user} #{db_config['database']}")
27
+ system("zcat < #{file} | #{password}psql -h #{host} #{user} #{db_config['database']}")
24
28
  end
25
29
 
26
30
  private
27
31
 
28
32
  def drop_tables!
29
- # drop_table_query = "drop schema public cascade; create schema public;"
30
33
  system("#{password}psql -h #{host} #{user} #{db_config['database']} -t -c \"select 'drop table \\\"' || tablename || '\\\" cascade;' from pg_tables where schemaname = 'public'\" | #{password}psql -h #{host} #{user} #{db_config['database']}");
31
- # system("echo \"#{drop_table_query}\" | #{password}psql -h #{host} #{user} #{db_config['database']}")
32
34
  end
33
35
  end
34
36
  end
@@ -1,5 +1,5 @@
1
1
  module Leipreachan
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  RAILS_VERSION = "3.1.12"
4
4
  RUBY_VERSION = IO.read("#{File.dirname(__FILE__)}/../../.ruby-version").strip.split("-").first
5
5
  end
data/lib/leipreachan.rb CHANGED
@@ -24,6 +24,7 @@ module Leipreachan
24
24
  :file_for_restore
25
25
 
26
26
  def initialize env
27
+ check_system_requirements!
27
28
  @max_days = (env['DAYS'] || MAX_DAYS).to_i
28
29
  @target_date = env['DATE']
29
30
  @backup_folder = env['DATE'].presence || Time.now.strftime("%Y%m%d")
@@ -38,6 +39,13 @@ module Leipreachan
38
39
  @db_config = ActiveRecord::Base.configurations[Rails.env]
39
40
  end
40
41
 
42
+ def check_system_requirements!
43
+ system_check_list.each do |command|
44
+ system("#{command} --help > /dev/null 2>&1")
45
+ raise "#{command} is required for Leipreachan backups" if $?.exitstatus > 0
46
+ end
47
+ end
48
+
41
49
  def backup!
42
50
  FileUtils.mkdir_p(backup_base_on(backup_folder))
43
51
  dbbackup!
@@ -49,11 +57,11 @@ module Leipreachan
49
57
  end
50
58
 
51
59
  def restore!
52
- dbrestore! get_file_for_restore
60
+ dbrestore! File.join(backup_base_on(backup_folder), get_file_for_restore)
53
61
  end
54
62
 
55
63
  def restorefile!
56
- dbrestore! file_for_restore || get_lastfile_for_restore
64
+ dbrestore! File.join(backup_base_on(backup_folder), file_for_restore || get_lastfile_for_restore)
57
65
  end
58
66
 
59
67
  def list
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leipreachan
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-29 00:00:00.000000000 Z
12
+ date: 2015-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler