Sha256: af4a9dc4f19244d6a9a9d2c82bb80232473c909a8d8d800b6571e3bb5f7bc290
Contents?: true
Size: 1.82 KB
Versions: 1
Compression:
Stored size: 1.82 KB
Contents
require 'aws-sdk' namespace :db do desc "Cria backup do banco e faz upload para S3" task dump_upload: :environment do def available_cmd cmd_name `type #{cmd_name}` $?.success? end def backup host = ActiveRecord::Base.connection_config[:host] || 'localhost' database = ActiveRecord::Base.connection_config[:database] user = ActiveRecord::Base.connection_config[:username] pass = ActiveRecord::Base.connection_config[:password] || "" path = Rails.root.join('backup') FileUtils.mkdir_p path arquivo = DateTime.now.strftime("%Y%m%d%H%M%S")+"_"+database caminho = File.join(path, arquivo) case ActiveRecord::Base.connection_config[:adapter] when 'mysql2' raise 'Adapter Mysql não instalado no sistema' unless available_cmd 'mysqldump' cmd = "mysqldump -h#{host} -u#{user} -p#{pass} #{database} > #{caminho}.dump" when 'postgresql' raise 'Adapter PostgreSQL não instalado no sistema' unless available_cmd 'pg_dump' cmd = "pg_dump -h #{host} -U #{user} -W #{pass} --format=c #{database} > #{caminho}.dump" when 'sqlite3' cmd = "cp #{Rails.root.join(database)} #{caminho}.dump" else raise "Adapter não suportado" end system cmd system "cd #{path}; tar -czf #{arquivo}.tar.gz #{arquivo}.dump" FileUtils.rm_f("#{caminho}.dump") "#{caminho}.tar.gz" end def upload path Aws.config.update({ region: ENV['AWS_REGION'], credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']) }) file = File.open(path) bucket = Aws::S3::Resource.new.bucket(ENV['AWS_BUCKET']) bucket.object(File.basename(path)).upload_file(file) file.close FileUtils.rm_f(path) end upload(backup) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
raro_generator-0.1.0 | lib/generators/backup/s3/templates/s3_backup.rake |