Created
September 25, 2008 09:40
-
-
Save sur/12794 to your computer and use it in GitHub Desktop.
rake task to create database dumps
This file contains hidden or 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
require 'yaml' | |
require 'fileutils' | |
desc "takes db backup and stores it to /var/backups/timestamp.sql with" | |
task :db_backup => :environment do | |
production = YAML.load(File.read(File.join(RAILS_ROOT, "config/database.yml")))['production'] | |
db = production['database'] | |
username = production['username'] | |
password = production['password'] | |
t = Time.now | |
filepath = "/var/backups/" | |
filename = "acnm_#{Time.now.strftime('%b_%d_%Y').downcase}.sql" | |
FileUtils.cd(filepath) | |
%x{mysqldump #{db} > #{filename} -u #{username} -p#{password}} | |
puts "=====================================================" | |
if File.exists?(filename) | |
%x{tar -czf #{filename + ".tar.gz"} #{filename}} | |
FileUtils.rm(filename) | |
puts "Database dump successfully created" | |
else | |
puts "ERRORS while creating database dump" | |
end | |
puts "=====================================================" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment