Class: ActivePostgres::Components::PgBackRest

Inherits:
Base
  • Object
show all
Defined in:
lib/active_postgres/components/pgbackrest.rb

Constant Summary collapse

LOG_ARCHIVE_CRON_FILE =
'/etc/cron.d/postgres-log-archive'
LOG_ARCHIVE_ENV_FILE =
'/etc/active-postgres-log-archive.env'
LOG_ARCHIVE_SCRIPT =
'/usr/local/bin/active-postgres-archive-logs'

Instance Attribute Summary

Attributes inherited from Base

#config, #secrets, #ssh_executor

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from ActivePostgres::Components::Base

Instance Method Details

#installObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/active_postgres/components/pgbackrest.rb', line 10

def install
  puts 'Installing pgBackRest for backups...'

  # Install on primary with full setup (stanza-create)
  install_on_host(config.primary_host, create_stanza: true)

  # Install on standbys (package + config only, no stanza-create)
  config.standby_hosts.each do |host|
    install_on_host(host, create_stanza: false)
  end
end

#install_on_standby(host) ⇒ Object



41
42
43
44
# File 'lib/active_postgres/components/pgbackrest.rb', line 41

def install_on_standby(host)
  puts "  Installing pgBackRest on standby #{host}..."
  install_on_host(host, create_stanza: false)
end

#list_backupsObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/active_postgres/components/pgbackrest.rb', line 94

def list_backups
  puts 'Available backups:'
  postgres_user = config.postgres_user

  output = nil
  ssh_executor.execute_on_host(config.primary_host) do
    output = capture(:sudo, '-u', postgres_user, 'pgbackrest', 'info')
  end

  puts output if output
end

#restartObject



37
38
39
# File 'lib/active_postgres/components/pgbackrest.rb', line 37

def restart
  puts "pgBackRest is a backup tool and doesn't run as a service."
end

#run_backup(type = 'full') ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_postgres/components/pgbackrest.rb', line 46

def run_backup(type = 'full')
  puts "Running #{type} backup..."
  postgres_user = config.postgres_user

  output = nil
  ssh_executor.execute_on_host(config.primary_host) do
    output = capture(:sudo, '-u', postgres_user, 'pgbackrest', '--stanza=main', "--type=#{type}", 'backup')
  end

  puts output if output
end

#run_restore(backup_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_postgres/components/pgbackrest.rb', line 58

def run_restore(backup_id)
  puts "Restoring from backup #{backup_id}..."
  postgres_user = config.postgres_user

  ssh_executor.execute_on_host(config.primary_host) do
    # Stop PostgreSQL
    execute :sudo, 'systemctl', 'stop', 'postgresql'

    # Restore
    output = capture(:sudo, '-u', postgres_user, 'pgbackrest', '--stanza=main', "--set=#{backup_id}", 'restore')
    puts output if output

    # Start PostgreSQL
    execute :sudo, 'systemctl', 'start', 'postgresql'
  end
end

#run_restore_at(target_time, target_action: 'promote') ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_postgres/components/pgbackrest.rb', line 75

def run_restore_at(target_time, target_action: 'promote')
  puts "Restoring to #{target_time} (PITR)..."
  postgres_user = config.postgres_user

  ssh_executor.execute_on_host(config.primary_host) do
    execute :sudo, 'systemctl', 'stop', 'postgresql'

    output = capture(:sudo, '-u', postgres_user, 'pgbackrest',
                     '--stanza=main',
                     '--type=time',
                     "--target=#{target_time}",
                     "--target-action=#{target_action}",
                     'restore')
    puts output if output

    execute :sudo, 'systemctl', 'start', 'postgresql'
  end
end

#uninstallObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_postgres/components/pgbackrest.rb', line 22

def uninstall
  puts 'Uninstalling pgBackRest...'

  config.all_hosts.each do |host|
    ssh_executor.execute_on_host(host) do
      execute :sudo, 'apt-get', 'remove', '-y', 'pgbackrest'
      execute :sudo, 'rm', '-f', '/etc/cron.d/pgbackrest-backup'
      execute :sudo, 'rm', '-f', '/etc/cron.d/pgbackrest-backup-incremental'
      execute :sudo, 'rm', '-f', LOG_ARCHIVE_CRON_FILE
      execute :sudo, 'rm', '-f', LOG_ARCHIVE_ENV_FILE
      execute :sudo, 'rm', '-f', LOG_ARCHIVE_SCRIPT
    end
  end
end