フォーク元 ZeTian/do_anduinos_distupgrade.sh

最終更新 2 weeks ago

修正履歴 2a5db433115cb47f03e0831b8ba22bd76c7e4762

do_anduinos_distupgrade.sh Raw
1#!/bin/bash
2
3#=================================================
4# AnduinOS Upgrade Script
5#=================================================
6# This script upgrades AnduinOS from 1.3.7 (plucky)
7# to 1.4.1 (questing).
8#
9# Usage:
10# ./do_anduinos_distupgrade.sh
11# (Script will auto-elevate to root/sudo)
12#=================================================
13
14set -e
15set -o pipefail
16set -u
17
18Green="\033[32m"
19Red="\033[31m"
20Yellow="\033[33m"
21Blue="\033[36m"
22Font="\033[0m"
23OK="${Green}[ OK ]${Font}"
24ERROR="${Red}[FAILED]${Font}"
25WARNING="${Yellow}[ WARN ]${Font}"
26
27# Backup Configuration
28BACKUP_ROOT="/var/backups/anduinos-upgrade"
29BACKUP_DIR="$BACKUP_ROOT/backup_$(date +%Y%m%d_%H%M%S)"
30PPA_BACKUP_DIR="$BACKUP_DIR/ppa"
31UBUNTU_SOURCE_BACKUP="$BACKUP_DIR/ubuntu_sources"
32
33# --- Helper Functions ---
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
47# --- Privilege Check ---
48
49function ensure_root() {
50 if [ "$EUID" -ne 0 ]; then
51 print_warn "This script requires root privileges."
52 print_ok "Attempting to escalate privileges via sudo..."
53
54 # Check if sudo is available
55 if ! command -v sudo &> /dev/null; then
56 print_error "sudo is not installed. Please run this script as root."
57 exit 1
58 fi
59
60 # Re-execute the script with sudo, preserving arguments
61 # exec replaces the current process, so the script runs anew as root
62 exec sudo "$0" "$@"
63 exit 0
64 fi
65 # If we are here, we are root (or sudo-ed successfully)
66}
67
68# --- Core Logic ---
69
70function rollback_on_error() {
71 print_error "An error occurred during the upgrade process"
72 print_warn "Starting rollback procedure..."
73
74 # Restore ubuntu.sources if backup exists
75 if [ -f "$UBUNTU_SOURCE_BACKUP/ubuntu.sources" ]; then
76 print_ok "Restoring ubuntu.sources..."
77 cp "$UBUNTU_SOURCE_BACKUP/ubuntu.sources" /etc/apt/sources.list.d/
78 print_ok "Restored ubuntu.sources"
79 fi
80
81 # Restore sources.list if backup exists
82 if [ -f "$UBUNTU_SOURCE_BACKUP/sources.list" ]; then
83 print_ok "Restoring sources.list..."
84 cp "$UBUNTU_SOURCE_BACKUP/sources.list" /etc/apt/
85 print_ok "Restored sources.list"
86 fi
87
88 # Restore PPA sources
89 if [ -d "$PPA_BACKUP_DIR" ]; then
90 ppa_count=$(ls -1 "$PPA_BACKUP_DIR" 2>/dev/null | wc -l)
91 if [ "$ppa_count" -gt 0 ]; then
92 print_ok "Restoring PPA sources..."
93 for file in "$PPA_BACKUP_DIR"/*; do
94 if [ -f "$file" ]; then
95 cp "$file" /etc/apt/sources.list.d/
96 print_ok "Restored $(basename "$file")"
97 fi
98 done
99 fi
100 fi
101
102 # Remove temporary apt configuration if exists
103 if [ -f "/etc/apt/apt.conf.d/99-local-versions" ]; then
104 rm -f /etc/apt/apt.conf.d/99-local-versions
105 fi
106
107 print_ok "Running apt update to restore repository state..."
108 apt update || true
109
110 print_warn "Rollback completed. System restored to previous state."
111 print_warn "Backup preserved in: $BACKUP_DIR"
112 exit 1
113}
114
115function judge() {
116 if [[ 0 -eq $? ]]; then
117 print_ok "$1 succeeded"
118 sleep 0.2
119 else
120 print_error "$1 failed"
121 rollback_on_error
122 fi
123}
124
125function check_disk_space() {
126 print_ok "Checking available disk space..."
127
128 mkdir -p "$BACKUP_DIR"
129
130 local root_space=$(df / | awk 'NR==2 {print $4}')
131 local root_space_mb=$((root_space / 1024))
132 local required_space=2048
133
134 print_ok "Available space in /: ${root_space_mb}MB"
135 print_ok "Backup location: $BACKUP_DIR"
136
137 if [ "$root_space_mb" -lt "$required_space" ]; then
138 print_error "Insufficient disk space in /. Required: ${required_space}MB, Available: ${root_space_mb}MB"
139 exit 1
140 fi
141
142 print_ok "Disk space check passed"
143}
144
145function update_system() {
146 print_ok "Running apt update and upgrade..."
147 apt update
148 judge "apt update"
149 apt upgrade -y
150 judge "apt upgrade"
151}
152
153function backup_ubuntu_sources() {
154 print_ok "Backing up Ubuntu official sources..."
155 mkdir -p "$UBUNTU_SOURCE_BACKUP"
156
157 if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
158 cp /etc/apt/sources.list.d/ubuntu.sources "$UBUNTU_SOURCE_BACKUP/"
159 print_ok "Backed up ubuntu.sources"
160 fi
161
162 if [ -f "/etc/apt/sources.list" ] && [ -s "/etc/apt/sources.list" ]; then
163 cp /etc/apt/sources.list "$UBUNTU_SOURCE_BACKUP/"
164 print_ok "Backed up sources.list"
165 fi
166 judge "Backup Ubuntu sources"
167}
168
169function backup_and_remove_ppa() {
170 print_ok "Backing up and temporarily removing PPA sources..."
171 mkdir -p "$PPA_BACKUP_DIR"
172
173 if [ -d "/etc/apt/sources.list.d" ]; then
174 for file in /etc/apt/sources.list.d/*; do
175 if [ -f "$file" ] && [ "$(basename "$file")" != "ubuntu.sources" ]; then
176 mv "$file" "$PPA_BACKUP_DIR/"
177 print_ok "Moved $(basename "$file") to backup"
178 fi
179 done
180 fi
181 judge "Backup and remove PPA sources"
182}
183
184function detect_apt_format() {
185 print_ok "Detecting APT source format..."
186 if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
187 print_ok "Detected new DEB822 format"
188 return 0
189 elif [ -f "/etc/apt/sources.list" ] && [ -s "/etc/apt/sources.list" ]; then
190 print_ok "Detected old format (sources.list)"
191 return 1
192 else
193 print_error "Cannot detect APT source format"
194 rollback_on_error
195 fi
196}
197
198function convert_old_to_new_format() {
199 print_ok "Converting old format to new DEB822 format..."
200
201 cp /etc/apt/sources.list /etc/apt/sources.list.backup
202
203 MIRROR_URL=$(grep -E "^deb " /etc/apt/sources.list | grep -v "security" | head -1 | awk '{print $2}')
204 [ -z "$MIRROR_URL" ] && MIRROR_URL="http://archive.ubuntu.com/ubuntu/"
205
206 SECURITY_URL=$(grep -E "^deb " /etc/apt/sources.list | grep "security" | head -1 | awk '{print $2}')
207 [ -z "$SECURITY_URL" ] && SECURITY_URL="http://security.ubuntu.com/ubuntu/"
208
209 bash -c "cat > /etc/apt/sources.list.d/ubuntu.sources" <<EOF
210Types: deb
211URIs: ${MIRROR_URL}
212Suites: plucky plucky-updates plucky-backports
213Components: main restricted universe multiverse
214Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
215
216Types: deb
217URIs: ${SECURITY_URL}
218Suites: plucky-security
219Components: main restricted universe multiverse
220Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
221EOF
222
223 bash -c 'echo "# This file is deprecated. See /etc/apt/sources.list.d/ubuntu.sources" > /etc/apt/sources.list'
224 judge "Convert to new format"
225}
226
227function replace_plucky_with_questing() {
228 print_ok "Replacing plucky with questing..."
229 sed -i 's/plucky/questing/g' /etc/apt/sources.list.d/ubuntu.sources
230 judge "Replace plucky with questing"
231
232 print_ok "Updating package lists..."
233 apt update
234 judge "apt update with questing"
235}
236
237function install_coreutils_uutils() {
238 print_ok "Installing coreutils-from-uutils..."
239 apt install -y coreutils-from-uutils
240 judge "Install coreutils-from-uutils"
241}
242
243function run_dist_upgrade() {
244 print_ok "Simulating upgrade..."
245 apt -s dist-upgrade > /dev/null
246 judge "apt simulation"
247
248 print_ok "Running full distribution upgrade..."
249 export DEBIAN_FRONTEND=noninteractive
250
251 bash -c 'cat > /etc/apt/apt.conf.d/99-local-versions <<EOF
252Dpkg::Options {
253 "--force-confdef";
254 "--force-confold";
255}
256EOF'
257
258 bash -c 'DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none \
259 apt-get -y dist-upgrade \
260 -o Dpkg::Options::="--force-confdef" \
261 -o Dpkg::Options::="--force-confold"'
262
263 judge "apt dist-upgrade"
264 rm -f /etc/apt/apt.conf.d/99-local-versions
265 unset DEBIAN_FRONTEND
266}
267
268function update_release_files() {
269 print_ok "Updating release info..."
270
271 if [ -f "/etc/os-release" ]; then
272 bash -c "cat > /etc/os-release" <<EOF
273PRETTY_NAME="AnduinOS 1.4.1"
274NAME="AnduinOS"
275VERSION_ID="1.4.1"
276VERSION="1.4.1 (questing)"
277VERSION_CODENAME=questing
278ID=ubuntu
279ID_LIKE=debian
280HOME_URL="https://www.anduinos.com/"
281SUPPORT_URL="https://github.com/Anduin2017/AnduinOS/discussions"
282BUG_REPORT_URL="https://github.com/Anduin2017/AnduinOS/issues"
283PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
284UBUNTU_CODENAME=questing
285EOF
286 fi
287
288 if [ -f "/etc/lsb-release" ]; then
289 bash -c "cat > /etc/lsb-release" <<EOF
290DISTRIB_ID=AnduinOS
291DISTRIB_RELEASE=1.4.1
292DISTRIB_CODENAME=questing
293DISTRIB_DESCRIPTION="AnduinOS 1.4.1"
294EOF
295 fi
296 judge "Update release files"
297}
298
299function restore_ppa_sources() {
300 print_ok "Restoring PPA sources..."
301 if [ -d "$PPA_BACKUP_DIR" ] && [ "$(ls -A $PPA_BACKUP_DIR)" ]; then
302 mv "$PPA_BACKUP_DIR"/* /etc/apt/sources.list.d/
303 print_ok "Restored PPAs"
304 apt update
305 judge "Update after PPA restore"
306 else
307 print_ok "No PPA sources to restore"
308 fi
309}
310
311function main() {
312 # 1. Ensure we are root first
313 ensure_root
314
315 print_ok "Starting AnduinOS upgrade process..."
316 echo -e "${Yellow}WARNING: Upgrading from 1.3.7 (plucky) to 1.4.1 (questing).${Font}"
317
318 # Since we are likely root now, we check if the script is running interactively before asking
319 # If we sudo-ed, we might have lost some TTY context, but usually 'read' works fine.
320 if [ -t 0 ]; then
321 read -p "Do you want to continue? (y/N): " confirm
322 if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
323 print_error "Aborted by user."
324 exit 1
325 fi
326 fi
327
328 check_disk_space
329 update_system
330 backup_ubuntu_sources
331 backup_and_remove_ppa
332
333 if ! detect_apt_format; then
334 convert_old_to_new_format
335 fi
336
337 replace_plucky_with_questing
338 install_coreutils_uutils
339 run_dist_upgrade
340 update_release_files
341 restore_ppa_sources
342
343 print_ok "Upgrade completed successfully!"
344 print_ok "System is now AnduinOS 1.4.1 (questing)"
345 print_ok "Backups: $BACKUP_DIR"
346 print_warn "Please reboot your system."
347}
348
349main