-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupgrade-mysql.sh
More file actions
211 lines (195 loc) · 6.35 KB
/
upgrade-mysql.sh
File metadata and controls
211 lines (195 loc) · 6.35 KB
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
#!/bin/bash
# bash <(curl -s https://raw.githubusercontent.com/KnoAll/cacti-template/dev/upgrade-mysql.sh) dev
green=$(tput setaf 2)
red=$(tput setaf 1)
tan=$(tput setaf 3)
reset=$(tput sgr0)
printinfo() {
if [ -z "$1" ]; then
printf "${tan}::: ${green}%s${reset}\n" "$@"
else
printf "${tan}::: ${green}%s${reset}\n" "$(date +%a_%R): $@"
fi
}
printwarn() {
printf "${tan}*** WARNING: %s${reset}\n" "$(date +%a_%R): $@"
}
printerror() {
printf "${red}!!! ERROR: %s${reset}\n" "$(date +%a_%R): $@"
}
if [[ $1 == "dev" || $1 == "--switch-dev" ]]; then
param1=$1
param2=$2
branch=dev
printwarn "Now on DEV MYSQL branch."
else
printinfo
branch=master
fi
printinfo "Checking for MariaDB upgrade..."
printinfo
if [[ `whoami` == "root" ]]; then
printerror "You ran me as root! Do not run me as root!"
exit 1
elif grep -q "Raspbian GNU/Linux 9" /etc/os-release; then
printerror "Sorry, Raspbian not supported for MariaDB upgrade yet, cannot proceed..."
printinfo
exit 1
if [[ `whoami` != "pi" ]]; then
printerror "Uh-oh. You are not logged in as the default pi user. Exiting..."
exit 1
else
os_dist=raspbian
os_name=Raspbian
webserver=apache2
verphp="$(php -v | grep -Po '(?<=PHP )([0-7.]+)' | cut -c-3)"
fi
elif grep -q "Raspbian GNU/Linux 10" /etc/os-release; then
printerror "Sorry, Raspbian not supported for MariaDB upgrade yet, cannot proceed..."
printinfo
exit 1
if [[ `whoami` != "pi" ]]; then
printerror "Uh-oh. You are not logged in as the default pi user. Exiting..."
printinfo
exit 1
else
os_dist=raspbian
os_name=Raspbian
webserver=apache2
verphp="$(php -v | grep -Po '(?<=PHP )([0-7.]+)' | cut -c-3)"
fi
elif grep -q "CentOS Linux 7" /etc/os-release; then
if [[ `whoami` != "cacti" ]]; then
printerror "Uh-oh. You are not logged in as the default cacti user. Exiting..."
printinfo
exit 1
else
os_dist=centos
os_name=CentOS7
webserver=httpd
pkg_mgr=yum
os_dist=centos
remi=remi-release-7.rpm
fi
elif grep -q "CentOS Linux 8" /etc/os-release; then
printerror "Sorry, CentOS8 not supported for MYSQL upgrade yet, cannot proceed..."
printinfo
exit 1
if [[ `whoami` != "cacti" ]]; then
printerror "Uh-oh. You are not logged in as the default Cacti user. Exiting..."
printinfo
exit 1
else
os_dist=centos
os_name=CentOS8
webserver=httpd
pkg_mgr=yum
os_dist=centos
remi=remi-release-8.rpm
fi
else
printerror "We don't appear to be on a supported OS. Exiting..."
printinfo
exit 1
fi
#installed MYSQL version
mysql_ver=$( mysql -u root -pcacti -N -B -e "select version();" )
if [ $? -ne 0 ];then
printerror "ERROR determining MariaDB version, cannot continue."
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=upgrade-mysql-error&write=0 )
exit 1
fi
prod_version=$( curl -s https://raw.githubusercontent.com/Cacti/cacti/master/include/cacti_version )
cacti_ver=$( cat /var/www/html/cacti/include/cacti_version )
shmysql_ver=$(echo $mysql_ver | cut -c-4)
#set upgrade version
mysql_version=10.5
mysql_description="v10.5.x"
function version { echo "$@" | gawk -F. '{ printf("%03d%03d%03d\n", $1,$2,$3); }'; }
function version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
checkCacti() {
if version_ge $cacti_ver $prod_version; then
printinfo
else
printerror "Installed Cacti v$cacti_ver <= minimum required Cacti v$prod_version, you must upgrade Cacti. Cannot upgrade MariaDB."
exit 1
fi
}
upgradeAsk () {
#check version of MYSQL installed
if [ "$(version "$mysql_version")" -gt "$(version "$shmysql_ver")" ]; then
printinfo
read -p "You are running MariaDB v$shmysql_ver.x. Do you want to upgrade your MariaDB install to $mysql_description? y/N: " upAsk
case "$upAsk" in
y | Y | yes | YES| Yes ) printinfo "Ok, let's go!"
if [[ $param1 == "dev" ]]; then
printwarn $param1
else
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=upgrade-mysql_$shmysql_ver&write=0 )
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=upgrade-mysql&write=0 )
fi
backupMYSQL
upgradeMYSQL
;;
* )
printwarn "OK, please consider upgrading, old versions of MariaDB are not updated and may contain known security and stability issues."
if [[ $param1 == "dev" ]]; then
printwarn $param1
else
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=decline-upgrade-mysql&write=0 )
fi
exit 1
;;
esac
else
printinfo "Installed MariaDB $mysql_ver >= current stable $mysql_description. Nothing to do."
exit 0
fi
}
backupMYSQL() {
printinfo "Backing up MariaDB server and databases. File will be saved in home directory as MariaBak_$(date +\%Y\%m\%d).gz."
sudo yum install -y -q MariaDB-backup
cd
sudo mariabackup --backup --user=root --password=cacti --stream=xbstream | gzip > MariaBak_$(date +\%Y\%m\%d).gz
if [ $? -ne 0 ];then
printerror "ERROR backing up MariaDB, cannot continue."
counter=$( curl -s http://www.kevinnoall.com/cgi-bin/counter/unicounter.pl?name=upgrade-mysql-error&write=0 )
exit 1
else
printinfo "MariaDB successfully backed up."
printinfo
fi
# To decompress backup use:
# gunzip -c <backup file>.gz | mbstream -x
}
upgradeMYSQL() {
printinfo "Setting up MariaDB repo"
sudo yum -y -q update
cd /etc/yum.repos.d
sudo mv MariaDB.repo MariaDB.repo.$shmysql_ver
sudo wget -q https://raw.githubusercontent.com/KnoAll/cacti-template/$branch/install/MariaDB.repo
printinfo "Enabling new $mysql_description"
sudo systemctl stop mariadb
sudo sed -i 's/innodb_additional_mem_pool/#innodb_additional_mem_pool/g' /etc/my.cnf
sudo yum remove -y -q MariaDB-server MariaDB-shared MariaDB-client MariaDB-common MariaDB-devel
sudo yum install -y -q MariaDB-server MariaDB-shared MariaDB-client MariaDB-common MariaDB-devel MariaDB-backup net-snmp php-mysql
sudo cp /etc/snmp/snmpd.conf.rpmsave /etc/snmp/snmpd.conf
sudo cp /etc/my.cnf.rpmsave /etc/my.cnf
sudo systemctl enable snmpd
sudo systemctl start snmpd
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo mysql_upgrade --silent -u root -pcacti
sudo systemctl restart httpd
sudo yum -y -q update
if [ $? -ne 0 ];then
printerror "ERROR upgrading MariaDB version."
else
mysql_ver=$( mysql -u root -pcacti -N -B -e "select version();" )
printinfo "MariaDB upgraded to $mysql_ver"
fi
}
checkCacti
upgradeAsk
printinfo
exit 0