-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_and_pgadmin_uninstallation.sh
executable file
·149 lines (130 loc) · 7.05 KB
/
postgres_and_pgadmin_uninstallation.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
#!/bin/bash
##########################################################################
# AUTHORSHIP #
#------------------------------------------------------------------------#
# Author: Victor Oliveira #
# Email: [email protected] #
# Start Date: 21/03/2025 #
# Update Dates: 22/03/2025 | 25/03/2025 | ---------- #
# Description: Script for the complete uninstallation of the #
# Postgres DBMS and the pgAdmin Graphical #
# Interface Tool #
##########################################################################
# NOTE:. DEFAULT VARIABLES CAN BE EDITED
# general variables
PG_CTN_NAME_DEFAULT="postgres-dbms"
PGADMIN_CTN_NAME_DEFAULT="pgadmin"
PG_IMG_NAME_DEFAULT="postgres:latest"
PGADMIN_IMG_NAME_DEFAULT="dpage/pgadmin4:latest"
PG_VOL_NAME_DEFAULT="pg_data_volume"
NETWORK_NAME_DEFAULT="postgres-dbms_pgadmin_bridge"
echo "==================================================================================================================="
echo " P o s t g r e s a n d p g A d m i n U n i n s t a l l "
echo "==================================================================================================================="
# check if docker is installed
if ! command -v docker &> /dev/null
then
echo -e "\nDocker is not installed in the environment.\nNOTE: Please install Docker before proceeding with this procedure!\n\nAccess the Docker Engine documentation at: https://docs.docker.com/engine/install/"
echo "==================================================================================================================="
exit 1
fi
echo
# reading user input data
read -p "Enter the name or container ID of the installed Postgres container (default: "$PG_CTN_NAME_DEFAULT"): " PG_CTN_NAME
read -p "Enter the name or container ID of the installed pgAdmin container (default: "$PGADMIN_CTN_NAME_DEFAULT"): " PGADMIN_CTN_NAME
read -p "Enter the name or image ID of the installed Postgres Docker image (default: "$PG_IMG_NAME_DEFAULT"): " PG_IMG_NAME
read -p "Enter the name or image ID of the installed pgAdmin Docker image (default: "$PGADMIN_IMG_NAME_DEFAULT"): " PGADMIN_IMG_NAME
read -p "Enter the name of the data volume associated with the installed Postgres instance (default: "$PG_VOL_NAME_DEFAULT"): " PG_VOL_NAME
read -p "Enter the name or network ID of the Docker network connecting the installed Postgres and pgAdmin instances (default: "$NETWORK_NAME_DEFAULT"): " NETWORK_NAME
# defining the variables with the names and/or ids of the containers, images, volumes and network
if [ -z "$PG_CTN_NAME" ]; then
PG_CTN_NAME=$PG_CTN_NAME_DEFAULT
fi
if [ -z "$PGADMIN_CTN_NAME" ]; then
PGADMIN_CTN_NAME=$PGADMIN_CTN_NAME_DEFAULT
fi
if [ -z "$PG_IMG_NAME" ]; then
PG_IMG_NAME=$PG_IMG_NAME_DEFAULT
fi
if [ -z "$PGADMIN_IMG_NAME" ]; then
PGADMIN_IMG_NAME=$PGADMIN_IMG_NAME_DEFAULT
fi
if [ -z "$PG_VOL_NAME" ]; then
PG_VOL_NAME=$PG_VOL_NAME_DEFAULT
fi
if [ -z "$NETWORK_NAME" ]; then
NETWORK_NAME=$NETWORK_NAME_DEFAULT
fi
echo -e "\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Container Removal Action ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨\n"
# removing postgres container
if docker ps -a --format '{{.Names}}' | grep -q "^$PG_CTN_NAME$"; then
if docker ps --format '{{.Names}}' | grep -q "^$PG_CTN_NAME$"; then
docker stop "$PG_CTN_NAME" > /dev/null 2>&1
echo "Container '$PG_CTN_NAME' STOPPED successfully."
fi
docker rm "$PG_CTN_NAME" > /dev/null 2>&1
echo -e "Container '$PG_CTN_NAME' REMOVED successfully.\n"
else
echo -e "Container '$PG_CTN_NAME' does not exist.\n"
fi
# removing pgadmin container
if docker ps -a --format '{{.Names}}' | grep -q "^$PGADMIN_CTN_NAME$"; then
if docker ps --format '{{.Names}}' | grep -q "^$PGADMIN_CTN_NAME$"; then
docker stop "$PGADMIN_CTN_NAME" > /dev/null 2>&1
echo "Container '$PGADMIN_CTN_NAME' STOPPED successfully."
fi
docker rm "$PGADMIN_CTN_NAME" > /dev/null 2>&1
echo "Container '$PGADMIN_CTN_NAME' REMOVED successfully."
else
echo "Container '$PGADMIN_CTN_NAME' does not exist."
fi
echo -e "\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Image Removal Action ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨\n"
# removing postgres image
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^$PG_IMG_NAME$"; then
docker rmi "$PG_IMG_NAME" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "Image '$PG_IMG_NAME' REMOVED successfully.\n"
else
echo -e "FAILED to remove image '$PG_IMG_NAME'. NOTE: It might still be in use or there is an issue with the image.\n"
fi
else
echo -e "Image '$PG_IMG_NAME' does not exist.\n"
fi
# removing pgadmin image
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^$PGADMIN_IMG_NAME$"; then
docker rmi "$PGADMIN_IMG_NAME" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Image '$PGADMIN_IMG_NAME' REMOVED successfully."
else
echo "FAILED to remove image '$PGADMIN_IMG_NAME'. NOTE: It might still be in use or there is an issue with the image."
fi
else
echo "Image '$PGADMIN_IMG_NAME' does not exist."
fi
echo -e "\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Volume Removal Action ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨\n"
# removing postgres volume
if docker volume ls --format '{{.Name}}' | grep -q "^$PG_VOL_NAME$"; then
docker volume rm "$PG_VOL_NAME" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Volume '$PG_VOL_NAME' REMOVED successfully."
else
echo "FAILED to remove volume '$PG_VOL_NAME'. Note: It might still be in use or there is an issue with the volume."
fi
else
echo "Volume '$PG_VOL_NAME' does not exist."
fi
echo -e "\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Network Removal Action ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨\n"
# removing network configuration
if docker network ls --format '{{.Name}}' | grep -q "^$NETWORK_NAME$"; then
docker network rm "$NETWORK_NAME" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Network '$NETWORK_NAME' REMOVED successfully."
else
echo "FAILED to remove the network '$NETWORK_NAME'. Note: It might still be in use or there is an issue with the network."
fi
else
echo "Network '$NETWORK_NAME' does not exist."
fi
echo -e "\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨"
echo -e "\nNOTE: Uninstallation procedure COMPLETED!\n==================================================================================================================="
exit 0