#!/bin/sh [ "$(id -u)" -ne 0 ] && exec /usr/local/bin/sudo "${0}" "${@}" echo -n "Saving configuration... " if [ ! -f /etc/diskless ] then echo "No flash setup detected." exit 1 fi # Find all files newer than /etc/diskless and print them out find_newer_files() { [ -d "${1}" ] || return 1 find "${1}" -type f -not -regex '.*/tmp/*.' -newer /etc/diskless -print } # Archive all newer files into a gzipped cpio archive archive_cpio() { [ -f "${2}" ] && return 1 ( cd /; find_newer_files "${1}" | cpio -o --quiet | gzip -9 > "${2}" ) || return 1 } # Archive specified files to named target archive() { archive_cpio "${1}" "${2:-/conf/tmp/${1}}.cpio.gz" || return 1 } # Mount /conf read-write, and upgrade to read-write if mounted read-only. if grep -q /conf /etc/fstab then _conf_mount=yes mount | grep -q /conf && _conf_mounted=yes mount | grep /conf | grep -q read-only && _conf_readonly=yes # Mount if unmounted [ ! "${_conf_mounted}" ] && mount -w /conf # Upgrade to read/write [ "${_conf_mounted}" -a "${_conf_readonly}" ] && mount -u -w /conf fi # Store config in temp staging area rm -Rf /conf/tmp || exit 1 mkdir -p /conf/tmp || exit 1 chmod 750 /conf/tmp || exit 1 # Grab a list of confpacks to store for pack in /conf/base/*/md_size do pack="$(basename "$(dirname "${pack}")")" archive "${pack}" || exit 1 done # Rotate current config to dated backup # Unlimited history method: (make sure you have provisions for removing the old backups, or this can get HUGE! dest="$(date -r "$(stat -f '%c' "/conf/default")" "+%Y-%m-%dT%H-%M-%S")" mv /conf/default "/conf/backup/${dest}" || exit 1 # Move temp config to current mv /conf/tmp /conf/default || mv "/conf/backup/${dest}" "/conf/default" # Umount /conf afterwards, if it wasn't mounted if [ "${_conf_mount}" ] then # Unmount if it wasn't mounted [ ! "${_conf_mounted}" ] && umount /conf # Downgrade to read-only if it was read-only [ "${_conf_mounted}" -a "${_conf_readonly}" ] && mount -u -r -f /conf fi echo "Done!"