Created
February 1, 2017 12:51
-
-
Save mariowise/cdaba1153236269311750491317e8871 to your computer and use it in GitHub Desktop.
localhost database ops task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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