Skip to content

Commit

Permalink
Use --file and --format with pg_dump
Browse files Browse the repository at this point in the history
  • Loading branch information
ianballou committed Jul 24, 2024
1 parent 5814c0e commit 214ff13
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def run
backup = ForemanMaintain::Utils::Backup.new(@backup_dir)
if feature(:instance).postgresql_local?
errors = []
[:candlepin_dump, :foreman_dump, :pulpcore_dump].each do |dump|
[:candlepin_dump, :foreman_dump, :pulpcore_dump, :container_gateway_dump].each do |dump|
next unless backup.file_map[dump][:present]

unless system("runuser - postgres -c 'test -r #{backup.file_map[dump][:path]}'")
Expand Down
27 changes: 18 additions & 9 deletions lib/foreman_maintain/concerns/base_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ def psql(query, config = configuration)
end

def ping(config = configuration)
execute?(psql_command(config),
:stdin => 'SELECT 1 as ping',
execute?(psql_command(config) + ' -c "SELECT 1 as ping"',
:hidden_patterns => [config['password']],
:user => config['user'])
end

def dump_db(file, config = configuration)
execute!(dump_command(config) + " > #{file}", :hidden_patterns => [config['password']], :user => config['user'])
if config['user']
execute!("chown -R #{config['user']}:#{config['user']} #{File.dirname(file)}")
execute!(dump_command(config) + " -f #{file}", :hidden_patterns => [config['password']], :user => config['user'])

Check failure on line 77 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/IdenticalConditionalBranches: Move `execute!(dump_command(config) + " -f #{file}", :hidden_patterns => [config['password']], :user => config['user'])` out of the conditional.

Check failure on line 77 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Layout/LineLength: Line is too long. [123/100]
else
execute!(dump_command(config) + " -f #{file}", :hidden_patterns => [config['password']], :user => config['user'])

Check failure on line 79 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/IdenticalConditionalBranches: Move `execute!(dump_command(config) + " -f #{file}", :hidden_patterns => [config['password']], :user => config['user'])` out of the conditional.

Check failure on line 79 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Layout/LineLength: Line is too long. [123/100]
end
end

def restore_dump(file, localdb, config = configuration)
Expand All @@ -84,12 +88,17 @@ def restore_dump(file, localdb, config = configuration)
# TODO: figure out how to completely ignore errors. Currently this
# sometimes exits with 1 even though errors are ignored by pg_restore
dump_cmd = ''

Check failure on line 90 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Lint/UselessAssignment: Useless assignment to variable - `dump_cmd`.
base_cmd = ''
if config['connection_string']

Check failure on line 92 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
dump_cmd = "pg_restore --no-privileges --clean --disable-triggers -n public -d #{config['database']} #{file}"
base_cmd = 'pg_restore'
else
dump_cmd = base_command(config, 'pg_restore') +
' --no-privileges --clean --disable-triggers -n public ' \
"-d #{config['database']} #{file}"
base_cmd = base_command(config, 'pg_restore')
end
dump_cmd = base_cmd +
' --no-privileges --clean --disable-triggers -n public ' \
"-d #{config['database']} #{file}"
if config['user']
execute!("chown -R #{config['user']}:#{config['user']} #{File.dirname(file)}")
end
execute!(dump_cmd, :hidden_patterns => [config['password']],
:valid_exit_statuses => [0, 1], :user => config['user'])
Expand Down Expand Up @@ -169,9 +178,9 @@ def psql_command(config)

def dump_command(config)
if config['connection_string']
"pg_dump -Fc #{config['connection_string']}"
"pg_dump --format=c #{config['connection_string']}"
else
base_command(config, 'pg_dump') + " -Fc #{config['database']}"
base_command(config, 'pg_dump') + " --format=c #{config['database']}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/foreman_maintain/utils/command_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(logger, command, options)
@logger = logger
@user = options[:user]
if @user && !@user.empty?
@command = "sudo -u #{@user} -- " + command
@command = "pushd /tmp && runuser -u #{@user} -- " + command + ' && popd'
else
@command = command
end
Expand Down

0 comments on commit 214ff13

Please sign in to comment.