lib/dokku_installer/postgres.rb in dokku-installer-cli-0.1.3 vs lib/dokku_installer/postgres.rb in dokku-installer-cli-0.1.4
- old
+ new
@@ -1,19 +1,19 @@
module DokkuInstaller
class Cli < Thor
- desc "postgres:backups", "List available PostgreSQL backups"
+ desc "postgres:backups", "List available PostgreSQL backups as a numbered list"
def postgres_backups
command = "ssh -t dokku@#{domain} postgres:backups #{app_name}"
puts "Running #{command}..."
backups = `#{command}`
- backups.split("\n").reverse.each_with_index do |line, index|
+ backups.split("\n").select{|backup| backup != "" }.reverse.each_with_index do |line, index|
number = "#{index + 1}"
if number.length < 2
number = " #{number}"
end
- puts "#{number}. #{line}"
+ puts "[#{number}] #{line}"
end
end
desc "postgres:backups:create", "Create a new PostgreSQL backup"
def postgres_backups_create
@@ -32,12 +32,12 @@
def postgres_backups_disable
run_command "postgres:backups:disable #{app_name}"
end
desc "postgres:backups:download <number>", "Download the numbered PostgreSQL backup"
- def postgres_backups_download(*args)
- number = args.first ? args.first : 1
+ def postgres_backups_download(number = nil)
+ number ||= 1
if backup = backup_filename(number)
command = "postgres:backups:download #{app_name} #{backup} > #{backup}"
puts "Saving to local file: #{backup}"
run_command(command)
@@ -51,14 +51,29 @@
command = "ssh -t root@#{domain} \"dokku postgres:backups:enable #{app_name} && service cron restart\""
puts "Running #{command}..."
exec(command)
end
- desc "postgres:backups:restore:local <number>", "Restore the numbered PostgreSQL backup locally"
- def postgres_backups_restore_local(*args)
+ desc "postgres:backups:restore <number>", "Restore a numbered PostgreSQL backup"
+ def postgres_backups_restore(number = nil)
+ if number.nil?
+ puts "You must specify a numbered backup."
+ exit
+ end
+
+ if backup = backup_filename(number)
+ command = "postgres:backups:restore #{app_name} #{backup}"
+ run_command(command)
+ else
+ puts "Invalid backup number"
+ end
+ end
+
+ desc "postgres:backups:restore:local <number>", "Restore a numbered PostgreSQL backup locally"
+ def postgres_backups_restore_local(number = nil)
# Download the backup file
- number = args.first ? args.first : 1
+ number ||= 1
if backup = backup_filename(number)
command = "ssh -t dokku@#{domain} postgres:backups:download #{app_name} #{backup} > #{backup}"
puts "Saving to local file: #{backup}"
`#{command}`
@@ -73,10 +88,39 @@
else
puts "Invalid backup number"
end
end
+ desc "postgres:export <file.sql>", "Export Postgres data to local file"
+ def postgres_export(file = "export.sql")
+ command = "postgres:dump #{app_name} > #{file}"
+ run_command(command)
+ end
+
+ desc "postgres:import <file.sql>", "Restore database data from a local file"
+ def postgres_import(file)
+ command = "postgres:restore #{app_name} < #{file}"
+ run_command(command)
+ end
+
private
+
+ def backup_filename(number)
+ # Make sure the number is valid or use 1
+ number = number.to_s.gsub(/\D/, "").to_i
+ number = 1 if number < 1
+
+ # Get the file name for the numbered backup
+ puts "Getting list of backups..."
+ command = "ssh -t dokku@#{domain} postgres:backups #{app_name}"
+ backups = `#{command}`
+ index = number - 1
+ if filename = backups.split("\n").select{|backup| backup != "" }.reverse[index]
+ filename.strip
+ else
+ nil
+ end
+ end
def psql_options
@psql_options ||= begin
restore_options = nil