-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_db.sh
More file actions
157 lines (140 loc) · 3.58 KB
/
load_db.sh
File metadata and controls
157 lines (140 loc) · 3.58 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# Script to automaticly load Postgres DB using RoR database.yml
# Error codes
readonly ENOENT=10 # No such file or directory
ME=`basename $0`
PREFIX="config_"
# Commands for formatted output
red=$(tput setf 4)
green=$(tput setf 2)
reset=$(tput sgr0)
toend=$(tput hpa $(tput cols))$(tput cub 6)
parse_opts() {
while getopts ":hb:r:" opt ;
do
case $opt in
b) set_branch $OPTARG;
set_variables $BRANCH
;;
r) FILE_PATH=$OPTARG;
;;
h) print_help;
exit 1
;;
*) echo "Wrong argument";
echo "To who help just type $ME -h";
exit 1
;;
esac
done
}
print_help() {
echo "PG dump DB"
echo
echo "Usage: $ME [arguments]"
echo "Arguments:"
echo -e " -b\t\tDefine environment, available dev/test/prod/stag. Default: prod"
echo -e " -r\t\tSet path to dump file. Default: /home/toor/dump.sql"
echo -e " -h\t\tShow this message."
echo
echo "Exit codes:"
echo " 0 - it\`s okay"
echo " 1 - minor errors"
echo " 10 - file not found"
}
defaults() {
FILE_PATH='/home/toor/dump.sql';
BRANCH=$PREFIX"production";
COPY=false;
set_variables $BRANCH
}
set_variables() {
# TODO: Seriously...do we need this low-level sh&t?
PASSWORD=$1"_password"
DATABASE=$1"_database"
USERNAME=$1"_username"
}
set_branch() {
case "$1" in
prod)
BRANCH=$PREFIX"production"
;;
dev)
BRANCH=$PREFIX"development"
;;
test)
BRANCH=$PREFIX"test"
;;
stag)
BRANCH=$PREFIX"staging"
;;
*)
echo $"Available branches: {prod|dev|test|stag}${red}${toend}[FAIL]"
exit 1
esac
}
check_config_file() {
if [ ! -f config/database.yml ]; then
echo -en "DB config file not found! Check config/database.yml${red}${toend}[FAIL]\n"
exit "$ENOENT" # Exits whole script with error code
fi
echo "Config file...${green}${toend}[OK]"
echo "${reset}"
}
check_dump_file() {
if [ ! -f $FILE_PATH ]; then
echo -en "Dump file not found! Check $FILE_PATH${red}${toend}[FAIL]\n"
exit "$ENOENT" # Exits whole script with error code
fi
echo "Dump file...${green}${toend}[OK]"
echo "${reset}"
}
check_parameters() {
if [ -z "${!DATABASE}" ]; then
parsing_error "Database name"
elif [ -z "${!USERNAME}" ]; then
parsing_error "Username"
elif [ -z "${!PASSWORD}" ]; then
parsing_error "Password"
fi
echo "Database params...${green}${toend}[OK]"
echo "${reset}"
}
parsing_error() {
echo "Invalid database.yml"
echo "$1 is invalid/missing${red}${toend}[FAIL]"
exit 1
}
# Credits to https://gist.github.com/pkuczynski/8665367
# Change to 4-spaces indent
# indent = length($1)/2; → indent = length($1)/7;
parse_yaml() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034'|tr -d '\015')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
load_db () {
check_config_file
check_dump_file
# TODO: change this to proper way
eval $(parse_yaml config/database.yml $PREFIX)
check_parameters
export PGPASSWORD="${!PASSWORD}"
psql -U ${!USERNAME} -h localhost ${!DATABASE} < $FILE_PATH
echo "Database loaded...${green}${toend}[OK]"
echo "${reset}"
}
# Function queue
defaults
parse_opts $@
load_db