bash-snippets/aptupdater.sh

180 lines
5.9 KiB
Bash

#!/bin/bash
tryUpdateFromSd()
{
local sd_name=$1
# cut received line by / and take the third found result. (for /dev/sdb1 it's sdb1)
local sd_folder_name=$(echo "${sd_name}" | cut -f 3 -d '/')
exit_condition=false
while [ "$exit_condition" == false ]
do
echo "Do you want to update from ${sd_name}? (Y/n)"
read input_char
if [ "$input_char" == "Y" ] || [ "$input_char" == "y" ]
then
echo " - Create /media/iso-$sd_folder_name"
mkdir /media/iso-$sd_folder_name
echo " - Mount ${sd_name} in /media/iso-$sd_folder_name"
mount $sd_name /media/iso-$sd_folder_name
echo " - Update the package manager."
cp /etc/apt/sources.list /etc/apt/sources-backup.list
mkdir /home/temp-apt
mv /etc/apt/sources.list.d/* /home/temp-apt/
echo "deb [trusted=yes] file:/media/iso-$sd_folder_name orel main contrib" > /etc/apt/sources.list
echo " - Update packages."
apt update
apt upgrade
echo " - Unmount ${sd_name}"
umount /media/iso-$sd_folder_name
rm -rf /media/iso-$sd_folder_name
echo " - Restore the package manager."
mv /etc/apt/sources-backup.list /etc/apt/sources.list
mv /home/temp-apt/* /etc/apt/sources.list.d/
rm -rf /home/temp-apt/
exit_condition=true
exit 0
elif [ "$input_char" == "N" ] || [ "$input_char" == "n" ]
then
echo "Skip "${sd_name}""
exit_condition=true
else
echo "Please, type Y to agree or N to decline."
echo "Input Ctrl+C to exit."
fi
done
}
tryUpdateFromDrive()
{
# Show devices with fdisk -l
# Filter all lines which contain "sd*" (where * -- any latin letter from B to Z) except for "sd*:"
# cut by space character and take the first found result
# so it is: /dev/sdb1 , for example
sd_arr_raw=$(fdisk -l | egrep "sd[b-zB-Z][^:]" | cut -f 1 -d ' ')
amount_of_sd=0
for sd_raw in $sd_arr_raw
do
amount_of_sd=$(($amount_of_sd+1))
echo "${sd_raw}; ID=${amount_of_sd}"
done
if (( $amount_of_sd < 1 ))
then
echo "Couldn't find external drives. Please, make sure they are connected and available."
exit 1
else
for sd_raw in $sd_arr_raw
do
tryUpdateFromSd "${sd_raw}"
done
exot 0
fi
}
tryUpdateFromFile()
{
exit_condition=false
while [ "$exit_condition" == false ]
do
echo "Do you want to update from ${filename}? (Y/n)"
read input_char
if [ "$input_char" == "y" ] || [ "$input_char" == "Y" ]
then
echo " - Create /media/iso-$filename"
mkdir /media/iso-$filename
echo " - Mount ${filename} in /media/iso-$filename"
mount $filename /media/iso-$filename
echo " - Update the package manager."
cp /etc/apt/sources.list /etc/apt/sources-backup.list
mkdir /home/temp-apt
mv /etc/apt/sources.list.d/* /home/temp-apt/
echo "deb [trusted=yes] file:/media/iso-$filename orel main contrib" > /etc/apt/sources.list
echo " - Update packages."
apt update
apt upgrade
echo " - Unmount ${filename}"
umount /media/iso-$filename
rm -rf /media/iso-$filename
echo " - Restore the package manager."
mv /etc/apt/sources-backup.list /etc/apt/sources.list
mv /home/temp-apt/* /etc/apt/sources.list.d/
rm -rf /home/temp-apt/
exit_condition=true
updated=true
exit 0
elif [ "$input_char" == "n" ] || [ "$input_char" == "N" ]
then
exit_condition=true
exit 0
else
echo "Please, type Y to agree or N to decline."
echo "Input Ctrl+C to exit."
fi
done
}
printHelp()
{
echo "iso-updater - tool for updating OS packages by apt."
echo " "
echo "USAGE: iso-updater [-OPTIONS...] [ARGS]"
echo " "
echo "OPTIONS:"
echo "-h, --help Show brief instructions."
echo " "
echo "-d, --drive Update the system from drive."
echo " Make sure there is one and only one external drive "
echo " connected. The program will read its partisions and "
echo " try to upgrade the system from each of them."
echo " Thoroughly follow all the instructions after launch."
echo " "
echo "-f, --file [PATH] Update the system from an .iso file."
echo " Make sure there is an .iso file needed to update "
echo " the system from and that the file is being placed "
echo " exactly at PATH. Thoroughly follow all "
echo " the instructions after launch."
echo " "
echo "Example: sudo iso-updater --file /home/user/iso/very-very-cool-iso-file.iso"
echo " "
echo " "
exit 0
}
while test $# -gt 0; do
case "$1" in
-h|--help)
printHelp
;;
-d|--drive)
if [[ $UID != 0 ]]; then
echo "Please, run with sudo:"
echo "sudo $0 $*"
exit 1
fi
tryUpdateFromDrive
exit 0
;;
-f|--file)
if [[ $UID != 0 ]]; then
echo "Please, run with sudo:"
echo "sudo $0 $*"
exit 1
fi
shift
filename=$1
if [ ! -f "$filename" ]
then
echo "Couldn't find $filename. Please, use correct filename and make sure it does exist."
exit 1
fi
tryUpdateFromFile
exit 0
;;
esac
echo "Couldn't recognize $1."
echo " "
shift
done
printHelp
exit 0