Last active
May 3, 2016 09:33
-
-
Save johndel/281eb0ef17fe7d5567a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 '[email protected]' | |
from '[email protected]' | |
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