]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - src/overlay/sbin/saveconfig
script/gentree; sbin/saveconfig: use /COPYRIGHT as the saveconfig time anchor, as...
[CDN/Mosi.git] / src / overlay / sbin / saveconfig
1 #!/bin/sh
2
3 [ "$(id -u)" -ne 0 ] && exec /usr/local/bin/sudo "${0}" "${@}"
4
5 echo -n "Saving configuration... "
6
7 if [ ! -f /etc/diskless ]
8 then
9     echo "No flash setup detected."
10     exit 1
11 fi
12
13 # Find all files newer than /COPYRIGHT and print them out
14 find_newer_files() {
15   [ -d "${1}" ] || return 1
16   find "${1}" -type f -not -regex '.*/tmp/*.' -newer /COPYRIGHT -print
17 }
18
19 # Archive all newer files into a gzipped cpio archive
20 archive_cpio() {
21   [ -f "${2}" ] && return 1
22   ( cd /; find_newer_files "${1}" | cpio -o --quiet | gzip -9 > "${2}" ) || return 1
23 }
24
25 # Archive specified files to named target
26 archive() {
27   archive_cpio "${1}" "${2:-/conf/tmp/${1}}.cpio.gz" || return 1
28 }
29
30 # Mount /conf read-write, and upgrade to read-write if mounted read-only.
31 if grep -q /conf /etc/fstab
32 then
33   _conf_mount=yes
34   mount | grep -q /conf && _conf_mounted=yes
35   mount | grep /conf | grep -q read-only && _conf_readonly=yes
36
37   # Mount if unmounted
38   [ ! "${_conf_mounted}" ] && mount -w /conf
39
40   # Upgrade to read/write
41   [ "${_conf_mounted}" -a "${_conf_readonly}" ] && mount -u -w /conf
42 fi
43
44 # Store config in temp staging area
45 rm -Rf /conf/tmp || exit 1
46 mkdir -p /conf/tmp || exit 1
47 chmod 750 /conf/tmp || exit 1
48
49 # Grab a list of confpacks to store
50 for pack in /conf/base/*/md_size
51 do
52   pack="$(basename "$(dirname "${pack}")")"
53   archive "${pack}" || exit 1
54 done
55
56 # Rotate current config to dated backup
57 # Unlimited history method: (make sure you have provisions for removing the old backups, or this can get HUGE!
58 dest="$(date -r "$(stat -f '%c' "/conf/default")" "+%Y-%m-%dT%H-%M-%S")"
59 mv /conf/default "/conf/backup/${dest}" || exit 1
60
61 # Move temp config to current
62 mv /conf/tmp /conf/default || mv "/conf/backup/${dest}" "/conf/default"
63
64 # Umount /conf afterwards, if it wasn't mounted
65 if [ "${_conf_mount}" ]
66 then
67   # Unmount if it wasn't mounted
68   [ ! "${_conf_mounted}" ] && umount /conf
69
70   # Downgrade to read-only if it was read-only
71   [ "${_conf_mounted}" -a "${_conf_readonly}" ] && mount -u -r -f /conf
72 fi
73
74 echo "Done!"