From 05b0d8c81b59c077b49f1b9215edba154d4f015a Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Sat, 13 Feb 2010 09:42:46 -0600 Subject: [PATCH] overlay/sbin/saveconfig: Stage config in temp area, for atomic swap --- overlay/sbin/saveconfig | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/overlay/sbin/saveconfig b/overlay/sbin/saveconfig index fc3cab3..3770956 100755 --- a/overlay/sbin/saveconfig +++ b/overlay/sbin/saveconfig @@ -8,14 +8,22 @@ then 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() { +# Archive all newer files into a gzipped cpio archive +archive_cpio() { [ -d "${1}" ] || return 1 - find_newer_files "${1}" | cpio -o | gzip -9 > "/conf/default/${1}.cpio.gz" + [ -f "${2}" ] && return 1 + find_newer_files "${1}" | cpio -o | gzip -9 > "${2}" +} + +# Archive specified files to named target +archive() { + archive_cpio "${1}" "${2}.cpio.gz" } # Mount /conf read-write, and remount if it already is. @@ -30,19 +38,18 @@ then fi fi +# Store config in temp staging area +mkdir -p /conf/tmp || exit 1 +archive /etc /conf/tmp/etc || exit 1 +archive /var /conf/tmp/var || exit 1 +# 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}" - -mkdir /conf/default +mv /conf/default "/conf/backup/${dest}" || exit 1 -cd / -# Copy changed config files to /conf/default (anything younger than /etc/diskless) -#find etc var -type f -not -regex '.*/tmp/.*' -newer /etc/diskless -print0 | cpio -p /conf/default/ -# This can be changed to allow compressed configuration here as well: -archive etc -archive var +# 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 [ -z "${_was_mounted}" -a "$(mount |grep -c "/conf")" -gt 0 ] -- 2.42.0