I have a small bash script that counts processed blocks from a blockchain. At time A I write the block height into the variable block1 and at time B I write the block height into the variable block2 In between I let the script sleep for 3600 seconds and then substract block1 from block2
Everything runs smooth so far and I store the data into a mysql database. But now my question:
How do I prevent a loss of data?
If I want to count all blocks in a day, with letting the script sleep 24 hours dont seems to be a good idea. When the script fails after 23 hours sleeping it waited for nothing. How can I get rid of this problem?
Script:
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/nebkas
block1=$( blockchain status |jq .sync_info.latest_block_height | sed -e 's/^"//' -e 's/"$//' )
sleep 3600
block2=$( blockchain status |jq .sync_info.latest_block_height | sed -e 's/^"//' -e 's/"$//' )
count=$(expr $block2 - $block1)
DB_USER='user';
DB_PASSWD='password';
DB_HOST='host';
DB_NAME='database';
TABLE='table';
mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME --host=$DB_HOST<< EOF
INSERT INTO $TABLE (\`blocks\`) VALUES ( "$count");
EOF
Write block1
to the database (perhaps with a timestamp) - if you do that every minute or 5 you can have a different script to pull the data you want out of the database.