]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - overlay/sbin/saveconfig
overlay/sbin/saveconfig: Stage config in temp area, for atomic swap
[CDN/Mosi.git] / overlay / sbin / saveconfig
1 #!/bin/sh
2
3 echo -n "Saving configuration... "
4
5 if [ ! -f /etc/diskless ]
6 then
7     echo "No flash setup detected."
8     exit 1
9 fi
10
11 # Find all files newer than /etc/diskless and print them out
12 find_newer_files() {
13   [ -d "${1}" ] || return 1
14   find "${1}" -type f -not -regex '.*/tmp/*.' -newer /etc/diskless -print
15 }
16
17 # Archive all newer files into a gzipped cpio archive
18 archive_cpio() {
19   [ -d "${1}" ] || return 1
20   [ -f "${2}" ] && return 1
21   find_newer_files "${1}" | cpio -o | gzip -9 > "${2}"
22 }
23
24 # Archive specified files to named target
25 archive() {
26   archive_cpio "${1}" "${2}.cpio.gz"
27 }
28
29 # Mount /conf read-write, and remount if it already is.
30 if [ $(grep -c /conf /etc/fstab) -gt 0 ]
31 then
32         mount -w /conf
33         if [ $? -ne 1 ]
34         then
35                 umount /conf
36                 mount -w /conf
37                 _was_mounted=true
38         fi
39 fi
40
41 # Store config in temp staging area
42 mkdir -p /conf/tmp || exit 1
43 archive /etc /conf/tmp/etc || exit 1
44 archive /var /conf/tmp/var || exit 1
45
46 # Rotate current config to dated backup
47 # Unlimited history method: (make sure you have provisions for removing the old backups, or this can get HUGE!
48 dest="$(date -r "$(stat -f '%c' "/conf/default")" "+%Y-%m-%dT%H-%M-%S")"
49 mv /conf/default "/conf/backup/${dest}" || exit 1
50
51 # Move temp config to current
52 mv /conf/tmp /conf/default || mv "/conf/backup/${dest}" "/conf/default"
53
54 # Umount /conf afterwards, if it wasn't mounted
55 if [ -z "${_was_mounted}" -a "$(mount |grep -c "/conf")" -gt 0 ]
56 then
57         umount /conf
58 fi
59
60 echo "Done!"