-
Notifications
You must be signed in to change notification settings - Fork 13
/
_plinode_backup.sh
410 lines (306 loc) · 12.8 KB
/
_plinode_backup.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/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 sample_bkup.vars ~/$PLI_DB_VARS_FILE
chmod 600 ~/$PLI_DB_VARS_FILE
echo
fi
source ~/$PLI_DB_VARS_FILE
}
FUNC_CHECK_DIRS(){
# Checks if NOT NULL for the 'DB_BACKUP_DIR'variable
if [ ! -z "$DB_BACKUP_DIR" ] ; then
#echo "checking DIR vars - apply default directory value..."
DB_BACKUP_DIR="plinode_backups"
# Checks if directory exists & creates if not + sets perms
# following logic attempts to resolve the leading Root '/' path issue
if [ ! -d "/$DB_BACKUP_DIR" ]; then
#echo -e "${GREEN} SETTING FOLDER PERMS ${NC}"
#echo "checking DIR vars - check directory exists & setting perms..."
sudo mkdir "/$DB_BACKUP_DIR"
sudo chown $USER_ID\:$DB_BACKUP_GUSER -R "/$DB_BACKUP_DIR"
sudo chmod g+rw "/$DB_BACKUP_DIR";
fi
else
#echo
#echo "checking vars - Detected NULL - setting 'default'value.."
DB_BACKUP_DIR="plinode_backups"
#echo "checking vars - var 'DB_BACKUP_DIR' value is now: $DB_BACKUP_DIR"
# 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.bak 's/DB_BACKUP_DIR=\"\"/DB_BACKUP_DIR=\"'$DB_BACKUP_DIR'\"/g' ~/$PLI_DB_VARS_FILE
fi
# Checks if directory exists & creates if not + sets perms
if [ ! -d "/$DB_BACKUP_DIR" ]; then
#echo -e "${GREEN} SETTING FOLDER PERMS ${NC}"
#echo "checking DIR vars - check directory exists & setting perms..."
sudo mkdir "/$DB_BACKUP_DIR"
sudo chown $USER_ID\:$DB_BACKUP_GUSER -R "/$DB_BACKUP_DIR"
sudo chmod g+rw "/$DB_BACKUP_DIR";
fi
# Updates the 'DB_BACKUP_PATH' & 'DB_BACKUP_OBJ' variable
DB_BACKUP_PATH="/$DB_BACKUP_DIR"
sudo chown $USER_ID\:$DB_BACKUP_GUSER -R "/$DB_BACKUP_DIR"
sudo chmod g+rw "/$DB_BACKUP_DIR"
### Based on the above changes in values originally read form var file
### we then update the other vars to reflect these changes so the whole
### script execution reflects these updates
DB_BACKUP_OBJ="$DB_BACKUP_PATH/$DB_BACKUP_FNAME"
CONF_BACKUP_OBJ="$DB_BACKUP_PATH/$NODE_BACKUP_FNAME"
#echo
#echo "checking vars - exiting directory check & continuing..."
#sleep 2s
#echo
echo "checking vars - your configured node backup PATH is: $DB_BACKUP_PATH"
sleep 2s
}
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.bak '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.bak 's/DB_BACKUP_GUSER=\"\"/DB_BACKUP_GUSER=\"nodebackup\"/g' ~/$PLI_DB_VARS_FILE
#export DB_BACKUP_GUSER="nodebackup"
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
# 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)
#echo "${DB_GUSER_MEMBER[@]}"
#elif [ -z "$GD_FUSER" ] && [ ! $(getent passwd gdbackup) ]; then
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 "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 2s
}
FUNC_CONF_BACKUP_LOCAL(){
### Call the setup script to set permissions & check installed pkgs
bash _plinode_setup_bkup.sh > /dev/null 2>&1
FUNC_DB_VARS
FUNC_DB_PRE_CHECKS # order is specific as pre checks for user/groups which are assigned to dirs
FUNC_CHECK_DIRS
#echo
#echo "local backup - running tar backup process for configuration files"
tar -cvpzf $CONF_BACKUP_OBJ ~/plinode* > /dev/null 2>&1
#sleep 2s
FUNC_CONF_BACKUP_ENC;
if [ "$_OPTION" == "-full" ]; then
FUNC_DB_BACKUP_LOCAL;
#FUNC_SCP_CMD
fi
FUNC_EXIT;
}
FUNC_DB_BACKUP_LOCAL(){
if [ "$_OPTION" == "-db" ]; then
### Call the setup script to set permissions & check installed pkgs
bash _plinode_setup_bkup.sh
FUNC_DB_VARS
FUNC_DB_PRE_CHECKS
FUNC_CHECK_DIRS
fi
# checks if the '.pgpass' credentials file exists - if not creates in home folder & copies to dest folder
# & sets perms
#echo "local backup - checking pgpass file exists - create if necessary"
if [ ! -e ~/.pgpass ]; then
cat <<EOF > ~/.pgpass
Localhost:5432:$DB_NAME:postgres:$DB_PWD_NEW
EOF
fi
#echo "local backup - setting pgpass file perms"
cp -p ~/.pgpass $DB_BACKUP_PATH/.pgpass
sudo chown postgres:postgres $DB_BACKUP_PATH/.pgpass
sudo chmod 600 $DB_BACKUP_PATH/.pgpass
sleep 2s
echo "local backup - running pgdump db backup process"
# switch to 'postgres' user and run command to create inital sql dump file
sudo su postgres -c "export PGPASSFILE="$DB_BACKUP_PATH/.pgpass"; pg_dump -c -w -U postgres $DB_NAME | gzip > $DB_BACKUP_OBJ" > /dev/null 2>&1
#echo
echo "local backup - successfully created unencrypted compressed gz DB file: "$DB_BACKUP_OBJ""
sudo chown $DB_BACKUP_FUSER:$DB_BACKUP_GUSER $DB_BACKUP_OBJ
# Calls the file encryption
FUNC_DB_BACKUP_ENC;
# check menu selection & that remote backup software configured
# GD_ENABLED set in FUNC_DB_PRE_CHECKS
#echo "$GD_ENABLED"
if [ "$_OPTION" == "-full" ] && [ "$GD_ENABLED" == "true" ]; then
FUNC_DB_BACKUP_REMOTE
fi
#echo "removing pgpass from backup directory"
sudo rm -f $DB_BACKUP_PATH/.pgpass
sleep 2s
FUNC_EXIT;
}
FUNC_DB_BACKUP_ENC(){
# runs GnuPG or gpg to encrypt the sql dump file - uses main keystore password as secret
# outputs file to new folder ready for upload
if [ -e $DB_BACKUP_OBJ ]; then
sudo gpg --yes --batch --passphrase=$PASS_KEYSTORE -o $ENC_PATH/$ENC_FNAME -c $DB_BACKUP_OBJ
error_exit;
#echo
echo "local backup - successfully created encrypted gpg DB file: "$ENC_FNAME""
sudo chown $DB_BACKUP_FUSER:$DB_BACKUP_GUSER $ENC_PATH/$ENC_FNAME
#echo
echo "local backup - securely erased unencrypted compressed gz DB file: "$DB_BACKUP_OBJ""
shred -uz -n 1 $DB_BACKUP_OBJ
fi
}
FUNC_CONF_BACKUP_ENC(){
if [ -e $CONF_BACKUP_OBJ ]; then
sudo gpg --yes --batch --passphrase=$PASS_KEYSTORE -o $ENC_PATH/$ENC_CONFNAME -c $CONF_BACKUP_OBJ
error_exit;
#echo
echo "local backup - successfully created encrypted gpg conf file: "$ENC_CONFNAME""
sudo chown $DB_BACKUP_FUSER:$DB_BACKUP_GUSER $ENC_PATH/$ENC_CONFNAME
#echo
echo "local backup - securely erase unencrypted conf file: "$CONF_BACKUP_OBJ""
shred -uz -n 1 $CONF_BACKUP_OBJ
fi
}
FUNC_DB_BACKUP_REMOTE(){
if [ "$_OPTION" == "-remote" ]; then
FUNC_DB_VARS
fi
# add check that gupload is installed!
# switches to gupload user to run cmd to upload encrypted file to your google drive - skips existing files
# add check for user account & installation
sudo su gdbackup -c "cd ~/; .google-drive-upload/bin/gupload -q -d /$DB_BACKUP_PATH/*.gpg -C $(hostname -f) --hide"
error_exit;
}
FUNC_SCP_CMD(){
# Provide SCP commands to connect to the VPS and download backups
echo
echo
echo -e "${GREEN} The SCP commands to copy your Plugin node backup files is as follows:${NC}"
echo
keys_arr=()
CPORT=$(sudo ss -tlpn | grep sshd | awk '{print$4}' | cut -d ':' -f 2 -s)
if [ -s "$HOME/.ssh/authorized_keys" ] && [ $CPORT != "22" ]; then
echo -e "${GREEN} INFO :: ssh-keys & non-std ssh port detected"
echo
echo -e "${GREEN} NOTE :: The following command(s) are based on the # of keys detected on this system"
echo -e "${GREEN} NOTE :: the path to your private key file has been assumed - please update as needed"
echo
IFS=$'\n' read -r -d '' -a keys_arr < <( cat ~/.ssh/authorized_keys | awk '{print$4}')
keys_arr_len=${#keys_arr[@]}
for (( i = 0 ; i < $keys_arr_len ; i++))
do
echo -e "${RED} scp -i ~/.ssh/${keys_arr[$i]}.key -P $CPORT $USER@$(hostname -I | awk '{print $1}'):/plinode_backups/*.gpg ~/${NC}"
done
elif [ $CPORT != "22" ]; then
echo -e "${GREEN} INFO :: non-std ssh port detected: $CPORT${NC}"
echo
echo -e "${RED} scp -P $CPORT $USER@$(hostname -I | awk '{print $1}'):/plinode_backups/*.gpg ~/${NC}"
else
echo -e "${RED} scp $USER@$(hostname -I | awk '{print $1}'):/plinode_backups/*.gpg ~/${NC}"
fi
echo
echo -e "${GREEN}#########################################################################${NC}"
echo
}
FUNC_EXIT(){
FUNC_SCP_CMD;
exit 0
}
error_exit(){
if [ $? != 0 ]; then
#echo
echo "ERROR - Exiting early"
exit 1
else
return
fi
}
case "$1" in
-full)
_OPTION="-full"
FUNC_CONF_BACKUP_LOCAL
;;
-conf)
_OPTION="-conf"
FUNC_CONF_BACKUP_LOCAL
;;
-db)
_OPTION="-db"
FUNC_DB_BACKUP_LOCAL
;;
-scp)
FUNC_SCP_CMD
;;
*)
#clear
echo
echo
echo -e "${GREEN}Usage: $0 {function}${NC}"
echo
echo -e "${GREEN}where {function} is one of the following;${NC}"
echo
echo -e "${GREEN} -full == performs a local backup of both config & DB files only${NC}"
echo -e "${GREEN} -conf == performs a local backup of config files only${NC}"
echo -e "${GREEN} -db == performs a local backup of DB files only${NC}"
echo
echo -e "${GREEN} -scp == displays the secure copy (scp) cmds to download backup files${NC}"
#echo
echo
echo
esac