Skip to content

Instantly share code, notes, and snippets.

@maran
Created January 14, 2009 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maran/47045 to your computer and use it in GitHub Desktop.
Save maran/47045 to your computer and use it in GitHub Desktop.
class SimpleMailer
require 'net/smtp'
attr_accessor :email_from, :email_to
def initialize(email_from, email_to)
@email_from, @email_to = email_from, email_to
end
def happy_mail
message = <<MSG
From: #{@email_from}
To: #{@email_to}
Subject: Succesfully synced database
On #{Time.now.strftime("%d-%m-%Y %H:%M")} the staging database was successfully synced.
MSG
mail(message)
end
def sad_mail(problem)
message = <<MSG
From: #{@email_from}
To: #{@email_to}
Subject: Problem syncing database
On #{Time.now.strftime("%d-%m-%Y %H:%M")} the staging database was NOT synced!
Problem: #{problem}
MSG
mail(message)
end
def mail(message)
Net::SMTP.start('crunch1.c1.noxa.nl') do |smtp|
smtp.send_message message, @email_from, @email_to
end
end
end
# 0) Setup
$DATE_FORMAT = "%Y%m%d"
$BACKUP_FILENAME = "mysecret-database-daily-#{Time.now.strftime($DATE_FORMAT)}.sql.gz"
$BACKUP_LOCATION = "/mnt/backups/"
$USERNAME = "ninja"
$PASSWORD = "pirate"
$DATABASE = "japanese_boat"
$MAIL_FROM = "aaarrrgh@brightlight-ict.nl"
$MAIL_TO = "maran@brightlight-ict.nl"
mailer = SimpleMailer.new($MAIL_FROM, $MAIL_TO)
# 1) Get backup from somewhere
unless system("cp #{$BACKUP_LOCATION}#{$BACKUP_FILENAME} ./ ")
mailer.sad_mail "Something went wrong while copying the backup, exiting..."
exit
end
# 2) Dump old tables
unless system("mysql --silent --skip-column-names -u #{$USERNAME} --password=\"#{$PASSWORD}\" #{$DATABASE} -e \"show tables\" | gawk '{print \"drop table \" $1 \";\"}' | mysql -u #{$USERNAME} --password=\"#{$PASSWORD}\" #{$DATABASE}")
mailer.sad_mail "Couldn't drop tables, exitings.."
exit
end
# 3) Import new database
unless system("zcat #{$BACKUP_FILENAME} | mysql -u #{$USERNAME} --password=#{$PASSWORD} #{$DATABASE}")
mailer.sad_mail "Couldn't import tables, exiting.."
exit
end
# 4) Everything went a-ok!
mailer.happy_mail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment