Dernière activité 2 weeks ago

Révision cf7488cf8a5d6cca83794c40d9e0e83a6dc0b15b

do_anduinos_distupgrade.sh Brut
1#!/bin/bash
2
3#=================================================
4# AnduinOS Upgrade Script
5#=================================================
6# This script upgrades AnduinOS from 1.3.7 (plucky)
7# to 1.4.0 (questing).
8#
9# Do NOT run this script as root. Run it as a normal
10# user with sudo privileges.
11#
12# Example:
13# bash ./upgrade.sh
14#=================================================
15
16set -e
17set -o pipefail
18set -u
19
20Green="\033[32m"
21Red="\033[31m"
22Yellow="\033[33m"
23Blue="\033[36m"
24Font="\033[0m"
25GreenBG="\033[42;37m"
26RedBG="\033[41;37m"
27OK="${Green}[ OK ]${Font}"
28ERROR="${Red}[FAILED]${Font}"
29WARNING="${Yellow}[ WARN ]${Font}"
30
31BACKUP_DIR="/tmp/anduinos_upgrade_backup_$(date +%Y%m%d_%H%M%S)"
32PPA_BACKUP_DIR="$BACKUP_DIR/ppa"
33UBUNTU_SOURCE_BACKUP="$BACKUP_DIR/ubuntu_sources"
34
35function print_ok() {
36 echo -e "${OK} ${Blue} $1 ${Font}"
37}
38
39function print_error() {
40 echo -e "${ERROR} ${Red} $1 ${Font}"
41}
42
43function print_warn() {
44 echo -e "${WARNING} ${Yellow} $1 ${Font}"
45}
46
47function judge() {
48 if [[ 0 -eq $? ]]; then
49 print_ok "$1 succeeded"
50 sleep 0.2
51 else
52 print_error "$1 failed"
53 exit 1
54 fi
55}
56
57function update_system() {
58 print_ok "Running apt update and upgrade..."
59 sudo apt update
60 judge "apt update"
61 sudo apt upgrade -y
62 judge "apt upgrade"
63}
64
65function backup_ubuntu_sources() {
66 print_ok "Backing up Ubuntu official sources..."
67
68 mkdir -p "$UBUNTU_SOURCE_BACKUP"
69
70 # Backup ubuntu.sources if exists
71 if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
72 sudo cp /etc/apt/sources.list.d/ubuntu.sources "$UBUNTU_SOURCE_BACKUP/"
73 print_ok "Backed up ubuntu.sources"
74 fi
75
76 # Backup sources.list if it exists and is not empty
77 if [ -f "/etc/apt/sources.list" ] && [ -s "/etc/apt/sources.list" ]; then
78 sudo cp /etc/apt/sources.list "$UBUNTU_SOURCE_BACKUP/"
79 print_ok "Backed up sources.list"
80 fi
81
82 judge "Backup Ubuntu sources"
83}
84
85function backup_and_remove_ppa() {
86 print_ok "Backing up and temporarily removing PPA sources..."
87
88 mkdir -p "$PPA_BACKUP_DIR"
89
90 # Move all files in /etc/apt/sources.list.d/ except ubuntu.sources
91 if [ -d "/etc/apt/sources.list.d" ]; then
92 for file in /etc/apt/sources.list.d/*; do
93 if [ -f "$file" ] && [ "$(basename "$file")" != "ubuntu.sources" ]; then
94 sudo mv "$file" "$PPA_BACKUP_DIR/"
95 print_ok "Moved $(basename "$file") to backup"
96 fi
97 done
98 fi
99
100 print_ok "PPA sources moved to: $PPA_BACKUP_DIR"
101 judge "Backup and remove PPA sources"
102}
103
104function detect_apt_format() {
105 print_ok "Detecting APT source format..."
106
107 if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
108 print_ok "Detected new DEB822 format (ubuntu.sources)"
109 return 0
110 elif [ -f "/etc/apt/sources.list" ] && [ -s "/etc/apt/sources.list" ]; then
111 print_ok "Detected old format (sources.list)"
112 return 1
113 else
114 print_error "Cannot detect APT source format"
115 exit 1
116 fi
117}
118
119function convert_old_to_new_format() {
120 print_ok "Converting old format to new DEB822 format..."
121
122 if [ ! -f "/etc/apt/sources.list" ] || [ ! -s "/etc/apt/sources.list" ]; then
123 print_error "/etc/apt/sources.list not found or empty"
124 exit 1
125 fi
126
127 # Backup the old sources.list
128 sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
129
130 # Extract mirror URLs from existing sources.list
131 # Try to detect main archive mirror
132 MIRROR_URL=$(grep -E "^deb " /etc/apt/sources.list | grep -v "security" | head -1 | awk '{print $2}')
133 if [ -z "$MIRROR_URL" ]; then
134 MIRROR_URL="http://archive.ubuntu.com/ubuntu/"
135 fi
136
137 # Try to detect security mirror
138 SECURITY_URL=$(grep -E "^deb " /etc/apt/sources.list | grep "security" | head -1 | awk '{print $2}')
139 if [ -z "$SECURITY_URL" ]; then
140 SECURITY_URL="http://security.ubuntu.com/ubuntu/"
141 fi
142
143 print_ok "Detected mirror URL: $MIRROR_URL"
144 print_ok "Detected security URL: $SECURITY_URL"
145
146 # Create new ubuntu.sources file using detected mirrors
147 sudo bash -c "cat > /etc/apt/sources.list.d/ubuntu.sources" <<EOF
148Types: deb
149URIs: ${MIRROR_URL}
150Suites: plucky plucky-updates plucky-backports
151Components: main restricted universe multiverse
152Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
153
154Types: deb
155URIs: ${SECURITY_URL}
156Suites: plucky-security
157Components: main restricted universe multiverse
158Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
159EOF
160
161 # Clear the old sources.list
162 sudo bash -c 'echo "# This file is deprecated. See /etc/apt/sources.list.d/ubuntu.sources" > /etc/apt/sources.list'
163
164 judge "Convert to new format"
165}
166
167function replace_plucky_with_questing() {
168 print_ok "Replacing plucky with questing in ubuntu.sources..."
169
170 if [ ! -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
171 print_error "/etc/apt/sources.list.d/ubuntu.sources not found"
172 exit 1
173 fi
174
175 sudo sed -i 's/plucky/questing/g' /etc/apt/sources.list.d/ubuntu.sources
176 judge "Replace plucky with questing"
177
178 print_ok "Running apt update with questing repositories..."
179 sudo apt update
180 judge "apt update with questing"
181}
182
183function install_coreutils_uutils() {
184 print_ok "Installing coreutils-from-uutils..."
185
186 sudo apt install -y coreutils-from-uutils
187 judge "Install coreutils-from-uutils"
188}
189
190function run_dist_upgrade() {
191 print_ok "Running apt dist-upgrade in non-interactive mode..."
192
193 # Set environment for non-interactive mode
194 export DEBIAN_FRONTEND=noninteractive
195
196 # Configure dpkg to keep local versions by default
197 sudo bash -c 'cat > /etc/apt/apt.conf.d/99-local-versions <<EOF
198Dpkg::Options {
199 "--force-confdef";
200 "--force-confold";
201}
202EOF'
203
204 # Run dist-upgrade
205 # sudo apt dist-upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"
206 sudo bash -c 'DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none \
207 apt-get -y dist-upgrade \
208 -o Dpkg::Options::="--force-confdef" \
209 -o Dpkg::Options::="--force-confold"'
210 judge "apt dist-upgrade"
211
212 # Remove temporary configuration
213 sudo rm -f /etc/apt/apt.conf.d/99-local-versions
214
215 unset DEBIAN_FRONTEND
216}
217
218function update_release_files() {
219 print_ok "Updating release information files..."
220
221 # Update /etc/os-release
222 if [ -f "/etc/os-release" ]; then
223 print_ok "Updating /etc/os-release..."
224 sudo sed -i 's/UBUNTU_CODENAME=plucky/UBUNTU_CODENAME=questing/g' /etc/os-release
225 sudo sed -i 's/VERSION_CODENAME=plucky/VERSION_CODENAME=questing/g' /etc/os-release
226 sudo sed -i 's/VERSION_ID="1\.3\.7"/VERSION_ID="1.4.0"/g' /etc/os-release
227 sudo sed -i 's/VERSION="1\.3\.7/VERSION="1.4.0/g' /etc/os-release
228 sudo sed -i 's/PRETTY_NAME="AnduinOS 1\.3\.7"/PRETTY_NAME="AnduinOS 1.4.0"/g' /etc/os-release
229 judge "Update /etc/os-release"
230 fi
231
232 # Update /etc/lsb-release
233 if [ -f "/etc/lsb-release" ]; then
234 print_ok "Updating /etc/lsb-release..."
235 sudo sed -i 's/DISTRIB_RELEASE=1\.3\.7/DISTRIB_RELEASE=1.4.0/g' /etc/lsb-release
236 sudo sed -i 's/DISTRIB_CODENAME=plucky/DISTRIB_CODENAME=questing/g' /etc/lsb-release
237 sudo sed -i 's/DISTRIB_DESCRIPTION="AnduinOS 1\.3\.7"/DISTRIB_DESCRIPTION="AnduinOS 1.4.0"/g' /etc/lsb-release
238 judge "Update /etc/lsb-release"
239 fi
240
241 print_ok "Release files updated successfully"
242}
243
244function restore_ppa_sources() {
245 print_ok "Restoring PPA sources..."
246
247 if [ -d "$PPA_BACKUP_DIR" ]; then
248 ppa_count=$(ls -1 "$PPA_BACKUP_DIR" 2>/dev/null | wc -l)
249
250 if [ "$ppa_count" -gt 0 ]; then
251 for file in "$PPA_BACKUP_DIR"/*; do
252 if [ -f "$file" ]; then
253 sudo mv "$file" /etc/apt/sources.list.d/
254 print_ok "Restored $(basename "$file")"
255 fi
256 done
257
258 print_ok "Running apt update with restored PPAs..."
259 sudo apt update
260 judge "Restore PPA sources and update"
261 else
262 print_ok "No PPA sources to restore"
263 fi
264 else
265 print_warn "PPA backup directory not found, skipping restore"
266 fi
267}
268
269function main() {
270 print_ok "Starting AnduinOS upgrade process..."
271
272 echo -e "${Yellow}WARNING: This script will upgrade your system from 1.3.7 (plucky) to 1.4.0 (questing).${Font}"
273 echo -e "${Yellow}Please ensure you have backed up important data before proceeding.${Font}"
274 read -p "Do you want to continue? (y/N): " confirm
275 if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
276 print_error "Upgrade process aborted by user."
277 exit 1
278 fi
279
280 # Check if running as root
281 if [[ "$(id -u)" -eq 0 ]]; then
282 print_error "This script must not be run as root. Please run as a normal user with sudo privileges."
283 exit 1
284 fi
285
286 # Update current system
287 update_system
288
289 # Backup Ubuntu official sources
290 backup_ubuntu_sources
291
292 # Backup and remove PPA sources
293 backup_and_remove_ppa
294
295 # Detect and convert APT format if needed
296 if ! detect_apt_format; then
297 convert_old_to_new_format
298 fi
299
300 # Replace plucky with questing
301 replace_plucky_with_questing
302
303 # Install coreutils-from-uutils
304 install_coreutils_uutils
305
306 # Run dist-upgrade
307 run_dist_upgrade
308
309 # Update release files
310 update_release_files
311
312 # Restore PPA sources
313 restore_ppa_sources
314
315 print_ok "Upgrade completed successfully!"
316 print_ok "Your system has been upgraded to AnduinOS 1.4.0 (questing)"
317 print_ok "Backup files are stored in: $BACKUP_DIR"
318 print_warn "Please reboot your system to complete the upgrade."
319}
320
321main
322