sovereign-stack/deployment/btcpayserver/remote_scripts/btcpay-restore.sh

116 lines
3.0 KiB
Bash
Raw Normal View History

2022-07-27 16:38:33 +00:00
#!/bin/bash -e
2022-10-29 15:34:17 +00:00
set -o pipefail -o errexit
2022-07-27 16:38:33 +00:00
if [ "$(id -u)" != "0" ]; then
2022-08-03 14:53:40 +00:00
echo "ERROR: This script must be run as root."
echo "➡️ Use the command 'sudo su -' (include the trailing hypen) and try again."
2022-07-27 16:38:33 +00:00
exit 1
fi
2022-11-16 17:23:02 +00:00
backup_path="$1"
2022-07-27 16:38:33 +00:00
if [ -z "$backup_path" ]; then
2022-08-03 14:53:40 +00:00
echo "ERROR: Usage: btcpay-restore.sh /path/to/backup.tar.gz"
2022-07-27 16:38:33 +00:00
exit 1
fi
if [ ! -f "$backup_path" ]; then
2022-08-03 14:53:40 +00:00
echo "ERROR: $backup_path does not exist."
2022-07-27 16:38:33 +00:00
exit 1
fi
if [[ "$backup_path" == *.gpg && -z "$BTCPAY_BACKUP_PASSPHRASE" ]]; then
2022-08-03 14:53:40 +00:00
echo "INFO: $backup_path is encrypted. Please provide the passphrase to decrypt it."
echo "INFO: Usage: BTCPAY_BACKUP_PASSPHRASE=t0pSeCrEt btcpay-restore.sh /path/to/backup.tar.gz.gpg"
2022-07-27 16:38:33 +00:00
exit 1
fi
# preparation
docker_dir=$(docker volume inspect generated_btcpay_datadir --format="{{.Mountpoint}}" | sed -e "s%/volumes/.*%%g")
restore_dir="$docker_dir/volumes/backup_datadir/_data/restore"
dbdump_name=postgres.sql.gz
btcpay_dir="$BTCPAY_BASE_DIRECTORY/btcpayserver-docker"
# ensure clean restore dir
2022-08-03 14:53:40 +00:00
echo "INFO: Cleaning restore directory $restore_dir."
rm -rf "$restore_dir"
mkdir -p "$restore_dir"
2022-07-27 16:38:33 +00:00
if [[ "$backup_path" == *.gpg ]]; then
2022-11-16 17:23:02 +00:00
echo "INFO: Decrypting backup file."
2022-07-27 16:38:33 +00:00
{
2022-08-03 14:53:40 +00:00
gpg -o "${backup_path%.*}" --batch --yes --passphrase "$BTCPAY_BACKUP_PASSPHRASE" -d "$backup_path"
2022-07-27 16:38:33 +00:00
backup_path="${backup_path%.*}"
2022-08-03 14:53:40 +00:00
echo "SUCESS: Decryption done."
2022-07-27 16:38:33 +00:00
} || {
2022-11-16 17:23:02 +00:00
echo "INFO: Decryption failed. Please check the error message above."
2022-07-27 16:38:33 +00:00
exit 1
}
fi
2022-08-03 14:53:40 +00:00
cd "$restore_dir"
2022-07-27 16:38:33 +00:00
2022-11-16 17:23:02 +00:00
echo "INFO: Extracting files in $(pwd)."
2022-08-03 14:53:40 +00:00
tar -h -xvf "$backup_path" -C "$restore_dir"
2022-07-27 16:38:33 +00:00
# basic control checks
if [ ! -f "$dbdump_name" ]; then
2022-08-03 14:53:40 +00:00
echo "ERROR: '$dbdump_name' does not exist."
2022-07-27 16:38:33 +00:00
exit 1
fi
if [ ! -d "volumes" ]; then
2022-08-03 14:53:40 +00:00
echo "ERROR: volumes directory does not exist."
2022-07-27 16:38:33 +00:00
exit 1
fi
2022-08-03 14:53:40 +00:00
cd "$btcpay_dir"
2022-07-27 16:38:33 +00:00
. helpers.sh
2022-08-03 14:53:40 +00:00
cd "$restore_dir"
2022-07-27 16:38:33 +00:00
{
2022-08-03 14:53:40 +00:00
echo "INFO: Restoring volumes."
2022-07-27 16:38:33 +00:00
# ensure volumes dir exists
if [ ! -d "$docker_dir/volumes" ]; then
2022-08-03 14:53:40 +00:00
mkdir -p "$docker_dir/volumes"
2022-07-27 16:38:33 +00:00
fi
# copy volume directories over
2022-08-03 14:53:40 +00:00
cp -r volumes/* "$docker_dir/volumes/"
2022-07-27 16:38:33 +00:00
# ensure datadirs excluded in backup exist
2022-08-03 14:53:40 +00:00
mkdir -p "$docker_dir/volumes/generated_postgres_datadir/_data"
2022-11-16 17:23:02 +00:00
echo "INFO: Volume restore done."
2022-07-27 16:38:33 +00:00
} || {
2022-11-16 17:23:02 +00:00
echo "INFO: Restoring volumes failed. Please check the error message above."
2022-07-27 16:38:33 +00:00
exit 1
}
{
2022-08-03 14:53:40 +00:00
echo "INFO: Starting database container"
docker-compose -f "$BTCPAY_DOCKER_COMPOSE" up -d postgres
2022-07-27 16:38:33 +00:00
dbcontainer=$(docker ps -a -q -f "name=postgres")
if [ -z "$dbcontainer" ]; then
2022-08-03 14:53:40 +00:00
echo "ERROR: Database container could not be started or found."
2022-07-27 16:38:33 +00:00
exit 1
fi
} || {
2022-08-03 14:53:40 +00:00
echo "ERROR: Starting database container failed. Please check the error message above."
2022-07-27 16:38:33 +00:00
exit 1
}
2022-08-03 14:53:40 +00:00
cd "$restore_dir"
2022-07-27 16:38:33 +00:00
{
2022-08-03 14:53:40 +00:00
echo "INFO: Restoring database..."
gunzip -c $dbdump_name | docker exec -i "$dbcontainer" psql -U postgres postgres -a
echo "SUCCESS: Database restore done."
2022-07-27 16:38:33 +00:00
} || {
2022-08-03 14:53:40 +00:00
echo "ERROR: Restoring database failed. Please check the error message above."
2022-07-27 16:38:33 +00:00
exit 1
}
2022-08-03 14:53:40 +00:00
echo "INFO: Cleaning up."
rm -rf "$restore_dir"
2022-07-27 16:38:33 +00:00
2022-08-03 14:53:40 +00:00
echo "SUCCESS: Restore done"