#!/bin/bash # The 'PostgreSQL' restore process # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Restore from a previously created backup clear; # Check to see if a backup has previously completed successfully # If a backup is in place then carry on and restore it if [ ! -f /home/foo/logs/postgresql_backup_created ]; then # Message echo ""; echo -e "\e[31mNo previous backup to restore!\e[0m"; else # Stop the running PostgreSQL service echo -e "\e[1;33mStop the running PostgreSQL service...\e[0m"; sudo systemctl stop postgresql.service; echo -e "\e[32mDone\e[0m"; echo ""; # Current PostgreSQL status echo -e "\e[1;33mCurrent PostgreSQL status...\e[0m"; sudo systemctl status postgresql; echo -e "\e[32mDone\e[0m"; echo ""; # chown the existing PostgreSQL installation echo -e "\e[1;33mchown the existing PostgreSQL installation...\e[0m"; sudo chown -R foo:foo /var/lib/postgresql/17/main/; echo -e "\e[32mDone\e[0m"; echo ""; # Remove the existing PostgreSQL files echo -e "\e[1;33mRemove the existing PostgreSQL files...\e[0m"; sudo rm -fr /var/lib/postgresql/17/main/*; echo -e "\e[32mDone\e[0m"; echo ""; # Copy the latest backup to the PostgreSQL target location echo -e "\e[1;33mCopy the latest backup to the PostgreSQL target location...\e[0m"; sudo cp -R /home/foo/backups/postgresql/* /var/lib/postgresql/17/main/; echo -e "\e[32mDone\e[0m"; echo ""; # Make the PostgreSQL user take ownership of the target location echo -e "\e[1;33mMake the PostgreSQL user take ownership of the target location...\e[0m"; sudo chown -R postgres:postgres /var/lib/postgresql/17/main/; echo -e "\e[32mDone\e[0m"; echo ""; # Start the PostgreSQL service echo -e "\e[1;33mStart the PostgreSQL service...\e[0m"; sudo systemctl start postgresql.service; echo -e "\e[32mDone\e[0m"; echo ""; # Current PostgreSQL status echo -e "\e[1;33mCurrent PostgreSQL status...\e[0m"; sudo systemctl status postgresql; echo -e "\e[32mDone\e[0m"; echo ""; fi