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
|
# File 'lib/techinform.rb', line 11
def restore(type, filename, dbname = nil, just_decrypt = false)
require 'highline'
dbname = filename if dbname.nil?
if !File.exist?(filename)
cli = HighLine.new
path = type == 'pg' ? PostgreBackup.new(database: filename).restore_path : MysqlBackup.new(database: filename).restore_path
filename = cli.choose(*Dir["#{path}/*"].sort - %w[. ..])
end
encrypted = filename.split('.').last == 'gpg'
if type == 'pg'
if encrypted && just_decrypt
puts "Decrypting postgres backup of #{filename}..."
`pv --wait #{filename} | gpg2 --decrypt | #{bzip2} > #{File.basename(filename, '.*') + '.bz2'}`
elsif encrypted
puts "Restoring postgres backup to database #{dbname}..."
`pv --wait #{filename} | gpg2 --decrypt | psql #{dbname} > /dev/null`
else
`pv --wait #{filename} | #{bunzip2} | psql #{dbname} > /dev/null`
end
else
if encrypted && just_decrypt
puts "Decrypting mysql backup of #{filename}..."
`pv --wait #{filename} | gpg2 --decrypt | #{bzip2} > #{File.basename(filename, '.*') + '.bz2'}`
elsif encrypted
puts "Restoring mysql backup to database #{dbname}..."
`pv --wait #{filename} | gpg2 --decrypt | mysql#{" -u#{ENV['DBUSER']}" if !ENV['DBUSER'].nil?}#{" -p#{ENV['PASSWORD']}" if !ENV['PASSWORD'].nil?} #{dbname}`
else
`pv --wait #{filename} | #{bunzip2} | mysql#{" -u#{ENV['DBUSER']}" if !ENV['DBUSER'].nil?}#{" -p#{ENV['PASSWORD']}" if !ENV['PASSWORD'].nil?} #{dbname}`
end
end
end
|