Skip to content
mhatal edited this page Apr 3, 2024 · 5 revisions

How is the backup built

We use a dedicated server to run a stacks node along with the api server, and it take a snapshot every day using the following script.

#!/bin/bash
set -e
REPO=alexgo-io/stacks-node-mainnet
NODE_DIR=/srv/stacks-node-mainnet
RELEASE_DIR=/mnt/volume_sfo3_04/gh-release
rm -rf $RELEASE_DIR/*
curl -s http://127.0.0.1:3999/extended/v1/status | jq > $RELEASE_DIR/status.json
curl -s http://127.0.0.1:3999/v2/info | jq > $RELEASE_DIR/info.json
export BLOCK=$(cat $RELEASE_DIR/status.json | jq .chain_tip.block_height)
export API_VERSION=$(cat $RELEASE_DIR/status.json | jq -r .server_version)
export NODE_VERSION=$(cat $RELEASE_DIR/info.json | jq -r .server_version)
export PG_VERSION=$(docker exec stacks_postgres postgres -V)

if [ -z "$BLOCK" ] || [ -z "$API_VERSION" ] || [ -z "$NODE_VERSION" ] || [ -z "$PG_VERSION" ]; then
  echo "Failed to get block/version info, aborted."
  exit -1
fi
echo "Starting backup at block height $BLOCK, $API_VERSION, $NODE_VERSION, $PG_VERSION"
date

cd $NODE_DIR
./stop.sh
export ZSTD_NBTHREADS=4
export ZSTD_CLEVEL=9
bash -c "tar -I zstd -Ocvp postgresql | split -b 2000M -d - $RELEASE_DIR/postgresql.tar.zstd." &
bash -c "tar -I zstd -Ocvp stacks-node | split -b 2000M -d - $RELEASE_DIR/stacks-node.tar.zstd." &
wait
./start.sh

cd $RELEASE_DIR

echo "Block height: $BLOCK, $NODE_VERSION, $API_VERSION, $PG_VERSION" > NOTES.md
echo >> NOTES.md
echo "To restore from this backup, run the following scripts:" >> NOTES.md

echo >> NOTES.md
echo '```bash
bash -c '"'" >> NOTES.md
ls postgresql.tar.zstd.* | sort | while read FILE; do
echo "curl -L https://github.com/$REPO/releases/download/block-$BLOCK/$FILE && \\" >> NOTES.md
done
echo 'true'"'"' | tar -I zstd -xvp
```' >> NOTES.md

echo >> NOTES.md
echo '```bash
bash -c '"'" >> NOTES.md
ls stacks-node.tar.zstd.* | sort | while read FILE; do
echo "curl -L https://github.com/$REPO/releases/download/block-$BLOCK/$FILE && \\" >> NOTES.md
done
echo 'true'"'"' | tar -I zstd -xvp
```' >> NOTES.md

gh release -R $REPO create block-$BLOCK -t "Block $BLOCK Snapshot" --draft --notes-file NOTES.md
gh release -R $REPO upload block-$BLOCK *.json
gh release -R $REPO upload block-$BLOCK postgresql.tar.zstd.* &
gh release -R $REPO upload block-$BLOCK stacks-node.tar.zstd.* &
wait
gh release -R $REPO edit block-$BLOCK --draft=false --latest
Clone this wiki locally