#!/usr/bin/env ruby # frozen_string_literal: true require 'optparse' require 'pg_export' ENV['KEEP_DUMPS'] = ENV['KEEP_DUMPS'] || '10' ENV['GATEWAY'] = 'ftp' interactive = false database = nil option_parser = OptionParser.new do |opts| opts.banner = 'Usage: pg_export [options]' opts.on('-g', '--gateway GATEWAY', String, '[Optional] ssh or ftp (default: ftp)') do |g| ENV['GATEWAY'] = g end opts.on('-d', '--database DATABASE', String, '[Required] Name of the database to export') do |d| database = d end opts.on('-k', '--keep [KEEP]', String, "[Optional] Number of dump files to keep on FTP (default: #{ENV['KEEP_DUMPS']})") do |keep| ENV['KEEP_DUMPS'] = keep end opts.on('-t', '--timestamped', '[Optional] Enables log messages with timestamps') do ENV['LOGGER_FORMAT'] = 'timestamped' end opts.on('-m', '--muted', '[Optional] Mutes log messages (overrides -t option)') do ENV['LOGGER_FORMAT'] = 'muted' end opts.on('-i', '--interactive', '[Optional] Interactive command line mode - for restoring dumps into databases') do interactive = true end opts.on('-v', '--version', 'Show version') do puts PgExport::VERSION exit end opts.on('-h', '--help', 'Show this message') do puts opts exit end opts.separator "\nSetting can be verified by running following commands:" opts.on('-c', '--configuration', 'Prints the configuration') do require 'pg_export/container' PgExport::Container.start(:config) puts PgExport::Container['config'].to_h exit end opts.on('-w', '--welcome', 'Tries connecting to the gateway (FTP or SSH) to verify the connection') do require 'pg_export/container' PgExport::Container.start(ENV['GATEWAY'].to_sym) gateway = PgExport::Container['factories.gateway_factory'].gateway puts gateway.welcome exit end end begin option_parser.parse! rescue OptionParser::ParseError => e warn e.message.capitalize warn 'Type "pg_export -h" for available options' exit end require 'pg_export/container' begin pg_export = if interactive PgExport.interactive else PgExport.plain end rescue PgExport::InitializationError => e warn 'Unable to initialize PgExport due to invalid configuration. Check you ENVs.' warn "Detailed message: #{e.message}" exit end pg_export.call(database) do |result| result.success { puts 'Success' } result.failure { |outcome| warn outcome[:message] } end