Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Created March 4, 2014 12:40
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save emad-elsaid/9345762 to your computer and use it in GitHub Desktop.
Save emad-elsaid/9345762 to your computer and use it in GitHub Desktop.
backup mysql database and send it to your email script
#!/usr/bin/env ruby
require 'mail'
mysql_username = 'root'
mysql_password = '123456'
mysql_database = 'test'
system("mysqldump --user=#{mysql_username} --password=#{mysql_password} #{mysql_database} > backup.sql")
# Credit to :
# http://stackoverflow.com/questions/12884711/how-to-send-email-via-smtp-with-rubys-mail-gem
options = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'xxxxxxxxx@gmail.com',
:user_name => 'xxxxxxxxx@gmail.com',
:password => 'xxxxxxxxx',
:authentication => 'plain',
:enable_starttls_auto => true
}
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
from options[:user_name]
to options[:user_name]
subject "Database #{mysql_database} backup #{Time.new}"
body "Database #{mysql_database} backup #{Time.new}"
add_file 'backup.sql'
end
File.delete 'backup.sql'
@vkroll
Copy link

vkroll commented Mar 4, 2014

Maybe a good idea to compress the output.

@emad-elsaid
Copy link
Author

yes sure, there is a lot of enhancements, feel free to fork and edit it i'll be more than happy.

@wyanez
Copy link

wyanez commented Mar 5, 2014

Thanks! very good, I did some small enhancements,i will fork and share.

@emad-elsaid
Copy link
Author

Great

@persianmcse
Copy link

2:in `require': no such file to load -- mail (LoadError) :( error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment