leipreachan 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Changelog ADDED
@@ -0,0 +1,16 @@
1
+ v1.1.0
2
+ [+] Allow restore any backup via Capistarano
3
+
4
+ v1.0.3
5
+ [+] English doc
6
+ [+] Capistrano 2 support
7
+ [+] Allow restore lasts backup via Capistarano
8
+ [+] Drop tables before restore
9
+ [*] Fix postgresql backup
10
+
11
+ v1.0.2
12
+ [*] Update gemspec
13
+ [*] Decrease depend gem versions
14
+
15
+ v1.0.0:
16
+ [+] Initial version
data/README.Eng.md ADDED
@@ -0,0 +1,87 @@
1
+ # Leipreachan
2
+
3
+ As easiest as it is possible to be way for a database backup creating
4
+
5
+ ## Installation
6
+
7
+ Add the follwing line to your Gemfile:
8
+
9
+ gem 'leipreachan'
10
+
11
+ Run:
12
+
13
+ $ bundle
14
+
15
+ Or install manually:
16
+
17
+ $ gem install leipreachan
18
+
19
+ ## Usage
20
+
21
+ ### Capistrano 3
22
+
23
+ To create the backup copies in Capistrano you need to add the following line to the 'Capfile':
24
+
25
+ require 'leipreachan/capistrano3'
26
+
27
+ This way the following tasks are added to Capistrano:
28
+
29
+ cap deploy:leipreachan:backup # Backup database
30
+ cap deploy:leipreachan:list # List of backups
31
+ cap deploy:leipreachan:restore # Restore database
32
+
33
+ If a backup copy is needed in course of an application deploy, the following file needs to be added to 'deploy.rb' line:
34
+
35
+ before "deploy:migrate", "deploy:leipreachan:backup"
36
+
37
+ The backup copy will be created before database migrations.
38
+
39
+ The backup copy is created in 'shared/backups' by default. If there is a necessity of a folder setup, there is a 'backups_folder' variable:
40
+
41
+ set :backups_folder, '../../current'
42
+
43
+ Gem uses a folder with a release as a basis. This should be considered in course of backup copy folder installation.
44
+
45
+ For a recovery of the created backup, the following line needs to be run:
46
+
47
+ $ cap [environment] deploy:leipreachan:restore
48
+
49
+ **!!!CAUTION!!!** The latest backup copy is recovered.
50
+
51
+ ### Integration in Whenever
52
+
53
+ Add the following lines to 'config/schedule.rb':
54
+
55
+ every 1.day, :at => '4:30 am' do
56
+ rake "leipreachan:backup"
57
+ end
58
+
59
+ A folder where the backups are stored can be changed. They are kept in './backups' by default. In other words, backups folder is created in the root folder of an application.
60
+
61
+ every 1.month, do
62
+ rake "leipreachan:backup DIR=/tmp/database_backups"
63
+ end
64
+
65
+ There is an option of setting up an amount of days for how long the backups should be kept (note, that the tweak doesn't come with the a number of backups of a daily folder; there are unlimited backups every day). By default, 30 is stored directories, divided by dates.
66
+
67
+ every 5.days, do
68
+ rake "leipreachan:backup DAYS=5"
69
+ end
70
+
71
+ ### Rake tasks usage.
72
+
73
+ There are several rake tasks for handling database backups:
74
+
75
+ rake leipreachan:backup # Backup project database; Options: DIR=backups RAILS_ENV=production DAYS=30
76
+ rake leipreachan:list # List of all backups; Options: DATE=20150130
77
+ rake leipreachan:restore # Restore project database; Options: DIR=backups RAILS_ENV=production
78
+
79
+ * leipreachan:backup – creates database backup.
80
+ * leipreachan:list – shows a list of days and a list of the backups within the last day.
81
+ * leipreachan:restore – restores a database from a backup copy.
82
+
83
+ The options that can be turned over from an environment:
84
+
85
+ * DIR – a folder where a backup can be saved.
86
+ * DATE – to show the backups at a specified date
87
+ * DAYS – how many days the backups are stored.
data/README.md CHANGED
@@ -20,12 +20,16 @@ gem 'leipreachan'
20
20
 
21
21
  ## Использование
22
22
 
23
- ### Capistrano 3
23
+ ### Capistrano 2/3
24
24
 
25
- Для создания резервных копий с помощью Capistrano добавьте в ваш 'Capfile' эту строку:
25
+ Для создания резервных копий с помощью Capistrano 3 добавьте в ваш 'Capfile' эту строку:
26
26
 
27
27
  require 'leipreachan/capistrano3'
28
28
 
29
+ В случае Capistrano 2 необходимо в файл 'deploy.rb' добавить эту строку:
30
+
31
+ require 'leipreachan/capistrano2'
32
+
29
33
  Это добавит следующие задачи в Capistrano:
30
34
 
31
35
  cap deploy:leipreachan:backup # Backup database
data/leipreachan.gemspec CHANGED
@@ -22,8 +22,8 @@ Gem::Specification.new do |s|
22
22
  s.required_ruby_version = ">= #{Leipreachan::RUBY_VERSION}"
23
23
 
24
24
  s.add_dependency 'bundler', '~> 1.3'
25
- s.add_dependency 'rails', ">= #{Leipreachan::RAILS_VERSION}"
26
25
 
26
+ s.add_development_dependency 'rails', ">= #{Leipreachan::RAILS_VERSION}"
27
27
  s.add_development_dependency 'rake', "~> 10.0"
28
28
  s.add_development_dependency 'rspec', '>= 3.2.0', '<4'
29
29
  s.add_development_dependency 'railties', ">= #{Leipreachan::RAILS_VERSION}"
@@ -0,0 +1,28 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ namespace :deploy do
3
+ set :folder, fetch(:backups_folder, '../../shared/backups')
4
+ namespace :leipreachan do
5
+ desc "Backup database"
6
+ task :backup, role: :app do
7
+ run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:backup DIR=#{fetch(:folder)}"
8
+ end
9
+
10
+ desc "List of backups"
11
+ task :list, role: :app do
12
+ run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} bundle exec rake leipreachan:list DIR=#{fetch(:folder)}"
13
+ end
14
+
15
+ desc "Restore database"
16
+ task :restore, role: :app do
17
+ puts "="*80
18
+ puts " WARNING!!! YOUR CURRENT DATABASE DATA WILL BE LOST!!!"
19
+ puts " Think twice before enter 'YES' and push the Enter button"
20
+ 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)}"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -26,13 +26,25 @@ namespace :deploy do
26
26
  on roles(:app) do
27
27
  within current_path do
28
28
  with rails_env: fetch(:rails_env) do
29
+ execute :bundle, :exec, :rake, "leipreachan:list DIR=#{fetch(:folder)}"
30
+ set :date, ask('Please specify date (default: last one)', '')
31
+ puts "Your choice: #{fetch(:date)}"
32
+
33
+ if fetch(:date).strip! != ''
34
+ execute :bundle, :exec, :rake, "leipreachan:list DIR=#{fetch(:folder)} DATE=#{fetch(:date)}"
35
+ end
36
+
37
+ set :filename, ask('Please specify filename for restore (default: last one)', '')
38
+ puts "Your choice: #{fetch(:filename)}"
39
+
29
40
  puts "="*80
30
41
  puts "WARNING!!! YOUR CURRENT DATABASE DATA WILL BE LOST!!!"
31
42
  puts "Think twice before enter 'YES' and push the Enter button"
32
43
  puts "="*80
44
+
33
45
  set :answercheck, ask('"YES" for restore last backup of the database: ', 'No')
34
46
  if fetch(:answercheck) == 'YES'
35
- execute :bundle, :exec, :rake, "leipreachan:restorelast DIR=#{fetch(:folder)}"
47
+ execute :bundle, :exec, :rake, "leipreachan:restorefile DIR=#{fetch(:folder)} DATE=#{fetch(:date)} FILE=#{fetch(:filename)}"
36
48
  end
37
49
  end
38
50
  end
data/lib/leipreachan.rb CHANGED
@@ -20,15 +20,17 @@ module Leipreachan
20
20
  :backup_folder,
21
21
  :backup_file,
22
22
  :base_path,
23
- :db_config
23
+ :db_config,
24
+ :file_for_restore
24
25
 
25
26
  def initialize env
26
27
  @max_days = (env['DAYS'] || MAX_DAYS).to_i
27
28
  @target_date = env['DATE']
28
- @backup_folder = env['DATE'] || Time.now.strftime("%Y%m%d")
29
+ @backup_folder = env['DATE'].presence || Time.now.strftime("%Y%m%d")
29
30
  @directory = env['DIR'] || DIRECTORY
30
31
  datetime_stamp = Time.now.strftime("%Y%m%d%H%M%S")
31
32
  @base_path = File.join(Rails.root, directory)
33
+ @file_for_restore = env['FILE']
32
34
 
33
35
  file_name = "#{datetime_stamp}.sql"
34
36
  @backup_file = File.join(backup_base_on(backup_folder), file_name)
@@ -50,8 +52,8 @@ module Leipreachan
50
52
  dbrestore! get_file_for_restore
51
53
  end
52
54
 
53
- def restorelast!
54
- dbrestore! get_lastfile_for_restore
55
+ def restorefile!
56
+ dbrestore! file_for_restore || get_lastfile_for_restore
55
57
  end
56
58
 
57
59
  def list
@@ -0,0 +1 @@
1
+ load File.expand_path('../../capistrano2/tasks/leipreachan.cap', __FILE__)
@@ -1,13 +1,29 @@
1
1
  module Leipreachan
2
2
  class Backuper < DBBackup
3
+ def user
4
+ @user ||= db_config['username'].present? ? "-u#{db_config['username']}" : ""
5
+ end
6
+
7
+ def password
8
+ @password ||= db_config['password'].present? ? "-p#{db_config['password']} " : ""
9
+ end
10
+
11
+ def host
12
+ @host ||= db_config['host'].present? ? db_config['host'] : "localhost"
13
+ end
14
+
3
15
  def dbbackup!
4
- password = db_config['password'].present? ? "-p#{db_config['password']} " : ""
5
- system("mysqldump -u#{db_config['username']} #{password}-i -c -q --single-transaction #{db_config['database']} | gzip > #{backup_file}.gz")
16
+ system("mysqldump -h #{host} #{user} #{password}-i -c -q --single-transaction #{db_config['database']} | gzip > #{backup_file}.gz")
6
17
  end
7
18
 
8
19
  def dbrestore! file
9
- password = db_config['password'].present? ? "-p#{db_config['password']} " : ""
10
- system("zcat < #{backup_base_on(backup_folder)}/#{file} | mysql -u#{db_config['username']} #{password}#{db_config['database']}")
20
+ system("zcat < #{backup_base_on(backup_folder)}/#{file} | mysql -h #{host} #{user} #{password}#{db_config['database']}")
21
+ end
22
+
23
+ private
24
+
25
+ def drop_tables!
26
+ system("mysql --silent --skip-column-names -e \"SHOW TABLES\" -h #{host} #{user} #{password}#{db_config['database']} | xargs -L1 -I% echo 'DROP TABLE `%`;' | mysql -v -h #{host} #{user} #{password}#{db_config['database']}")
11
27
  end
12
28
  end
13
29
  end
@@ -1,29 +1,34 @@
1
1
  module Leipreachan
2
2
  class Backuper < DBBackup
3
+ def user
4
+ @user ||= db_config['user'].present? ? "-U #{db_config['user']}" : ""
5
+ end
6
+
7
+ def password
8
+ @password ||= db_config['password'].present? ? "PGPASSWORD='#{db_config['password']}' " : ""
9
+ end
10
+
11
+ def host
12
+ @host ||= db_config['host'].present? ? db_config['host'] : "localhost"
13
+ end
14
+
3
15
  def dbbackup!
4
- username = db_config['username'].present? ? "-U #{db_config['username']}" : ""
5
- password = db_config['password'].present? ? "PGPASSWORD='#{db_config['password']}' " : ""
6
- system("#{password}pg_dump #{username} #{db_config['database']} | gzip > #{backup_file}.gz")
16
+ system("#{password}pg_dump -h #{host} #{user} #{db_config['database']} | gzip > #{backup_file}.gz")
7
17
  end
8
18
 
9
19
  def dbrestore! file
10
- username = db_config['username'].present? ? "-U #{db_config['username']}" : ""
11
- password = db_config['password'].present? ? "PGPASSWORD='#{db_config['password']}' " : ""
12
-
13
20
  puts "Will be restored -> #{file}"
14
21
  puts ""
15
- drop_pg!
16
- system("zcat < #{backup_base_on(backup_folder)}/#{file} | #{password}psql #{username} #{db_config['database']}")
22
+ drop_tables!
23
+ system("zcat < #{backup_base_on(backup_folder)}/#{file} | #{password}psql -h #{host} #{user} #{db_config['database']}")
17
24
  end
18
25
 
19
26
  private
20
27
 
21
- def drop_pg!
22
- username = db_config['username'].present? ? "-U #{db_config['username']}" : ""
23
- password = db_config['password'].present? ? "PGPASSWORD='#{db_config['password']}' " : ""
24
- drop_table_query = "drop schema public cascade; create schema public;"
25
-
26
- system("echo \"#{drop_table_query}\" | #{password}psql #{username} #{db_config['database']}")
28
+ def drop_tables!
29
+ # drop_table_query = "drop schema public cascade; create schema public;"
30
+ 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']}")
27
32
  end
28
33
  end
29
34
  end
@@ -1,5 +1,5 @@
1
1
  module Leipreachan
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  RAILS_VERSION = "3.1.12"
4
4
  RUBY_VERSION = IO.read("#{File.dirname(__FILE__)}/../../.ruby-version").strip.split("-").first
5
5
  end
@@ -11,9 +11,9 @@ namespace :leipreachan do
11
11
  require 'leipreachan'
12
12
  Leipreachan.get_backuper_for(ENV).restore!
13
13
  end
14
- task restorelast: [:environment] do
14
+ task restorefile: [:environment] do
15
15
  require 'leipreachan'
16
- Leipreachan.get_backuper_for(ENV).restorelast!
16
+ Leipreachan.get_backuper_for(ENV).restorefile!
17
17
  end
18
18
  desc "Backup project database; Options: DIR=backups RAILS_ENV=production DAYS=30"
19
19
  task backup: [:environment] do
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.0.2
4
+ version: 1.1.0
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-28 00:00:00.000000000 Z
12
+ date: 2015-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -35,7 +35,7 @@ dependencies:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: 3.1.12
38
- type: :runtime
38
+ type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
@@ -109,15 +109,19 @@ files:
109
109
  - .gitignore
110
110
  - .rspec
111
111
  - .ruby-version
112
+ - Changelog
112
113
  - Gemfile
113
114
  - LICENSE.txt
115
+ - README.Eng.md
114
116
  - README.md
115
117
  - Rakefile
116
118
  - bin/console
117
119
  - bin/setup
118
120
  - leipreachan.gemspec
121
+ - lib/capistrano2/tasks/leipreachan.cap
119
122
  - lib/capistrano3/tasks/leipreachan.cap
120
123
  - lib/leipreachan.rb
124
+ - lib/leipreachan/capistrano2.rb
121
125
  - lib/leipreachan/capistrano3.rb
122
126
  - lib/leipreachan/mysql2.rb
123
127
  - lib/leipreachan/postgresql.rb