backup task handles postgresql dumps
This commit is contained in:
parent
0c818cf359
commit
68d5f9d196
1 changed files with 29 additions and 14 deletions
|
|
@ -3,9 +3,9 @@ namespace :backup do
|
|||
require File.join(Rails.root, 'config', 'initializers', '_load_app_config.rb')
|
||||
require 'cloudfiles'
|
||||
|
||||
task :mysql do
|
||||
task :database do
|
||||
NUMBER_OF_DAYS = 3
|
||||
puts("event=backup status=start type=mysql")
|
||||
puts("event=backup status=start type=database")
|
||||
db = YAML::load(File.open(File.join(File.dirname(__FILE__), '..','..', 'config', 'database.yml')))
|
||||
user = db['production']['username']
|
||||
password = db['production']['password']
|
||||
|
|
@ -20,7 +20,21 @@ namespace :backup do
|
|||
puts "Logging into Cloud Files"
|
||||
|
||||
cf = CloudFiles::Connection.new(:username => AppConfig[:cloudfiles_username], :api_key => AppConfig[:cloudfiles_api_key])
|
||||
mysql_container = cf.container("MySQL Backup")
|
||||
|
||||
if db['production']['adapter'] == 'postgresql'
|
||||
container = cf.container("PostgreSQL Backup")
|
||||
|
||||
puts "Dumping PostgreSQL at #{Time.now.to_s}"
|
||||
`mkdir -p /tmp/backup/postgres`
|
||||
`PGPASSFILE=/etc/pgpass.conf nice pg_dump -h localhost -p 5432 -U #{user} #{database} > /tmp/backup/postgres/backup.txt `
|
||||
|
||||
puts "Gzipping dump at #{Time.now.to_s}"
|
||||
tar_name = "postgres_#{Time.now.to_i}.tar"
|
||||
`nice tar cfPz /tmp/backup/#{tar_name} /tmp/backup/postgres`
|
||||
|
||||
file = container.create_object(tar_name)
|
||||
elsif db['production']['adapter'] == 'mysql2'
|
||||
container = cf.container("MySQL Backup")
|
||||
|
||||
puts "Dumping Mysql at #{Time.now.to_s}"
|
||||
`mkdir -p /tmp/backup/mysql`
|
||||
|
|
@ -30,20 +44,21 @@ namespace :backup do
|
|||
tar_name = "mysql_#{Time.now.to_i}.tar"
|
||||
`nice tar cfPz /tmp/backup/#{tar_name} /tmp/backup/mysql`
|
||||
|
||||
file = mysql_container.create_object(tar_name)
|
||||
file = container.create_object(tar_name)
|
||||
end
|
||||
|
||||
puts "Uploading archive at #{Time.now.to_s}"
|
||||
if file.write File.open("/tmp/backup/" + tar_name)
|
||||
puts("event=backup status=success type=mysql")
|
||||
puts("event=backup status=success type=database")
|
||||
`rm /tmp/backup/#{tar_name}`
|
||||
|
||||
files = mysql_container.objects
|
||||
files = container.objects
|
||||
files.sort!.pop(NUMBER_OF_DAYS * 24)
|
||||
files.each do |file|
|
||||
mysql_container.delete_object(file)
|
||||
container.delete_object(file)
|
||||
end
|
||||
else
|
||||
puts("event=backup status=failure type=mysql")
|
||||
puts("event=backup status=failure type=database")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue