#!/usr/bin/env ruby require 'optparse' require 'ostruct' require 'pg_export' require 'pg_export/interactive' LOGS_DEFAULT = ->(_, _, _, message) { "#{message}\n" } LOGS_TIMESTAMPED = ->(severity, datetime, progname, message) { "#{datetime} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{progname} #{severity}: #{message}\n" } LOGS_MUTED = ->(_, _, _, _) {} PgExport::Logging.logger.formatter = LOGS_DEFAULT options = OpenStruct.new option_parser = OptionParser.new do |opts| opts.banner = 'Usage: pg_export [options]' opts.on('-d', '--database DATABASE', '[Required] Name of the database to export') do |database| options.database = database end opts.on('-k', '--keep [KEEP]', Integer, '[Optional] Number of dump files to keep on FTP (default: 10)') do |keep| options.keep = keep end opts.on('-t', '--timestamped', '[Optional] Enables log messages with timestamps') do options.timestamped = true end opts.on('-i', '--interactive', 'Interactive, command line mode, for restoring dumps into databases') do options.interactive = true 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 puts PgExport::Configuration.new.to_s exit end opts.on('-f', '--ftp', 'Tries connecting to FTP to verify the connection') do PgExport::FtpService.new(PgExport::Configuration.new.ftp_params) exit end end begin option_parser.parse! options.database ||= 'undefined' if options.interactive PgExport::Logging.logger.formatter = LOGS_TIMESTAMPED if options.timestamped PgExport::Logging.logger.formatter = LOGS_MUTED if options.interactive pg_export = PgExport.new do |config| config.database = options.database if options.database config.keep_dumps = options.keep if options.keep end pg_export.extend(PgExport::Interactive) if options.interactive pg_export.call rescue OptionParser::MissingArgument, PgExport::InvalidConfigurationError => e puts "Error: #{e}" puts option_parser rescue PgExport::PgDumpError => e puts e end