From 724d580e85b0bd1bb913e741d7bcd6c9f45b697d Mon Sep 17 00:00:00 2001 From: blockchainluffy Date: Thu, 3 Jul 2025 13:09:47 +0530 Subject: [PATCH 1/5] feat: add backup and restore scripts and steps --- _container_scripts/keyper-db-init.sh | 13 ++- docker-compose.yml | 1 + scripts/RESTORE.md | 82 +++++++++++++++++ scripts/backup.sh | 68 +++++++++++++++ scripts/restore.sh | 126 +++++++++++++++++++++++++++ 5 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 scripts/RESTORE.md create mode 100755 scripts/backup.sh create mode 100755 scripts/restore.sh diff --git a/_container_scripts/keyper-db-init.sh b/_container_scripts/keyper-db-init.sh index 2853102..dca98d2 100755 --- a/_container_scripts/keyper-db-init.sh +++ b/_container_scripts/keyper-db-init.sh @@ -2,4 +2,15 @@ set -e -createdb -U postgres keyper +echo "Checking for backup dump file..." +if [ -f "/var/lib/postgresql/dump/keyper.dump" ]; then + echo "Backup dump found, restoring database with full schema and data..." + # Create the database first + createdb -U postgres keyper 2>/dev/null || echo "Database already exists" + # Restore the database with full schema and data + pg_restore -U postgres -d keyper --create --clean -v /var/lib/postgresql/dump/keyper.dump + echo "Database restore completed." +else + echo "No backup dump file found, creating fresh database..." + createdb -U postgres keyper +fi diff --git a/docker-compose.yml b/docker-compose.yml index 2fc286d..fd1bbe1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -47,6 +47,7 @@ services: volumes: - ./data/db:/var/lib/postgresql/data - ./_container_scripts/keyper-db-init.sh:/docker-entrypoint-initdb.d/keyper-db-init.sh:ro + - ./data/db-data:/var/lib/postgresql/dump healthcheck: test: pg_isready -U postgres start_period: "30s" diff --git a/scripts/RESTORE.md b/scripts/RESTORE.md new file mode 100644 index 0000000..cbcf810 --- /dev/null +++ b/scripts/RESTORE.md @@ -0,0 +1,82 @@ +# Backup and Restore Guide + +## Backup Process + +### Creating a Backup + +1. **Ensure services are running** - The backup process requires the database to be accessible +2. **Run the backup script**: + ```bash + ./scripts/backup.sh + ``` +3. **Backup location** - Backups are stored in `data/backups/` directory +4. **Backup naming** - Files are named with timestamp: `shutter-api-keyper-YYYY-MM-DDTHH-MM-SS.tar.xz` + +### What Gets Backed Up + +- Database dump (`keyper.dump`) - Contains full schema and data from the `keyper` database +- Chain data (`data/chain/`) - Blockchain data and configuration +- Keyper configuration (`config/`) - Application configuration files +- Environment variables - Metrics configuration settings + +## Restore Process + +### Prerequisites + +- **Empty keyper instance** - The restore should be performed on a fresh, empty deployment +- **No running services** - Ensure all Docker containers are stopped before restore +- **Backup file available** - The backup archive should be present in `data/backups/` directory + +### Restore Steps + +1. **Setup environment**: + ```bash + cp example-api.env .env + # Edit .env with your configuration values + ``` + +2. **Extract backup** (if needed): + ```bash + # Backup files are automatically extracted during restore + # No manual extraction required + ``` + +3. **Run restore script**: + ```bash + ./scripts/restore.sh + ``` + - This will automatically find the latest backup in `data/backups/` + - Prompts for confirmation before proceeding + - Restores all data to appropriate locations + +4. **Start services**: + ```bash + docker compose up -d + ``` + +### Restore Locations + +- **Database**: `data/db-data/keyper.dump` - Automatically restored to PostgreSQL +- **Chain data**: `data/chain/` - Keyper chain data and configuration +- **Configuration**: `config/` - Application configuration files +- **Environment**: `.env` - Updated with restored metrics settings + +### Important Notes + +- **Database restoration** - The database is automatically restored on first startup via the initialization script +- **Service order** - Restore must be completed before starting any services +- **Data integrity** - The restore process overwrites existing data; ensure you have a clean instance +- **Configuration review** - Review restored configuration files before starting services + +### Troubleshooting + +- **No backup found** - Ensure backup files exist in `data/backups/` directory +- **Permission errors** - Ensure proper file permissions on backup files +- **Configuration issues** - Verify that restored configuration files are valid + +### Verification + +After restore and startup: +1. Check database connectivity and table presence +2. Verify chain is propagating using chain container's logs +4. Test keyper, and see if it generates decryption keyshares \ No newline at end of file diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 0000000..f637840 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +set -euo pipefail + +R='\033[0;31m' +G='\033[0;32m' +Y='\033[0;33m' +B='\033[0;34m' +DEF='\033[0m' + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +ARCHIVE_NAME="shutter-api-keyper-$(date +%Y-%m-%dT%H-%M-%S).tar.xz" + +source "${SCRIPT_DIR}/../.env" + +WORKDIR=$(mktemp -d) + +cleanup() { + rv=$? + set +e + echo -e "${R}Unexpected error, exit code: $rv, cleaning up.${DEF}" + docker compose unpause || true + exit $rv +} + +trap cleanup EXIT + +echo -e "${G}Creating backup archive${DEF}" + +mkdir -p "${SCRIPT_DIR}/../data/backups" + +echo -e "${B}[1/7] Pausing all services except database...${DEF}" +docker compose pause keyper || true +docker compose pause chain || true + +echo -e "${B}[2/7] Creating database dump...${DEF}" +docker compose exec db pg_dump -U postgres -d keyper -Fc --create --clean -f /var/lib/postgresql/dump/keyper.dump + +echo -e "${B}[3/7] Pausing database...${DEF}" +docker compose pause db || true + +echo -e "${B}[4/7] Copying data...${DEF}" +cp -a "${SCRIPT_DIR}/../data/chain/" "${WORKDIR}/chain" +cp -a "${SCRIPT_DIR}/../data/db-data/keyper.dump" "${WORKDIR}/keyper.dump" +cp -a "${SCRIPT_DIR}/../config" "${WORKDIR}/keyper-config" + +mkdir -p "${WORKDIR}/metrics-config" +cat > "${WORKDIR}/metrics-config/settings.env" < /data/backups/${ARCHIVE_NAME}" + +echo -e "${B}[7/7] Cleaning up...${DEF}" +rm -rf "$WORKDIR" + +echo -e "${G}Done, backup archive created at ${B}data/backups/${ARCHIVE_NAME}${DEF}" + +echo -e "\n\n${R}WARNING, IMPORTANT!${DEF}" +echo -e "${Y}If you import this backup, make sure to stop this deployment first!${DEF}" + +trap - EXIT \ No newline at end of file diff --git a/scripts/restore.sh b/scripts/restore.sh new file mode 100755 index 0000000..6ab4167 --- /dev/null +++ b/scripts/restore.sh @@ -0,0 +1,126 @@ +#!/usr/bin/env bash + +set -euo pipefail + +R='\033[0;31m' +G='\033[0;32m' +Y='\033[0;33m' +B='\033[0;34m' +DEF='\033[0m' + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +BACKUPS_DIR="${SCRIPT_DIR}/../data/backups" + +WORKDIR=$(mktemp -d) + +cleanup() { + rv=$? + set +e + echo -e "${R}Unexpected error, exit code: $rv, cleaning up.${DEF}" + rm -rf "$WORKDIR" || true + exit $rv +} + +trap cleanup EXIT + +echo -e "${G}Restoring from latest backup${DEF}" + +# Check if backups directory exists +if [ ! -d "$BACKUPS_DIR" ]; then + echo -e "${R}Error: Backups directory not found at $BACKUPS_DIR${DEF}" + exit 1 +fi + +# Find the latest backup file +LATEST_BACKUP=$(find "$BACKUPS_DIR" -name "shutter-api-keyper-*.tar.xz" -type f | sort | tail -n 1) + +if [ -z "$LATEST_BACKUP" ]; then + echo -e "${R}Error: No backup files found in $BACKUPS_DIR${DEF}" + exit 1 +fi + +echo -e "${B}Found latest backup: ${Y}$(basename "$LATEST_BACKUP")${DEF}" + +# Confirm with user +echo -e "${Y}WARNING: This will overwrite existing data!${DEF}" +read -p "Are you sure you want to continue? (y/N): " -n 1 -r +echo +if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo -e "${R}Restore cancelled.${DEF}" + exit 0 +fi + +echo -e "${B}[1/6] Stopping services...${DEF}" +docker compose down || true + +echo -e "${B}[2/6] Extracting backup archive...${DEF}" +docker run --rm -v "$LATEST_BACKUP:/backup.tar.xz:ro" -v "$WORKDIR:/extract" alpine:3.20.1 ash -c "apk -q --no-progress --no-cache add xz && tar -xf /backup.tar.xz -C /extract" + +echo -e "${B}[3/6] Restoring chain data...${DEF}" +if [ -d "$WORKDIR/chain" ]; then + mkdir -p "${SCRIPT_DIR}/../data/chain" + rm -rf "${SCRIPT_DIR}/../data/chain" + cp -a "$WORKDIR/chain" "${SCRIPT_DIR}/../data/chain" + echo -e "${G}✓ Chain data restored${DEF}" +else + echo -e "${Y}⚠ No chain data found in backup${DEF}" + exit 1 +fi + +echo -e "${B}[4/6] Restoring keyper configuration...${DEF}" +if [ -d "$WORKDIR/keyper-config" ]; then + mkdir -p "${SCRIPT_DIR}/../config" + rm -rf "${SCRIPT_DIR}/../config" + cp -a "$WORKDIR/keyper-config" "${SCRIPT_DIR}/../config" + echo -e "${G}✓ Keyper configuration restored${DEF}" +else + echo -e "${Y}⚠ No keyper-config found in backup${DEF}" + exit 1 +fi + +echo -e "${B}[5/6] Restoring database dump...${DEF}" +if [ -f "$WORKDIR/keyper.dump" ]; then + mkdir -p "${SCRIPT_DIR}/../data/db-data" + cp "$WORKDIR/keyper.dump" "${SCRIPT_DIR}/../data/db-data/keyper.dump" + echo -e "${G}✓ Database dump restored${DEF}" +else + echo -e "${Y}⚠ No database dump found in backup${DEF}" + exit 1 +fi + +echo -e "${B}[6/6] Restoring environment variables...${DEF}" +if [ -f "$WORKDIR/metrics-config/settings.env" ]; then + # Read the restored settings and update .env file + source "$WORKDIR/metrics-config/settings.env" + + # Update the .env file with restored values + if [ -f "${SCRIPT_DIR}/../.env" ]; then + # Backup current .env + cp "${SCRIPT_DIR}/../.env" "${SCRIPT_DIR}/../.env.backup.$(date +%Y%m%d_%H%M%S)" + + # Update PUSHGATEWAY variables in .env + sed -i.bak "s/^PUSHGATEWAY_URL=.*/PUSHGATEWAY_URL=${PUSHGATEWAY_URL:-}/" "${SCRIPT_DIR}/../.env" + sed -i.bak "s/^PUSHGATEWAY_USERNAME=.*/PUSHGATEWAY_USERNAME=${PUSHGATEWAY_USERNAME:-}/" "${SCRIPT_DIR}/../.env" + sed -i.bak "s/^PUSHGATEWAY_PASSWORD=.*/PUSHGATEWAY_PASSWORD=${PUSHGATEWAY_PASSWORD:-}/" "${SCRIPT_DIR}/../.env" + + # Clean up backup files + rm -f "${SCRIPT_DIR}/../.env.bak" + + echo -e "${G}✓ Environment variables restored${DEF}" + else + echo -e "${Y}⚠ .env file not found, skipping environment restore${DEF}" + fi +else + echo -e "${Y}⚠ No metrics-config/settings.env found in backup${DEF}" +fi + +echo -e "${B}Cleaning up...${DEF}" +rm -rf "$WORKDIR" + +echo -e "${G}Restore completed successfully!${DEF}" +echo -e "${Y}Next steps:${DEF}" +echo -e "1. Review the restored configuration files" +echo -e "2. Start the services: ${B}docker compose up -d${DEF}" +echo -e "3. The database will be automatically restored on first startup" + +trap - EXIT From e91479d2d296dcfd744563157588518bfac445c0 Mon Sep 17 00:00:00 2001 From: blockchainluffy Date: Fri, 4 Jul 2025 17:40:41 +0530 Subject: [PATCH 2/5] fix: compatibility by not expecting dump dir --- scripts/backup.sh | 4 ++-- scripts/restore.sh | 26 ++++++++++++-------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/scripts/backup.sh b/scripts/backup.sh index f637840..4524d00 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -34,14 +34,14 @@ docker compose pause keyper || true docker compose pause chain || true echo -e "${B}[2/7] Creating database dump...${DEF}" -docker compose exec db pg_dump -U postgres -d keyper -Fc --create --clean -f /var/lib/postgresql/dump/keyper.dump +docker compose exec db pg_dump -U postgres -d keyper -Fc --create --clean -f /var/lib/postgresql/data/keyper.dump echo -e "${B}[3/7] Pausing database...${DEF}" docker compose pause db || true echo -e "${B}[4/7] Copying data...${DEF}" cp -a "${SCRIPT_DIR}/../data/chain/" "${WORKDIR}/chain" -cp -a "${SCRIPT_DIR}/../data/db-data/keyper.dump" "${WORKDIR}/keyper.dump" +cp -a "${SCRIPT_DIR}/../data/db/keyper.dump" "${WORKDIR}/keyper.dump" cp -a "${SCRIPT_DIR}/../config" "${WORKDIR}/keyper-config" mkdir -p "${WORKDIR}/metrics-config" diff --git a/scripts/restore.sh b/scripts/restore.sh index 6ab4167..4e9e95e 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -25,13 +25,11 @@ trap cleanup EXIT echo -e "${G}Restoring from latest backup${DEF}" -# Check if backups directory exists if [ ! -d "$BACKUPS_DIR" ]; then echo -e "${R}Error: Backups directory not found at $BACKUPS_DIR${DEF}" exit 1 fi -# Find the latest backup file LATEST_BACKUP=$(find "$BACKUPS_DIR" -name "shutter-api-keyper-*.tar.xz" -type f | sort | tail -n 1) if [ -z "$LATEST_BACKUP" ]; then @@ -41,7 +39,6 @@ fi echo -e "${B}Found latest backup: ${Y}$(basename "$LATEST_BACKUP")${DEF}" -# Confirm with user echo -e "${Y}WARNING: This will overwrite existing data!${DEF}" read -p "Are you sure you want to continue? (y/N): " -n 1 -r echo @@ -90,21 +87,22 @@ fi echo -e "${B}[6/6] Restoring environment variables...${DEF}" if [ -f "$WORKDIR/metrics-config/settings.env" ]; then - # Read the restored settings and update .env file - source "$WORKDIR/metrics-config/settings.env" + PUSHGATEWAY_URL=$(grep '^PUSHGATEWAY_URL=' "$WORKDIR/metrics-config/settings.env" | cut -d'=' -f2-) + PUSHGATEWAY_USERNAME=$(grep '^PUSHGATEWAY_USERNAME=' "$WORKDIR/metrics-config/settings.env" | cut -d'=' -f2-) + PUSHGATEWAY_PASSWORD=$(grep '^PUSHGATEWAY_PASSWORD=' "$WORKDIR/metrics-config/settings.env" | cut -d'=' -f2-) - # Update the .env file with restored values if [ -f "${SCRIPT_DIR}/../.env" ]; then - # Backup current .env cp "${SCRIPT_DIR}/../.env" "${SCRIPT_DIR}/../.env.backup.$(date +%Y%m%d_%H%M%S)" - # Update PUSHGATEWAY variables in .env - sed -i.bak "s/^PUSHGATEWAY_URL=.*/PUSHGATEWAY_URL=${PUSHGATEWAY_URL:-}/" "${SCRIPT_DIR}/../.env" - sed -i.bak "s/^PUSHGATEWAY_USERNAME=.*/PUSHGATEWAY_USERNAME=${PUSHGATEWAY_USERNAME:-}/" "${SCRIPT_DIR}/../.env" - sed -i.bak "s/^PUSHGATEWAY_PASSWORD=.*/PUSHGATEWAY_PASSWORD=${PUSHGATEWAY_PASSWORD:-}/" "${SCRIPT_DIR}/../.env" - - # Clean up backup files - rm -f "${SCRIPT_DIR}/../.env.bak" + awk -v url="$PUSHGATEWAY_URL" \ + -v username="$PUSHGATEWAY_USERNAME" \ + -v password="$PUSHGATEWAY_PASSWORD" \ + '{ + if ($0 ~ /^PUSHGATEWAY_URL=/) print "PUSHGATEWAY_URL=\"" url "\""; + else if ($0 ~ /^PUSHGATEWAY_USERNAME=/) print "PUSHGATEWAY_USERNAME=\"" username "\""; + else if ($0 ~ /^PUSHGATEWAY_PASSWORD=/) print "PUSHGATEWAY_PASSWORD=\"" password "\""; + else print $0; + }' "${SCRIPT_DIR}/../.env" > "${SCRIPT_DIR}/../.env.tmp" && mv "${SCRIPT_DIR}/../.env.tmp" "${SCRIPT_DIR}/../.env" echo -e "${G}✓ Environment variables restored${DEF}" else From 9c5855b9edb3ce5a1ac1af26436d275f224de73b Mon Sep 17 00:00:00 2001 From: blockchainluffy <47202792+blockchainluffy@users.noreply.github.com> Date: Thu, 10 Jul 2025 14:24:31 +0530 Subject: [PATCH 3/5] Update scripts/RESTORE.md Co-authored-by: Ulrich Petri --- scripts/RESTORE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/RESTORE.md b/scripts/RESTORE.md index cbcf810..f265949 100644 --- a/scripts/RESTORE.md +++ b/scripts/RESTORE.md @@ -23,7 +23,7 @@ ### Prerequisites -- **Empty keyper instance** - The restore should be performed on a fresh, empty deployment +- **Empty keyper instance** - The restore *must* be performed on a fresh, empty deployment - **No running services** - Ensure all Docker containers are stopped before restore - **Backup file available** - The backup archive should be present in `data/backups/` directory From 07bec371f986032cbea568cacef7e3d72f8a79e1 Mon Sep 17 00:00:00 2001 From: blockchainluffy <47202792+blockchainluffy@users.noreply.github.com> Date: Thu, 10 Jul 2025 14:31:31 +0530 Subject: [PATCH 4/5] Update docker-compose.yml Co-authored-by: Ulrich Petri --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index fd1bbe1..8ad0cf3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -47,7 +47,7 @@ services: volumes: - ./data/db:/var/lib/postgresql/data - ./_container_scripts/keyper-db-init.sh:/docker-entrypoint-initdb.d/keyper-db-init.sh:ro - - ./data/db-data:/var/lib/postgresql/dump + - ./data/db-dump:/var/lib/postgresql/dump healthcheck: test: pg_isready -U postgres start_period: "30s" From d300fd3e2312dd1210b96deaed80fe4162ef75f6 Mon Sep 17 00:00:00 2001 From: blockchainluffy Date: Thu, 10 Jul 2025 17:21:38 +0530 Subject: [PATCH 5/5] fix: delete dump file after restore and other PR comments --- _container_scripts/keyper-db-init.sh | 6 ++--- scripts/RESTORE.md | 22 +++++------------ scripts/backup.sh | 14 ++++++----- scripts/restore.sh | 36 +++++++++++++--------------- 4 files changed, 32 insertions(+), 46 deletions(-) diff --git a/_container_scripts/keyper-db-init.sh b/_container_scripts/keyper-db-init.sh index dca98d2..3096a93 100755 --- a/_container_scripts/keyper-db-init.sh +++ b/_container_scripts/keyper-db-init.sh @@ -5,10 +5,8 @@ set -e echo "Checking for backup dump file..." if [ -f "/var/lib/postgresql/dump/keyper.dump" ]; then echo "Backup dump found, restoring database with full schema and data..." - # Create the database first - createdb -U postgres keyper 2>/dev/null || echo "Database already exists" - # Restore the database with full schema and data - pg_restore -U postgres -d keyper --create --clean -v /var/lib/postgresql/dump/keyper.dump + pg_restore -U postgres -d postgres --create --clean -v /var/lib/postgresql/dump/keyper.dump + rm -f /var/lib/postgresql/dump/keyper.dump echo "Database restore completed." else echo "No backup dump file found, creating fresh database..." diff --git a/scripts/RESTORE.md b/scripts/RESTORE.md index f265949..45774d1 100644 --- a/scripts/RESTORE.md +++ b/scripts/RESTORE.md @@ -17,7 +17,7 @@ - Database dump (`keyper.dump`) - Contains full schema and data from the `keyper` database - Chain data (`data/chain/`) - Blockchain data and configuration - Keyper configuration (`config/`) - Application configuration files -- Environment variables - Metrics configuration settings +- Environment variables - Except Signing Key ## Restore Process @@ -35,13 +35,7 @@ # Edit .env with your configuration values ``` -2. **Extract backup** (if needed): - ```bash - # Backup files are automatically extracted during restore - # No manual extraction required - ``` - -3. **Run restore script**: +2. **Run restore script**: ```bash ./scripts/restore.sh ``` @@ -49,6 +43,9 @@ - Prompts for confirmation before proceeding - Restores all data to appropriate locations +3. **Set the Signing Key**: + - After restoring, update the `.env` file by setting the `SIGNING_KEY` environment variable to the same value used in your original deployment. + 4. **Start services**: ```bash docker compose up -d @@ -56,7 +53,7 @@ ### Restore Locations -- **Database**: `data/db-data/keyper.dump` - Automatically restored to PostgreSQL +- **Database**: `data/db-dump/keyper.dump` - Automatically restored to PostgreSQL - **Chain data**: `data/chain/` - Keyper chain data and configuration - **Configuration**: `config/` - Application configuration files - **Environment**: `.env` - Updated with restored metrics settings @@ -73,10 +70,3 @@ - **No backup found** - Ensure backup files exist in `data/backups/` directory - **Permission errors** - Ensure proper file permissions on backup files - **Configuration issues** - Verify that restored configuration files are valid - -### Verification - -After restore and startup: -1. Check database connectivity and table presence -2. Verify chain is propagating using chain container's logs -4. Test keyper, and see if it generates decryption keyshares \ No newline at end of file diff --git a/scripts/backup.sh b/scripts/backup.sh index 4524d00..b646ba5 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -44,12 +44,14 @@ cp -a "${SCRIPT_DIR}/../data/chain/" "${WORKDIR}/chain" cp -a "${SCRIPT_DIR}/../data/db/keyper.dump" "${WORKDIR}/keyper.dump" cp -a "${SCRIPT_DIR}/../config" "${WORKDIR}/keyper-config" -mkdir -p "${WORKDIR}/metrics-config" -cat > "${WORKDIR}/metrics-config/settings.env" < "${WORKDIR}/env-config/.env" + echo -e "${G}✓ Environment configuration backed up (private key replaced with placeholder)${DEF}" +else + echo -e "${Y}⚠ .env file not found, skipping environment backup${DEF}" +fi echo -e "${B}[5/7] Resuming services...${DEF}" docker compose unpause || true diff --git a/scripts/restore.sh b/scripts/restore.sh index 4e9e95e..bd8fd6a 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -77,39 +77,35 @@ fi echo -e "${B}[5/6] Restoring database dump...${DEF}" if [ -f "$WORKDIR/keyper.dump" ]; then - mkdir -p "${SCRIPT_DIR}/../data/db-data" - cp "$WORKDIR/keyper.dump" "${SCRIPT_DIR}/../data/db-data/keyper.dump" + mkdir -p "${SCRIPT_DIR}/../data/db-dump" + cp "$WORKDIR/keyper.dump" "${SCRIPT_DIR}/../data/db-dump/keyper.dump" echo -e "${G}✓ Database dump restored${DEF}" else echo -e "${Y}⚠ No database dump found in backup${DEF}" exit 1 fi -echo -e "${B}[6/6] Restoring environment variables...${DEF}" -if [ -f "$WORKDIR/metrics-config/settings.env" ]; then - PUSHGATEWAY_URL=$(grep '^PUSHGATEWAY_URL=' "$WORKDIR/metrics-config/settings.env" | cut -d'=' -f2-) - PUSHGATEWAY_USERNAME=$(grep '^PUSHGATEWAY_USERNAME=' "$WORKDIR/metrics-config/settings.env" | cut -d'=' -f2-) - PUSHGATEWAY_PASSWORD=$(grep '^PUSHGATEWAY_PASSWORD=' "$WORKDIR/metrics-config/settings.env" | cut -d'=' -f2-) - +echo -e "${B}[6/6] Restoring environment configuration...${DEF}" +if [ -f "$WORKDIR/env-config/.env" ]; then if [ -f "${SCRIPT_DIR}/../.env" ]; then cp "${SCRIPT_DIR}/../.env" "${SCRIPT_DIR}/../.env.backup.$(date +%Y%m%d_%H%M%S)" - awk -v url="$PUSHGATEWAY_URL" \ - -v username="$PUSHGATEWAY_USERNAME" \ - -v password="$PUSHGATEWAY_PASSWORD" \ - '{ - if ($0 ~ /^PUSHGATEWAY_URL=/) print "PUSHGATEWAY_URL=\"" url "\""; - else if ($0 ~ /^PUSHGATEWAY_USERNAME=/) print "PUSHGATEWAY_USERNAME=\"" username "\""; - else if ($0 ~ /^PUSHGATEWAY_PASSWORD=/) print "PUSHGATEWAY_PASSWORD=\"" password "\""; - else print $0; - }' "${SCRIPT_DIR}/../.env" > "${SCRIPT_DIR}/../.env.tmp" && mv "${SCRIPT_DIR}/../.env.tmp" "${SCRIPT_DIR}/../.env" + CURRENT_SIGNING_KEY=$(grep '^SIGNING_KEY=' "${SCRIPT_DIR}/../.env" 2>/dev/null || echo "") - echo -e "${G}✓ Environment variables restored${DEF}" + cp "$WORKDIR/env-config/.env" "${SCRIPT_DIR}/../.env" + + if [ -n "$CURRENT_SIGNING_KEY" ]; then + echo "$CURRENT_SIGNING_KEY" >> "${SCRIPT_DIR}/../.env" + fi + + echo -e "${G}✓ Environment configuration restored (private key preserved)${DEF}" else - echo -e "${Y}⚠ .env file not found, skipping environment restore${DEF}" + cp "$WORKDIR/env-config/.env" "${SCRIPT_DIR}/../.env" + echo -e "${G}✓ Environment configuration restored${DEF}" + echo -e "${Y}⚠ No existing SIGNING_KEY found, you'll need to set it manually${DEF}" fi else - echo -e "${Y}⚠ No metrics-config/settings.env found in backup${DEF}" + echo -e "${Y}⚠ No env-config/.env found in backup${DEF}" fi echo -e "${B}Cleaning up...${DEF}"