Sha256: 80a89aba5c726fc457d2dc05a7a281c2081228cd5bdba1d7e654184b7684030d

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require "daily_weekly_monthly/processor"
require "daily_weekly_monthly/downloader"
require "daily_weekly_monthly/notifier"

module DailyWeeklyMonthly
  class Runner
    DEFAULTS = {
      backups_dir: File.expand_path("~/backups"),
      output_extension: "pgdump.gz",
      day_of_week: 1,
      day_of_month: 1,
      days_to_keep: 7,
      weeks_to_keep: 5,
      months_to_keep: 12,
      smtp_server: false,
      smtp_port: false,
      notify: false,
    }.freeze

    def initialize backup_command, options = {}
      @backup_command = backup_command
      @options = DEFAULTS.merge(options)
    end

    def call
      processor = Processor.new(backup, @options[:backups_dir], @options[:output_extension])
      processor.call("daily", keep: @options[:days_to_keep])
      processor.call("weekly", keep: @options[:weeks_to_keep]) if weekly_backup?
      processor.call("monthly", keep: @options[:months_to_keep]) if monthly_backup?
    end

    private

    def weekly_backup?
      Date.today.wday == @options[:day_of_week]
    end

    def monthly_backup?
      Date.today.day == @options[:day_of_month]
    end

    def backup
      Downloader.new(@backup_command).call
    rescue StandardError => e
      Notifier.new(@options[:smtp_server], @options[:smtp_port]).call(e, @options[:notify]) if @options[:notify]
      raise e
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
daily_weekly_monthly-0.0.4 lib/daily_weekly_monthly/runner.rb
daily_weekly_monthly-0.0.3 lib/daily_weekly_monthly/runner.rb