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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
#!/bin/bash
########################################################################################################################
# apt-downgrade-bin-packages-to-official-archive.sh
########################################################################################################################
#
# All rights reserved Ⓒ 2017-2023 sdxlive.com
#
# Written by Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License.
# You may not use this file except in compliance with the License. You may obtain a copy of the License at
#
# https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made.
# You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
#
########################################################################################################################
########################################################################################################################
#
# Downgrading some (light mode) or all (full mode) installed binary packages with a version ending with +<os_version> or
# -<os_version> to the package version provided by the official Debian/Ubuntu repositories.
# It is possible to pass a perl regular expression to narrow the search for installed binary packages which need to be
# downgraded.
#
########################################################################################################################
#
# Requirements:
# ------------
#
# - We assume that apt alongside all its dependencies are already installed.
# - We expect that 'deb-src' URIs are set in your sources.list
#
########################################################################################################################
#
# Parameters:
# ----------
#
# - <os_version>: binary packages version ending with +<os_version> or -<os_version>
# For instance:
# * 10|10.0 matches buster Debian distribution
# * 11|11.0 matches bullseye Debian distribution
# * 19.04 matches disco Ubuntu distribution
# * 19.10 matches eoan Ubuntu distribution
# * 20.04 matches focal Ubuntu distribution
# * 20.10 matches groovy Ubuntu distribution
# - <mode>: one of
# * light: downgrade only the binary packages with a version ending with +/-<os_version> for which there is the
# same core version available from the Debian/Ubuntu official repositories
# Its typical use-case is to be used just before/right after an OS distribution upgrade.
# * full: downgrade all the binary packages with a version ending with +/-<os_version> if at least one version
# is available from the Debian/Ubuntu official repositories
# Its typical use-case is to clear your Debian/Ubuntu system of all binary packages installed from the
# DR/PPA for a specific distribution, except for the ones which have no equivalent in the official
# repositories.
# - <auto>: optional, one of
# * true|yes: the binary packages will be downgraded without any user's confirmation
# Use with caution.
# * false|no: the binary packages will be downgraded only with user's confirmation for each source package
# (default)
# - <versions_group>: optional, one of
# + positive: downgrading only the subset of binary packages with a version ending with +<os_version>
# + negative: downgrading only the subset of binary packages with a version ending with -<os_version>
# + all: downgrading all binary packages with a version ending with +<os_version> or -<os_version>
# (default)
# - <perl_regex_include_pattern>: optional perl regular expression used with 'grep -P --' to narrow the search for the
# matched installed binary packages. Some special characters may need to be escaped.
# Use with caution: setting <auto> to false is strongly advised when using
# <perl_regex_include_pattern>.
# - <perl_regex_exclude_pattern>: optional perl regular expression used with 'grep -Pv --' to exclude some binary packages
# from the search. Some special characters may need to be escaped.
# Use with caution: setting <auto> to false is strongly advised when using
# <perl_regex_exclude_pattern>.
#
#######################################################################################################################
#
# Usage example:
# -------------
#
# 1) light mode:
# =============
#
# Downgrading all binary packages with a version ending with +10.0 or -10.0 when the same core version is available from
# official repositories.
# For instance here, systemd will be downgraded to 241-5 but wireshark won't:
# systemd:
# Installed: 241-5+10.0
# Candidate: 241-5+10.0
# Version table:
# *** 241-5+10.0 100
# 100 /var/lib/dpkg/status
# 241-5 500
# 500 http://deb.debian.org/debian testing/main amd64 Packages
# wireshark:
# Installed: 3.0.2+10.0
# Candidate: 3.0.2+10.0
# Version table:
# *** 3.0.2+10.0 100
# 100 /var/lib/dpkg/status
# 2.6.9-1 500
# 500 http://deb.debian.org/debian testing/main amd64 Packages
#
# apt-downgrade-bin-packages-to-official-archive.sh 10.0 light true
#
# 2) full mode:
# ============
#
# Downgrading all binary packages with a version ending with +10.0 or -10.0 when any version is available from
# official repositories.
# For instance here, systemd will be downgraded to 241-5 and wireshark will also be downgraded to 2.6.9-1:
# systemd:
# Installed: 241-5+10.0
# Candidate: 241-5+10.0
# Version table:
# *** 241-5+10.0 100
# 100 /var/lib/dpkg/status
# 241-5 500
# 500 http://deb.debian.org/debian testing/main amd64 Packages
# wireshark:
# Installed: 3.0.2+10.0
# Candidate: 3.0.2+10.0
# Version table:
# *** 3.0.2+10.0 100
# 100 /var/lib/dpkg/status
# 2.6.9-1 500
# 500 http://deb.debian.org/debian testing/main amd64 Packages
#
# apt-downgrade-bin-packages-to-official-archive.sh 19.10 full true positive '.*' 'git|perl|python'
#
########################################################################################################################
# set -x
# Expanding aliases
shopt -s expand_aliases
#------------------------------------apt-get-src-package-name--------------------------------------
# Getting the source package name from a binary package name.
function apt-get-src-package-name ()
{
local bin_package_name=$1
local bin_package_version=$2
local PARAMETERS_NUMBER=2
case "$#" in
${PARAMETERS_NUMBER})
;;
*)
echo -e ''$_{1..180}'\b-'
echo Wrong number of parameters: $(basename $0) $@
echo -e ''$_{1..180}'\b-'
return 1
;;
esac
if [[ -n "$bin_package_version" ]]; then
src_package_name=$(apt-cache -q=0 2>&1 show ${bin_package_name}=${bin_package_version} | grep Source: | head -n 1 | cut -d ' ' -f 2)
else
src_package_name=$(apt-cache -q=0 2>&1 show ${bin_package_name} | grep Source: | head -n 1 | cut -d ' ' -f 2)
fi
if [[ -z "$src_package_name" ]]; then
src_package_name=$(apt-cache -q=0 2>&1 showsrc ${bin_package_name} | grep Package: | head -n 1 | cut -d ' ' -f 2)
if [[ -z "$src_package_name" ]]; then
echo ''
return 1
fi
fi
echo "$src_package_name"
return 0
}
#------------------------------------apt-downgrade-bin-packages-to-official-archive.sh-------------
os_version=$1
mode=$2
auto=$3
versions_group=$4
perl_regex_include_pattern=$5
perl_regex_exclude_pattern=$6
PARAMETERS_NUMBER=2
case "$#" in
0)
end_of_help_message_line_number=$(awk '/^# set -x/{ print NR; exit }' $(which apt-downgrade-bin-packages-to-official-archive.sh))
((end_of_help_message_line_number--))
if [[ (-n $(apt-cache -q=0 2>&1 policy zenity | grep Installed)) && (! ($(apt-cache -q=0 2>&1 policy zenity | grep Installed) =~ none)) ]]; then
awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-bin-packages-to-official-archive.sh) | zenity --text-info --title "apt-downgrade-bin-packages-to-official-archive.sh help" --width 1350 --height 1000
else
awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-bin-packages-to-official-archive.sh)
fi
exit 1
;;
1)
case "$1" in
-h|--help)
end_of_help_message_line_number=$(awk '/^# set -x/{ print NR; exit }' $(which apt-downgrade-bin-packages-to-official-archive.sh))
((end_of_help_message_line_number--))
if [[ (-n $(apt-cache -q=0 2>&1 policy zenity | grep Installed)) && (! ($(apt-cache -q=0 2>&1 policy zenity | grep Installed) =~ none)) ]]; then
awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-bin-packages-to-official-archive.sh) | zenity --text-info --title "apt-downgrade-bin-packages-to-official-archive.sh help" --width 1350 --height 1000
else
awk -v var=$end_of_help_message_line_number 'NR >= 2 && NR <= var' $(which apt-downgrade-bin-packages-to-official-archive.sh)
fi
exit 1
;;
*)
echo -e ''$_{1..180}'\b-'
echo Wrong number of parameters: $(basename $0) $@
echo -e ''$_{1..180}'\b-'
exit 1
;;
esac
;;
$PARAMETERS_NUMBER)
auto=false
versions_group=all
perl_regex_include_pattern=''
perl_regex_exclude_pattern=''
;;
$((PARAMETERS_NUMBER+1)))
versions_group=all
perl_regex_include_pattern=''
perl_regex_exclude_pattern=''
;;
$((PARAMETERS_NUMBER+2)))
perl_regex_include_pattern=''
perl_regex_exclude_pattern=''
;;
$((PARAMETERS_NUMBER+3)))
perl_regex_exclude_pattern=''
;;
$((PARAMETERS_NUMBER+4)))
;;
*)
echo -e ''$_{1..180}'\b-'
echo Wrong number of parameters: $(basename $0) $@
echo -e ''$_{1..180}'\b-'
exit 1
;;
esac
dpkg-lock.sh apt update
# Installing dependencies needed to play an alert or a completion sound when necessary
dpkg-lock.sh apt -y install alsa-utils emacspeak pidgin-data
# Escaping the '$.*?/|\^(){}+@[]' characters for later grep -P searched string
# Letters, digits and '-' must not be escaped
alias escape_grep_pcre_search_string=$'sed -e \'s|[]$.*?/|\^(){}+@[]|\\\&|g\''
# '
# Escaping the '$.*?/|\^(){}+@[]' characters for later sed -E|-r searched string
# Letters, digits and '-' must not be escaped
alias escape_sed_ere_search_string=$'sed -e \'s|[]$.*?/|\^(){}+@[]|\\\&|g\''
# '
grep_esc_os_version="$(echo "$os_version" | escape_grep_pcre_search_string)"
os_distribution=$(lsb_release -a 2>/dev/null | grep "Distributor ID" | cut -d ':' -f 2)
os_distribution=${os_distribution//[[:space:]]/}
case $os_distribution in
Debian)
official_archives_uri=deb.debian.org/debian
;;
Ubuntu)
official_archives_uri=archive.ubuntu.com/ubuntu
;;
*)
echo -e ''$_{1..180}'\b-'
echo Unknown distribution $os_distribution
echo -e ''$_{1..180}'\b-'
exit 1
;;
esac
grep_esc_official_archives_uri="$(echo "$official_archives_uri" | escape_grep_pcre_search_string)"
# Binary packages installed from distribution $os_version
bin_package_names=()
case $versions_group in
positive)
if [[ -n "$perl_regex_exclude_pattern" ]]; then
readarray -t bin_package_names < <(apt list --installed 2>/dev/null | grep -Pv -- "$perl_regex_exclude_pattern" | grep -P -- "$perl_regex_include_pattern" | grep -P -- "\+${grep_esc_os_version} " | cut -d '/' -f 1)
else
readarray -t bin_package_names < <(apt list --installed 2>/dev/null | grep -P -- "$perl_regex_include_pattern" | grep -P -- "\+${grep_esc_os_version} " | cut -d '/' -f 1)
fi
;;
negative)
if [[ -n "$perl_regex_exclude_pattern" ]]; then
readarray -t bin_package_names < <(apt list --installed 2>/dev/null | grep -Pv -- "$perl_regex_exclude_pattern" | grep -P -- "$perl_regex_include_pattern" | grep -P -- "-${grep_esc_os_version} " | cut -d '/' -f 1)
else
readarray -t bin_package_names < <(apt list --installed 2>/dev/null | grep -P -- "$perl_regex_include_pattern" | grep -P -- "-${grep_esc_os_version} " | cut -d '/' -f 1)
fi
;;
all)
bin_package_names_1=()
bin_package_names_2=()
if [[ -n "$perl_regex_exclude_pattern" ]]; then
readarray -t bin_package_names_1 < <(apt list --installed 2>/dev/null | grep -Pv -- "$perl_regex_exclude_pattern" | grep -P -- "$perl_regex_include_pattern" | grep -P -- "\+${grep_esc_os_version} " | cut -d '/' -f 1)
readarray -t bin_package_names_2 < <(apt list --installed 2>/dev/null | grep -Pv -- "$perl_regex_exclude_pattern" | grep -P -- "$perl_regex_include_pattern" | grep -P -- "-${grep_esc_os_version} " | cut -d '/' -f 1)
else
readarray -t bin_package_names_1 < <(apt list --installed 2>/dev/null | grep -P -- "$perl_regex_include_pattern" | grep -P -- "\+${grep_esc_os_version} " | cut -d '/' -f 1)
readarray -t bin_package_names_2 < <(apt list --installed 2>/dev/null | grep -P -- "$perl_regex_include_pattern" | grep -P -- "-${grep_esc_os_version} " | cut -d '/' -f 1)
fi
bin_package_names=("${bin_package_names_1[@]}" "${bin_package_names_2[@]}")
# Repacking in case the first array is empty
python_bin_package_names=("${python_bin_package_names[@]}")
;;
*)
echo -e ''$_{1..180}'\b-'
echo Unknown versions group $versions_group
echo -e ''$_{1..180}'\b-'
exit 1
;;
esac
# Sorting the array
if [[ ${#bin_package_names[@]} -ne 0 ]]; then
sorted_bin_package_names=()
IFS=$'\n' sorted_bin_package_names=($(sort -uV <<<"${bin_package_names[*]}"))
unset IFS
else
aplay /usr/share/sounds/purple/login.wav
exit 0
fi
downgraded_bin_package_names_versions=()
for ((s=0; s<${#sorted_bin_package_names[@]}; s++))
do
bin_package_family_names_versions=()
bin_package_name="${sorted_bin_package_names[${s}]}"
if [[ -z "$bin_package_name" ]]; then
continue
fi
bin_package_installed_version=$(apt-cache -q=0 2>&1 policy "$bin_package_name" | grep Installed | sed -E 's|^.*?: (.*)$|\1|g')
src_package_name=$(apt-get-src-package-name "$bin_package_name" "$bin_package_installed_version")
if [[ -z "$src_package_name" ]]; then
continue
fi
bin_package_installed_core_version=$(echo "$bin_package_installed_version" | sed -E "s|^(.*)[\+-]${os_version}$|\1|g")
grep_esc_bin_package_name="$(echo "$bin_package_name" | escape_grep_pcre_search_string)"
bin_package_official_candidate_version=$(apt-cache -q=0 2>&1 madison "$bin_package_name" | grep -P "$grep_esc_bin_package_name" | grep -P "$grep_esc_official_archives_uri" | grep amd64 | head -n 1 | cut -d '|' -f 2)
# Removing all spaces, tabs, line breaks
bin_package_official_candidate_version=${bin_package_official_candidate_version//[[:space:]]/}
if [[ -n "$bin_package_official_candidate_version" ]]; then
bin_package_family_names_versions=("${bin_package_name}@${bin_package_installed_version}@${bin_package_installed_core_version}@${bin_package_official_candidate_version}")
# Removing $bin_package_family_name from ${sorted_bin_package_names[@]} array
# Beware that the unset creates a gap in the array but modifies its size
unset "sorted_bin_package_names[${s}]"
# It also messes with the index if the removed cell index is lower than or equal to the current cell index
((s--))
# Rebuilding the array to fill the gap
sorted_bin_package_names=("${sorted_bin_package_names[@]}")
else
# This binary package is not available in the official repositories
continue
fi
# Searching for all installed binary packages which share the same source package as $bin_package_name
for ((index=0; index<${#sorted_bin_package_names[@]}; index++))
do
bin_package_name_2="${sorted_bin_package_names[${index}]}"
if [[ (-z "$bin_package_name_2") || ("$bin_package_name_2" == "$bin_package_name") ]]; then
continue
fi
bin_package_installed_version_2=$(apt-cache -q=0 2>&1 policy "$bin_package_name_2" | grep Installed | sed -E 's|^.*?: (.*)$|\1|g')
src_package_name_2=$(apt-get-src-package-name "$bin_package_name_2" "$bin_package_installed_version_2")
if [[ "$src_package_name_2" == "$src_package_name" ]]; then
# Removing $bin_package_family_name from ${sorted_bin_package_names[@]} array
# Beware that the unset creates a gap in the array but modifies its size
unset "sorted_bin_package_names[${index}]"
# It also messes with the index if the removed cell index is lower than or equal to the current cell index
((index--))
# Rebuilding the array to fill the gap
sorted_bin_package_names=("${sorted_bin_package_names[@]}")
bin_package_installed_core_version_2=$(echo "$bin_package_installed_version_2" | sed -E "s|^(.*)[\+-]${os_version}$|\1|g")
grep_esc_bin_package_name_2="$(echo "$bin_package_name_2" | escape_grep_pcre_search_string)"
bin_package_official_candidate_version_2=$(apt-cache -q=0 2>&1 madison "$bin_package_name_2" | grep -P "$grep_esc_bin_package_name_2" | grep -P "$grep_esc_official_archives_uri" | grep amd64 | head -n 1 | cut -d '|' -f 2)
# Removing all spaces, tabs, line breaks
bin_package_official_candidate_version_2=${bin_package_official_candidate_version_2//[[:space:]]/}
if [[ -n "$bin_package_official_candidate_version_2" ]]; then
bin_package_family_names_versions=("${bin_package_family_names_versions[@]}" "${bin_package_name_2}@${bin_package_installed_version_2}@${bin_package_installed_core_version_2}@${bin_package_official_candidate_version_2}")
else
# This binary package is not available in the official repositories
continue
fi
fi
done
unset donwgrade_group
# Building the $donwgrade_group with all the binary packages from the same source family and their official candidate version
for bin_package_family_name_versions in "${bin_package_family_names_versions[@]}"
do
bin_package_family_name=$(echo "$bin_package_family_name_versions" | cut -d '@' -f 1)
bin_package_family_name_official_candidate_version=$(echo "$bin_package_family_name_versions" | cut -d '@' -f 4)
case "$mode" in
light)
bin_package_family_name_core_version=$(echo "$bin_package_family_name_versions" | cut -d '@' -f 3)
if [[ "$bin_package_family_name_core_version" == "$bin_package_family_name_official_candidate_version" ]]; then
if [[ -z "$donwgrade_group" ]]; then
donwgrade_group="${bin_package_family_name}=${bin_package_family_name_official_candidate_version}"
else
donwgrade_group="${donwgrade_group} ${bin_package_family_name}=${bin_package_family_name_official_candidate_version}"
fi
fi
;;
full)
if [[ -n "$bin_package_family_name_official_candidate_version" ]]; then
if [[ -z "$donwgrade_group" ]]; then
donwgrade_group="${bin_package_family_name}=${bin_package_family_name_official_candidate_version}"
else
donwgrade_group="${donwgrade_group} ${bin_package_family_name}=${bin_package_family_name_official_candidate_version}"
fi
fi
;;
*)
printf "\n"
echo -e ''$_{1..180}'\b-'
echo "Unknown mode: $mode"
echo -e ''$_{1..180}'\b-'
exit 1
;;
esac
done
# Downgrading all the binary packages from the same source family
if [[ -n $donwgrade_group ]]; then
printf "\n"
echo -e ''$_{1..180}'\b='
echo "Downgrading all installed binary packages from source package: $src_package_name"
echo "to: $donwgrade_group"
echo -e ''$_{1..180}'\b='
case $auto in
true|yes)
dpkg-lock.sh apt --show-progress --assume-yes --allow-change-held-packages --allow-downgrades install $donwgrade_group
return_code=$?
if [[ $return_code -eq 0 ]]; then
downgraded_bin_package_names_versions=("${downgraded_bin_package_names_versions[@]}" "${bin_package_family_names_versions[@]}")
else
aplay /usr/share/emacs/site-lisp/emacspeak/sounds/classic/alert-user.wav
exit 1
fi
;;
false|no)
dpkg-lock.sh apt --show-progress --allow-change-held-packages --allow-downgrades install $donwgrade_group
return_code=$?
if [[ $return_code -eq 0 ]]; then
downgraded_bin_package_names_versions=("${downgraded_bin_package_names_versions[@]}" "${bin_package_family_names_versions[@]}")
elif [[ $return_code -ne 1 ]]; then
aplay /usr/share/emacs/site-lisp/emacspeak/sounds/classic/alert-user.wav
exit 1
fi
;;
*)
printf "\n"
echo -e ''$_{1..180}'\b-'
echo "Unknown auto: $auto"
echo -e ''$_{1..180}'\b-'
exit 1
;;
esac
fi
done
if [[ ${#downgraded_bin_package_names_versions[@]} -ne 0 ]]; then
printf "\n"
echo -e ''$_{1..180}'\b='
echo "The following binary packages have been downgraded from their installed version to their official candidate version:"
echo -e ''$_{1..180}'\b='
echo "bin_package_name <--> bin_package_installed_version <--> bin_package_installed_core_version <--> bin_package_official_candidate_version "
echo -e ''$_{1..180}'\b='
printf "%s\n" "${downgraded_bin_package_names_versions[@]}" | sort -uV | sed -E 's|@| <--> |g'
fi
aplay /usr/share/sounds/purple/login.wav
exit 0
|