Skip to content

Instantly share code, notes, and snippets.

@Sujimichi
Created January 29, 2016 19:20
Show Gist options
  • Save Sujimichi/0efc5e86ee41c27fc99f to your computer and use it in GitHub Desktop.
Save Sujimichi/0efc5e86ee41c27fc99f to your computer and use it in GitHub Desktop.
A (slightly offensive) set of rake tasks for backing up, listing backups and restoring backups for a pg rails app (with checks to ensure server and console aren't running before performing restore)
namespace :db do
task :backup => :environment do
db = ActiveRecord::Base.connection_config[:database]
file = Rails.root.join("db", "backups", "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{db}.psql")
puts "Preparing to back yo shit up....\ngimme dat password fool:"
system "pg_dump -F c -v -h localhost -d #{db} -f #{file}"
puts "\nI done stuffed your worthless data in #{file}\n"
end
task :list_backups => :environment do
db_folder = Rails.root.join("db", "backups")
backups = Dir.entries(db_folder).select{|i| i.match(/.psql$/)}.sort.reverse
puts "\nbitch you got these backups available (most recent first yo);\n\n"
backups.each_with_index do |backup, index|
gap = Array.new([backups.size.to_s.length-index.to_s.length, 0].max){" "}.join
puts "[#{index}]#{gap}- #{backup}"
end
puts "\nrun rake db:restore[x]\nwhere x is one of the muthafuckin' numbers in the [] above, or don't, whatever\n"
end
task :restore, [:n] => :environment do |task, args|
db_folder = Rails.root.join("db", "backups")
backups = Dir.entries(db_folder).select{|i| i.match(/.psql$/)}.sort.reverse
go_no_go = true
#check is rails server is running and hurl abuse if it is
server_running = !Dir.entries(Rails.root.join("tmp", "pids")).select{|i| i.match(/.pid$/)}.empty?
if server_running
puts "\nare you stupid?! The server is still running. Shut that shit down first!!"
go_no_go = false
end
#check if a console exists and give user option to stop or carry on
console_running = !(`ps auxwww | grep rails_console`).split("\n").select{|l| !l.include?("grep")}.empty?
if go_no_go && console_running
puts "\nA rails console is still running. maybe it's console from another app, but given your incompetance it's probably attached to this one's DB so ya gotta shut it down\nIf it's another console hit 'y' to continue"
go_no_go = STDIN.gets.chomp.downcase.eql?("y")
end
if go_no_go
puts "\nbitch you didn't specify a number to restore, what am I telepathic? fuck it, I'll just restore the most recent one\n" unless args.n
n = args.n.to_i #if arg.n is nil calling to_i will result in 0.
restore = backups[n]
end
#halt if backup file can't be found
if go_no_go && restore.nil?
puts "\nI couldn't find a database to restore, bitch"
go_no_go = false
end
#double check before actually doing the deed.
if go_no_go
puts "\nso, like...gonna restore this one - #{restore} - whack 'y' to confirm"
go_no_go = STDIN.gets.chomp.downcase.eql?("y")
end
if go_no_go
db = ActiveRecord::Base.connection_config[:database]
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
file = Rails.root.join("db", "backups", restore)
#system "pg_restore -F c -c -C #{file}" <--slower than drugged sloth
system "pg_restore --dbname #{db} -v #{file}" #<-- this is much faster
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment