Module: Capistrano::Ops::Backup::Helper

Defined in:
lib/capistrano/ops/backup/helper.rb

Instance Method Summary collapse

Instance Method Details

#backup_file_name(type) ⇒ Object



7
8
9
10
# File 'lib/capistrano/ops/backup/helper.rb', line 7

def backup_file_name(type)
  regex = type == 'storage' ? "'.{0,}\.tar.gz'" : "'.{0,}\.dump'"
  @backup_file_name ||= capture "cd #{shared_path}/backups && ls -lt | grep -E -i #{regex} | head -n 1 | awk '{print $9}'"
end

#backup_file_sizeObject



12
13
14
# File 'lib/capistrano/ops/backup/helper.rb', line 12

def backup_file_size
  @backup_file_size ||= capture "cd #{shared_path}/backups && wc -c #{@backup_file_name} | awk '{print $1}'"
end

#cleanup_backup(backup_file, message) ⇒ Object



23
24
25
26
27
# File 'lib/capistrano/ops/backup/helper.rb', line 23

def cleanup_backup(backup_file, message)
  puts message
  execute "cd #{shared_path}/backups && rm #{backup_file}"
  puts 'Temporary backup deleted'
end

#download_backup(backup_file, type) ⇒ Object



16
17
18
19
20
21
# File 'lib/capistrano/ops/backup/helper.rb', line 16

def download_backup(backup_file, type)
  puts "Downloading #{type} backup"
  download! "#{shared_path}/backups/#{backup_file}", backup_file
  puts "Download finished\nDeleting temporary backup..."
  cleanup_backup(backup_file, "Download finished\nDeleting temporary backup...")
end

#prepare_envObject



41
42
43
44
45
# File 'lib/capistrano/ops/backup/helper.rb', line 41

def prepare_env
  @env = "RAILS_ENV=#{fetch(:stage)}"
  @path_cmd = "PATH=$HOME/.rbenv/versions/#{RUBY_VERSION}/bin:$PATH"
  @test_command = "cd #{release_path} && #{@path_cmd} && #{@env}"
end

#question(question, default = 'n', &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/capistrano/ops/backup/helper.rb', line 29

def question(question, default = 'n', &block)
  print "#{question} #{default.downcase == 'n' ? '(y/N)' : '(Y/n)'}: "
  input = $stdin.gets.strip.downcase
  answer = (input.empty? ? default : input).downcase.to_s

  if %w[y n].include?(answer)
    yield(answer == 'y')
  else
    question(question, default, &block)
  end
end

#size_str(size) ⇒ Object



47
48
49
50
51
52
# File 'lib/capistrano/ops/backup/helper.rb', line 47

def size_str(size)
  units = %w[B KB MB GB TB]
  e = (Math.log(size) / Math.log(1024)).floor
  s = format('%.2f', size.to_f / 1024**e)
  s.sub(/\.?0*$/, units[e])
end