diff --git a/btcpay-backup.sh b/btcpay-backup.sh index 49b1d2e..aa15a5d 100755 --- a/btcpay-backup.sh +++ b/btcpay-backup.sh @@ -28,10 +28,10 @@ fi . "$BASH_PROFILE_SCRIPT" docker_dir=$(docker volume inspect generated_btcpay_datadir --format="{{.Mountpoint}}" | sed -e "s%/volumes/.*%%g") -dbdump_name=postgres.sql.gz +postgres_dump_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" +postgres_dump_path="$docker_dir/$postgres_dump_name" backup_path="$backup_dir/backup.tar.gz" # ensure backup dir exists @@ -42,31 +42,50 @@ fi cd $btcpay_dir . helpers.sh -dbcontainer=$(docker ps -a -q -f "name=postgres_1") -if [ -z "$dbcontainer" ]; then +# Postgres database +postgres_container=$(docker ps -a -q -f "name=postgres_1") +if [ -z "$postgres_container" ]; then printf "\n" - echo "ℹ️ Database container is not up and running. Starting BTCPay Server …" + echo "ℹ️ Postgres container is not up and running. Starting BTCPay Server …" 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 - echo "🚨 Database container could not be started or found." + postgres_container=$(docker ps -a -q -f "name=postgres_1") + if [ -z "$postgres_container" ]; then + echo "🚨 Postgres container could not be started or found." exit 1 fi fi printf "\n" -echo "ℹ️ Dumping database …" +echo "ℹ️ Dumping Postgres database …" { - docker exec $dbcontainer pg_dumpall -c -U postgres | gzip > $dbdump_path - echo "✅ Database dump done." + docker exec $postgres_container pg_dumpall -c -U postgres | gzip > $postgres_dump_path + echo "✅ Postgres database dump done." } || { - echo "🚨 Dumping failed. Please check the error message above." + echo "🚨 Dumping Postgres database failed. Please check the error message above." exit 1 } +# Optional: MariaDB database +mariadb_container=$(docker ps -a -q -f "name=mariadb_1") +if [ ! -z "$mariadb_container" ]; then + mariadb_dump_name=mariadb.sql.gz + mariadb_dump_path="$docker_dir/$mariadb_dump_name" + # MariaDB container exists and is running - dump it + printf "\n" + echo "ℹ️ Dumping MariaDB database …" + { + docker exec $mariadb_container mysqldump -u root -pwordpressdb -A --add-drop-database | gzip > $mariadb_dump_path + echo "✅ MariaDB database dump done." + } || { + echo "🚨 Dumping MariaDB database failed. Please check the error message above." + exit 1 + } +fi + +# BTCPay Server backup printf "\nℹ️ Stopping BTCPay Server …\n\n" btcpay_down @@ -85,12 +104,13 @@ echo "ℹ️ Archiving files in $(pwd)…" --exclude="volumes/generated_litecoin_datadir/_data/chainstate" \ --exclude="volumes/generated_litecoin_datadir/_data/indexes" \ --exclude="volumes/generated_litecoin_datadir/_data/debug.log" \ + --exclude="volumes/generated_mariadb_datadir" \ --exclude="volumes/generated_postgres_datadir" \ --exclude="volumes/generated_electrumx_datadir" \ --exclude="volumes/generated_lnd_bitcoin_datadir/_data/data/graph" \ --exclude="volumes/generated_clightning_bitcoin_datadir/_data/lightning-rpc" \ --exclude="**/logs/*" \ - -cvzf $backup_path $dbdump_name volumes/generated_* + -cvzf $backup_path $postgres_dump_name $mariadb_dump_name volumes/generated_* echo "✅ Archive done." if [ ! -z "$BTCPAY_BACKUP_PASSPHRASE" ]; then @@ -122,6 +142,6 @@ cd $btcpay_dir btcpay_up printf "\nℹ️ Cleaning up …\n\n" -rm $dbdump_path +rm $postgres_dump_path printf "✅ Backup done => $backup_path\n\n" diff --git a/btcpay-restore.sh b/btcpay-restore.sh index 214b5c6..e9bbb7f 100755 --- a/btcpay-restore.sh +++ b/btcpay-restore.sh @@ -28,7 +28,7 @@ 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 +postgres_dump_name=postgres.sql.gz btcpay_dir="$BTCPAY_BASE_DIRECTORY/btcpayserver-docker" # ensure clean restore dir @@ -54,8 +54,8 @@ echo "ℹ️ Extracting files in $(pwd) …" tar -xvf $backup_path -C $restore_dir # basic control checks -if [ ! -f "$dbdump_name" ]; then - printf "\n🚨 $dbdump_name does not exist.\n\n" +if [ ! -f "$postgres_dump_name" ]; then + printf "\n🚨 $postgres_dump_name does not exist.\n\n" exit 1 fi @@ -64,6 +64,10 @@ if [ ! -d "volumes" ]; then exit 1 fi +if [ -f "mariadb.sql.gz" ]; then + mariadb_dump_name=mariadb.sql.gz +fi + cd $btcpay_dir . helpers.sh @@ -82,6 +86,9 @@ cd $restore_dir cp -r volumes/* $docker_dir/volumes/ # ensure datadirs excluded in backup exist mkdir -p $docker_dir/volumes/generated_postgres_datadir/_data + if [ ! -z "$mariadb_dump_name" ]; then + mkdir -p $docker_dir/volumes/generated_mariadb_datadir/_data + fi echo "✅ Volume restore done." } || { echo "🚨 Restoring volumes failed. Please check the error message above." @@ -91,40 +98,80 @@ cd $restore_dir exit 1 } +# Start Postgres database { - printf "\nℹ️ Starting database container …\n" + printf "\nℹ️ Starting Postgres database container …\n" docker-compose -f $BTCPAY_DOCKER_COMPOSE up -d postgres sleep 10 - dbcontainer=$(docker ps -a -q -f "name=postgres") - if [ -z "$dbcontainer" ]; then - echo "🚨 Database container could not be started or found." + postgres_container=$(docker ps -a -q -f "name=postgres_1") + if [ -z "$postgres_container" ]; then + echo "🚨 Postgres database container could not be started or found." printf "\nℹ️ Restarting BTCPay Server …\n\n" cd $btcpay_dir btcpay_up exit 1 fi } || { - echo "🚨 Starting database container failed. Please check the error message above." + echo "🚨 Starting Postgres database container failed. Please check the error message above." printf "\nℹ️ Restarting BTCPay Server …\n\n" cd $btcpay_dir btcpay_up exit 1 } +# Optional: Start MariaDB database +if [ ! -z "$mariadb_dump_name" ]; then + { + printf "\nℹ️ Starting MariaDB database container …\n" + docker-compose -f $BTCPAY_DOCKER_COMPOSE up -d mariadb + sleep 10 + mariadb_container=$(docker ps -a -q -f "name=mariadb_1") + if [ -z "$mariadb_container" ]; then + echo "🚨 MariaDB database container could not be started or found." + printf "\nℹ️ Restarting BTCPay Server …\n\n" + cd $btcpay_dir + btcpay_up + exit 1 + fi + } || { + echo "🚨 Starting MariaDB database container failed. Please check the error message above." + printf "\nℹ️ Restarting BTCPay Server …\n\n" + cd $btcpay_dir + btcpay_up + exit 1 + } +fi + cd $restore_dir +# Postgres database { - printf "\nℹ️ Restoring database …" - gunzip -c $dbdump_name | docker exec -i $dbcontainer psql -U postgres postgres -a - echo "✅ Database restore done." + printf "\nℹ️ Restoring Postgres database …" + gunzip -c $postgres_dump_name | docker exec -i $postgres_container psql -U postgres postgres + echo "✅ Postgres database restore done." } || { - echo "🚨 Restoring database failed. Please check the error message above." + echo "🚨 Restoring Postgres database failed. Please check the error message above." printf "\nℹ️ Restarting BTCPay Server …\n\n" cd $btcpay_dir btcpay_up exit 1 } +# Optional: MariaDB database +if [ ! -z "$mariadb_dump_name" ]; then + { + printf "\nℹ️ Restoring MariaDB database …" + gunzip -c $mariadb_dump_name | docker exec -i $mariadb_container mysql -u root -pwordpressdb + printf "\n✅ MariaDB database restore done." + } || { + echo "🚨 Restoring MariaDB database failed. Please check the error message above." + printf "\nℹ️ Restarting BTCPay Server …\n\n" + cd $btcpay_dir + btcpay_up + exit 1 + } +fi + printf "\nℹ️ Restarting BTCPay Server …\n\n" cd $btcpay_dir btcpay_up