Skip to content

Instantly share code, notes, and snippets.

@juan-apa
Last active October 19, 2022 16:30
Show Gist options
  • Save juan-apa/9a1d3ad8dd7a25ec87cb20cf930883a0 to your computer and use it in GitHub Desktop.
Save juan-apa/9a1d3ad8dd7a25ec87cb20cf930883a0 to your computer and use it in GitHub Desktop.
Restorer tool for restoring dumps
#!/usr/bin/env ruby
# frozen_string_literal: true
# Usage examples:
#
# For making dumps:
# restorer.sh -b 6053 # Backups the dump-6053.dump file
# restorer.sh --backup 6053 # Backups the dump-6053.dump file
#
# For restoring dumps:
# restorer.sh -r 6053. # Restores the dump-6053.dump file
# restorer.sh --restore 6053 # Restores the dump-6053.dump file
#
# For listing dumps:
# restorer.sh -l # Lists the dumps in the dumps folder
# restorer.sh --list. # Lists the dumps in the dumps folder
require 'pathname'
BACKUP_METHODS = %w[--backup -b].freeze
RESTORE_METHODS = %w[--restore -r].freeze
LIST_METHODS = %w[-l --list].freeze
METHODS_DICT = {
BACKUP_METHODS: BACKUP_METHODS,
RESTORE_METHODS: RESTORE_METHODS,
LIST_METHODS: LIST_METHODS
}.freeze
SUPPORTED_METHODS = METHODS_DICT.values.flatten.freeze
DEFAULT_DB_NAME = 'rails_development'.freeze
method = ARGV[0] # The first argument contains the action to execute
param = ARGV[1] # the second argument contains the backup to restore
db_name = ARGV[2] || DEFAULT_DB_NAME # the third argument contains the database name
unless SUPPORTED_METHODS.include?(method)
puts("Unsupported param '#{method}'. Did you mean #{SUPPORTED_METHODS.join(', ')} ?")
exit(1)
end
# 1. get path for dumps
default_location_raw = %x[echo $RESTORER_PATH]
location = default_location_raw.empty? ? Pathname.new(default_location_raw) : Pathname.new('~/.pgrestorer')
location.mkpath unless location.exist?
puts "Backup location: #{location}"
if (METHODS_DICT[:BACKUP_METHODS] + METHODS_DICT[:RESTORE_METHODS]).include?(method)
dump_name = "DUMP-#{param}.dump"
file_path = "#{location.to_s.strip}/#{dump_name}"
unless param
# 2. get the dump name from STDIN
puts('The file will have the following format DUMP-<number>.dump')
puts('Insert the dump identifier: ')
dump_name = "DUMP-#{STDIN.gets.chomp}.dump"
file_path = "#{location.to_s.strip}#{dump_name}"
puts("The file will be located in: #{file_path}")
end
end
# Run the backup/restore/list tool
if METHODS_DICT[:BACKUP_METHODS].include?(method)
puts("BACKUPING in pg_dump -Fc #{db_name} > #{file_path}")
response = %x[pg_dump -Fc #{db_name} > #{file_path}]
exit_status = $?.exitstatus
puts("Finished backup with status code #{exit_status}")
exit(exit_status)
elsif METHODS_DICT[:RESTORE_METHODS].include?(method)
puts('RESTORING...')
puts "Restoring #{file_path} to #{db_name}"
response = %x[pg_restore -d #{db_name} --verbose --clean --no-acl --no-owner #{file_path}]
exit_status = $?.exitstatus
puts("Finished backup with status code #{exit_status}")
exit(exit_status)
elsif METHODS_DICT[:LIST_METHODS].include?(method)
response = %x[ls -l #{location.to_s}].split("\n").select { |line| /dump/.match(line) }.map{ |line| line[47..100].strip }
puts(response)
puts('')
end
puts('FINISHED')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment