Moar backup

Moar restore

An Iceshrimp backup and restore process, now with an added Menu.

Why?

I’ve written in the past about backing up and restoring my Calckey instance and more recently about my Firefish instance.

As I refuse to stop tinkering I recently moved to Iceshrimp .

Clearly this needs a backup and restore process too.

The Iceshrimp backup and restore process

This whole process is a bit simpler than the previous ones.

Firstly, a backup process takes a copy of the database and creates a tiny file as a flag to say that it completed successfully.

The second backup process takes copies of files and creates another tiny file as a flag to say that it has completed successfully.

The restore process is a menu asking whether you wish to restore either the database or the files.

Neither restore process will continue unless the successful completion flag is in place.

Iceshrimp backup

The backup process is split into two parts:

  • A backup of the Iceshrimp database
  • A backup of:
    • the default.yml file
    • the nginx conf
    • the iceshrimp.service file
    • the iceshrimp /files/ directory

Create server directories

Firstly, ssh into the server and:

mkdir -p /home/foo/Backups/00.logs

mkdir -p /home/foo/Backups/10.sites/iceshrimp/database

mkdir -p /home/foo/Backups/10.sites/iceshrimp/configs/

mkdir -p /home/foo/Backups/10.sites/iceshrimp/files

.pgpass

.pgpass is a PostgreSQL feature that allows you to securely store the connection information.

The file .pgpass in a user’s home directory can contain passwords to be used if the connection requires a password

https://www.postgresql.org/docs/current/libpq-pgpass.html

Obviously the .pgpass file needs creating, and appropriate permissions granting.

Firstly, create the .pgpass file:

sudo nano /home/$USER/.pgpass

#hostname:port:database:username:password
*:*:*:ThePostgresAccount:ThePostgresAccountPassword

Then, own it:

sudo chown $USER:$USER /home/$USER/.pgpass

Finally, give the file the correct permissions:

chmod 600 /home/$USER/.pgpass

Write it to the environment variables:

export PGPASSFILE='/home/$USER/.pgpass'

Check the environment variable using:

env

The ‘pg_dump’ command will now not ask for a password:

pg_dump 'iceshrimp' -U postgres -h localhost -p 5432 > "/home/foo/Backups/10.sites/iceshrimp/database/iceshrimp_backup_database.sql" -Fc

Backup the Iceshrimp database

This is a flat copy of the Iceshrimp PostgreSQL database, not the whole database as previous backup scripts have created.

  • The script first removes the file showing that a previous backup was successful
  • The removed file is replaced at the end to show that the backup has successfully completed
  • The iceshrimp_restore.sh ‘Restore the ‘iceshrimp_backup_database’ menu option will not run if this file is not in place

Firstly, create the database backup script:

sudo nano /etc/init.d/backup_iceshrimp_db.sh

Then give the file the correct permissions:

sudo chmod +x /etc/init.d/backup_iceshrimp_db.sh

backup_iceshrimp_db.sh

#!/bin/bash

clear

# The iceshrimp database backup process
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Remove the file showing that a previous backup was successful
echo ""
echo -e "\e[1;33mRemove the file showing that a previous backup was successful...\e[0m"
rm /home/foo/Backups/00.logs/iceshrimp_backup_database;
echo -e "\e[32mDone\e[0m"

# Create a new 'iceshrimp_backup_database' file
echo ""
echo -e "\e[1;33mCreate a new 'iceshrimp_backup_database' file...\e[0m"
pg_dump 'iceshrimp' -U postgres -h localhost -p 5432 > "/home/foo/Backups/10.sites/iceshrimp/database/iceshrimp_backup_database.sql" -Fc;
echo -e "\e[32mDone\e[0m"

# Create a file to show that the backup has successfully completed
echo ""
echo -e "\e[1;33mCreate a file to show that the backup has successfully completed...\e[0m"
touch /home/foo/Backups/00.logs/iceshrimp_backup_database;
echo -e "\e[32mDone\e[0m"

Backup the Iceshrimp files

The iceshrimp /files/ directory holds all of the ‘Drive’ files.

(All of those avatars, headers, wallpapers and meme collections.)

  • The script first removes the file showing that a previous backup was successful
  • The removed file is replaced at the end to show that the backup has successfully completed
  • The iceshrimp_restore.sh ‘Restore the iceshrimp /files/ directory’ menu option will not run if this file is not in place

Firstly, create the /files/ backup script:

sudo nano /etc/init.d/backup_iceshrimp_files.sh

Then, give the file the correct permissions:

sudo chmod +x /etc/init.d/backup_iceshrimp_files.sh

backup_iceshrimp_files.sh

#!/bin/bash

clear

# The files backup process
# ~~~~~~~~~~~~~~~~~~~~~~~~
# Remove the file showing that a previous backup was successful
echo ""
echo -e "\e[1;33mRemove the file showing that a previous backup was successful...\e[0m"
rm /home/foo/Backups/00.logs/iceshrimp_backup_files;
echo -e "\e[32mDone\e[0m"

# Backup all of the iceshrimp files
echo ""
echo -e "\e[1;33mBackup the iceshrimp files...\e[0m"
# Remove old configs
rm -r /home/foo/Backups/10.sites/iceshrimp/configs/*;
#
# Backup the default.yml
cp -r /home/foo/iceshrimp/.config/default.yml /home/foo/Backups/10.sites/iceshrimp/configs/;
#
# Backup the iceshrimp.nginx.conf
cp -r /etc/nginx/sites-available/iceshrimp.nginx.conf /home/foo/Backups/10.sites/iceshrimp/configs/;
#
# Backup the iceshrimp.service
cp -r /etc/systemd/system/iceshrimp.service /home/foo/Backups/10.sites/iceshrimp/configs/;
#
# Remove old /files/
rm -r /home/foo/Backups/10.sites/iceshrimp/files/*;
#
# Backup the iceshrimp /files/ directory
cp -r /home/foo/iceshrimp/files/. /home/foo/Backups/10.sites/iceshrimp/files/;
echo -e "\e[32mDone\e[0m"

# Create a file to show that the backup has successfully completed
echo ""
echo -e "\e[1;33mCreate a file to show that the backup has successfully completed...\e[0m"
touch /home/foo/Backups/00.logs/iceshrimp_backup_files;
echo -e "\e[32mDone\e[0m"

Iceshrimp Restore

This script creates a small menu that presents two options:

  • “Restore the iceshrimp database”: This process will restore the iceshrimp database (if a backup exists)
    • Check to see if a backup has previously completed successfully
    • If a backup is in place then carry on and restore it
  • “Restore the iceshrimp /files/ directory”: This process will restore the iceshrimp /files/ (if a backup exists)
    • Check to see if a backup has previously completed successfully
    • If a backup is in place then carry on and restore it

NOTE: The backup_iceshrimp_files.sh script also creates a copy of the default.yml, the iceshrimp.nginx.conf and the iceshrimp.service files.

These files are not part of this restore script and will have to be restored manually.

To restore both the database and the /files/ directory, the menu will have to be cycled through twice.

Firstly, create the restore script:

sudo nano /etc/init.d/restore_iceshrimp.sh

Then, give the file the correct permissions:

sudo chmod +x /etc/init.d/restore_iceshrimp.sh

restore_iceshrimp.sh

#!/bin/bash

HEIGHT=15
WIDTH=75
CHOICE_HEIGHT=4
BACKTITLE="Restore iceshrimp database and / or files"
TITLE="Restoring Iceshrimp"
MENU="Choose one of the following options:"

OPTIONS=(1 "Restore the iceshrimp database"
         2 "Restore the iceshrimp /files/ directory")

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear

case $CHOICE in
        1)
            echo -e "\e[1;33mRestoring the iceshrimp database\e[0m"
            echo -e "\e[1;33mThis process will restore the iceshrimp database (if a backup exists)\e[0m"

            # 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/Backups/00.logs/iceshrimp_backup_database ]; then

                # Message
                echo ""
                echo -e "\e[31mNo previous backup to restore!\e[0m"

            else
                # Restore the 'iceshrimp_backup_database' file
                echo ""
                echo -e "\e[1;33mRestore the 'iceshrimp_backup_database' file' file...\e[0m"
                sleep 2
                pg_restore -U postgres -h localhost -p 5432 -d iceshrimp -v "/home/foo/Backups/10.sites/iceshrimp/database/iceshrimp_backup_database.sql"

                # Message
                echo ""
                echo -e "\e[32mRestored the iceshrimp database backup\e[0m"
            fi

            # Pause
            sleep 2

            # Reload the menu
            bash /etc/init.d/restore_iceshrimp.sh
            ;;
        2)
            echo -e "\e[1;33mRestoring the iceshrimp /files/ directory\e[0m"
            echo -e "\e[1;33mThis process will restore the iceshrimp /files/ (if a backup exists)\e[0m"

            # 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/Backups/00.logs/iceshrimp_backup_files ]; then

                # Message
                echo ""
                echo -e "\e[31mNo previous backup to restore!\e[0m"

            else
                # Restore the iceshrimp /files/ directory
                echo ""
                echo -e "\e[1;33mRestore the iceshrimp /files/ directory...\e[0m"
                sleep 2
                sudo cp /home/foo/Backups/10.sites/iceshrimp/files/* /home/foo/iceshrimp/files

                # Message
                echo ""
                echo -e "\e[32mRestored the iceshrimp /files/ directory\e[0m"
            fi

            # Pause
            sleep 2

            # Reload the menu
            bash /etc/init.d/restore_iceshrimp.sh
            ;;
esac

crontab -e

Backup the iceshrimp database daily at midnight by running the script in a normal crontab.

#
# Backup the iceshrimp database daily at midnight
0 0 * * * sh /etc/init.d/backup_iceshrimp_db.sh
#

sudo crontab -e

Backup the iceshrimp files daily at midnight by running the script in a root crontab.

#
# Backup the iceshrimp files daily at midnight
0 0 * * * sh /etc/init.d/backup_iceshrimp_files.sh
#

Finally

(I did mention this in a prior post but it bears repeating.)

For anyone interested in getting into the SQL side, pgAdmin is a useful tool.

I installed it using:

curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg

sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/jammy pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'

sudo apt update

sudo apt install pgadmin4

A screenshot of the Linux application pgAdmin, used to administer PostgreSQL databases. The Calckey Fediverse Server uses PostgreSQL as its back end darabase engine. pgAdmin Screenshot

However you Fediverse, Enjoy!