Module: StorageHelper

Defined in:
lib/capistrano/ops/rails/lib/tasks/storage/storage_helper.rb

Instance Method Summary collapse

Instance Method Details

#backup_cmd(settings = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/capistrano/ops/rails/lib/tasks/storage/storage_helper.rb', line 33

def backup_cmd(settings = {})
  @backup_path = settings[:backup_path]
  @date = Time.now.to_i
  @filename = "storage_#{@date}.tar.gz"
  FileUtils.mkdir_p(@backup_path) unless Dir.exist?(@backup_path)
  "tar -zcf #{@backup_path}/#{@filename} -C #{settings[:storage_path]} ."
end

#configurationObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/capistrano/ops/rails/lib/tasks/storage/storage_helper.rb', line 4

def configuration
  @configuration ||=
    {
      backup_path: path_resolver('backups'),
      storage_path: path_resolver('storage'),
      backups_enabled: env_or_production('BACKUPS_ENABLED'),
      external_backup: env_or_production('EXTERNAL_BACKUP_ENABLED'),
      keep_local_backups: env_or_production('KEEP_LOCAL_STORAGE_BACKUPS'),
      backup_provider: ENV['BACKUP_PROVIDER']
    }
end

#create_local_backup(filename, storage_path, backup_path) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/capistrano/ops/rails/lib/tasks/storage/storage_helper.rb', line 48

def create_local_backup(filename, storage_path, backup_path)
  FileUtils.mkdir_p(backup_path) unless Dir.exist?(backup_path)
  response = system(backup_cmd(backup_path: backup_path, storage_path: storage_path))
  FileUtils.rm_rf("#{@backup_path}/#{filename}") unless response
  puts response ? "Backup created: #{backup_path}/#{@filename} (#{size_str(File.size("#{@backup_path}/#{@filename}"))})" : 'Backup failed removing dump file'
  response
end

#message(result = false, settings = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/capistrano/ops/rails/lib/tasks/storage/storage_helper.rb', line 20

def message(result = false, settings = {})
  @backup_path = settings[:backup_path]
  @filename = settings[:filename]
  messages = []
  if result
    messages << "Backup of storage folder successfully finished at #{Time.now}"
    messages << "Backup path:\`#{@backup_path}/#{@filename}\`"
  else
    messages << "Backup of storage folder failed at #{Time.now}"
  end
  messages.join("\n")
end

#size_str(size) ⇒ Object



41
42
43
44
45
46
# File 'lib/capistrano/ops/rails/lib/tasks/storage/storage_helper.rb', line 41

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

#titleObject



16
17
18
# File 'lib/capistrano/ops/rails/lib/tasks/storage/storage_helper.rb', line 16

def title
  ENV['DEFAULT_URL'] || "#{Rails.env} Backup"
end