This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
export-db.sh
executable file
·58 lines (45 loc) · 1.65 KB
/
export-db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
function exists() { command -v "$1" >/dev/null 2>&1 ; }
set -euo pipefail
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
timestamp=`date +%Y-%m-%d_%H-%M-%S`
bcnode_container_name=bcnode
database_volume_name=db
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare ${param}="true"
fi
shift
done
if ! exists awk || ! exists pv || ! exists gzip ; then
echo >&2 -e "${RED}Error: searching PATH fails to find executables among: awk pv gzip ${NC}"
exit 1
fi
existing_volume=$(docker volume ls -q -f name=${database_volume_name} | grep -w ${database_volume_name}) || true
if [ -z ${existing_volume} ]; then
echo >&2 -e "${RED}Error: No named Docker volume \"${database_volume_name}\" found!${NC}"
exit 1
fi
docker rm -f exportdb > /dev/null 2>&1 || true
# This way the export can run even if bcnode isn't running atm (which is probably safer anyway)
echo -e "${GREEN}Starting dummy container to access ${database_volume_name} volume...${NC}"
docker run -d --rm --name exportdb -v ${database_volume_name}:/root alpine tail -f /dev/null
tmp_dir=`mktemp -d`
echo -e "${GREEN}Extracting local blockchain database to ${tmp_dir}...${NC}"
docker cp exportdb:/root ${tmp_dir}/_data
echo -e "${GREEN}Compressing database, this will take a while...${NC}"
cwd=$(pwd)
cd ${tmp_dir}
rm ./_data/.chainstate.db
rm ./_data/db/IDENTITY
rm ./_data/db/LOCK
tar cf - ./ -P | pv -s $(du -sb ./ | awk '{print $1}') | gzip > ${cwd}/bcnode-db-${timestamp}.tar.gz
echo -e "${GREEN}Cleaning up...${NC}"
docker rm -f exportdb > /dev/null 2>&1
rm -rf ${tmp_dir}
echo -e "${GREEN}Done.${NC}"
echo "bcnode-db-${timestamp}.tar.gz"