-
Notifications
You must be signed in to change notification settings - Fork 13
/
_plinode_setup_bkup.sh
178 lines (133 loc) · 4.97 KB
/
_plinode_setup_bkup.sh
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
# Authenticate sudo perms before script execution to avoid timeouts or errors
sudo -l > /dev/null 2>&1
# Set Colour Vars
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Get current user id and store as var
USER_ID=$(getent passwd $EUID | cut -d: -f1)
GROUP_ID=$(getent group $EUID | cut -d: -f1)
if [ -e ~/"plinode_$(hostname -f)".vars ]; then
source ~/"plinode_$(hostname -f)".vars
fi
FUNC_DB_VARS(){
## VARIABLE / PARAMETER DEFINITIONS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PLI_DB_VARS_FILE="plinode_$(hostname -f)"_bkup.vars
if [ ! -e ~/$PLI_DB_VARS_FILE ]; then
#clear
echo
echo
echo -e "${GREEN} #### NOTICE: No backup VARIABLES file found.. ####${NC}"
echo
echo -e "${GREEN} ..creating local backup vars file '$HOME/$PLI_DB_VARS_FILE' ${NC}"
cp -n ~/plugin-deployment/sample_bkup.vars ~/$PLI_DB_VARS_FILE
chmod 600 ~/$PLI_DB_VARS_FILE
fi
source ~/$PLI_DB_VARS_FILE
}
FUNC_PKG_CHECK(){
BKUP_PACKAGES=(gpg shred gunzip)
#echo -e "${GREEN}#########################################################################"
#echo -e "${GREEN}## CHECK NECESSARY PACKAGES HAVE BEEN INSTALLED...${NC}"
for i in "${BKUP_PACKAGES[@]}"
do
hash $i &> /dev/null
if [ $? -eq 1 ]; then
#echo >&2 "package "$i" not found. installing...."
sudo apt install -y "$i" > /dev/null 2>&1
fi
#echo "packages "$i" exist. proceeding...."
done
}
FUNC_CHECK_DIRS(){
DB_BACKUP_DIR="plinode_backups"
if [ ! -d "/$DB_BACKUP_DIR" ]; then
echo -e "${NC} SETTING FOLDER PERMS ${NC}"
sudo mkdir "/$DB_BACKUP_DIR"
fi
# adds the variable value to the VARS file
#echo
#echo "checking vars - updating file "$PLI_DB_VARS_FILE" variable 'DB_BACKUP_DIR' to: "$DB_BACKUP_DIR""
sed -i 's/DB_BACKUP_DIR=\"\"/DB_BACKUP_DIR=\"'$DB_BACKUP_DIR'\"/g' ~/$PLI_DB_VARS_FILE
#echo "checking vars - creating directory: "$DB_BACKUP_DIR""
#echo "checking vars - assigning permissions for directory: "/$DB_BACKUP_DIR""
sudo chown $USER_ID\:$DB_BACKUP_GUSER -R "/$DB_BACKUP_DIR"
sudo chmod g+rw "/$DB_BACKUP_DIR"
# Updates the 'DB_BACKUP_PATH' & 'DB_BACKUP_OBJ' variable
#echo "checking vars - assigning 'DB_BACKUP_PATH' variable: "$DB_BACKUP_PATH""
DB_BACKUP_PATH="/$DB_BACKUP_DIR"
}
FUNC_DB_PRE_CHECKS(){
# check that necessary user / groups are in place
#check DB_BACKUP_FUSER values
if [ -z "$DB_BACKUP_FUSER" ]; then
export DB_BACKUP_FUSER="$USER_ID"
#echo
#echo "pre-check vars - Detected NULL for 'DB_BACKUP_FUSER' - we set the variable to: "$USER_ID""
# adds the variable value to the VARS file
#echo
#echo ".pre-check vars - updating file "$PLI_DB_VARS_FILE" variable 'DB_BACKUP_FUSER' to: $USER_ID"
sed -i 's/DB_BACKUP_FUSER=\"\"/DB_BACKUP_FUSER=\"'$USER_ID'\"/g' ~/$PLI_DB_VARS_FILE
fi
# check shared group '$DB_BACKUP_GUSER' exists & set permissions
#if [ -z "$DB_BACKUP_GUSER" ] && [ ! $(getent group nodebackup) ]; then
#echo
#echo "pre-check vars - variable 'DB_BACKUP_GUSER is: NULL && 'default' does not exist"
#echo "pre-check vars - creating group 'nodebackup'"
sudo groupadd nodebackup > /dev/null 2>&1
# adds the variable value to the VARS file
#echo
#echo "pre-check vars - updating file "$PLI_DB_VARS_FILE" variable DB_BACKUP_GUSER to: nodebackup"
sed -i 's/DB_BACKUP_GUSER=\"\"/DB_BACKUP_GUSER=\"nodebackup\"/g' ~/$PLI_DB_VARS_FILE
DB_BACKUP_GUSER="nodebackup"
#elif [ ! -z "$DB_BACKUP_GUSER" ] && [ ! $(getent group $DB_BACKUP_GUSER) ]; then
#echo
#echo "pre-check vars - variable 'DB_BACKUP_GUSER is: NOT NULL && does not exist"
#echo
#echo "pre-check vars - creating group "$DB_BACKUP_GUSER""
#sudo groupadd $DB_BACKUP_GUSER > /dev/null 2>&1
#echo "pre-check vars - updating file "$PLI_DB_VARS_FILE" variable DB_BACKUP_GUSER to: nodebackup"
#sed -i.bak 's/DB_BACKUP_GUSER=\"\"/DB_BACKUP_GUSER=\"$DB_BACKUP_GUSER\"/g' ~/$PLI_DB_VARS_FILE
#fi
# add users to the group
#echo
#echo "pre-check vars - checking if gdrive user exits"
if [ ! -z "$GD_FUSER" ]; then
#echo
#echo "pre-check vars - setting group members for backups - with gdrive"
DB_GUSER_MEMBER=(postgres $USER_ID $GD_FUSER)
else
GD_ENABLED=false
#echo
#echo "pre-check vars - setting group members for backups - without gdrive"
DB_GUSER_MEMBER=(postgres $USER_ID)
#echo "${DB_GUSER_MEMBER[@]}"
fi
#echo
#echo
#echo "pre-check vars - assiging user-group permissions.."
for _user in "${DB_GUSER_MEMBER[@]}"
do
hash $_user &> /dev/null
#echo "...adding user "$_user" to group "$DB_BACKUP_GUSER""
sudo usermod -aG "$DB_BACKUP_GUSER" "$_user" > /dev/null 2>&1
done
sleep 1s
}
error_exit()
{
if [ $? != 0 ]; then
#echo
echo "ERROR - Exiting early"
exit 1
else
return
fi
}
FUNC_PKG_CHECK
FUNC_DB_VARS
FUNC_DB_PRE_CHECKS
FUNC_CHECK_DIRS
echo "COMPELTED BACKUP SETUP SCRIPT"