-
Notifications
You must be signed in to change notification settings - Fork 4
/
Rakefile
62 lines (53 loc) · 1.84 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# require 'FileUtils'
require 'YAML'
config = YAML.load(File.read("config.yml"))
%w{dbuser dbpassword}.each do |key|
Object.const_set(key.upcase, config["config"][key])
end
ABSPATH = File.expand_path(File.dirname(__FILE__))
LISTS = Dir["#{ABSPATH}/lists/*"].map {|f| f.split("/").last}
namespace :backup do
namespace :list do
LISTS.each do |list|
desc "Backup every database listed in lists/#{list}"
task list.to_sym do
FileUtils::mkdir_p("#{ABSPATH}/backups/#{list}")
databases = File.readlines("#{ABSPATH}/lists/#{list.strip}").map {|l| l.strip}
databases.each do |database|
system "mysqldump -u#{DBUSER} -p#{DBPASSWORD} #{database} > #{ABSPATH}/backups/#{list}/#{database}.sql"
puts "Backed up #{database} to backups/#{list}/#{database}.sql"
end
end
end
end
end
namespace :restore do
namespace :list do
LISTS.each do |list|
desc "Restore everything in backups/#{list}"
task list.to_sym do
Dir["#{ABSPATH}/backups/#{list}/*"].each do |file|
db_name = File.basename(file, ".sql")
system("mysql -u#{DBUSER} -p#{DBPASSWORD} -e 'drop database #{db_name}'")
system("mysql -u#{DBUSER} -p#{DBPASSWORD} -e 'create database #{db_name}'")
system("mysql -u#{DBUSER} -p#{DBPASSWORD} #{db_name} < #{file}")
puts "Restored #{db_name} from #{file}"
end
end
end
end
end
namespace :drop do
namespace :list do
LISTS.each do |list|
desc "Drop every database listed in lists/#{list}"
task list.to_sym do
databases = File.readlines("#{ABSPATH}/lists/#{list.strip}").map {|l| l.strip}
databases.each do |database|
system("mysql -u#{DBUSER} -p#{DBPASSWORD} -e 'drop database #{database}'")
puts "Dropped #{database}."
end
end
end
end
end