Created
July 5, 2021 13:14
-
-
Save hsntgm/a34bab43fd22d7e50e87b42b761a5adb to your computer and use it in GitHub Desktop.
Handy gentoo auto update cron script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# eix depended handy gentoo update cron script. | |
# * Prioritize portage upgrade if available. | |
# * Runs perl-cleaner for perl upgrade process. | |
# * Warns for reboot if pam upgraded. | |
# * Sends upgrade summary via mail. | |
#====================================================================================== | |
# send mail configuration: | |
# If you want to recieve script output as a mail please set 'send_mail' to 1. Default 0. | |
# You need to have properly configured mail server with 'mail' command. | |
# If you use 'sendmail' you have to edit script otherwise script cannot send mail. | |
send_mail=0 | |
# Adjust mail options: | |
mail_to="[email protected]" | |
mail_from="From: Gentoo System <[email protected]>" | |
mail_subject="Gentoo Update Informations" | |
# depclean configuration: | |
# Set depclean for excluding packages. In default gentoo-sources excluded. | |
exclude="--exclude gentoo-sources" | |
# etc-update configuration: | |
# If you want to update config files after update please set to -3. | |
# If you want to keep config files same and discard all updates set to -7. | |
# If you want to exit without doing anything set to -1. Default -1 | |
keep_config="-1" | |
#====================================================================================== | |
# Execution start time | |
ext_s=`date +%s` | |
converter() { | |
h=$(bc <<< "${1}/3600") | |
m=$(bc <<< "(${1}%3600)/60") | |
s=$(bc <<< "${1}%60") | |
printf "%02d:%02d:%05.2f\n" $h $m $s | |
} | |
# Check 'eix' is installed | |
if ! command -v eix > /dev/null 2>&1; then | |
echo -e "You need to install necessary package first!" | |
echo -e "emerge -a app-portage/eix" | |
exit 1 | |
fi | |
# Check is system able to send mail with 'mail' command | |
if ! command -v mail > /dev/null 2>&1; then | |
send_mail=0 | |
elif [[ "$send_mail" -eq 1 ]]; then | |
tfile="/tmp/$$.$RANDOM.gentoo" | |
fi | |
# Sync packages and update db | |
echo "Syncing.." | |
eix-sync -q && updatedb | |
if eix --installed --upgrade | grep -q "[U]"; then | |
echo "Updating gentoo.." | |
if [[ "$send_mail" -eq 1 ]]; then | |
# Get the list of upgradeable packages & send output to file | |
echo "The following packages updated:" > "$tfile" | |
echo "------------------------------------------------------" >> "$tfile" | |
eix --installed --upgrade | grep -w "[U]" &>> "$tfile" | |
echo "------------------------------------------------------" >> "$tfile" | |
fi | |
# Check portage needs to be upgraded first | |
if eix --installed --upgrade | grep -q "sys-apps/portage"; then | |
emerge --ask=n -u1 --quiet --quiet-build sys-apps/portage | |
if [[ "$send_mail" -eq 1 ]]; then | |
echo "Portage Updated = Yes" >> "$tfile" | |
fi | |
elif [[ "$send_mail" -eq 1 ]]; then | |
echo "Portage Updated = No" >> "$tfile" | |
fi | |
# Check perl will be upgraded too | |
if eix --installed --upgrade | grep -q "dev-lang/perl"; then | |
perl_cleaner=1 | |
if [[ "$send_mail" -eq 1 ]]; then | |
echo "Perl Updated = Yes" >> "$tfile" | |
fi | |
elif [[ "$send_mail" -eq 1 ]]; then | |
echo "Perl Updated = No" >> "$tfile" | |
fi | |
# Check pam will be upgraded too, it needs reboot | |
if eix --installed --upgrade | grep -q "sys-libs/pam"; then | |
if [[ "$send_mail" -eq 1 ]]; then | |
echo "Pam Updated = Yes --> REBOOT REQUIRED" >> "$tfile" | |
fi | |
elif [[ "$send_mail" -eq 1 ]]; then | |
echo "Pam Updated = No" >> "$tfile" | |
fi | |
# Start the upgrade process | |
emerge --ask=n --update --deep --changed-use --quiet --quiet-build --quiet-fail @world && | |
eselect news read > /dev/null 2>&1 && | |
emerge @preserved-rebuild --quiet && | |
revdep-rebuild -q && | |
# Start cleaning process | |
emerge --depclean $exclude --quiet && | |
eclean -d distfiles > /dev/null 2>&1 | |
# Complete perl related processes | |
if [[ -n "$perl_cleaner" ]]; then | |
emerge --ask=n -uDN --complete-graph=y --with-bdeps=y --backtrack=100 --autounmask-keep-masks=y --quiet --quiet-build --quiet-fail @world && | |
perl-cleaner --all -q && | |
emerge @preserved-rebuild --quiet && | |
revdep-rebuild -q && | |
# Start cleaning process | |
emerge --depclean $exclude --quiet && | |
eclean -d distfiles > /dev/null 2>&1 | |
fi | |
# Finish the update process, set configs & env | |
echo "$keep_config" | etc-update && | |
updatedb && | |
env-update > /dev/null 2>&1 && | |
source /etc/profile && | |
echo "Gentoo updated.." | |
# Execution end time | |
ext_f=`date +%s` | |
# Calculate execution runtime | |
runtime=$((ext_f-ext_s)) | |
# Send mail | |
if [[ "$send_mail" -eq 1 ]]; then | |
echo "------------------------------------------------------" >> "$tfile" | |
echo "The update process took $(converter $runtime)." >> "$tfile" | |
cat "$tfile" | mail -s "$mail_subject" -a "$mail_from" "$mail_to" | |
trap "{ rm -f $tfile; }" EXIT | |
fi | |
else | |
echo "There is no update currently.." | |
if [[ "$send_mail" -eq 1 ]]; then | |
echo "There is no update currently.." | mail -s "$mail_subject" -a "$mail_from" "$mail_to" | |
trap "{ rm -f $tfile; }" EXIT | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment