Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- _options="c:n:h"
- _container=""
- _backup_name=""
- usage() {
- echo """
- Usage $0 [-c <db container] [-n <backup name>]
- -c - container with database
- -n - file name for backup
- All parameters are optional, default values are provided
- """ >&2
- }
- if test -t 1; then
- # see if it supports colors...
- ncolors=$(tput colors)
- if test -n "$ncolors" && test $ncolors -ge 8; then
- bold="$(tput bold)"
- underline="$(tput smul)"
- standout="$(tput smso)"
- normal="$(tput sgr0)"
- black="$(tput setaf 0)"
- red="$(tput setaf 1)"
- green="$(tput setaf 2)"
- yellow="$(tput setaf 3)"
- blue="$(tput setaf 4)"
- magenta="$(tput setaf 5)"
- cyan="$(tput setaf 6)"
- white="$(tput setaf 7)"
- fi
- fi
- while getopts $_options _option; do
- case $_option in
- c )
- _container=$OPTARG
- ;;
- n )
- _backup_name=$OPTARG
- ;;
- h )
- usage
- exit
- ;;
- \? )
- echo "Error. Unknown option -$OPTARG" >&2
- exit 1
- ;;
- : )
- echo "Error. Missing option argument for -$OPTARG" >&2
- exit 1
- ;;
- esac
- done
- echo "${normal}"
- if [ -z "$_container" ]; then
- _container="dpd-shivah-postgres"
- echo "Using container name ${yellow}${_container}${normal}"
- fi
- if [ -z "$_backup_name" ]; then
- _current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
- [ "$?" -ne "0" ] && echo "${red}Error determining current git branch${normal}" >&2 && exit 1
- if [ ! -z "$_current_branch" ]; then
- [ ! -f "./target" ] && mkdir -p ./target
- fi
- _backup_name="./target/bcp-$_current_branch.sql"
- echo "Restoring from ${yellow}${_backup_name}${normal}"
- fi
- if [ ! -f "$_backup_name" ]; then
- echo "${red}Backup does not exist${normal}" >&2
- exit 1
- fi
- docker exec $_container psql -U shivah postgres -c "drop database shivah" > /dev/null 2>&1
- [ "$?" -ne "0" ] && echo "${red}Error droping database${normal}" >&2 && exit 1
- docker exec $_container psql -U shivah postgres -c "create database shivah" > /dev/null 2>&1
- [ "$?" -ne "0" ] && echo "${red}Error creating database${normal}" >&2 && exit 1
- cat $_backup_name | docker exec --interactive $_container psql -U shivah > /dev/null 2>&1
- echo
- if [ "$?" -eq "0" ]; then
- echo "${green}Restore successful${normal}"
- else
- echo "${red}Error restoring data${normal}" >&2
- fi
Add Comment
Please, Sign In to add comment