mina-data_sync 1.0.0.beta2 → 1.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,6 +38,17 @@ mina data_sync:pull # pulls remote to local
38
38
  mina data_sync:push # pushes local to remote
39
39
  ```
40
40
 
41
+ You can use the only parts of the sync:
42
+
43
+ ``` ruby
44
+ mina data_sync:dump_remote
45
+ mina data_sync:dump_local
46
+ mina data_sync:copy_local_to_remote
47
+ mina data_sync:copy_remote_to_local
48
+ mina data_sync:restore_remote
49
+ mina data_sync:restore_local
50
+ ```
51
+
41
52
  ## Configruation
42
53
 
43
54
  configurable variables with defaults
@@ -45,8 +56,7 @@ configurable variables with defaults
45
56
  set :database_path, "config/database.yml"
46
57
  set :remote_backup_path, 'tmp'
47
58
  set :local_backup_path, -> { ENV['DATA_SYNC_BACKUP_PATH'] || 'tmp' }
48
- set :restore_data, -> { ENV['restore'] || 'true' } # if false will not restore backup
49
- set :dump_data, -> { ENV['dump'] || 'true' } # if false will not dump and copy backup
59
+ set :backup_file, -> { %{#{fetch(:repository).split('/').last.split('.').first}-#{fetch(:rails_env)}-#{Date.today}.sql} }
50
60
  ```
51
61
 
52
62
  ## Contributing
@@ -1,5 +1,4 @@
1
1
  set :database_path, 'config/database.yml'
2
2
  set :remote_backup_path, 'tmp'
3
3
  set :local_backup_path, -> { ENV['DATA_SYNC_BACKUP_PATH'] || 'tmp' }
4
- set :restore_data, -> { ENV['restore'] || 'true' }
5
- set :dump_data, -> { ENV['dump'] || 'true' }
4
+ set :backup_file, -> { ENV['DATA_SYNC_BACKUP_FILE'] || %{#{fetch(:repository).split('/').last.split('.').first}-#{fetch(:rails_env)}-#{Date.today}.sql} }
@@ -36,10 +36,33 @@ DATA_SYNC = <<-BASH
36
36
  };
37
37
  BASH
38
38
 
39
- def config
40
- "#{fetch(:rails)} runner 'puts ActiveRecord::Base.connection.instance_variable_get(:@config).to_json'"
39
+ def dump_restore(rails_root, backup_path, mode: :dump, backend: :local)
40
+ set :rails_env, 'development' if backend == :local
41
+ comment %{#{mode == :dump ? 'Dumping' : 'Restoring'} database}
42
+ command %{cd #{rails_root}}
43
+ command DATA_SYNC.to_s
44
+ command %{mkdir -p #{backup_path}}
45
+ command %{CONFIG=$(#{fetch(:rails)} runner "puts ActiveRecord::Base.connection.instance_variable_get(:@config).to_json")}
46
+ comment %{$(data_sync "#{mode}" "#{backup_path}/#{fetch(:backup_file)}" "$CONFIG")}
47
+ command %{eval $(data_sync "#{mode}" "#{backup_path}/#{fetch(:backup_file)}" "$CONFIG")}
41
48
  end
42
49
 
43
- def backup_file
44
- "#{fetch(:repository).split('/').last.split('.').first}-#{fetch(:rails_env)}-#{Date.today}.sql"
50
+ def rsync_db(mode: :remote_to_local)
51
+ if mode == :remote_to_local
52
+ run :local do
53
+ comment %{Copying backup}
54
+ command %{mkdir -p #{fetch(:local_backup_path)}}
55
+ comment %{Backup: #{fetch(:local_backup_path)}/#{fetch(:backup_file)}}
56
+ command %{rsync --progress -e "ssh -p #{fetch(:port)}" #{fetch(:user)}@#{fetch(:domain)}:#{fetch(:current_path)}/#{fetch(:remote_backup_path)}/#{fetch(:backup_file)} #{fetch(:local_backup_path)}/#{fetch(:backup_file)}}
57
+ end
58
+ else
59
+ run :remote do
60
+ command %{mkdir -p #{fetch(:current_path)}/#{fetch(:remote_backup_path)}}
61
+ end
62
+ run :local do
63
+ comment %{Copying backup}
64
+ comment %{Backup: #{fetch(:remote_backup_path)}/#{fetch(:backup_file)}}
65
+ command %{rsync --progress -e "ssh -p #{fetch(:port)}" #{fetch(:local_backup_path)}/#{fetch(:backup_file)} #{fetch(:user)}@#{fetch(:domain)}:#{fetch(:current_path)}/#{fetch(:remote_backup_path)}/#{fetch(:backup_file)}}
66
+ end
67
+ end
45
68
  end
@@ -1,53 +1,46 @@
1
1
  namespace :data_sync do
2
- task setup_sync: :environment do
3
- read_conf(database_path, rails_env)
2
+ task :dump_remote do
3
+ run :remote do
4
+ dump_restore(fetch(:current_path), fetch(:remote_backup_path), mode: :dump, backend: :remote)
5
+ end
6
+ end
7
+
8
+ task :dump_local do
9
+ run :local do
10
+ dump_restore('.', fetch(:local_backup_path), mode: :dump, backend: :local)
11
+ end
12
+ end
13
+
14
+ task :copy_local_to_remote do
15
+ rsync_db(mode: :local_to_remote)
4
16
  end
5
17
 
6
- task pull: :setup_sync do
18
+ task :copy_remote_to_local do
19
+ rsync_db(mode: :remote_to_local)
20
+ end
21
+
22
+ task :restore_remote do
23
+ exit unless TTY::Prompt.new.yes?("Are you sure you want to restore #{fetch(:rails_env)} database?")
7
24
  run :remote do
8
- comment 'Dumping database'
9
- command "cd #{fetch(:current_path)}"
10
- command "#{DATA_SYNC}"
11
- command "mkdir -p #{fetch(:remote_backup_path)}"
12
- command "CONFIG=$(#{fetch(:rails)} runner 'puts ActiveRecord::Base.connection.instance_variable_get(:@config).to_json')"
13
- command %(data_sync "dump" "#{fetch(:remote_backup_path)}/#{fetch(:backup_file)}" "$CONFIG")
14
- command %(eval $(data_sync "dump" "#{fetch(:remote_backup_path)}/#{fetch(:backup_file)}" "$CONFIG"))
15
- end if fetch(:dump_data) == 'true'
25
+ dump_restore(fetch(:current_path), fetch(:remote_backup_path), mode: :restore, backend: :remote)
26
+ end
27
+ end
16
28
 
29
+ task :restore_local do
17
30
  run :local do
18
- if fetch(:dump_data) == 'true'
19
- comment 'Copying backup'
20
- command "mkdir -p #{fetch(:local_backup_path)}"
21
- command "rsync --progress -e 'ssh -p #{fetch(:port)}' #{fetch(:user)}@#{fetch(:domain)}:#{fetch(:current_path)}/#{fetch(:remote_backup_path)}/#{fetch(:backup_file)} #{fetch(:local_backup_path)}/#{fetch(:backup_file)}"
22
- end
23
- if fetch(:restore_data) == 'true'
24
- comment 'Restoring database'
25
- command "#{DATA_SYNC}"
26
- command "CONFIG=$(RAILS_ENV=development #{fetch(:bundle_bin)} exec rails runner 'puts ActiveRecord::Base.connection.instance_variable_get(:@config).to_json')"
27
- command %(data_sync "restore" "#{fetch(:local_backup_path)}/#{fetch(:backup_file)}" "$CONFIG")
28
- command %(eval $(data_sync "restore" "#{fetch(:local_backup_path)}/#{fetch(:backup_file)}" "$CONFIG"))
29
- end
31
+ dump_restore('.', fetch(:local_backup_path), mode: :restore, backend: :local)
30
32
  end
31
33
  end
32
34
 
33
- task push: :setup_sync do
34
- run :local do
35
- comment 'Dumping database'
36
- command "#{DATA_SYNC}"
37
- command "CONFIG=$(RAILS_ENV=development #{fetch(:bundle_bin)} exec rails runner 'puts ActiveRecord::Base.connection.instance_variable_get(:@config).to_json')"
38
- command %(data_sync "dump" "#{fetch(:local_backup_path)}/#{fetch(:backup_file)}" "$CONFIG")
39
- command %(eval $(data_sync "dump" "#{fetch(:local_backup_path)}/#{fetch(:backup_file)}" "$CONFIG"))
40
- comment 'Copying backup'
41
- command "rsync --progress -e 'ssh -p #{fetch(:port)}' #{fetch(:local_backup_path)}/#{fetch(:backup_file)} #{fetch(:user)}@#{fetch(:domain)}:#{fetch(:current_path)}/#{fetch(:remote_backup_path)}/#{fetch(:backup_file)}"
42
- end if fetch(:dump_data) == 'true'
35
+ task :pull do
36
+ invoke :'data_sync:dump_remote'
37
+ invoke :'data_sync:copy_remote_to_local'
38
+ invoke :'data_sync:restore_local'
39
+ end
43
40
 
44
- run :remote do
45
- comment 'Restoring database'
46
- command "cd #{fetch(:current_path)}"
47
- command "#{DATA_SYNC}"
48
- command "CONFIG=$(#{fetch(:rails)} runner 'puts ActiveRecord::Base.connection.instance_variable_get(:@config).to_json')"
49
- command %(data_sync "restore" "#{fetch(:remote_backup_path)}/#{fetch(:backup_file)}" "$CONFIG")
50
- command %(eval $(data_sync "restore" "#{fetch(:remote_backup_path)}/#{fetch(:backup_file)}" "$CONFIG"))
51
- end if fetch(:restore_data) == 'true'
41
+ task :push do
42
+ invoke :'data_sync:dump_local'
43
+ invoke :'data_sync:copy_local_to_remote'
44
+ invoke :'data_sync:restore_remote'
52
45
  end
53
46
  end
@@ -1,5 +1,5 @@
1
1
  module Mina
2
2
  module DataSync
3
- VERSION = '1.0.0.beta2'
3
+ VERSION = '1.0.0.beta3'
4
4
  end
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'erb'
3
+ require 'tty-prompt'
3
4
  require 'mina/data_sync/defaults'
4
5
  require 'mina/data_sync/helpers'
5
6
  require 'mina/data_sync/version'
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "pry", "~> 0"
24
24
 
25
25
  spec.add_dependency 'mina', '~> 1.0.0.beta2'
26
+ spec.add_dependency 'tty-prompt'
26
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mina-data_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta2
4
+ version: 1.0.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stjepan Hadjic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-07 00:00:00.000000000 Z
11
+ date: 2016-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.0.0.beta2
69
+ - !ruby/object:Gem::Dependency
70
+ name: tty-prompt
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Description
70
84
  email:
71
85
  - d4be4st@gmail.com
@@ -74,6 +88,7 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
91
+ - ".rubocop.yml"
77
92
  - Gemfile
78
93
  - LICENSE.txt
79
94
  - README.md