FIX: Restore process for dump taken with `pg_dump` 10.3+.

* Since we can no longer restore into a different schema,
  we will move tables in the public schema into the backup schema
  first before restoring the dump file which goes into the public
  schema. The downside to this approach is that we will increase
  the downtime experienced during the restore process. Downtime
  would equal the duration of restoring the dump file.
This commit is contained in:
Guo Xiang Tan 2018-03-09 13:18:47 +08:00
parent 39e679d3cb
commit 8fd47314d9
1 changed files with 40 additions and 12 deletions

View File

@ -39,23 +39,41 @@ module BackupRestore
validate_metadata validate_metadata
extract_dump extract_dump
restore_dump dumped_by_version = Gem::Version.new(get_dumped_by_version)
### READ-ONLY / START ### if dumped_by_version >= Gem::Version.new("10.3") ||
enable_readonly_mode dumped_by_version >= Gem::Version.new("9.5.12")
pause_sidekiq enable_readonly_mode
wait_for_sidekiq
switch_schema! pause_sidekiq
wait_for_sidekiq
migrate_database BackupRestore.move_tables_between_schemas("public", "backup")
reconnect_database restore_dump
reload_site_settings migrate_database
clear_emoji_cache reconnect_database
disable_readonly_mode reload_site_settings
### READ-ONLY / END ### clear_emoji_cache
disable_readonly_mode
else
restore_dump
enable_readonly_mode
pause_sidekiq
wait_for_sidekiq
switch_schema!
migrate_database
reconnect_database
reload_site_settings
clear_emoji_cache
disable_readonly_mode
end
extract_uploads extract_uploads
rescue SystemExit rescue SystemExit
@ -238,6 +256,16 @@ module BackupRestore
end end
end end
def get_dumped_by_version
output = Discourse::Utils.execute_command(
File.extname(@dump_filename) == '.gz' ? 'zgrep' : 'grep',
'-m1', @dump_filename, '-e', "pg_dump",
failure_message: "Failed to check version of pg_dump used to generate the dump file"
)
output.match(/version (\d+(\.\d+)?)/)[1]
end
def restore_dump_command def restore_dump_command
if File.extname(@dump_filename) == '.gz' if File.extname(@dump_filename) == '.gz'
"gzip -d < #{@dump_filename} | #{sed_command} | #{psql_command} 2>&1" "gzip -d < #{@dump_filename} | #{sed_command} | #{psql_command} 2>&1"