Module: Mimas::Plugins::Server::PostgreSQL
- Defined in:
- lib/mimas/plugins/server/postgresql.rb
Instance Method Summary collapse
- #configure_postgresql(sudo_user:) ⇒ Object
- #install_backup_script(sudo_user:) ⇒ Object
- #install_postgresql(sudo_user:) ⇒ Object
Instance Method Details
#configure_postgresql(sudo_user:) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mimas/plugins/server/postgresql.rb', line 11 def configure_postgresql(sudo_user:) ssh(self, user: sudo_user) do |session| config_file_path = session.run "sudo -u postgres psql -AtXc 'SHOW config_file'", capture: true config_dir = Pathname.new(config_file_path.strip).parent config_files = find_files("postgresql/**/*.conf") config_files = config_files.map do |path| [ path, path.split("/postgresql/")[1] ] end.to_h tmp_dir = Pathname.new("/").join("tmp", "postgresql") config_files.each do |file, destination| destination_parent_folder = Pathname.new(destination).parent session.run "mkdir -p #{tmp_dir.join(destination_parent_folder)}" session.upload file, tmp_dir.join(destination) end session.run "sudo rsync --archive --recursive --chown=postgres:postgres #{tmp_dir.join("*")} #{config_dir}" session.run "rm -rf #{tmp_dir}" session.run "sudo systemctl restart postgresql" end end |
#install_backup_script(sudo_user:) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mimas/plugins/server/postgresql.rb', line 36 def install_backup_script(sudo_user:) ssh(self, user: sudo_user) do |session| pg_home = Pathname.new("/").join("var", "lib", "postgresql") private_key_file_name = "pg_dump_private_key.pem" # Create certificate and private key for backups session.run "sudo -u postgres bash -c \"cd ~; openssl req -x509 -newkey rsa:4096 -keyout #{private_key_file_name} -out pg_dump_certificate.pem -days 36500 -nodes -subj '/CN=PostgreSQL Backup Certificate'\"" session.run "sudo mv #{pg_home.join(private_key_file_name)} /tmp/" session.run "sudo chmod 666 #{Pathname.new("/tmp").join(private_key_file_name)}" session.download Pathname.new("/tmp").join(private_key_file_name).to_s, Pathname.new(".").join("tmp", private_key_file_name).to_s session.run "sudo rm #{Pathname.new("/tmp").join(private_key_file_name)}" session.run "sudo -u postgres bash -c 'mkdir -p #{pg_home.join("bin")}'" backup_script = find_files("mimas_pg_backup.sh").first session.upload backup_script, Pathname.new("/tmp").join("mimas_pg_backup").to_s session.run "sudo mv #{Pathname.new("/tmp").join("mimas_pg_backup")} #{pg_home.join("bin/")}" session.run "sudo chown postgres:postgres #{pg_home.join("bin", "mimas_pg_backup")}" session.run "sudo chmod 700 #{pg_home.join("bin", "mimas_pg_backup")}" say "The private key to decrypt backups has been downloaded to your project's `tmp/` folder. Please store this safely, ideally in a password manager!".bold say "To schedule backups, run `/var/lib/postgresql/bin mimas_pg_backup -d DB_NAME -o OUTPUT_DIR" end end |
#install_postgresql(sudo_user:) ⇒ Object
5 6 7 8 9 |
# File 'lib/mimas/plugins/server/postgresql.rb', line 5 def install_postgresql(sudo_user:) ssh(self, user: sudo_user) do |session| session.run "sudo apt-get install postgresql postgresql-contrib -y" end end |