-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-services-snapshot-schema-drop.sh
2315 lines (2311 loc) · 93.2 KB
/
aws-services-snapshot-schema-drop.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
#
#
##########################################
#
# begin bash shell script
#
##########################################
#
###############################################################################
#
# >>>> begin documentation <<<<
#
###############################################################################
#
#
##########################################################################################################
#
# Structure:
# A) documentation (this section)
# B) initialize (initializes variables and tables)
# C) functionDefinition (defines functions)
# D) setup (loads files, tables, and variables )
# E) main (creates program output)
#
# For quick access, search for 'begin' or 'end' and the section name
# Example: 'begin setup'
#
##########################################################################################################
#
# ------------------------------------------------------------------------------------
#
# MIT License
#
# Copyright (c) 2018 Enterprise Group, Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# ------------------------------------------------------------------------------------
#
# File: aws-services-snapshot-schema-drop.sh
# Source: https://github.com/Enterprise-Group-Ltd/aws-services-snapshot
#
script_version=0.0.7
#
# Dependencies:
# - postgresql instance running on EC2; setup instructions here:
# - bash shell
#
#
#
# Tested on:
# Windows Subsystem for Linux (WSL)
# Windows 10 Enterprise: 1709
# Build: 16299.248
# OS: Ubuntu 16.04 xenial
# Kernel: x86_64 Linux 4.4.0-43-Microsoft
# Shell: bash 4.3.48
# jq 1.5-1-a5b5cbe
# aws-cli/1.14.36 Python/2.7.12 Linux/4.4.0-43-Microsoft botocore/1.8.40
#
# AWS EC2
# OS: Amazon Linux
# AMI: amzn-ami-hvm-2017.09.1.20180115-x86_64-gp2 (ami-97785bed)
# Kernel: x86_64 Linux 4.9.77-31.58.amzn1.x86_64
# Shell: bash 4.2.46
# jq: 1.5
# aws-cli/1.14.32 Python/2.7.13 Linux/4.9.77-31.58.amzn1.x86_64 botocore/1.8.36
#
#
# By: Douglas Hackney
# https://github.com/dhackney
#
#
# Type: PostreSQL utility
#
# Description:
# This utility executes a series of shell and psql commands that drop PostgreSQL schemas created
# by the AWS Services Snapshot utility
#
#
# Roadmap:
# - none at this time
#
#
#
###############################################################################
#
# >>>> end documentation <<<<
#
###############################################################################
#
###############################################################################
#
# >>>> --- begin: initialize <<<<
#
###############################################################################
#
# set the environmental variables
#
# set to catch errors in a pipeling
set -o pipefail
#
# set to suppress NOTICE console output from psql
PGOPTIONS='--client-min-messages=warning'
export PGOPTIONS
#
###############################################################################
#
#
# initialize the script variables
#
#
count_schema=0
counter_schema=0
db_host=""
db_name=""
db_port=""
db_schema=""
db_user=""
feed_write_log=""
filter_aws_account=""
filter_cloud=""
filter_date_max=""
filter_date_max_input=""
filter_day=""
filter_hhmmss=""
filter_month=""
filter_timestamp=""
filter_util=""
filter_year=""
query_schema_drop=""
query_schema_list=""
query_schema_list_filtered=""
query_schema_list_line_filtered=""
schemas_dropped=""
verbose=""
#
###############################################################################
#
#
# initialize the baseline variables
#
this_utility_acronym="spsds"
this_utility_filename_plug="snapshot-drop-schema"
date_file="$(date +"%Y-%m-%d-%H%M%S")"
date_file_underscore="$(date +"%Y_%m_%d_%H%M%S")"
this_path="$(pwd)"
this_file="$(basename "$0")"
full_path="${this_path}"/"$this_file"
this_log_temp_file_full_path="$this_path"/"$this_utility_filename_plug"-log-temp.log
this_user="$(whoami)"
count_this_file_tasks="$(cat "$full_path" | grep -c "\-\-\- begin\: " )"
count_this_file_tasks_end="$(cat "$full_path" | grep -c "\-\-\- end\: " )"
count_this_file_tasks_increment="$(cat "$full_path" | grep -c "fnCounterIncrementTask" )"
count_this_file_tasks_increment=$((count_this_file_tasks_increment-3))
counter_this_file_tasks=0
logging="x"
counter_schema=0
#
db_host="localhost"
db_name="aws_snapshot"
db_port="5432"
db_type="postresql"
db_user="ec2-user"
#
write_path="$this_path"/aws-"$this_utility_filename_plug"-"$date_file"
write_path_schemas="$write_path"/"$this_utility_filename_plug"-files
this_path_temp="$write_path"/"$this_utility_acronym"-temp-"$date_file"
this_file_account_region_services_all="$write_path_schemas"/"aws-""$this_utility_filename_plug"-"$date_file"-all-services.json
this_file_account_services_all="$write_path_schemas"/"aws-"-"$this_utility_filename_plug"-"$date_file"-all-services.json
this_log_file="aws-""$this_utility_filename_plug"-"$date_file"-"$log_suffix".log
this_log_file_errors=aws-"$this_utility_filename_plug"-"$date_file"-errors.log
this_log_file_full_path="$write_path"/"$this_log_file"
this_log_file_errors_full_path="$write_path"/"$this_log_file_errors"
this_summary_report="aws-""$this_utility_filename_plug"-"$date_file"-summary-report.txt
this_summary_report_full_path="$write_path"/"$this_summary_report"
#
#
###############################################################################
#
# >>>> --- end: initialize <<<<
#
###############################################################################
#
#
###############################################################################
#
# >>>> --- begin: functionDefinition <<<<
#
###############################################################################
#
#######################################################################
#
#
# function to display the Usage
#
#
function fnUsage()
{
echo ""
echo " ---------------------------------- AWS Service Snapshot Schema Drop utility usage -----------------------------------"
echo ""
echo " This utility drops PostgreSQL schemas older than a specific date timestamp "
echo ""
echo " This script will: "
echo " * Drop PostgreSQL schemas from the database: 'aws_snapshot' "
echo ""
echo "----------------------------------------------------------------------------------------------------------------------"
echo ""
echo " Usage:"
echo " aws-services-snapshot-schema-drop.sh -t timestamp "
echo ""
echo " Optional parameters: -b y -g y "
echo ""
echo " Where: "
echo " -t - The date timestamp. All schemas older than this date timestamp will be dropped"
echo " Example: -t 2018-03-28-182417 "
echo ""
echo " -b - Verbose console output. Set to 'y' for verbose console output. Temp files are not deleted. "
echo " Example: -b y "
echo ""
echo " -g - Logging on / off. Default is off. Set to 'y' to create an info log. Set to 'z' to create a debug log. "
echo " Note: logging mode is slower and debug log mode will be very slow and resource intensive on large jobs. "
echo " Example: -g y "
echo ""
echo " -h - Display this message"
echo " Example: -h "
echo ""
echo " ---version - Display the script version"
echo " Example: --version "
echo ""
echo ""
exit 1
}
#
#######################################################################
#
#
# function to echo the progress bar to the console
#
# source: https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script
#
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function fnProgressBar()
{
#
# Process data
let _progress=(${1}*100/"${2}"*100)/100
let _done=(${_progress}*4)/10
let _left=40-"$_done"
# Build progressbar string lengths
_fill="$(printf "%${_done}s")"
_empty="$(printf "%${_left}s")"
#
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r Overall Progress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
}
#
#######################################################################
#
#
# function to update the task progress bar
#
# source: https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script
#
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function fnProgressBarTask()
{
#
# Process data
let _progress_task=(${1}*100/"${2}"*100)/100
let _done_task=(${_progress_task}*4)/10
let _left_task=40-"$_done_task"
# Build progressbar string lengths
_fill_task="$(printf "%${_done_task}s")"
_empty_task="$(printf "%${_left_task}s")"
#
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r Schema Progress : [${_fill_task// /#}${_empty_task// /-}] ${_progress_task}%%"
}
#
#######################################################################
#
#
# function to display the subtask text
#
function fnTaskSubText()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "value of variable: 'counter_schema': "$counter_schema" "
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "value of variable: 'count_schema': "$count_schema" "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "This job takes a while. Please wait..."
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "Dropping schema: "$query_schema_list_filtered_date_line" "
fnWriteLog ${LINENO} level_0""
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} ""
}
#
#######################################################################
#
#
# function to display the task progress bar on the console
#
# parameter 1 = counter
# paramter 2 = count
#
function fnProgressBarTaskDisplay()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnProgressBarTaskDisplay' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 " ---------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnProgressBarTask "$1" "$2"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " ---------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
}
#
#######################################################################
#
#
# function to echo the header to the console
#
function fnHeader()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnHeader' "
fnWriteLog ${LINENO} ""
#
clear
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} "--------------------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} "--------------------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header"
fnWriteLog ${LINENO} level_0 ""
fnProgressBar ${counter_this_file_tasks} ${count_this_file_tasks}
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
}
#
#######################################################################
#
#
# function to echo to the console and write to the log file
#
function fnWriteLog()
{
# clear IFS parser
IFS=
# write the output to the console
fnOutputConsole "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
# if logging is enabled, then write to the log
if [[ ("$logging" = "y") || ("$logging" = "z") || ("$logging" = "x") ]]
then
# write the output to the log
fnOutputLog "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
fi
# reset IFS parser to default values
unset IFS
}
#
#######################################################################
#
#
# function to echo to the console
#
function fnOutputConsole()
{
#
# console output section
#
# test for verbose
if [ "$verbose" = "y" ] ;
then
# if verbose console output then
# echo everything to the console
#
# strip the leading 'level_0'
if [ "$2" = "level_0" ] ;
then
# if the line is tagged for display in non-verbose mode
# then echo the line to the console without the leading 'level_0'
echo " Line: "$1" "$3" "$4" "$5" "$6" "$7" "$8" "$9""
else
# if a normal line echo all to the console
echo " Line: "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9""
fi
else
# test for minimum console output
if [ "$2" = "level_0" ] ;
then
# echo ""
# echo "console output no -v: the logic test for level_0 was true"
# echo ""
# if the line is tagged for display in non-verbose mode
# then echo the line to the console without the leading 'level_0'
echo " "$3" "$4" "$5" "$6" "$7" "$8" "$9""
fi
fi
#
#
}
#
#######################################################################
#
#
# function to write to the log file
#
function fnOutputLog()
{
# log output section
#
# load the timestamp
thislogdate="$(date +"%Y-%m-%d-%H:%M:%S")"
#
# ----------------------------------------------------------
#
# normal logging
#
# append the line to the log variable
# the variable is written to the log file on exit by function fnWriteLogFile
#
#
if [ "$2" = "level_0" ] ;
then
# if the line is tagged for logging in non-verbose mode
# then write the line to the log without the leading 'level_0'
this_log+="$(echo "${thislogdate} Line: "$1" "$3" "$4" "$5" "$6" "$7" "$8" "$9"" 2>&1)"
else
# if a normal line write the entire set to the log
this_log+="$(echo "${thislogdate} Line: "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"" 2>&1)"
fi
#
# append the new line
# do not quote this variable
this_log+=$'\n'
#
#
# ---------------------------------------------------------
#
# 'use this for debugging' - debug logging
#
# if the script is crashing and you cannot debug it from the 'info' mode log produced by -g y,
# then enable 'verbose' console output and 'debug' logging mode
#
# note that the 'debug' form of logging is VERY slow on big jobs
#
# use parameters: -b y -g z
#
# if the script crashes before writing out the log you can scroll back in the console to
# identify the line number where the problem is located
#
#
}
#
#######################################################################
#
#
# function to append the log variable to the temp log file
#
function fnWriteLogTempFile()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnWriteLogTempFile' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "Appending the log variable to the temp log file"
fnWriteLog ${LINENO} ""
echo "$this_log" >> "$this_log_temp_file_full_path"
# empty the temp log variable
this_log=""
}
#
#######################################################################
#
#
# function to write log variable to the log file
#
function fnWriteLogFile()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnWriteLogFile' "
fnWriteLog ${LINENO} ""
#
# append the temp log file onto the log file
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} "Writing temp log to log file"
fnWriteLog ${LINENO} "Value of variable 'this_log_temp_file_full_path': "
fnWriteLog ${LINENO} "$this_log_temp_file_full_path"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "Value of variable 'this_log_file_full_path': "
fnWriteLog ${LINENO} "$this_log_file_full_path"
fnWriteLog ${LINENO} level_0 ""
# write the contents of the variable to the temp log file
fnWriteLogTempFile
cat "$this_log_temp_file_full_path" >> "$this_log_file_full_path"
echo "" >> "$this_log_file_full_path"
echo "Log end" >> "$this_log_file_full_path"
# delete the temp log file
rm -f "$this_log_temp_file_full_path"
}
#
##########################################################################
#
#
# function to delete the work files
#
function fnDeleteWorkFiles()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnDeleteWorkFiles' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in delete work files "
fnWriteLog ${LINENO} "value of variable 'verbose': "$verbose" "
fnWriteLog ${LINENO} ""
if [ "$verbose" != "y" ] ;
then
# if not verbose console output then delete the work files
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "In non-verbose mode: Deleting work files"
fnWriteLog ${LINENO} ""
feed_write_log="$(rm -f "$this_path_temp"/"$this_utility_acronym"-* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
feed_write_log="$(rm -f "$this_path_temp"/"$this_utility_acronym"_* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "value of variable 'this_log_file_full_path' "$this_log_file_full_path" "
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
feed_write_log="$(rm -f "$write_path_schemas"/"$this_utility_acronym"* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
feed_write_log="$(rm -f "$write_path_schemas"/"$this_utility_acronym"* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
feed_write_log="$(rm -r -f "$this_path_temp" 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
#
# if no errors, then delete the error log file
count_error_lines="$(cat "$this_log_file_errors_full_path" | wc -l)"
if (( "$count_error_lines" < 3 ))
then
rm -f "$this_log_file_errors_full_path"
fi
else
# in verbose mode so preserve the work files
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "In verbose mode: Preserving work files "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "work files are here: "$this_path" "
fnWriteLog ${LINENO} level_0 ""
fi
}
#
##########################################################################
#
#
# function to handle command or pipeline errors
#
function fnErrorPipeline()
{
# #
##########################################################################
#
#
# begin function 'fnErrorPipeline'
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " begin function 'fnErrorPipeline' "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnErrorPipeline' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Command or Command Pipeline Error "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " System Error while running the previous command or pipeline "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the error message above "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error at script line number: "$error_line_pipeline" "
fnWriteLog ${LINENO} level_0 ""
if [[ ("$logging" = "y") || ("$logging" = "z") ]]
then
fnWriteLog ${LINENO} level_0 " The log will also show the error message and other environment, variable and diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$this_log_file_full_path" "
fi
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# append the temp log onto the log file
fnWriteLogTempFile
# write the log variable to the log file
fnWriteLogFile
exit 1
}
#
##########################################################################
#
#
# function for psql errors
#
function fnErrorPsql()
{
# #
##########################################################################
#
#
# begin function 'fnErrorPsql'
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " begin function 'fnErrorPsql' "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnErrorPsql' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error at script line number: "$error_line_psql" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " There was a psql error while querying the database "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the psql error message above "
fnWriteLog ${LINENO} level_0 ""
if [[ ("$logging" = "y") || ("$logging" = "z") ]]
then
fnWriteLog ${LINENO} level_0 " The log will also show the AWS error message and other diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$write_path"/ "
fnWriteLog ${LINENO} level_0 " "$this_log_file" "
fi
fnWriteLog ${LINENO} level_0 " The log will also show the psql error message and other diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$this_log_file_full_path" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "--------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# append the temp log onto the log file
fnWriteLogTempFile
# write the log variable to the log file
fnWriteLogFile
exit 1
}
#
##########################################################################
#
#
# function to increment the schema counter
#
function fnCounterIncrementSchema()
{
# #
##########################################################################
#
#
# begin function 'fnCounterIncrementSchema'
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " begin function 'fnCounterIncrementSchema' "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnCounterIncrementSchema' "
fnWriteLog ${LINENO} ""
#
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "increment the schema counter: 'counter_schema'"
counter_schema="$((counter_schema+1))"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "post-increment value of variable 'counter_schema': "$counter_schema" "
fnWriteLog ${LINENO} ""
#
# #
##########################################################################
#
#
# end function 'fnCounterIncrementSchema'
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " end function 'fnCounterIncrementSchema' "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
}
#
##########################################################################
#
#
# function to increment the task counter
#
function fnCounterIncrementTask()
{
# #
##########################################################################
#
#
# begin function 'fnCounterIncrementTask'
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " begin function 'fnCounterIncrementTask' "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnCounterIncrementTask' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "incrementing the task counter"
counter_this_file_tasks="$((counter_this_file_tasks+1))"
fnWriteLog ${LINENO} "value of variable 'counter_this_file_tasks': "$counter_this_file_tasks" "
fnWriteLog ${LINENO} "value of variable 'count_this_file_tasks': "$count_this_file_tasks" "
fnWriteLog ${LINENO} ""
# #
##########################################################################
#
#
# end function 'fnCounterIncrementTask'
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " end function 'fnCounterIncrementTask' "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
}
#
# run once for initialization
fnWriteLog ${LINENO} "increment the task counter"
fnCounterIncrementTask
#
# run again for functionDefinition
fnWriteLog ${LINENO} "increment the task counter"
fnCounterIncrementTask
#
#
###############################################################################
#
# >>>> --- end: functionDefinition <<<<
#
###############################################################################
#
###############################################################################
#
# >>>> --- begin: setup <<<<
#
###############################################################################
#
#
###########################################################################################################################
#
#
# enable logging to capture initial segments
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " enable logging to capture initial segments "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
logging="x"
#
###########################################################################################################################
#
#
# build the menu and header text line and bars
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " build the menu and header text line and bars "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
text_header='AWS Services Snapshot Schema Drop Utility v'
count_script_version_length=${#script_version}
count_text_header_length=${#text_header}
count_text_block_length=$(( count_script_version_length + count_text_header_length ))
count_text_width_menu=104
count_text_width_header=83
count_text_side_length_menu=$(( (count_text_width_menu - count_text_block_length) / 2 ))
count_text_side_length_header=$(( (count_text_width_header - count_text_block_length) / 2 ))
count_text_bar_menu=$(( (count_text_side_length_menu * 2) + count_text_block_length + 2 ))
count_text_bar_header=$(( (count_text_side_length_header * 2) + count_text_block_length + 2 ))
# source and explanation for the following use of printf is here: https://stackoverflow.com/questions/5799303/print-a-character-repeatedly-in-bash
text_bar_menu_build="$(printf '%0.s-' $(seq 1 "$count_text_bar_menu") )"
text_bar_header_build="$(printf '%0.s-' $(seq 1 "$count_text_bar_header") )"
text_side_menu="$(printf '%0.s-' $(seq 1 "$count_text_side_length_menu") )"
text_side_header="$(printf '%0.s-' $(seq 1 "$count_text_side_length_header") )"
text_menu="$(echo "$text_side_menu"" ""$text_header""$script_version"" ""$text_side_menu")"
text_menu_bar="$(echo "$text_bar_menu_build")"
text_header="$(echo " ""$text_side_header"" ""$text_header""$script_version"" ""$text_side_header")"
text_header_bar="$(echo " ""$text_bar_header_build")"
#
###########################################################################################################################
#
#
# display initializing message
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " display initializing message "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
clear
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " This utility drops schemas from the PostgreSQL that are older than a specific date timestamp "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " This script will: "
fnWriteLog ${LINENO} level_0 " - Drop PostgreSQL schemas from the database 'aws_snapshot' "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please wait "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Checking the input parameters and initializing the app "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Depending on connection speed and PostgreSQL response, this can take "
fnWriteLog ${LINENO} level_0 " from a few seconds to a few minutes "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
#
#
###################################################
#
#
# log the task counts
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " log the task counts "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} "value of variable 'count_this_file_tasks': "$count_this_file_tasks" "
fnWriteLog ${LINENO} "value of variable 'count_this_file_tasks_end': "$count_this_file_tasks_end" "
fnWriteLog ${LINENO} "value of variable 'count_this_file_tasks_increment': "$count_this_file_tasks_increment" "
#
###################################################
#
#
# check command line parameters
# check for -h
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " check command line parameters "
fnWriteLog ${LINENO} " check for -h "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
if [[ "$1" = "-h" ]]
then
clear
fnUsage
fi
#
###################################################
#
#
# check command line parameters
# check for --version
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " check command line parameters "
fnWriteLog ${LINENO} " check for --version "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
if [[ "$1" = "--version" ]]
then
clear
echo ""
echo "'AWS Services Snapshot Schema Drop' script version: "$script_version" "
echo ""
exit
fi
#
###################################################
#
#
# check command line parameters
# if less than 2, then display the Usage
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " check command line parameters "
fnWriteLog ${LINENO} " if less than 2, then display the Usage "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
if [[ "$#" -lt 2 ]]
then
clear
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " ERROR: You did not enter all of the required parameters "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " You must provide a timestamp for the profile parameter: -t "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Example: "$0" -t 2018-03-27-134318 "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnUsage
fi
#
###################################################
#
#
# check command line parameters
# if too many parameters, then display the error message and useage
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " check command line parameters "
fnWriteLog ${LINENO} " if too many parameters, then display the error message and useage "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""