snapshots/snapshot.sh

58 lines
2.9 KiB
Bash
Raw Normal View History

2019-11-07 20:06:10 +01:00
#!/bin/bash
# set -x
echo "Running $0"
2019-11-13 15:37:05 +01:00
hash aws 2>/dev/null || { echo >&2 '`aws` command not found. Install it: sudo apt install awscli'; exit 1; }
2019-12-13 16:10:30 +01:00
[ "$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials)" == "ec2-snapshotter" ] || { echo >&2 'This instance is missing the "ec2-snapshotter" IAM role'; exit 1; }
2019-11-13 15:37:05 +01:00
2019-11-13 14:53:19 +01:00
user=$(whoami)
2019-11-07 20:06:10 +01:00
echo "Starting servers"
sudo docker-compose up --detach
echo "Waiting until lbrycrdd has caught up to the blockchain tip"
HEIGHT=$(curl -s https://explorer.lbry.com/api/v1/status | egrep -o 'height":([0-9])+' | egrep -o '[0-9]+')
while true; do
#set +eo pipefail
2019-11-13 14:53:19 +01:00
info=$(sudo docker exec ${user}_lbrycrd_1 lbrycrd-cli -conf=/etc/lbry/lbrycrd.conf getblockchaininfo 2>/dev/null)
2019-11-07 20:06:10 +01:00
ret=$?
if [ "$ret" != 0 ]; then echo "Waiting for lbrycrd to start"; sleep 5; continue; fi
WALLET_BLOCKS=$(echo '{"id":1,"method":"blockchain.block.get_server_height"}' | nc localhost 50001 | egrep -m 1 -o 'result": [0-9]+' | egrep -o '[0-9]+')
if [ -z "$WALLET_BLOCKS" ]; then echo "Waiting for wallet server to bind port"; sleep 5; continue; fi
#set -eo pipefail
HEADERS=$(echo "$info" | grep headers | egrep -o '[0-9]+')
BLOCKS=$(echo "$info" | grep blocks | egrep -o '[0-9]+')
echo "$HEIGHT $HEADERS $BLOCKS $WALLET_BLOCKS"
if [[ "$HEADERS" -ge "$HEIGHT" && "$BLOCKS" -ge "$HEADERS" && "$WALLET_BLOCKS" -ge "$BLOCKS" ]]; then break; else sleep 1; fi
done
echo "final: $HEIGHT $HEADERS $BLOCKS $WALLET_BLOCKS"
2019-11-29 18:07:48 +01:00
LBRYCRD_VERSION=$(sudo docker exec ${user}_lbrycrd_1 lbrycrd-cli -conf=/etc/lbry/lbrycrd.conf getnetworkinfo 2>/dev/null | grep -m 1 subversion | egrep -o '[0-9\.]+')
2019-11-21 18:42:57 +01:00
WALLET_SERVER_VERSION=$(echo '{"id":1,"method":"server.version"}' | nc localhost 50001 | egrep -m 1 -o 'result[^,]+' | cut -d'"' -f3)
2019-11-07 20:06:10 +01:00
echo "Stopping servers"
sudo docker-compose down
# TODO: get volume data locations from `docker volume inspect` instead of hardcoding them below
2019-11-21 18:42:57 +01:00
BLOCKCHAIN_SNAPSHOT="$HOME/blockchain_snapshot_${WALLET_BLOCKS}_v${LBRYCRD_VERSION}_$(date +%F).tar.bz2"
2019-11-07 20:06:10 +01:00
echo "Making blockchain snapshot $BLOCKCHAIN_SNAPSHOT"
2019-11-13 14:53:19 +01:00
sudo tar -cjvf "$BLOCKCHAIN_SNAPSHOT" -C /var/lib/docker/volumes/${user}_lbrycrd/_data --group=$user --owner=$user blocks/ chainstate/ claimtrie/ indexes/
2019-11-07 20:06:10 +01:00
echo "Uploading blockchain snapshot to s3"
aws s3 cp "$BLOCKCHAIN_SNAPSHOT" s3://snapshots.lbry.com/blockchain/ --region us-east-2
2019-11-21 18:42:57 +01:00
WALLET_SNAPSHOT="$HOME/wallet_snapshot_${WALLET_BLOCKS}_v${WALLET_SERVER_VERSION}_$(date +%F).tar.bz2"
2019-11-07 20:06:10 +01:00
echo "Making wallet snapshot $SNAPSHOT"
2019-11-13 14:53:19 +01:00
sudo tar -cjvf "$WALLET_SNAPSHOT" -C /var/lib/docker/volumes/${user}_wallet_server/_data --group=$user --owner=$user claims.db hist/ meta/ utxo/
2019-11-07 20:06:10 +01:00
echo "Uploading wallet snapshot to s3"
aws s3 cp "$WALLET_SNAPSHOT" s3://snapshots.lbry.com/wallet/ --region us-east-2
rm -f "$BLOCKCHAIN_SNAPSHOT" "$WALLET_SNAPSHOT"
# shutdown instance (which will terminate it if shutdown behavior is set to terminate)
# sudo poweroff