1
+ #! /bin/bash
2
+
3
+ # we have to check since we cannot continue unless
4
+ if [ -f /etc/redhat-release ]; then
5
+ CENTOS_MAJOR=" $( cat /etc/redhat-release | awk -F' [^0-9]+' ' { print $2 }' ) "
6
+
7
+ if [[ " $CENTOS_MAJOR " != " 8" && " $CENTOS_MAJOR " != " 9" ]]; then
8
+ echo " Unsupported OS version, only support CentOS/RHEL 8 and 9."
9
+ exit 1
10
+ fi
11
+ fi
12
+
13
+ if [ -f /etc/lsb-release ]; then
14
+ UBUNTU_YEAR=" $( lsb_release -sr | cut -d ' .' -f 1) " ;
15
+ UBUNTU_RELEASE=" $( lsb_release -cs) "
16
+
17
+ if [[ " $UBUNTU_YEAR " != " 20" && " $UBUNTU_YEAR " != " 22" && " $UBUNTU_YEAR " != " 24" ]]; then
18
+ echo " Unsupported OS version, only support Ubuntu 20 and 22 and 24."
19
+ exit 1
20
+ fi
21
+ fi
22
+
23
+ # check if authentication is required
24
+ isAuth=0
25
+ if grep -Eq ' ^\s*authorization\s*:\s*enabled' /etc/mongod.conf; then
26
+ isAuth=1
27
+ fi
28
+
29
+ # check if we have previous upgrade needed
30
+ FEATVER=$( mongosh admin --eval " printjson(db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ).featureCompatibilityVersion)" --quiet) ;
31
+ VER=$( mongod -version | grep " db version" | cut -d ' ' -f 3 | cut -d ' v' -f 2)
32
+
33
+ if [ " $isAuth " -eq " 1" ]; then
34
+ echo " Since authentication is enabled, we cannot verify if you need to run this upgrade script"
35
+ echo " "
36
+ echo " Please run this command with authentication parameters:"
37
+ echo " "
38
+ echo " mongosh admin --eval \" db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 } )\" "
39
+ echo " "
40
+ echo " and continue only if \" featureCompatibilityVersion\" is 7.0 "
41
+ echo " "
42
+ read -r -p " Is your \" featureCompatibilityVersion\" version is 7.0? [y/N] " response
43
+ if [[ " $response " =~ ^([yY][eE][sS]| [yY])$ ]]
44
+ then
45
+ echo " Continue upgrading"
46
+ else
47
+ echo " Stopping script"
48
+ exit 0;
49
+ fi
50
+
51
+ fi
52
+
53
+ if [ -x " $( command -v mongosh) " ]; then
54
+ if echo " $VER " | grep -q -i " 8.0" ; then
55
+ if echo " $FEATVER " | grep -q -i " 7.0" ; then
56
+ echo " run this command to upgrade to 8.0" ;
57
+ echo " mongosh admin --eval \" db.adminCommand( { setFeatureCompatibilityVersion: \\\" 8.0\\\" , confirm: true } )\" " ;
58
+ else
59
+ echo " We already have version 8.0" ;
60
+ fi
61
+ exit 0;
62
+ elif echo " $VER " | grep -q -i " 7.0" ; then
63
+ if echo " $FEATVER " | grep -q -i " 6.0" ; then
64
+ echo " run this command before upgrading to 8.0 and rerunning this script" ;
65
+ echo " mongosh admin --eval \" db.adminCommand( { setFeatureCompatibilityVersion: \\\" 7.0\\\" , confirm: true } )\" " ;
66
+ exit 0;
67
+ else
68
+ echo " Upgrading to MongoDB 8.0" ;
69
+ fi
70
+ else
71
+ echo " Unsupported MongodB version $VER " ;
72
+ echo " Upgrade to MongoDB 7.0 first and then run this script" ;
73
+ exit 1;
74
+ fi
75
+
76
+ if [ -f /etc/redhat-release ]; then
77
+ # backup of systemd unit file and mongod.conf file
78
+ \c p /usr/lib/systemd/system/mongod.service /usr/lib/systemd/system/mongod.service.bak
79
+ \c p -f /etc/mongod.conf /etc/mongod.conf.bak
80
+ # uninstall mognodb
81
+ yum erase -y mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
82
+ fi
83
+
84
+ if [ -f /etc/lsb-release ]; then
85
+ # uninstall mognodb
86
+ apt-get remove -y mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
87
+ fi
88
+ fi
89
+
90
+ if [ -f /etc/redhat-release ]; then
91
+ # install latest mongodb
92
+ # select source based on release
93
+ echo " [mongodb-org-8.0]
94
+ name=MongoDB Repository
95
+ baseurl=https://repo.mongodb.org/yum/redhat/${CENTOS_MAJOR} /mongodb-org/8.0/x86_64/
96
+ gpgcheck=1
97
+ enabled=1
98
+ gpgkey=https://pgp.mongodb.com/server-8.0.asc" > /etc/yum.repos.d/mongodb-org-8.0.repo
99
+
100
+ yum install -y mongodb-org
101
+ \c p -f /etc/mongod.conf.bak /etc/mongod.conf
102
+ fi
103
+
104
+ if [ -f /etc/lsb-release ]; then
105
+ # install latest mongodb
106
+ curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
107
+
108
+ echo " deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu ${UBUNTU_RELEASE} /mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
109
+ apt-get update
110
+ # install mongodb
111
+ apt-get -o Dpkg::Options::=" --force-confdef" -o Dpkg::Options::=" --force-confold" install -y mongodb-org --force-yes || (echo " Failed to install mongodb." ; exit)
112
+ fi
113
+
114
+ if [ -f /etc/redhat-release ]; then
115
+ # Restoring systemd unit file
116
+ \c p -f /usr/lib/systemd/system/mongod.service.bak /usr/lib/systemd/system/mongod.service
117
+ systemctl daemon-reload
118
+ fi
119
+
120
+ # mongodb might need to be started
121
+ systemctl restart mongod || echo " mongodb systemctl job does not exist"
122
+
123
+ # nc not available on latest centos
124
+ # until nc -z localhost 27017; do echo Waiting for MongoDB; sleep 1; done
125
+ 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++;}'
126
+
127
+ if [ " $isAuth " -eq " 1" ]; then
128
+ echo " run this command with authentication to upgrade to 8.0"
129
+ echo " mongosh admin --eval \" db.adminCommand( { setFeatureCompatibilityVersion: \\ \8.0\\\" , confirm: true } )\" "
130
+ elif ! mongosh admin --eval " printjson(db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ))" ; then
131
+ echo " Could not connect to MongodB, run this command when Mongo is up and running"
132
+ echo " mongosh admin --eval \" db.adminCommand( { setFeatureCompatibilityVersion: \\\" 8.0\\\" , confirm: true } )\" "
133
+ else
134
+ mongosh admin --eval " printjson(db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ))"
135
+ mongosh admin --eval " db.adminCommand( { setFeatureCompatibilityVersion: \" 8.0\" , confirm: true } )"
136
+ echo " Finished upgrading script"
137
+ fi
0 commit comments