ansible/roles/pleroma/templates/recover.sh
2020-08-02 19:36:46 -05:00

62 lines
2.0 KiB
Bash

#! /bin/sh
#
# recover.sh
# Recover a Pleroma server from our S3 bucket
# Copyright (C) 2020 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
set -e
# Get to our Pleroma dir
export PLEROMA_DIR="/opt/pleroma"
cd "$PLEROMA_DIR" || exit 50
# Make sure we have a backup
if ! aws s3 ls "s3://{{ aws.backup_bucket }}/{{ pleroma_url }}/" > /dev/null 2>&1; then
echo "No backups available"
exit 0
fi
# If we have existing Pleroma uploads, remove them
if [ -d /var/lib/pleroma/uploads ]; then
echo "Backing up current uploads"
tar czf "recover-uploads-$(date -Iseconds).tar.gz" /var/lib/pleroma/uploads --remove-files --force-local
fi
# If it STILL exists, then we have a problem
if [ -d /var/lib/pleroma/uploads ]; then
echo "Uploads still found after tarring; bailing"
exit 51
fi
# Get our latest good uploads backup
backup_up="$(aws s3 ls "s3://{{ aws.backup_bucket }}/{{ pleroma_url }}/" | grep uploads | tail -n 1 | awk '{print $4}')"
# And our latest good DB backup
backup_db="$(aws s3 ls "s3://{{ aws.backup_bucket }}/{{ pleroma_url }}/" | grep pgdump | tail -n 1 | awk '{print $4}')"
echo "Restoring backup: $backup_up $backup_db"
# Get our backups
aws s3 cp "s3://{{ aws.backup_bucket }}/{{ pleroma_url }}/$backup_up" uploads.tgz
aws s3 cp "s3://{{ aws.backup_bucket }}/{{ pleroma_url }}/$backup_db" db.pgdump.gz
# Decompress
tar xzf uploads.tgz
gunzip db.pgdump.gz
# Find uploads
uploaddir="$(find ./var -type d -name "uploads" | head -n 1)"
# Ensure we're stopped
systemctl stop pleroma
# Move it into place
mv "$uploaddir" /var/lib/pleroma/
# Assure ownership
chown pleroma. /var/lib/pleroma/uploads
# Remove the var dir, if it exists
if [ -d "var" ]; then
rm -rf "$PLEROMA_DIR/var"
fi
# Drop and recreate our database
sudo -Hu postgres psql -c 'DROP DATABASE pleroma;'
sudo -Hu postgres psql -c 'CREATE DATABASE pleroma;'
cat db.pgdump | sudo -Hu postgres pg_restore -d pleroma -v -1 db.pgdump
sudo -Hu postgres vacuumdb --all --analyze-in-stages
# FIRE IT UP
systemctl start pleroma