-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup
executable file
·36 lines (29 loc) · 1.11 KB
/
backup
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
#!/usr/bin/env ruby
require 'yaml'
script_path = File.expand_path(File.dirname(File.dirname(__FILE__)))
rails_env = "production"
file = "#{script_path}/current/config/database.yml"
shared_path = "#{script_path}/shared"
begin
config = YAML::load(File.open(file))
adapter = config[rails_env]["adapter"]
database = config[rails_env]["database"]
db_username = config[rails_env]["username"]
db_password = config[rails_env]["password"]
timestamp = Time.new.to_i.to_s
file_name = "#{database}-#{timestamp}.sql"
archive_ext = "7z"
dump_file_path = "#{shared_path}/backup/#{file_name}"
output_file = "#{dump_file_path}.#{archive_ext}"
system "mkdir -p #{shared_path}/backup"
if adapter == "postgresql"
puts "Dumping #{database}"
system "pg_dump -U #{db_username} #{database} > #{dump_file_path}"
puts "Archiving #{database} to #{output_file}"
system "cd #{shared_path} && 7z a #{output_file} #{dump_file_path} && rm #{dump_file_path}"
else
puts "Cannot backup, adapter #{adapter} is not implemented for backup yet"
end
rescue Errno::ENOENT
puts "No such file or directory #{file}"
end