/ Published in: Ruby
This handy little script will backup your website\'s database (MySQL) and files and put them into an amazon s3 bucket. You will need to make sure your host has the aws/s3 ruby gem installed. Can easily be extended to include the email folders as well. MAKE SURE TO PLACE IN A NON-WEB-ACCESSIBLE FOLDER! And don\'t forget to schedule the task to run via cron.
Expand |
Embed | Plain Text
#!/usr/bin/ruby require 'rubygems' require 'aws/s3' require 'net/smtp' AWS::S3::Base.establish_connection!( :access_key_id => 'Your S3 Access Key Goes Here', :secret_access_key => 'Your S3 Secret Access Key Goes Here' ) $bucket = 'bucket_name_goes_here' db_host = 'localhost' db_user = 'database_username' db_pass = 'database_password' db_name = 'database_name' site_dir = '/full/path/to/website/root' $backup_name = 'example_com_backup' $notify_on_error = true $notify_from = 'myself@example.com' $notify_to = 'myself@example.com' $notify_port = 25 $notify_server = 'mail.example.com' $notify_domain = 'example.com' $notify_user = 'myself+example.com' $notify_pass = 'emailpassword' $notify_authtype = :plain def store_file(file) file_base = File.basename(file) object_path = $backup_name+"/"+file_base response = AWS::S3::S3Object.store(object_path, open(file), $bucket) if response.error? and $notify_on_error error_email("Failed to store #{file}") end end def error_email(message) #http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html Net::SMTP.start($notify_server, $notify_port, $notify_domain, $notify_user, $notify_pass, $notify_authtype) do |smtp| smtp.open_message_stream($notify_from, [$notify_to]) do |f| f.puts "From: #{$notify_from}" f.puts "To: #{$notify_to}" f.puts 'Subject: Website Backup Script Error' f.puts message end end end #get database dump db_file = "#{$backup_name}.sql" if(system("mysqldump --add-drop-table --user=#{db_user} --password=#{db_pass} --host=#{db_host} #{db_name} > #{db_file}")) store_file(db_file) else error_email("Failed to create #{db_file}") end File.delete(db_file) #get filesystem tar.gz site_file = File.join(Dir.getwd, "#{$backup_name}.tar.gz") Dir.chdir(site_dir) if(system("tar -czf #{site_file} *")) store_file(site_file) else error_email("Failed to create #{site_file}") end File.delete(site_file)
You need to login to post a comment.
