Skip to content

Instantly share code, notes, and snippets.

@mariowise
Created February 1, 2017 12:51
Show Gist options
  • Save mariowise/cdaba1153236269311750491317e8871 to your computer and use it in GitHub Desktop.
Save mariowise/cdaba1153236269311750491317e8871 to your computer and use it in GitHub Desktop.
localhost database ops task
namespace :db do
FOLDER = "tmp/dumps"
def credentials
password = (ENV['DEV_DB_PASSWORD'] != "") ? "-p#{ENV['DEV_DB_PASSWORD']}" : ""
"-u #{ENV['DEV_DB_USER']} #{password}"
end
def database_name
"#{ENV['DEV_DB_NAME']}_development"
end
task :backup do
file_name = Time.now.to_i
system "mkdir -p #{FOLDER}"
system "mysqldump #{credentials} #{database_name} > #{FOLDER}/#{file_name}.sql"
end
namespace :backup do
task :clean do
system "rm #{FOLDER}/*.sql"
end
task :restore do
file_name = `ls #{FOLDER}`.split("\n")
.map { |i| i.gsub(".sql", "") if i }
.map { |i| i.to_i if i }
.compact
.max
system "mysql #{credentials} #{database_name} < #{FOLDER}/#{file_name}.sql"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment