wiki:AptitudeUpdateScript

Version 3 (modified by chris, 10 months ago) (diff)

--

These two scripts are used to update the Debian packages on the servers and to record the updates in /root/Changelog.

These are best installed from this repo.

a-up

#!/usr/bin/env bash

# This script runs "aptitude full-upgrade" and updates the Changelog 

# location of the logchange script, we assume it has been 
# installed int he same directory as this script is in
#DIR="/usr/local/bin"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LOGCHANGE="$DIR/logchange"

# check that the script is being run by root
if [[ "$(id -u)" != "0" ]] ; then
  echo "You must run '$0' as root or via sudo" 
  exit 1
fi

# check that the logchange script is installed
if [[ ! -f "${LOGCHANGE}" ]] ; then
  echo "You need to install the '${LOGCHANGE}' script before you can run $0"
  exit 2
fi

# For Ubuntu Touch
UBUNTU_TOUCH=$( uname -a | grep 'ubuntu-phablet' )
if [[ "${UBUNTU_TOUCH}" ]]; then
  echo "Ubuntu touch so skipping kernel update"
  export FLASH_KERNEL_SKIP="true"
fi

# check that packages are installed and if not then install, this depends on the 
# package name matching the binary in /usr/bin, this check is done first because 
# it's faster 
DEPENDENCIES="apt-show-versions aptitude"
for d in ${DEPENDENCIES}; do
  if [[ ! -f "/usr/bin/${d}" ]] ; then
    DEP_CHECK=$(dpkg -s ${d} 2>&1 | grep "Status: install ok installed")
    if [[ ! "${DEP_CHECK}" ]]; then
       echo "Installing '${d}'"
       apt-get install ${d}
    fi
  fi
done

# get updates
apt-get -qq update
# get a list of updates
UPDATES=$(apt-show-versions -b -u | xargs)
# if we have updates then install them and write the list of updates to the 
# Changelog
if [[ "${UPDATES}" ]]; then
  echo "About to upgrade '${UPDATES}'"
  ${LOGCHANGE} "${UPDATES} : updated"
  aptitude full-upgrade
  # Delete dowloaded packages to free up space
  if [[ "${UBUNTU_TOUCH}" ]]; then
    apt-get clean
  else
    aptitude autoclean
    echo "If ownCloud has been updated check if /etc/apache2/conf-available/owncloud.conf" 
    echo "has changed by looking at the timestamps, if it has merge the changes into"
    echo "/etc/apache2/sites-available/owncloud.conf and then run the following commands:"
    echo "rm /etc/apache2/conf-enabled/owncloud.conf"
    echo "chown owncloud:owncloud -R /var/www/owncloud/apps"
    echo "chown owncloud:owncloud -R /var/www/owncloud/assets"
    echo "chown owncloud:owncloud -R /var/www/owncloud/config"
    echo "chown owncloud:owncloud -R /var/www/owncloud/data"
    echo "chown owncloud:owncloud -R /var/www/owncloud/tmp"
    echo "su - owncloud -s /bin/bash"
    echo "php console.php upgrade"
  fi
  exit 0
else
  echo "No updates today"
  exit 0
fi

logchange

#!/usr/bin/env bash

# This is a script to update the /root/Changelog

# File name of the Changelog
CHANGELOG_NAME="Changelog"

DATESTAMP="$(date +%Y-%m-%d)"
TIMESTAMP="$(date +%Y-%m-%d-%H-%M-%S)"

# check that the script is being run by root
if [[ "$(id -u)" != "0" ]] ; then
  echo "You must run $0 as root or via sudo" 
  exit 2
fi

# check that $HOME is set
if [[ ${HOME} ]]; then
  CHANGELOG_DIR="${HOME}"
  CHANGELOG="${CHANGELOG_DIR}/${CHANGELOG_NAME}"
  BACKUP_DIR="${CHANGELOG_DIR}/.${CHANGELOG_NAME}"
  CHANGELOG_BACKUP="${BACKUP_DIR}/${CHANGELOG_NAME}.${TIMESTAMP}"
else
  echo "You are homeless?"
  exit 1
fi

# get a username and if we can't use root
if [[ ${SUDO_USER} ]]; then
  CHANGELOG_EDITOR="${SUDO_USER}"
elif [[ ${USER} ]]; then
  CHANGELOG_EDITOR="${USER}"
else
  CHANGELOG_EDITOR="root"
fi

# check for some changes on standard input
if [[ $1 ]]; then
  CHANGES="$1"
elif [[ ! "$1" ]]; then
  echo "Type the changes you would like recorded then [ENTER]:"
  read changes
  CHANGES=${changes}
fi

# create the backup directory if it doesn't exist 
if [[ ! -d "${BACKUP_DIR}" ]] ; then
  mkdir ${BACKUP_DIR}
fi

# backup the CHANGELOG if it exists
if [[ -f "${CHANGELOG}" ]] ; then
  cp -a ${CHANGELOG} ${CHANGELOG_BACKUP}
fi

# write the date and user to the CHANGELOG 
echo -e "${DATESTAMP}\t${CHANGELOG_EDITOR}" > ${CHANGELOG}

# write what we have done, taken from standard in
echo -e "\t*\t${CHANGES}" >> ${CHANGELOG}

# write a blank line 
echo >> ${CHANGELOG}

# write the backup back to the $CHANGELOG
if [[ -f "${CHANGELOG_BACKUP}" ]] ; then
  cat ${CHANGELOG_BACKUP} >> ${CHANGELOG}
fi

# gz the backup if it exists
if [[ -f "${CHANGELOG_BACKUP}" ]] ; then
  gzip ${CHANGELOG_BACKUP}
fi

exit 0