raro_generator 0.0.4 → 0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f173151b914463165dbae8a629e1a8d9765c7df
4
- data.tar.gz: cf721a19d92fb86c8d93ce87c54f7f09888b585b
3
+ metadata.gz: 988eb855407871700443486cd0f80ac7a6a23b7b
4
+ data.tar.gz: 02498a6d71fa5f869ce0294b12e5cb46312bd6f0
5
5
  SHA512:
6
- metadata.gz: e1ef437fc5062110edb3aa07fdb1d8acc089f18e3b1761a9fd11e6de51ba4edd930d4c8094f510718d74eb0fd985f7fe32b1b98a67e61f90cee0455e3dd623d4
7
- data.tar.gz: dc53b01f24ea10435478b38f56f05f8293933ccdc15c2411d7b5d17beb74945cf7f97daf2b1268a9a9f056fc46da7cb41b158618633f7210a46e004ddbb9bf5c
6
+ metadata.gz: 97429298f9b62343b5f460f6bc1489c975083322df99b0378d277e7d7926421b73f6ee4f7e83fe3539991079cd15d3a33c8389a5ba91f99bc7439bec482451b7
7
+ data.tar.gz: 9ba42832e9582e6700c114055e4e9d1348bd1f3c608a90d8fdf720c43ebb924e72f50f0b3f8ec9fdac3e4245c8a632db14314ac4ec047ed5dce13b23f0c852b2
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Gera o rotina de backup do banco de dados do projeto
3
+
4
+ Example:
5
+ rails generate backup:s3
6
+
7
+ This will create:
8
+ arquivos para gerar backup
@@ -0,0 +1,41 @@
1
+ module Backup
2
+ class S3Generator < Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :name, type: :string, default: ""
5
+
6
+ def copy_initializer_file
7
+ template 's3_backup.rake', "lib/tasks/s3_backup.rake"
8
+ template 'schedule.rb', "config/schedule.rb"
9
+
10
+ append_file 'config/deploy.rb' do
11
+ <<-RUBY
12
+
13
+
14
+ set :whenever_command, "bundle exec whenever"
15
+ require "whenever/capistrano"
16
+ RUBY
17
+ end
18
+
19
+ inject_into_file 'config/deploy/production.rb', before: "load 'deploy/assets'" do
20
+ <<-RUBY
21
+ set :whenever_environment, 'production'
22
+
23
+ RUBY
24
+ end
25
+
26
+ append_file 'Gemfile' do
27
+ <<-RUBY
28
+
29
+ # Backup
30
+ gem 'aws-sdk', '~> 2'
31
+
32
+ # Crontab
33
+ gem 'whenever', require: false
34
+ RUBY
35
+ end
36
+ Bundler.with_clean_env do
37
+ run "bundle install"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,59 @@
1
+ require 'aws-sdk'
2
+
3
+ namespace :db do
4
+ desc "Cria backup do banco e faz upload para S3"
5
+ task dump_upload: :environment do
6
+
7
+ def available_cmd cmd_name
8
+ `type #{cmd_name}`
9
+ $?.success?
10
+ end
11
+
12
+ def backup
13
+ host = ActiveRecord::Base.connection_config[:host] || 'localhost'
14
+ database = ActiveRecord::Base.connection_config[:database]
15
+ user = ActiveRecord::Base.connection_config[:username]
16
+ pass = ActiveRecord::Base.connection_config[:password] || ""
17
+
18
+ path = Rails.root.join('backup')
19
+ FileUtils.mkdir_p path
20
+ arquivo = DateTime.now.strftime("%Y%m%d%H%M%S")+"_"+database
21
+ caminho = File.join(path, arquivo)
22
+
23
+ case ActiveRecord::Base.connection_config[:adapter]
24
+ when 'mysql2'
25
+ raise 'Adapter Mysql não instalado no sistema' unless available_cmd 'mysqldump'
26
+ cmd = "mysqldump -h#{host} -u#{user} -p#{pass} #{database} > #{caminho}.dump"
27
+ when 'postgresql'
28
+ raise 'Adapter PostgreSQL não instalado no sistema' unless available_cmd 'pg_dump'
29
+ cmd = "pg_dump -h #{host} -U #{user} -W #{pass} --format=c #{database} > #{caminho}.dump"
30
+ when 'sqlite3'
31
+ cmd = "cp #{Rails.root.join(database)} #{caminho}.dump"
32
+ else
33
+ raise "Adapter não suportado"
34
+ end
35
+
36
+ system cmd
37
+ system "cd #{path}; tar -czf #{arquivo}.tar.gz #{arquivo}.dump"
38
+ FileUtils.rm_f("#{caminho}.dump")
39
+ "#{caminho}.tar.gz"
40
+ end
41
+
42
+ def upload path
43
+ Aws.config.update({
44
+ region: ENV['AWS_REGION'],
45
+ credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
46
+ })
47
+
48
+ file = File.open(path)
49
+ bucket = Aws::S3::Resource.new.bucket(ENV['AWS_BUCKET'])
50
+
51
+ bucket.object(File.basename(path)).upload_file(file)
52
+ file.close
53
+ FileUtils.rm_f(path)
54
+ end
55
+
56
+ upload(backup)
57
+ end
58
+
59
+ end
@@ -0,0 +1,3 @@
1
+ every 1.day, at: '1:00 am' do
2
+ rake "db:dump_upload"
3
+ end
@@ -1,3 +1,3 @@
1
1
  module RaroGenerator
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raro_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonardo Herbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-05 00:00:00.000000000 Z
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -567,6 +567,10 @@ files:
567
567
  - lib/generators/assets/autocomplete/autocomplete_generator.rb
568
568
  - lib/generators/assets/precompile_local/USAGE
569
569
  - lib/generators/assets/precompile_local/precompile_local_generator.rb
570
+ - lib/generators/backup/s3/USAGE
571
+ - lib/generators/backup/s3/s3_generator.rb
572
+ - lib/generators/backup/s3/templates/s3_backup.rake
573
+ - lib/generators/backup/s3/templates/schedule.rb
570
574
  - lib/generators/deploy/homologacao/USAGE
571
575
  - lib/generators/deploy/homologacao/homologacao_generator.rb
572
576
  - lib/generators/deploy/homologacao/templates/deploy.rb
@@ -625,7 +629,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
625
629
  version: '0'
626
630
  requirements: []
627
631
  rubyforge_project:
628
- rubygems_version: 2.4.6
632
+ rubygems_version: 2.4.5.1
629
633
  signing_key:
630
634
  specification_version: 4
631
635
  summary: RaroGenerator.