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') ...@@ -73,8 +73,6 @@ f = File.open('smtp.yml')
SMTP = YAML::load( f ) SMTP = YAML::load( f )
f.close f.close
puts SMTP.inspect
# =========== END OF CUSTOMIZABLE SETTINGS ============= # =========== END OF CUSTOMIZABLE SETTINGS =============
# Server urls. Used for obtaining the list of svn repositories # Server urls. Used for obtaining the list of svn repositories
...@@ -89,14 +87,18 @@ REMOTE_REPOSITORY_URLS = { ...@@ -89,14 +87,18 @@ REMOTE_REPOSITORY_URLS = {
'production' => 'http://svn.ohwr.org' 'production' => 'http://svn.ohwr.org'
} }
def title(text, level = 0) @log_io = StringIO.new()
puts
log text, level
puts
end
def log(message, level = 0) 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 end
def remote_repository_url(repository) def remote_repository_url(repository)
...@@ -125,8 +127,9 @@ end ...@@ -125,8 +127,9 @@ end
def send_email(subject, body) def send_email(subject, body)
Pony.mail( Pony.mail(
:to => ['to'], :to => SMTP['to'],
:subject => "Error in OHWR backup: #{subject}", :cc => SMTP['cc'],
:subject => subject,
:body => body, :body => body,
:via => :smtp, :via => :smtp,
:attachments => [], :attachments => [],
...@@ -147,13 +150,15 @@ def shell(command) ...@@ -147,13 +150,15 @@ def shell(command)
err = StringIO.new err = StringIO.new
status = Open4::popen4(command) do |pid, stdin, stdout, stderr| status = Open4::popen4(command) do |pid, stdin, stdout, stderr|
@log_io << stdout.read.strip
err << stderr.read.strip err << stderr.read.strip
end end
if status.exitstatus == 0 then if status.exitstatus == 0 then
log "#{command} - ok" log "#{command} - ok"
else 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}") fail("Command failed(#{status.exitstatus}) - #{command}:\n#{err.string}")
end end
end end
...@@ -207,7 +212,7 @@ namespace :backup do ...@@ -207,7 +212,7 @@ namespace :backup do
task :prepare_folders do task :prepare_folders do
title "Preparing local folders" 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| [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
end end
...@@ -261,7 +266,13 @@ namespace :backup do ...@@ -261,7 +266,13 @@ namespace :backup do
rdiff_get '/home/sympa/list_data', File.join(LOCAL_SYMPA_PATH,'list_data') rdiff_get '/home/sympa/list_data', File.join(LOCAL_SYMPA_PATH,'list_data')
end 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)" 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 end
to: 'egarcia@splendeo.es' to: 'support@splendeo.es'
cc: 'fjuan@splendeo.es'
server: 'smtp.gmail.com' server: 'smtp.gmail.com'
port: '587' port: '587'
user_name: 'contact@splendeo.es' 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