forked from andresgongora/synth-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchlinux-update-helper.sh
executable file
·259 lines (190 loc) · 6.48 KB
/
archlinux-update-helper.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
#!/bin/sh
## +-----------------------------------+-----------------------------------+
## | |
## | ARCHLINUX UPDATE HELPER |
## | |
## | Copyright (c) 2017, Andres Gongora <[email protected]>. |
## | |
## | This program is free software: you can redistribute it and/or modify |
## | it under the terms of the GNU General Public License as published by |
## | the Free Software Foundation, either version 3 of the License, or |
## | (at your option) any later version. |
## | |
## | This program is distributed in the hope that it will be useful, |
## | but WITHOUT ANY WARRANTY; without even the implied warranty of |
## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
## | GNU General Public License for more details. |
## | |
## | You should have received a copy of the GNU General Public License |
## | along with this program. If not, see <http://www.gnu.org/licenses/>. |
## | |
## +-----------------------------------------------------------------------+
##
## This script performs the following operations:
## 1. Update mirrolist for maximum speed
## 2. Update pacman & create log
## 3. Optimize pacman
##
## This script requires super-user privileges
##
################################################################################
## FUNCTIONS ##
################################################################################
################################################################################
## ECHO HELPERS (Inspired by Zach Holman)
## TEXT FORMATING
TXT_NORMAL='\033[0m'
TXT_HEADER='\033[1;97m'
TXT_INFO='\033[0;34m'
TXT_SUCCESS='\033[0;32m'
TXT_USER='\033[0;33m'
TXT_WARN='\033[1;33m'
TXT_FAIL='\033[1;31m'
## MESSAGE BANNER
MSG_PLAIN=" "
MSG_INFO="[ ${TXT_INFO}..${TXT_NORMAL} ]"
MSG_USER="[ ${TXT_USER}??${TXT_NORMAL} ]"
MSG_SUCCESS="[ ${TXT_SUCCESS}OK${TXT_NORMAL} ]"
MSG_WARN="[ ${TXT_WARN}!!${TXT_NORMAL} ]"
MSG_FAIL="[${TXT_FAIL}FAIL${TXT_NORMAL}]"
printHeader ()
{
printf "\n\r\t${TXT_HEADER}$1${TXT_NORMAL}\n\n"
}
printPlain ()
{
printf "\r\t${MSG_PLAIN} $1\n"
}
printInfo ()
{
printf "\r\t${MSG_INFO} $1\n"
}
printUser ()
{
printf "\r\t${MSG_USER} $1\n"
}
printSuccess ()
{
printf "\r\t${MSG_SUCCESS} $1\n"
}
printWarn ()
{
printf "\r\t${MSG_WARN} $1\n"
}
printFail ()
{
printf "\r\t${MSG_FAIL} $1\n"
echo ''
exit
}
################################################################################
## UPDATE MIRROLIST FOR MAXIMUM SPEED
updateMirrorlist ()
{
printHeader "Optimizing mirrorlist..."
printInfo "Checking if reflector is installed"
if [ -f /usr/bin/reflector ]; then
printSuccess "Reflector already installed"
else
printInfo "Reflector not installed. Installing:\n"
sudo pacman -Sy reflector --noconfirm --color=auto
echo ""
if [ -f /usr/bin/reflector ]; then
printSuccess "Reflector successfully installed"
else
printFail "Could not install reflector"
fi
fi
printInfo "Creating backup mirrorlist"
MIRRORLIST=/etc/pacman.d/mirrorlist
BACKUP=/etc/pacman.d/mirrorlist.backup
if [ -f $BACKUP ]; then
sudo rm $BACKUP # remove old backup if it exists
fi
sudo cp $MIRRORLIST $BACKUP
if [ -f $BACKUP ]; then
printSuccess "Mirrorlist backed up to $BACKUP"
else
printFail "Could not backup current mirrorlist to $BACKUP"
fi
printInfo "Optimizing mirrorlist for maximum download speed:\n"
sudo reflector -l 5 --verbose --sort rate --save /etc/pacman.d/mirrorlist
echo ""
printInfo "Refreshing package list:\n"
sudo pacman -Syy --color=auto
echo ""
printSuccess "Mirrorlist updated and packages refreshed"
}
################################################################################
## UPDATE SYSTEM
updateSystem ()
{
printHeader "Updating system..."
printInfo "Downloading new packages"
sudo pacman -Suwq --noconfirm --color=auto
echo""
LOGFILE=~/pacman.log
printInfo "A brief pacman log will be stored at $LOGFILE"
printInfo "Updating system:\n"
sudo pacman -Su --noconfirm | tee $LOGFILE
echo ""
printSuccess "System updated"
}
################################################################################
## OPTIMIZE PACMAN
optimizePacman ()
{
local prunechace=
local action=
local THISTTY=$(tty)
printHeader "Optimizing pacman..."
printInfo "Removing orphan packages from the system"
printInfo "If it complains about no targets, it means that you have no orphans:\n"
sudo pacman -Rns --noconfirm --color=auto $(pacman -Qtdq)
echo ""
printPromt "Remove old packages from cache? (Not recommended) y/N: "
exec 6<&0
exec 0<"$THISTTY"
read -n 1 action
exec 0<&6 6<&-
case "$action" in
y )
prunechace=true;;
Y )
prunechace=true;;
* )
prunechace=false;;
esac
if [ "$prunechace" == "true" ]; then
printInfo "Removing uninstalled packages:\n"
sudo pacman -Sc --noconfirm --color=auto
echo ""
else
printInfo "... skipping"
fi
printInfo "Compacting pacman database for faster access in the future:\n"
sudo pacman-optimize
printSuccess "Pacman optimized"
}
################################################################################
## MAIN ##
################################################################################
## GREET MESSAGE
printHeader "ARCHLINUX SYSTEM-UPDATE HELPER"
printWarn "I will use super-user privileges...\n"
## GET SU PRIVILEGES
sudo -v
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
## RUN SCRIPT
updateMirrorlist
updateSystem
optimizePacman
## GOODBY MESSAGE
printHeader "SUCCESS! SYSTEM UPDATED AND OPTIMIZED"
printWarn "But remmember to check the logs to see if you have to:"
printWarn " >> Replace old config files with .pacnew files"
printWarn " >> Act on any alert or warning"
printWarn "If needed, you may find a more extensive log at /var/log/pacman.log"
printWarn "Reference: https://wiki.archlinux.org/index.php/System_maintenance"
echo "\n"
### EOF ###