Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created June 18, 2019 17:49
Show Gist options
  • Save mttjohnson/310e776a4ba24f019f03ad7dd6e07b7b to your computer and use it in GitHub Desktop.
Save mttjohnson/310e776a4ba24f019f03ad7dd6e07b7b to your computer and use it in GitHub Desktop.
Create Archived Revision of File Changes
#!/usr/bin/env bash
set -eu
# ----------------------------------------
# Variables
# ----------------------------------------
INSTALL_DIR="/var/www/html"
SHARED_DIR="/var/www/shared/html"
TIMESTAMP=$(date -u +"%Y%m%dT%H%M%SZ")
# ----------------------------------------
# Validate local.xml
# ----------------------------------------
# check for existence and create archived revision if changed
# Make sure archived_revisions directory exists
[[ ! -d ${SHARED_DIR}/app/etc/archived_revisions ]] && mkdir -p ${SHARED_DIR}/app/etc/archived_revisions
# Make sure source is a real file and not a symlink
[[ ! -f ${INSTALL_DIR}/app/etc/local.xml ]] || [[ -L ${INSTALL_DIR}/app/etc/local.xml ]] && echo 1>&2 "ERROR: local.xml does not appear to be a normal file!" && exit 1
# Check for differences
# Compute digest hash of each file contents for comparing
SOURCE_DIGEST=$(cat ${INSTALL_DIR}/app/etc/local.xml | openssl dgst -sha256 -binary | xxd -p -c 256)
LATEST_REV_DIGEST=$(cat ${SHARED_DIR}/app/etc/archived_revisions/local.xml.latest | openssl dgst -sha256 -binary | xxd -p -c 256)
if [[ "${SOURCE_DIGEST}" != "${LATEST_REV_DIGEST}" ]]; then
# display differences
echo ""
diff -u ${INSTALL_DIR}/app/etc/local.xml ${SHARED_DIR}/app/etc/archived_revisions/local.xml.latest
echo -e "\nlocal.xml differences were found... archiving a revision\n"
# copy file to create archived revision
cp -a ${INSTALL_DIR}/app/etc/local.xml ${SHARED_DIR}/app/etc/archived_revisions/local.xml_${TIMESTAMP}
echo "${SHARED_DIR}/app/etc/archived_revisions/local.xml_${TIMESTAMP}"
# update latest symlink
cd ${SHARED_DIR}/app/etc/archived_revisions
ln -sf local.xml_${TIMESTAMP} local.xml.latest
echo "local.xml.latest -> local.xml_${TIMESTAMP}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment