Commit e6c3c681 authored by egarcia's avatar egarcia

backup now sends sucess + error emails with logs

parent ff534331
......@@ -73,8 +73,6 @@ f = File.open('smtp.yml')
SMTP = YAML::load( f )
f.close
puts SMTP.inspect
# =========== END OF CUSTOMIZABLE SETTINGS =============
# Server urls. Used for obtaining the list of svn repositories
......@@ -89,14 +87,18 @@ REMOTE_REPOSITORY_URLS = {
'production' => 'http://svn.ohwr.org'
}
def title(text, level = 0)
puts
log text, level
puts
end
@log_io = StringIO.new()
def log(message, level = 0)
puts "#{'==' * level}> #{message}"
text = "#{'==' * level}> #{message}"
@log_io.puts text
puts text
end
def title(text, level = 0)
log ""
log text, level
log ""
end
def remote_repository_url(repository)
......@@ -125,8 +127,9 @@ end
def send_email(subject, body)
Pony.mail(
:to => ['to'],
:subject => "Error in OHWR backup: #{subject}",
:to => SMTP['to'],
:cc => SMTP['cc'],
:subject => subject,
:body => body,
:via => :smtp,
:attachments => [],
......@@ -147,13 +150,15 @@ def shell(command)
err = StringIO.new
status = Open4::popen4(command) do |pid, stdin, stdout, stderr|
@log_io << stdout.read.strip
err << stderr.read.strip
end
if status.exitstatus == 0 then
log "#{command} - ok"
else
send_email("(#{status.exitstatus}) - #{command}", err.string)
send_email("ERROR in OHWR backup (#{status.exitstatus}) - #{command}", "#{err.string}\n\nlog:\n#{@log_io.string}")
title("ERROR")
fail("Command failed(#{status.exitstatus}) - #{command}:\n#{err.string}")
end
end
......@@ -207,7 +212,7 @@ namespace :backup do
task :prepare_folders do
title "Preparing local folders"
[LOCAL_REPOSITORY_PATH, LOCAL_GIT_PATH, LOCAL_DB_PATH, LOCAL_FILES_PATH, File.join(LOCAL_SYMPA_PATH,'arc'), File.join(LOCAL_SYMPA_PATH,'list_data')].each do |path|
shell "mkdir #{path}"
shell "mkdir -p #{path}"
end
end
......@@ -261,7 +266,13 @@ namespace :backup do
rdiff_get '/home/sympa/list_data', File.join(LOCAL_SYMPA_PATH,'list_data')
end
desc "Sends an email saying - everything went ok"
task :send_ok_email do
title "Sending OK email"
send_email("OHWR backup sucessful", "complete log: \n #{@log_io.string}")
end
desc "Implements all backup (svn+db+files+sympa)"
task :all => [:svn, :git, :db, :files, :sympa]
task :all => [:svn, :git, :db, :files, :sympa, :send_ok_email]
end
to: 'egarcia@splendeo.es'
to: 'support@splendeo.es'
cc: 'fjuan@splendeo.es'
server: 'smtp.gmail.com'
port: '587'
user_name: 'contact@splendeo.es'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment