Sha256: cf168e791d11bb5759bf4a99c5d1421f9e379b4afc9ddf57d84ba456d796b104

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require_relative './postgres_helper'
namespace :pg do
  include PostgresHelper

  task :dump do
    backup_path = configs[:backup_path]
    backups_enabled = configs[:backups_enabled]
    external_backup = configs[:external_backup]
    database = configs[:database]
    date = Time.now.to_i
    filename = "#{database}_#{date}.dump"

    unless backups_enabled
      puts 'dump: Backups are disabled'
      exit(0)
    end

    notification = Notification::Api.new
    commandlist = dump_cmd(configs)

    system "mkdir -p #{backup_path}" unless Dir.exist?(backup_path)

    result = system(commandlist)

    if ENV['BACKUP_PROVIDER'].present? && external_backup && result
      puts "Uploading #{filename} to #{ENV['BACKUP_PROVIDER']}..."
      provider = Backup::Api.new
      begin
        provider.upload("#{backup_path}/#{filename}", filename.to_s, 'file')
        puts "#{filename} uploaded to #{ENV['BACKUP_PROVIDER']}"
      rescue StandardError => e
        puts "#{filename} upload failed: #{e.message}"
      end
    end
    notification.send_backup_notification(result, title, content(result, { database: database, backup_path: backup_path, filename: filename }),
                                          { date: date, backup_path: backup_path, database: database })
    puts result ? "Backup created: #{backup_path}/#{filename} (#{size_str(File.size("#{backup_path}/#{filename}"))})" : 'Backup failed removing dump file'

    system "rm #{backup_path}/#{filename}" unless result
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
capistrano-ops-1.0.8 lib/capistrano/ops/rails/lib/tasks/pg/dump.rake
capistrano-ops-1.0.7 lib/capistrano/ops/rails/lib/tasks/pg/dump.rake