Skip to content

Commit c8fe3c2

Browse files
committed
Use --file and --format with pg_dump
1 parent 068d4ac commit c8fe3c2

9 files changed

Lines changed: 37 additions & 19 deletions

File tree

definitions/checks/restore/validate_postgresql_dump_permissions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def run
1313
backup = ForemanMaintain::Utils::Backup.new(@backup_dir)
1414
if feature(:instance).postgresql_local?
1515
errors = []
16-
[:candlepin_dump, :foreman_dump, :pulpcore_dump].each do |dump|
16+
[:candlepin_dump, :foreman_dump, :pulpcore_dump, :container_gateway_dump].each do |dump|
1717
next unless backup.file_map[dump][:present]
1818

1919
unless system("runuser - postgres -c 'test -r #{backup.file_map[dump][:path]}'")

definitions/procedures/backup/online/container_gateway_db.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class ContainerGatewayDB < ForemanMaintain::Procedure
1212

1313
def run
1414
with_spinner('Getting Container Gateway DB dump') do
15-
feature(:container_gateway_database).dump_db(File.join(@backup_dir, 'container_gateway.dump'))
15+
feature(:container_gateway_database).
16+
dump_db(File.join(@backup_dir, 'container_gateway.dump'))
1617
end
1718
end
1819
end

definitions/procedures/restore/container_gateway_dump.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def restore_container_gateway_dump(backup, spinner)
2323
if backup.file_map[:container_gateway_dump][:present]
2424
spinner.update('Restoring container gateway dump')
2525
local = feature(:container_gateway_database).local?
26-
feature(:container_gateway_database).restore_dump(backup.file_map[:container_gateway_dump][:path], local)
26+
feature(:container_gateway_database).
27+
restore_dump(backup.file_map[:container_gateway_dump][:path], local)
2728
end
2829
end
2930
end

definitions/scenarios/restore.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def compose
5050
add_step_with_context(Procedures::Installer::UpgradeRakeTask)
5151
add_step_with_context(Procedures::Crond::Start) if feature(:cron)
5252
end
53-
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize
53+
# rubocop:enable Metrics/MethodLength
5454

5555
def restore_sql_dumps(backup)
5656
if feature(:instance).postgresql_local?
@@ -72,6 +72,7 @@ def restore_sql_dumps(backup)
7272
add_step(Procedures::Service::Stop.new(:only => ['postgresql']))
7373
end
7474
end
75+
# rubocop:enable Metrics/AbcSize
7576

7677
def set_context_mapping
7778
context.map(:backup_dir,

lib/foreman_maintain/concerns/base_database.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ def psql(query, config = configuration)
6666
end
6767

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

7574
def dump_db(file, config = configuration)
76-
execute!(dump_command(config) + " > #{file}", :hidden_patterns => [config['password']], :user => config['user'])
75+
if config['user']
76+
execute!("chown -R #{config['user']}:#{config['user']} #{File.dirname(file)}")
77+
end
78+
execute!(dump_command(config) + " -f #{file}",
79+
:hidden_patterns => [config['password']], :user => config['user'])
7780
end
7881

7982
def restore_dump(file, localdb, config = configuration)
@@ -83,13 +86,17 @@ def restore_dump(file, localdb, config = configuration)
8386
else
8487
# TODO: figure out how to completely ignore errors. Currently this
8588
# sometimes exits with 1 even though errors are ignored by pg_restore
86-
dump_cmd = ''
87-
if config['connection_string']
88-
dump_cmd = "pg_restore --no-privileges --clean --disable-triggers -n public -d #{config['database']} #{file}"
89-
else
90-
dump_cmd = base_command(config, 'pg_restore') +
91-
' --no-privileges --clean --disable-triggers -n public ' \
92-
"-d #{config['database']} #{file}"
89+
base_cmd = ''
90+
base_cmd = if config['connection_string']
91+
'pg_restore'
92+
else
93+
base_command(config, 'pg_restore')
94+
end
95+
dump_cmd = base_cmd +
96+
' --no-privileges --clean --disable-triggers -n public ' \
97+
"-d #{config['database']} #{file}"
98+
if config['user']
99+
execute!("chown -R #{config['user']}:#{config['user']} #{File.dirname(file)}")
93100
end
94101
execute!(dump_cmd, :hidden_patterns => [config['password']],
95102
:valid_exit_statuses => [0, 1], :user => config['user'])
@@ -169,9 +176,9 @@ def psql_command(config)
169176

170177
def dump_command(config)
171178
if config['connection_string']
172-
"pg_dump -Fc #{config['connection_string']}"
179+
"pg_dump --format=c #{config['connection_string']}"
173180
else
174-
base_command(config, 'pg_dump') + " -Fc #{config['database']}"
181+
base_command(config, 'pg_dump') + " --format=c #{config['database']}"
175182
end
176183
end
177184

lib/foreman_maintain/utils/backup.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def sql_tar_files_exist?
235235
def sql_dump_files_exist?
236236
file_map[:foreman_dump][:present] ||
237237
file_map[:candlepin_dump][:present] ||
238-
file_map[:pulpcore_dump][:present]
238+
file_map[:pulpcore_dump][:present] ||
239239
file_map[:container_gateway_dump][:present]
240240
end
241241

lib/foreman_maintain/utils/command_runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(logger, command, options)
1313
@logger = logger
1414
@user = options[:user]
1515
if @user && !@user.empty?
16-
@command = "sudo -u #{@user} -- " + command
16+
@command = "runuser -u #{@user} -- " + command
1717
else
1818
@command = command
1919
end

test/definitions/checks/restore/validate_postgresql_dump_permissions_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
:foreman_dump => { :present => true, :path => '/nonexistant/foreman.dump' },
1313
:candlepin_dump => { :present => true, :path => '/nonexistant/candlepin.dump' },
1414
:pulpcore_dump => { :present => true, :path => '/nonexistant/pulpcore.dump' },
15+
:container_gateway_dump => { :present => true, :path => '/nonexistant/container_gateway.dump' }
1516
}
1617
ForemanMaintain::Utils::Backup.any_instance.stubs(:file_map).returns(file_map)
1718
end
@@ -53,7 +54,7 @@
5354
result = run_check(subject)
5455
refute result.success?, 'Check expected to fail'
5556
expected = "The 'postgres' user needs read access to the following files:\n" \
56-
"/nonexistant/candlepin.dump\n/nonexistant/foreman.dump\n/nonexistant/pulpcore.dump"
57+
"/nonexistant/candlepin.dump\n/nonexistant/foreman.dump\n/nonexistant/pulpcore.dump\n/nonexistant/container_gateway.dump"
5758
assert_equal result.output, expected
5859
end
5960

@@ -66,6 +67,8 @@
6667
returns(true)
6768
subject.stubs(:system).with("runuser - postgres -c 'test -r /nonexistant/foreman.dump'").
6869
returns(true)
70+
subject.stubs(:system).with("runuser - postgres -c 'test -r /nonexistant/container_gateway.dump'").
71+
returns(true)
6972
result = run_check(subject)
7073
refute result.success?, 'Check expected to fail'
7174
expected = "The 'postgres' user needs read access to the following files:\n" \

test/definitions/scenarios/restore_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ module Scenarios
5656
assume_feature_present(:foreman_database, :configuration => {})
5757
assume_feature_present(:candlepin_database, :configuration => {})
5858
assume_feature_present(:pulpcore_database, :configuration => {})
59+
assume_feature_present(:container_gateway_database, :configuration => {})
5960

6061
ForemanMaintain::Utils::Backup.any_instance.stubs(:file_map).returns(
6162
{
6263
:foreman_dump => { :present => true },
6364
:candlepin_dump => { :present => true },
6465
:pulpcore_dump => { :present => true },
66+
:container_gateway_dump => { :present => true },
6567
:pulp_data => { :present => true },
6668
:pgsql_data => { :present => false },
6769
:metadata => { :present => false },
@@ -73,6 +75,7 @@ module Scenarios
7375
assert_scenario_has_step(scenario, Procedures::Restore::ForemanDump)
7476
assert_scenario_has_step(scenario, Procedures::Restore::CandlepinDump)
7577
assert_scenario_has_step(scenario, Procedures::Restore::PulpcoreDump)
78+
assert_scenario_has_step(scenario, Procedures::Restore::ContainerGatewayDump)
7679
end
7780

7881
it 'does not try to drop/restore DB dumps if present and DB data present' do
@@ -96,6 +99,7 @@ module Scenarios
9699
refute_scenario_has_step(scenario, Procedures::Restore::ForemanDump)
97100
refute_scenario_has_step(scenario, Procedures::Restore::CandlepinDump)
98101
refute_scenario_has_step(scenario, Procedures::Restore::PulpcoreDump)
102+
refute_scenario_has_step(scenario, Procedures::Restore::ContainerGatewayDump)
99103
end
100104

101105
it 'does not try to drop/restore DB dumps when these are absent' do
@@ -109,6 +113,7 @@ module Scenarios
109113
refute_scenario_has_step(scenario, Procedures::Restore::ForemanDump)
110114
refute_scenario_has_step(scenario, Procedures::Restore::CandlepinDump)
111115
refute_scenario_has_step(scenario, Procedures::Restore::PulpcoreDump)
116+
refute_scenario_has_step(scenario, Procedures::Restore::ContainerGatewayDump)
112117
end
113118
end
114119

0 commit comments

Comments
 (0)