-
Notifications
You must be signed in to change notification settings - Fork 975
/
Copy pathupgrade.mongo.70.sh
146 lines (126 loc) · 5.89 KB
/
upgrade.mongo.70.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
#!/bin/bash
#we have to check since we cannot continue unless
if [ -f /etc/redhat-release ]; then
CENTOS_MAJOR="$(cat /etc/redhat-release |awk -F'[^0-9]+' '{ print $2 }')"
if [[ "$CENTOS_MAJOR" != "8" && "$CENTOS_MAJOR" != "9" ]]; then
echo "Unsupported OS version, only support CentOS/RHEL 8 and 9."
exit 1
fi
fi
if [ -f /etc/lsb-release ]; then
UBUNTU_YEAR="$(lsb_release -sr | cut -d '.' -f 1)";
UBUNTU_RELEASE="$(lsb_release -cs)"
if [[ "$UBUNTU_YEAR" != "20" && "$UBUNTU_YEAR" != "22" ]]; then
echo "Unsupported OS version, only support Ubuntu 20 and 22."
exit 1
fi
fi
#check if authentication is required
isAuth=0
if grep -Eq '^\s*authorization\s*:\s*enabled' /etc/mongod.conf; then
isAuth=1
fi
#check if we have previous upgrade needed
FEATVER=$(mongosh admin --eval "printjson(db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ).featureCompatibilityVersion)" --quiet);
VER=$(mongod -version | grep "db version" | cut -d ' ' -f 3 | cut -d 'v' -f 2)
if [ "$isAuth" -eq "1" ]; then
echo "Since authentication is enabled, we cannot verify if you need to run this upgrade script"
echo ""
echo "Please run this command with authentication parameters:"
echo ""
echo "mongosh admin --eval \"db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 } )\""
echo ""
echo "and continue only if \"featureCompatibilityVersion\" is 6.0 "
echo ""
read -r -p "Is your \"featureCompatibilityVersion\" version is 6.0? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
then
echo "Continue upgrading"
else
echo "Stopping script"
exit 0;
fi
fi
if [ -x "$(command -v mongosh)" ]; then
if echo "$VER" | grep -q -i "7.0" ; then
if echo "$FEATVER" | grep -q -i "6.0" ; then
echo "run this command to upgrade to 7.0";
echo "mongosh admin --eval \"db.adminCommand( { setFeatureCompatibilityVersion: \\\"7.0\\\" } )\"";
else
echo "We already have version 7.0";
fi
exit 0;
elif echo "$VER" | grep -q -i "6.0" ; then
if echo "$FEATVER" | grep -q -i "5.0" ; then
echo "run this command before upgrading to 7.0";
echo "mongosh admin --eval \"db.adminCommand( { setFeatureCompatibilityVersion: \\\"6.0\\\" } )\"";
exit 0;
else
echo "Upgrading to MongoDB 7.0";
fi
else
echo "Unsupported MongodB version $VER";
echo "Upgrade to MongoDB 6.0 first and then run this script";
exit 1;
fi
if [ -f /etc/redhat-release ]; then
#backup of systemd unit file and mongod.conf file
\cp /usr/lib/systemd/system/mongod.service /usr/lib/systemd/system/mongod.service.bak
\cp -f /etc/mongod.conf /etc/mongod.conf.bak
#uninstall mognodb
yum erase -y mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
fi
if [ -f /etc/lsb-release ]; then
#uninstall mognodb
apt-get remove -y mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
fi
fi
if [ -f /etc/redhat-release ]; then
#install latest mongodb
#select source based on release
echo "[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/${CENTOS_MAJOR}/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc" > /etc/yum.repos.d/mongodb-org-7.0.repo
yum install -y mongodb-org
\cp -f /etc/mongod.conf.bak /etc/mongod.conf
fi
if [ -f /etc/lsb-release ]; then
#install latest mongodb
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu ${UBUNTU_RELEASE}/mongodb-org/7.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list ;
apt-get update
#install mongodb
apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install -y mongodb-org --force-yes || (echo "Failed to install mongodb." ; exit)
fi
if [ -f /etc/redhat-release ]; then
#Restoring systemd unit file
\cp -f /usr/lib/systemd/system/mongod.service.bak /usr/lib/systemd/system/mongod.service
systemctl daemon-reload
fi
# check and comment out journal: enabled: true in mongod.conf
CONF_FILE="/etc/mongod.conf"
if grep -qP '^\s*journal\s*:\s*$' "$CONF_FILE" && grep -qP '^\s*enabled\s*:\s*true\s*$' "$CONF_FILE"; then
echo "Commenting out journal: enabled: true in $CONF_FILE"
sed -i '/^\s*journal\s*:/ { N; s/\(.*\n\s*\)\(enabled\s*:\s*true\s*$\)/# \1# \2/ }' "$CONF_FILE"
else
echo "Could not find 'journal: enabled: true' in $CONF_FILE or it's already commented."
fi
#mongodb might need to be started
systemctl restart mongod || echo "mongodb systemctl job does not exist"
#nc not available on latest centos
#until nc -z localhost 27017; do echo Waiting for MongoDB; sleep 1; done
mongosh --nodb --eval 'var conn; print("Waiting for MongoDB connection on port 27017. Exit if incorrect port"); var cnt = 0; while(!conn && cnt <= 300){try{conn = new Mongo("localhost:27017");}catch(Error){}sleep(1000);cnt++;}'
if [ "$isAuth" -eq "1" ]; then
echo "run this command with authentication to upgrade to 7.0"
echo "mongosh admin --eval \"db.adminCommand( { setFeatureCompatibilityVersion: \\\"7.0\\\" } )\""
elif ! mongosh admin --eval "printjson(db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ))" ; then
echo "Could not connect to MongodB, run this command when Mongo is up and running"
echo "mongosh admin --eval \"db.adminCommand( { setFeatureCompatibilityVersion: \\\"7.0\\\" } )\""
else
mongosh admin --eval "printjson(db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ))"
mongosh admin --eval "db.adminCommand( { setFeatureCompatibilityVersion: \"7.0\", confirm: true } )"
echo "Finished upgrading script"
fi