replicator/lib/ruby/pg-wal-plugin.rb
# Provision a standby server.  This shuts down the server. 
Provision a standby server.  This shuts down the server.
Provision a standby server . This shuts down the server .
def provision()
    if ! is_standby()
      raise UserError, "You can only provision a standby"
    end
    config = getConfig()
    pg_data     = config.getProperty("postgresql.data")
    pg_archive  = config.getProperty("postgresql.archive")
    master_user = config.getProperty("postgresql.master.user")
    trigger     = config.getProperty("postgresql.pg_standby.trigger")
    dynamicConfig = getPgWalProperties()
    master_host = dynamicConfig.getProperty("postgresql.master.host")
    master_port = dynamicConfig.getProperty("postgresql.master.port")
    stop_pg_server(true)
    status_pg_server(false) 
    puts("Cleaning up data about when the WAL files were generated");
    FileUtils.mkdir_p(@pg_wal_when) 
    exec_cmd("rm -r -f #{@pg_wal_when}/*")
    exec_cmd("rm -f #{@wal_restored_when}")
    puts("Cleaning up old archive logs");
    exec_cmd("rm -r -f #{pg_archive}/*")
    hostname = `hostname`.chomp
    exec_cmd("scp #{@archiving_active_template} #{master_host}:#{@archiving_active_standby}")
    retry_sleep = 5
    retry_i = 0
    retry_max = @master_max_startup_time / retry_sleep
    begin
      exec_cmd("psql -h#{master_host} -p#{master_port} -U#{master_user} -t -c \"select 'archiving_active written at '||pg_switch_xlog()\";")
    rescue SystemError => e
      puts "Failed to execute pg_switch_xlog() on a master: " + e.message
      if retry_i <= retry_max
        retry_i += 1
        puts "Has master finished recovery? Retry #{retry_i} of #{retry_max} in #{retry_sleep} seconds..."
        sleep retry_sleep
        retry
      else
        raise e
      end
    end
    begin
      puts "### Starting backup on #{master_host}:#{master_port}"
      exec_cmd("psql -h#{master_host} -p#{master_port} -U#{master_user} -t -c \"select 'Starting online backup at WAL file '|| pg_xlogfile_name(pg_start_backup('base_backup'));\"")
      puts "### Rsyncing files from #{master_host}:#{pg_data}..."
      exec_cmd2("rsync -azv --delete --exclude=*pg_log* --exclude=*pg_xlog* --exclude=postgresql.conf --exclude=pg_hba.conf --exclude=server.crt --exclude=server.key #{master_host}:#{pg_data}/ #{pg_data}/", 24)
    ensure
      puts "### Ending backup on #{master_host}:#{master_port}..."
      exec_cmd("psql -h#{master_host} -p#{master_port} -U#{master_user} -t -c \"select 'Stopping online backup at WAL file ' ||pg_xlogfile_name(pg_stop_backup());\"")
    end
    puts("Cleaning up data directory")
    exec_cmd("rm -f #{pg_data}/recovery.*")
    exec_cmd("rm -f #{pg_data}/logfile")
    exec_cmd("rm -f #{pg_data}/postmaster.pid")
    puts("Cleaning up pg_xlogs directory")
    exec_cmd("rm -f #{pg_data}/pg_xlog/0*")
    exec_cmd("rm -f #{pg_data}/pg_xlog/archive_status/0*")
    log("Performing standby online procedure")
    exec_cmd("cp #{@recovery_conf} #{pg_data}")
    exec_cmd("rm -f #{trigger}")
    if File.exists?(trigger)
      raise SystemError, "Unable to remove trigger file: #{trigger}"
    end
    log("Provisioning operation succeeded");
  end
def provision ( ) if ! is_standby ( ) raise UserError , "You can only provision a standby" end config = getConfig ( ) pg_data = config . getProperty ( "postgresql.data" ) pg_archive = config . getProperty ( "postgresql.archive" ) master_user = config . getProperty ( "postgresql.master.user" ) trigger = config . getProperty ( "postgresql.pg_standby.trigger" ) dynamicConfig = getPgWalProperties ( ) master_host = dynamicConfig . getProperty ( "postgresql.master.host" ) master_port = dynamicConfig . getProperty ( "postgresql.master.port" ) stop_pg_server ( true ) status_pg_server ( false ) puts ( "Cleaning up data about when the WAL files were generated" ) ; FileUtils . mkdir_p ( @pg_wal_when ) exec_cmd ( "rm -r -f #{@pg_wal_when}/*" ) exec_cmd ( "rm -f #{@wal_restored_when}" ) puts ( "Cleaning up old archive logs" ) ; exec_cmd ( "rm -r -f #{pg_archive}/*" ) hostname = ` hostname ` . chomp exec_cmd ( "scp #{@archiving_active_template} #{master_host}:#{@archiving_active_standby}" ) retry_sleep = 5 retry_i = 0 retry_max = @master_max_startup_time / retry_sleep begin exec_cmd ( "psql -h#{master_host} -p#{master_port} -U#{master_user} -t -c \"select 'archiving_active written at '||pg_switch_xlog()\";" ) rescue SystemError => e puts "Failed to execute pg_switch_xlog() on a master: " + e . message if retry_i <= retry_max retry_i += 1 puts "Has master finished recovery? Retry #{retry_i} of #{retry_max} in #{retry_sleep} seconds..." sleep retry_sleep retry else raise e end end begin puts "### Starting backup on #{master_host}:#{master_port}" exec_cmd ( "psql -h#{master_host} -p#{master_port} -U#{master_user} -t -c \"select 'Starting online backup at WAL file '|| pg_xlogfile_name(pg_start_backup('base_backup'));\"" ) puts "### Rsyncing files from #{master_host}:#{pg_data}..." exec_cmd2 ( "rsync -azv --delete --exclude=*pg_log* --exclude=*pg_xlog* --exclude=postgresql.conf --exclude=pg_hba.conf --exclude=server.crt --exclude=server.key #{master_host}:#{pg_data}/ #{pg_data}/" , 24 ) ensure puts "### Ending backup on #{master_host}:#{master_port}..." exec_cmd ( "psql -h#{master_host} -p#{master_port} -U#{master_user} -t -c \"select 'Stopping online backup at WAL file ' ||pg_xlogfile_name(pg_stop_backup());\"" ) end puts ( "Cleaning up data directory" ) exec_cmd ( "rm -f #{pg_data}/recovery.*" ) exec_cmd ( "rm -f #{pg_data}/logfile" ) exec_cmd ( "rm -f #{pg_data}/postmaster.pid" ) puts ( "Cleaning up pg_xlogs directory" ) exec_cmd ( "rm -f #{pg_data}/pg_xlog/0*" ) exec_cmd ( "rm -f #{pg_data}/pg_xlog/archive_status/0*" ) log ( "Performing standby online procedure" ) exec_cmd ( "cp #{@recovery_conf} #{pg_data}" ) exec_cmd ( "rm -f #{trigger}" ) if File . exists? ( trigger ) raise SystemError , "Unable to remove trigger file: #{trigger}" end log ( "Provisioning operation succeeded" ) ; end
Provision a standby server.
Provision a standby server .
# Make sure we are a standby. # Get static properties.  # Get dynamic properties.  # Shut down the standby server, ignoring errors.  # Check PostgreSQL is stopped. # Clean up the archived WAL files times' directory.  # Ensure folder is created. # Clean up the archive log directory.  # Ensure remote server knows to ship WAL logs to us.  # Switch logs on master. # This has to be in a block so we don't forget to terminate backup.  # Backup and rsync data files.  # TENT-159: accepting exit code 24 - partial transfer due to vanished source files. # Clean up the PG data directory.  # Clean up the pg_xlog directory.   # Add recovery.conf file.  # Ensure standby won't activate immediately.   # Copy archive logs. -- OOPS! This deletes files that have already shipped. #puts "### Rsyncing archive logs" #exec_cmd("rsync -avz --delete #{master_host}:#{pg_archive}/ #{pg_archive}/ > /dev/null")