sovereign-stack/deployment/project/btcpayserver/remote_scripts/btcpay-backup.sh

117 lines
3.7 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
# Please be aware of these important issues:
#
# - Old channel state is toxic and you can loose all your funds, if you or someone
# else closes a channel based on the backup with old state - and the state changes
# often! If you publish an old state (say from yesterday's backup) on chain, you
# WILL LOSE ALL YOUR FUNDS IN A CHANNEL, because the counterparty will publish a
# revocation key!
if [ "$(id -u)" != "0" ]; then
2022-11-16 17:20:16 +00:00
echo "INFO: 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
# preparation
docker_dir=$(docker volume inspect generated_btcpay_datadir --format="{{.Mountpoint}}" | sed -e "s%/volumes/.*%%g")
dbdump_name=postgres.sql.gz
btcpay_dir="$BTCPAY_BASE_DIRECTORY/btcpayserver-docker"
backup_dir="$docker_dir/volumes/backup_datadir/_data"
dbdump_path="$docker_dir/$dbdump_name"
backup_path="$backup_dir/backup.tar.gz"
# ensure backup dir exists
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
cd "$btcpay_dir"
. helpers.sh
dbcontainer=$(docker ps -a -q -f "name=postgres_1")
if [ -z "$dbcontainer" ]; then
printf "\n"
2022-11-16 17:20:16 +00:00
echo "INFO: Database container is not up and running. Starting BTCPay Server."
2022-07-27 16:38:33 +00:00
docker volume create generated_postgres_datadir
docker-compose -f "$BTCPAY_DOCKER_COMPOSE" up -d postgres
printf "\n"
dbcontainer=$(docker ps -a -q -f "name=postgres_1")
if [ -z "$dbcontainer" ]; then
2022-11-16 17:20:16 +00:00
echo "INFO: Database container could not be started or found."
2022-07-27 16:38:33 +00:00
exit 1
fi
fi
printf "\n"
2022-11-16 17:20:16 +00:00
echo "INFO: Dumping database."
2022-07-27 16:38:33 +00:00
{
docker exec "$dbcontainer" pg_dumpall -c -U postgres | gzip > "$dbdump_path"
2022-11-16 17:20:16 +00:00
echo "INFO: Database dump done."
2022-07-27 16:38:33 +00:00
} || {
2022-11-16 17:20:16 +00:00
echo "ERROR: Dumping failed. Please check the error message above."
2022-07-27 16:38:33 +00:00
exit 1
}
2022-11-16 17:20:16 +00:00
echo "Stopping BTCPay Server..."
2022-07-27 16:38:33 +00:00
btcpay_down
printf "\n"
2022-11-16 17:20:16 +00:00
cd "$docker_dir"
echo "Archiving files in $(pwd)."
2022-07-27 16:38:33 +00:00
{
tar \
--exclude="volumes/backup_datadir" \
2022-08-03 14:53:11 +00:00
--exclude="volumes/generated_bitcoin_datadir/_data/blocks" \
--exclude="volumes/generated_bitcoin_datadir/_data/chainstate" \
--exclude="volumes/generated_bitcoin_datadir/_data/debug.log" \
--exclude="volumes/generated_bitcoin_datadir/_data/testnet3/blocks" \
--exclude="volumes/generated_bitcoin_datadir/_data/testnet3/chainstate" \
--exclude="volumes/generated_bitcoin_datadir/_data/testnet3/debug.log" \
--exclude="volumes/generated_bitcoin_datadir/_data/regtest/blocks" \
--exclude="volumes/generated_bitcoin_datadir/_data/regtest/chainstate" \
--exclude="volumes/generated_bitcoin_datadir/_data/regtest/debug.log" \
2022-07-27 16:38:33 +00:00
--exclude="volumes/generated_postgres_datadir" \
2022-08-03 14:53:11 +00:00
--exclude="volumes/generated_tor_relay_datadir" \
2022-07-27 16:38:33 +00:00
--exclude="volumes/generated_clightning_bitcoin_datadir/_data/lightning-rpc" \
--exclude="**/logs/*" \
-cvzf "$backup_path" "$dbdump_name" volumes/generated_*
2022-11-16 17:20:16 +00:00
echo "INFO: Archive done."
2022-07-27 16:38:33 +00:00
2022-11-16 17:20:16 +00:00
if [ -n "$BTCPAY_BACKUP_PASSPHRASE" ]; then
2022-07-27 16:38:33 +00:00
printf "\n"
2022-11-16 17:20:16 +00:00
echo "INFO: BTCPAY_BACKUP_PASSPHRASE is set, the backup will be encrypted."
2022-07-27 16:38:33 +00:00
{
2022-11-16 17:20:16 +00:00
gpg -o "$backup_path.gpg" --batch --yes -c --passphrase "$BTCPAY_BACKUP_PASSPHRASE" "$backup_path"
2022-07-27 16:38:33 +00:00
rm "$backup_path"
backup_path="$backup_path.gpg"
2022-11-16 17:20:16 +00:00
echo "INFO: Encryption done."
2022-07-27 16:38:33 +00:00
} || {
2022-11-16 17:20:16 +00:00
echo "INFO: Encrypting failed. Please check the error message above."
echo "INFO: Restarting BTCPay Server."
2022-07-27 16:38:33 +00:00
cd "$btcpay_dir"
2022-11-16 17:20:16 +00:00
2022-07-27 16:38:33 +00:00
exit 1
}
fi
} || {
2022-11-16 17:20:16 +00:00
echo "INFO: Archiving failed. Please check the error message above."
echo "Restarting BTCPay Server"
cd "$btcpay_dir"
2022-07-27 16:38:33 +00:00
exit 1
}
2022-11-16 17:20:16 +00:00
printf "Restarting BTCPay Server."
cd "$btcpay_dir"
2022-07-27 16:38:33 +00:00
2022-11-16 17:20:16 +00:00
echo "Cleaning up."
rm "$dbdump_path"
2022-07-27 16:38:33 +00:00
2022-11-16 17:20:16 +00:00
echo "INFO: Backup done => $backup_path."