Skip to content

Instantly share code, notes, and snippets.

@johndel
Last active May 3, 2016 09:33
Show Gist options
  • Save johndel/281eb0ef17fe7d5567a1 to your computer and use it in GitHub Desktop.
Save johndel/281eb0ef17fe7d5567a1 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'aws-sdk'
require 'mail'
# The filename for using it during the script and the location
filename = "#{Time.now.to_s.split(" ").first}_data_only_dump.sql"
store_location = "/home/your_user/your_backups/#{filename}"
# Local backup of a postgres database
`pg_dump -U your_user your_db_name -f #{store_location} --data-only`
# AWS Configuration
AWS.config( :access_key_id => 'your_access_key_id',
:secret_access_key => 'your_secret_access_key',
:region => 'eu-west-1')
bucket = "your_bucket/"
base_name = File.basename(filename)
# Upload to S3
s3 = AWS::S3.new
s3.buckets[bucket].objects[base_name].write(:file => filename)
# Send me an email, using gmail
options = { :address => "smtp.gmail.com",
:port => 587,
:user_name => 'your_username@your_domain.whatever',
:password => 'your_password',
:authentication => 'plain',
:enable_starttls_auto => true }
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
to 'whatever@whatever.whatever'
from 'whatever@whatever.whatever'
subject "Whatever subject db backup "
body "Whatever body"
add_file store_location
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment