Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: milestone-2013…
Fetching contributors…

Cannot retrieve contributors at this time

executable file 43 lines (37 sloc) 1.419 kb
#!/usr/bin/env ruby
require File.expand_path(File.dirname(__FILE__) + '/shared')
BACKUP_DIR_ROOT = "/var/backups/mysql"
MAX_BACKUPS = optional_config('max-backups', 10)
if enc = optional_config(:encrypt)
abort "*** ERROR: please set the configuration option backup-mysql.encrypt.key" if !enc['key']
abort "*** ERROR: please set the configuration option backup-mysql.encrypt.recipients" if !enc['recipients'] || enc['recipients'].empty?
encrypt_command = "gpg --sign --encrypt -u '#{enc['key']}'"
enc['recipients'].each do |recipient|
encrypt_command << " --recipient '#{recipient}'"
end
end
databases = `echo show databases | mysql`.strip.split("\n")
databases.shift
databases.delete("information_schema")
databases.delete("mysql")
now = Time.now.strftime("%Y-%m-%d-%H:%M:%S")
backup_dir = "#{BACKUP_DIR_ROOT}/#{now}"
sh "mkdir -p #{backup_dir}"
for database in databases
dump_command = "mysqldump --single-transaction -C #{database} | gzip --best"
if encrypt_command
dump_command << " | #{encrypt_command}"
extension = "sql.gz.gpg"
else
extension = "sql.gz"
end
sh "#{dump_command} | #{pv_or_cat} > #{backup_dir}/#{database}.#{extension}"
end
puts "Cleaning up, keeping only #{MAX_BACKUPS} most recent backups..."
dirs = Dir["#{BACKUP_DIR_ROOT}/*"].sort.reverse
keep = dirs[0..MAX_BACKUPS]
delete = dirs - keep
delete.each do |dir|
sh "rm -rf #{dir}"
end
sh "chmod -R o-rwx #{BACKUP_DIR_ROOT}"
Jump to Line
Something went wrong with that request. Please try again.