Advertisement
gregorst

Shift Blockchain.db.gz creator

Sep 13th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.65 KB | None | 0 0
  1. #!/bin/bash
  2. VERSION="0.2"
  3.  
  4. export LC_ALL=en_US.UTF-8
  5. export LANG=en_US.UTF-8
  6. export LANGUAGE=en_US.UTF-8
  7.  
  8. #============================================================
  9. #= snapshot.sh v0.2 created by mrgr                         =
  10. #= Please consider voting for delegate mrgr        
  11. #= Modified by gregorst            =
  12. #============================================================
  13. echo " "
  14.  
  15. if [ ! -f ../shift/app.js ]; then
  16.   echo "Error: No shift installation detected. Exiting."
  17.   exit 1
  18. fi
  19.  
  20. if [ "\$USER" == "root" ]; then
  21.   echo "Error: SHIFT should not be run be as root. Exiting."
  22.   exit 1
  23. fi
  24.  
  25. SHIFT_CONFIG=~/shift/config.json
  26. DB_NAME="$(grep "database" $SHIFT_CONFIG | cut -f 4 -d '"')"
  27. DB_USER="$(grep "user" $SHIFT_CONFIG | cut -f 4 -d '"')"
  28. DB_PASS="$(grep "password" $SHIFT_CONFIG | cut -f 4 -d '"' | head -1)"
  29. SNAPSHOT_COUNTER=snapshot/counter.json
  30. SNAPSHOT_LOG=snapshot/snapshot.log
  31. if [ ! -f "snapshot/counter.json" ]; then
  32.   mkdir -p snapshot
  33.   sudo chmod a+x shift-snapshot.sh
  34.   echo "0" > $SNAPSHOT_COUNTER
  35.   sudo chown postgres:${USER:=$(/usr/bin/id -run)} snapshot
  36.   sudo chmod -R 777 snapshot
  37. fi
  38. SNAPSHOT_DIRECTORY=snapshot/
  39.  
  40.  
  41. NOW=$(date +"%d-%m-%Y - %T")
  42. ################################################################################
  43.  
  44. create_snapshot() {
  45.   export PGPASSWORD=$DB_PASS
  46.   echo " + Creating snapshot"
  47.   echo "--------------------------------------------------"
  48.   echo "..."
  49.   sudo su postgres -c "pg_dump -Fp $DB_NAME > $SNAPSHOT_DIRECTORY'blockchain.db'"
  50.   blockHeight=`psql -d $DB_NAME -U $DB_USER -h localhost -p 5432 -t -c "select height from blocks order by height desc limit 1;"`
  51.   dbSize=`psql -d $DB_NAME -U $DB_USER -h localhost -p 5432 -t -c "select pg_size_pretty(pg_database_size('$DB_NAME'));"`
  52.   gzip snapshot/blockchain.db
  53.  
  54.   if [ $? != 0 ]; then
  55.     echo "X Failed to create snapshot." | tee -a $SNAPSHOT_LOG
  56.     exit 1
  57.   else
  58.     echo "$NOW -- OK snapshot created successfully at block$blockHeight ($dbSize)." | tee -a $SNAPSHOT_LOG
  59.   fi
  60.  
  61. }
  62.  
  63. restore_snapshot(){
  64.   echo " + Restoring snapshot"
  65.   echo "--------------------------------------------------"
  66.   SNAPSHOT_FILE=`ls -t snapshot/shift_db* | head  -1`
  67.   if [ -z "$SNAPSHOT_FILE" ]; then
  68.     echo "****** No snapshot to restore, please consider create it first"
  69.     echo " "
  70.     exit 1
  71.   fi
  72.   echo "Snapshot to restore = $SNAPSHOT_FILE"
  73.  
  74.   read -p "Please stop node app.js first, are you ready (y/n)? " -n 1 -r
  75.   if [[ ! $REPLY =~ ^[Yy]$ ]]
  76.   then
  77.      echo "***** Please stop node.js first.. then execute restore again"
  78.      echo " "
  79.      exit 1
  80.   fi
  81.  
  82. #snapshot restoring..
  83.   export PGPASSWORD=$DB_PASS
  84.   pg_restore -d $DB_NAME "$SNAPSHOT_FILE" -U $DB_USER -h localhost -c -n public
  85.  
  86.   if [ $? != 0 ]; then
  87.     echo "X Failed to restore."
  88.     exit 1
  89.   else
  90.     echo "OK snapshot restored successfully."
  91.   fi
  92.  
  93. }
  94.  
  95. show_log(){
  96.   echo " + Snapshot Log"
  97.   echo "--------------------------------------------------"
  98.   cat snapshot/snapshot.log
  99.   echo "--------------------------------------------------END"
  100. }
  101.  
  102. ################################################################################
  103.  
  104. case $1 in
  105. "create")
  106.   create_snapshot
  107.   ;;
  108. "restore")
  109.   restore_snapshot
  110.   ;;
  111. "log")
  112.   show_log
  113.   ;;
  114. "hello")
  115.   echo "Hello my friend - $NOW"
  116.   ;;
  117. "help")
  118.   echo "Available commands are: "
  119.   echo "  create   - Create new snapshot"
  120.   echo "  restore  - Restore the last snapshot available in folder snapshot/"
  121.   echo "  log      - Display log"
  122.   ;;
  123. *)
  124.   echo "Error: Unrecognized command."
  125.   echo ""
  126.   echo "Available commands are: create, restore, log, help"
  127.   echo "Try: bash shift-snapshot.sh help"
  128.   ;;
  129. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement