Skip to content

Instantly share code, notes, and snippets.

@mlawrie
Created September 12, 2012 22:20
Show Gist options
  • Save mlawrie/3710389 to your computer and use it in GitHub Desktop.
Save mlawrie/3710389 to your computer and use it in GitHub Desktop.
Simple encrypted backup for a Rails mysql database
#Generates aes-256 encrypted bz2-compressed mysql dumps named "[app name] ([timestamp]).backup"
#To decrypt:
#cat backupname.backup | openssl enc -d -aes-256-cbc -salt -pass pass:[password] > backupname.sql
require 'yaml'
require 'encryptor'
def backup(src, name)
puts "reading config from #{src}/config/database.yml"
config = YAML::load(File.open "#{src}/config/database.yml")
db = config["production"]["database"]
user = config["production"]["username"]
pass = config["production"]["password"].strip
encode_cmd = "bzip2 -c | openssl enc -aes-256-cbc -salt -pass pass:#{@encpass}"
dump_cmd = "mysqldump -u #{user} --password=#{pass} #{db}"
outfile = "\"#{name} (#{Time.now.utc}).backup\""
cmd = "#{dump_cmd} | #{encode_cmd} > #{outfile}"
puts "generating #{outfile}"
system(cmd)
system("chmod 500 #{outfile}")
end
@encpass = "something"
backup("/path/to/app", "Name of app")
backup("/path/to/other/app", "Other app")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment