class SimpleBackup::Utils::Mailer

Public Class Methods

new() click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 9
def initialize()
  @to = []
  @cc = []
  @bcc = []
  @hostname = Socket.gethostbyname(Socket.gethostname).first
end

Public Instance Methods

bcc(bcc) click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 32
def bcc(bcc)
  @bcc << bcc
end
cc(cc) click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 28
def cc(cc)
  @cc << cc
end
from(from) click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 20
def from(from)
  @from = from
end
send() click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 36
def send
  @@logger.info "Setting sender to: #{@from}"
  from = @from
  @@logger.scope_start :info, "Adding recipients:"
  to = @to
  to.each do |mail|
    @@logger.info "to: #{mail}"
  end
  cc = @cc
  cc.each do |mail|
    @@logger.info "cc: #{mail}"
  end
  bcc = @bcc
  bcc.each do |mail|
    @@logger.info "bcc: #{mail}"
  end
  @@logger.scope_end

  @subject_prefix += '[FAILED]' if SimpleBackup.status == :failed

  subject = "%s Backup %s for %s" % [@subject_prefix, TIMESTAMP, @hostname]
  @@logger.debug "Subject: #{subject}"

  body = get_body

  mail = Mail.new do
    from    from
    to      to
    cc      cc
    bcc     bcc
    subject subject.strip
    body    body
  end

  mail.delivery_method :sendmail
  @@logger.debug "Setting delivery method to sendmail"

  mail.deliver
  @@logger.info "Notification sent"
end
subject_prefix(prefix) click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 16
def subject_prefix(prefix)
  @subject_prefix = prefix
end
to(to) click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 24
def to(to)
  @to << to
end

Private Instance Methods

disk_usage() click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 110
def disk_usage
  content = "%16s %25s %12s %12s %12s  %12s\n" % ['Mount', 'Filesystem', 'Size', 'Used', 'Available', 'Percent used']

  usage = Utils::Disk::usage
  usage[:mounts].each do |m|
    percent_usage = (m[:percent] * 100).to_s
    percent_usage = '(!!) ' + percent_usage if m[:high_usage_exceeded]
    content += "%16s %25s %8s MiB %8s MiB %8s MiB  %11s%%\n" % [m[:mount], m[:fs], m[:size], m[:used], m[:available], percent_usage]
  end

  content += "\nHigh usage treshold exceeded!\nMax usage is #{usage[:high_usage]} where treshold is set to #{Utils::Disk::high_usage_treshold}\n" if usage[:high_usage_exceeded]

  content
end
get_body() click to toggle source
# File lib/simple_backup/utils/mailer.rb, line 78
      def get_body
        sources = ''

        SimpleBackup::Sources.instance.each do |name, source|
          sources += " - %s\n" % source.desc
        end

        body = <<MAIL
Hi,

Backup #{TIMESTAMP} was created!

Backup contains:
#{sources}
Disk usage after backup:
#{disk_usage}
Backup log:
------------
#{@@logger.buffer.join("\n")}
------------

Have a nice day,
  SimpleBackup

-- 
Mail was sent automatically
Do not respond!
MAIL

        body
      end