frenky666

restore-dev-db.sh

Nov 19th, 2021 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.36 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. _options="c:n:h"
  4.  
  5. _container=""
  6. _backup_name=""
  7.  
  8. usage() {
  9. echo """
  10. Usage $0 [-c <db container] [-n <backup name>]
  11.  -c - container with database
  12.  -n - file name for backup
  13.  
  14.  All parameters are optional, default values are provided
  15. """ >&2
  16. }
  17.  
  18. if test -t 1; then
  19.   # see if it supports colors...
  20.   ncolors=$(tput colors)
  21.  
  22.   if test -n "$ncolors" && test $ncolors -ge 8; then
  23.     bold="$(tput bold)"
  24.     underline="$(tput smul)"
  25.     standout="$(tput smso)"
  26.     normal="$(tput sgr0)"
  27.     black="$(tput setaf 0)"
  28.     red="$(tput setaf 1)"
  29.     green="$(tput setaf 2)"
  30.     yellow="$(tput setaf 3)"
  31.     blue="$(tput setaf 4)"
  32.     magenta="$(tput setaf 5)"
  33.     cyan="$(tput setaf 6)"
  34.     white="$(tput setaf 7)"
  35.   fi
  36.  
  37. fi
  38.  
  39. while getopts $_options _option; do
  40.   case $_option in
  41.     c )
  42.       _container=$OPTARG
  43.       ;;
  44.     n )
  45.       _backup_name=$OPTARG
  46.       ;;
  47.  
  48.     h )
  49.       usage
  50.       exit
  51.       ;;
  52.     \? )
  53.       echo "Error. Unknown option -$OPTARG" >&2
  54.       exit 1
  55.       ;;
  56.     : )
  57.       echo "Error. Missing option argument for -$OPTARG" >&2
  58.       exit 1
  59.       ;;
  60.   esac
  61. done
  62.  
  63. echo "${normal}"
  64.  
  65. if [ -z "$_container" ]; then
  66.   _container="dpd-shivah-postgres"
  67.   echo "Using container name ${yellow}${_container}${normal}"
  68. fi
  69.  
  70. if [ -z "$_backup_name" ]; then
  71.   _current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  72.   [ "$?" -ne "0" ] && echo "${red}Error determining current git branch${normal}" >&2 && exit 1
  73.  
  74.   if [ ! -z "$_current_branch" ]; then
  75.     [ ! -f "./target" ] && mkdir -p ./target
  76.   fi
  77.  
  78.   _backup_name="./target/bcp-$_current_branch.sql"
  79.  
  80.   echo "Restoring from ${yellow}${_backup_name}${normal}"
  81. fi
  82.  
  83. if [ ! -f "$_backup_name" ]; then
  84.   echo "${red}Backup does not exist${normal}" >&2
  85.   exit 1
  86. fi
  87.  
  88. docker exec $_container psql -U shivah postgres -c "drop database shivah" > /dev/null 2>&1
  89. [ "$?" -ne "0" ] && echo "${red}Error droping database${normal}" >&2 && exit 1
  90.  
  91. docker exec $_container psql -U shivah postgres -c "create database shivah" > /dev/null 2>&1
  92. [ "$?" -ne "0" ] && echo "${red}Error creating database${normal}" >&2 && exit 1
  93.  
  94. cat $_backup_name | docker exec --interactive $_container psql -U shivah > /dev/null 2>&1
  95.  
  96. echo
  97.  
  98. if [ "$?" -eq "0" ]; then
  99.   echo "${green}Restore successful${normal}"
  100. else
  101.   echo "${red}Error restoring data${normal}" >&2
  102. fi
Add Comment
Please, Sign In to add comment