0

一般来说,我可以每天从 cron 或手动完全更新我的所有系统。我在下面的目标是为 CentOS 或 Ubuntu(理想情况下也在 Alpine 和 SLES 等)提供跨平台代码来执行此操作。下面的很大一部分来自其他地方(找不到哪里),但它被打破了,我不得不调整一些位以使其正常工作。

  1. 这样做的想法是仅在经过一段时间(此处为 24 小时)后才执行更新 - 我是否正在检查/var/cache/aptDebian/Ubuntu 和/var/cache/dnf/expired_repos.jsonRHEL/Fedora/CentOS 的正确文件?我不完全确定,因为有时我认为应该触发更新,所以我认为其他东西正在更新这些文件,所以我很好奇是否有更好的文件可以定位?

  2. 您是否会说我正在捕获您希望完全更新系统的所有内容(在更新/修复损坏/升级方面),并且我是否按照正确的顺序进行操作?我我已经被 Ubuntu 覆盖了,但也许我错过了 CentOS 的东西?

  3. 我也特别想使它与 Alpine 兼容,因为这是我学习 docker 等的首选,所以希望在我弄乱我的 Alpine WSL 实例时无缝更新那些(SLES 少,但会很高兴拥有),即完全更新Alpine的正确方法是什么,我应该测试什么文件来检查该发行版中的更新时间?

MANAGER=
type apt    &> /dev/null && MANAGER=apt    && DISTRO="Debian/Ubuntu"
type yum    &> /dev/null && MANAGER=yum    && DISTRO="RHEL/Fedora/CentOS"
type dnf    &> /dev/null && MANAGER=dnf    && DISTRO="RHEL/Fedora/CentOS"   # $MANAGER=dnf will be default if both dnf and yum are present
type zypper &> /dev/null && MANAGER=zypper && DISTRO="SLES"
type apk    &> /dev/null && MANAGER=apk    && DISTRO="Alpine"

### print_header() will display up to 3 arguments as a simple banner
print_header() {
    printf "\n\n\n####################\n"
    printf "#\n"
    if [ "$1" != "" ]; then printf "# $1\n"; fi
    if [ "$2" != "" ]; then printf "# $2\n"; fi
    if [ "$3" != "" ]; then printf "# $3\n"; fi
    printf "#\n"
    printf "####################\n\n"
}

function getLastUpdate()
{
    if [[ $MANAGER == "apt" ]]; then local updateDate="$(stat -c %Y '/var/cache/apt')"; fi   # %Y  time of last data modification, in seconds since Epoch
    if [[ $MANAGER == "dnf" ]]; then local updateDate="$(stat -c %Y '/var/cache/dnf/expired_repos.json')"; fi   # %Y  time of last data modification, in seconds since Epoch
    local nowDate="$(date +'%s')"   # %s seconds since 1970-01-01 00:00:00 UTC
    echo $((nowDate - updateDate))  # simple arithmetic with $(( ))
}

function runDistroUpdate()
{
    local updateInterval="${1}"     # An update interval can be specifide as $1, otherwise default to a value below
    local lastUpdate="$(getLastUpdate)"

    if [[ -z "$updateInterval" ]]   # "$(isEmptyString "${updateInterval}")" = 'true'
    then
        updateInterval="$((24 * 60 * 60))"   # Adjust this to how often to do updates, setting to 24 hours in seconds
    fi
    updateIntervalReadable=$(printf '%dh:%dm:%ds\n' $((updateInterval/3600)) $((updateInterval%3600/60)) $((updateInterval%60)))

    if [[ "${lastUpdate}" -gt "${updateInterval}" ]]   # only update if $updateInterval is more than 24 hours
    then
        print_header "apt updates will run as last update was more than ${updateIntervalReadable} ago"
        if [ "$MANAGER" == "apt" ]; then sudo apt --fix-broken install -y; fi   # Check and fix any broken installs, do before and after updates
        if [ "$MANAGER" == "apt" ]; then sudo apt dist-upgrade -y; fi
        if [ "$MANAGER" == "apt" ]; then sudo apt-get update --ignore-missing -y; fi   # Not sure if this is needed
        sudo $MANAGER update -y
        sudo $MANAGER upgrade -y
        sudo $MANAGER install ca-certificates -y
        sudo $MANAGER autoremove -y
        which apt-file &> /dev/null && sudo apt-file update   # update apt-file cache but only if apt-file is installed
        if [ "$MANAGER" == "apt" ]; then sudo apt --fix-broken install -y; fi   # Check and fix any broken installs, do before and after updates
    else
        local lastUpdate="$(date -u -d @"${lastUpdate}" +'%-Hh %-Mm %-Ss')"
        print_header "Skip apt-get update because its last run was ${updateIntervalReadable} ago"
    fi
}

runDistroUpdate
4

0 回答 0