Skip to content

Conversation

@blockchainluffy
Copy link
Contributor

closes #29

Copy link
Contributor

@ulope ulope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall but some changes needed.

- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also includes the private key, should be mentioned here.

Copy link
Contributor Author

@blockchainluffy blockchainluffy Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now copying whole env file except private key


echo -e "${B}[4/7] Copying data...${DEF}"
cp -a "${SCRIPT_DIR}/../data/chain/" "${WORKDIR}/chain"
cp -a "${SCRIPT_DIR}/../data/db/keyper.dump" "${WORKDIR}/keyper.dump"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICS the source path is wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the backup is created in the db folder only as previous docker setup, would not have new volume mapped in the compose (db-dump). The idea is to store the dump in the same dir where postgres data is stored and then copy it to db-dump at the time of restore. LMK, if needs more clarification

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean in case an operator updates to the new version but doesn't run compose up before using the backup script?

Yeah I guess that's fine.

@blockchainluffy blockchainluffy requested a review from ulope July 10, 2025 11:52
Copy link

@jannikluhn jannikluhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments, but looks good in general. My biggest question/concern is about pausing vs stopping.

mkdir -p "${SCRIPT_DIR}/../data/backups"

echo -e "${B}[1/7] Pausing all services except database...${DEF}"
docker compose pause keyper || true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this swallows errors, shouldn't we abort in that case? Same for the other pause commands below and for the down command in the recovery script.


echo -e "${B}[3/6] Restoring chain data...${DEF}"
if [ -d "$WORKDIR/chain" ]; then
mkdir -p "${SCRIPT_DIR}/../data/chain"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line seems unnecessary, given that the directory is removed immediately in the next line (that rm -rf doesn't care if the directory exists or not). Similarly below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just to make sure the rm -rf does not fail but I have handled that now

mkdir -p "${SCRIPT_DIR}/../data/backups"

echo -e "${B}[1/7] Pausing all services except database...${DEF}"
docker compose pause keyper || true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered stopping the containers instead of pausing them? This would leave them in a more well defined state (e.g. not in the middle of a db transaction or an RPC call).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great suggestion, I tried with compose down and it works, changed in the scripts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I initially went with pause in the dappnode export scripts, which this inherited from, was that it should cause minimal service interruption and for postgres it's important to not have any writes while doing a file level backup.

Arguably the length of service downtime won't be much different, so yes, stopping is probably more safe.

Although I would strongly argue to use stop instead of down since down kills the logs.

Copy link
Contributor

@ulope ulope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more small issue, after that it should be GTG.


echo -e "${B}[4/7] Copying data...${DEF}"
cp -a "${SCRIPT_DIR}/../data/chain/" "${WORKDIR}/chain"
cp -a "${SCRIPT_DIR}/../data/db/keyper.dump" "${WORKDIR}/keyper.dump"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean in case an operator updates to the new version but doesn't run compose up before using the backup script?

Yeah I guess that's fine.

exit 1
fi

LATEST_BACKUP=$(find "$BACKUPS_DIR" -name "shutter-api-keyper-*.tar.xz" -type f | sort | tail -n 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary to do anything here now, but a note for future improvement:

Some people like to run their containers on really stripped down OSs, therefore it would be more robust to use an ad-hoc container to do these shell actions (i.e. not relying on find, sort and tail to be present on the host). Similar to how it's done below with the tar command.

Comment on lines 133 to 135
if [ -n "$CURRENT_SIGNING_KEY" ]; then
echo "$CURRENT_SIGNING_KEY" >> "${SCRIPT_DIR}/../.env"
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test that this works?
Unless I'm missing something this will lead to there being two SIGNING_KEY lines in the file, one at the top with the placeholder and the actual one at the bottom.
This will at least be really confusing to the user if it works at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, nice catch, fixed it to just have a placeholder for signing key after restore.

Co-authored-by: Ulrich Petri <[email protected]>
@ylembachar ylembachar linked an issue Aug 6, 2025 that may be closed by this pull request
- **Backup file available** - The backup archive should be present in the specified backup directory
- **Signing key available** - You must have the original `SIGNING_KEY` value from your deployment

### Restore Steps

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a fresh install, the data dir doesn't exist, so users would need to create it manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added command to create data directory, if it does not exist already in restore.sh

@blockchainluffy blockchainluffy force-pushed the feat/api-keyper-backup-scripts branch from ba5ce7e to 18df3c5 Compare August 8, 2025 05:08
docker compose stop chain

echo -e "${B}[2/6] Creating database dump...${DEF}"
docker compose exec db pg_dump -U postgres -d keyper -Fc --create --clean -f /var/lib/postgresql/data/keyper.dump

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to delete this dump from the container during the clean and restart exit, to avoid growing the volume with every backup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the dump file gets overwritten on every backup, anyway I can add command to delete the dump file when backup archive is created

@@ -0,0 +1,114 @@
# Backup and Restore Guide

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename this file to BACKUP_RESTORE.md so that it is clear that it contains the instructions for both? Thank you.

@blockchainluffy blockchainluffy force-pushed the feat/api-keyper-backup-scripts branch from 708776f to 3f16a25 Compare August 8, 2025 11:11
@blockchainluffy blockchainluffy merged commit 22f4868 into shutter-api Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement keyper backup script

5 participants