SHellcheck compliance updates.

This commit is contained in:
Derek Smith 2022-08-03 10:53:40 -04:00
parent fc6d7fda90
commit 39f51b26d8
Signed by: farscapian
GPG Key ID: 8F1CD799CCA516CC

View File

@ -3,25 +3,25 @@
set -o pipefail -o errexit -x set -o pipefail -o errexit -x
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
printf "\n🚨 This script must be run as root.\n" echo "ERROR: This script must be run as root."
printf "➡️ Use the command 'sudo su -' (include the trailing hypen) and try again.\n\n" echo "➡️ Use the command 'sudo su -' (include the trailing hypen) and try again."
exit 1 exit 1
fi fi
backup_path=$1 backup_path=$1
if [ -z "$backup_path" ]; then if [ -z "$backup_path" ]; then
printf "\n Usage: btcpay-restore.sh /path/to/backup.tar.gz\n\n" echo "ERROR: Usage: btcpay-restore.sh /path/to/backup.tar.gz"
exit 1 exit 1
fi fi
if [ ! -f "$backup_path" ]; then if [ ! -f "$backup_path" ]; then
printf "\n🚨 $backup_path does not exist.\n\n" echo "ERROR: $backup_path does not exist."
exit 1 exit 1
fi fi
if [[ "$backup_path" == *.gpg && -z "$BTCPAY_BACKUP_PASSPHRASE" ]]; then if [[ "$backup_path" == *.gpg && -z "$BTCPAY_BACKUP_PASSPHRASE" ]]; then
printf "\n🔐 $backup_path is encrypted. Please provide the passphrase to decrypt it." echo "INFO: $backup_path is encrypted. Please provide the passphrase to decrypt it."
printf "\n Usage: BTCPAY_BACKUP_PASSPHRASE=t0pSeCrEt btcpay-restore.sh /path/to/backup.tar.gz.gpg\n\n" echo "INFO: Usage: BTCPAY_BACKUP_PASSPHRASE=t0pSeCrEt btcpay-restore.sh /path/to/backup.tar.gz.gpg"
exit 1 exit 1
fi fi
@ -32,103 +32,87 @@ dbdump_name=postgres.sql.gz
btcpay_dir="$BTCPAY_BASE_DIRECTORY/btcpayserver-docker" btcpay_dir="$BTCPAY_BASE_DIRECTORY/btcpayserver-docker"
# ensure clean restore dir # ensure clean restore dir
printf "\n Cleaning restore directory $restore_dir …\n\n" echo "INFO: Cleaning restore directory $restore_dir."
rm -rf $restore_dir rm -rf "$restore_dir"
mkdir -p $restore_dir mkdir -p "$restore_dir"
if [[ "$backup_path" == *.gpg ]]; then if [[ "$backup_path" == *.gpg ]]; then
echo "🔐 Decrypting backup file …" echo "🔐 Decrypting backup file …"
{ {
gpg -o "${backup_path%.*}" --batch --yes --passphrase "$BTCPAY_BACKUP_PASSPHRASE" -d $backup_path gpg -o "${backup_path%.*}" --batch --yes --passphrase "$BTCPAY_BACKUP_PASSPHRASE" -d "$backup_path"
backup_path="${backup_path%.*}" backup_path="${backup_path%.*}"
printf "✅ Decryption done.\n\n" echo "SUCESS: Decryption done."
} || { } || {
echo "🚨 Decryption failed. Please check the error message above." echo "🚨 Decryption failed. Please check the error message above."
exit 1 exit 1
} }
fi fi
cd $restore_dir cd "$restore_dir"
echo " Extracting files in $(pwd)" echo " Extracting files in $(pwd)"
tar -h -xvf $backup_path -C $restore_dir tar -h -xvf "$backup_path" -C "$restore_dir"
# basic control checks # basic control checks
if [ ! -f "$dbdump_name" ]; then if [ ! -f "$dbdump_name" ]; then
printf "\n🚨 $dbdump_name does not exist.\n\n" echo "ERROR: '$dbdump_name' does not exist."
exit 1 exit 1
fi fi
if [ ! -d "volumes" ]; then if [ ! -d "volumes" ]; then
printf "\n🚨 volumes directory does not exist.\n\n" echo "ERROR: volumes directory does not exist."
exit 1 exit 1
fi fi
cd $btcpay_dir cd "$btcpay_dir"
. helpers.sh . helpers.sh
printf "\n Stopping BTCPay Server …\n\n" #echo "INFO: Stopping BTCPay Server."
btcpay_down #btcpay_down
cd $restore_dir cd "$restore_dir"
{ {
printf "\n Restoring volumes …\n" echo "INFO: Restoring volumes."
# ensure volumes dir exists # ensure volumes dir exists
if [ ! -d "$docker_dir/volumes" ]; then if [ ! -d "$docker_dir/volumes" ]; then
mkdir -p $docker_dir/volumes mkdir -p "$docker_dir/volumes"
fi fi
# copy volume directories over # copy volume directories over
cp -r volumes/* $docker_dir/volumes/ cp -r volumes/* "$docker_dir/volumes/"
# ensure datadirs excluded in backup exist # ensure datadirs excluded in backup exist
mkdir -p $docker_dir/volumes/generated_postgres_datadir/_data mkdir -p "$docker_dir/volumes/generated_postgres_datadir/_data"
echo "✅ Volume restore done." echo "✅ Volume restore done."
} || { } || {
echo "🚨 Restoring volumes failed. Please check the error message above." echo "🚨 Restoring volumes failed. Please check the error message above."
printf "\n Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1 exit 1
} }
{ {
printf "\n Starting database container …\n" echo "INFO: Starting database container"
docker-compose -f $BTCPAY_DOCKER_COMPOSE up -d postgres docker-compose -f "$BTCPAY_DOCKER_COMPOSE" up -d postgres
dbcontainer=$(docker ps -a -q -f "name=postgres") dbcontainer=$(docker ps -a -q -f "name=postgres")
if [ -z "$dbcontainer" ]; then if [ -z "$dbcontainer" ]; then
echo "🚨 Database container could not be started or found." echo "ERROR: Database container could not be started or found."
printf "\n Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1 exit 1
fi fi
} || { } || {
echo "🚨 Starting database container failed. Please check the error message above." echo "ERROR: Starting database container failed. Please check the error message above."
printf "\n Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1 exit 1
} }
cd $restore_dir cd "$restore_dir"
{ {
printf "\n Restoring database …" echo "INFO: Restoring database..."
gunzip -c $dbdump_name | docker exec -i $dbcontainer psql -U postgres postgres -a gunzip -c $dbdump_name | docker exec -i "$dbcontainer" psql -U postgres postgres -a
echo " Database restore done." echo "SUCCESS: Database restore done."
} || { } || {
echo "🚨 Restoring database failed. Please check the error message above." echo "ERROR: Restoring database failed. Please check the error message above."
printf "\n Restarting BTCPay Server …\n\n"
cd $btcpay_dir
btcpay_up
exit 1 exit 1
} }
printf "\n Restarting BTCPay Server …\n\n" echo "INFO: Cleaning up."
cd $btcpay_dir rm -rf "$restore_dir"
btcpay_up
printf "\n Cleaning up …\n\n" echo "SUCCESS: Restore done"
rm -rf $restore_dir
printf "✅ Restore done\n\n"