-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_cfg
executable file
·137 lines (130 loc) · 4.9 KB
/
save_cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
#
# Copyright (c) 2006 Mathieu Arnold
# Copyright (c) 2010 Alex Bakhtin
# Copyright (c) 2018 Portrat Benoit
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# 2018 version based on: $FreeBSD: releng/11.2/tools/tools/nanobsd/Files/root/save_cfg 309884 2016-12-12 11:02:55Z brueffer $
# 2018 vesrion adds support for symbolic links but requires rsync as a dependency.
#
set -e
trap "umount /cfg" 1 2 15 EXIT
mount /cfg
(
cd /etc
for filename in "$@" `find * -type f` `find * -type l`
do
if [ ! -f /cfg/$filename -a ! -f /cfg/.ignore/$filename ]
then
#
# If file doesn't exist in /cfg and file is not in the 'ignore' list
# then check if this file is exactly the same as original file
# in nanobsd image
#
if ! cmp -hs /etc/$filename /conf/base/etc/$filename
then
file_path=`echo "$filename" | sed 's/\/[^/]*$//'`
if [ $file_path != $filename ]
then
if [ ! -d /etc/$file_path ]
then
# should never go here unless we have some errors in
# sed script extracting file path
echo "Error: Path /etc/$file_path is not directory."
exit 1;
fi
fi
#
# Ask user - how should we handle this file.
# Add to cfg (y/n/i)?
# y) -> save this file in /cfg
# n) -> do not save this file in /cfg for current script invocation ONLY
# i) -> add file to ignore list (/cfg/.ignore hiereachy) and never save
# try to add this file to /cfg.
#
# touch is used to add files to /cfg to keep the script flow straight and easy
#
read -p "New file /etc/$filename found. Add to /cfg (y/n/i)? " key
case "$key" in
[yY])
if [ $file_path != $filename ]
then
mkdir -vp /cfg/$file_path
fi
touch /cfg/$filename && echo "File /etc/$filename added to /cfg."
;;
[iI])
mkdir -vp /cfg/.ignore
if [ $file_path != $filename ]
then
mkdir -vp /cfg/.ignore/$file_path
fi
touch /cfg/.ignore/$filename && echo "File /etc/$filename added to ignore list."
;;
esac
fi
fi
done
#
# Actually check all files in /cfg and save if necessary
#
cd /cfg
for filename in "$@" `find * -type f` `find * -type l`
do
if [ -L /etc/$filename ]
then
cmp -hs /etc/$filename /cfg/$filename && continue
diff -du "/cfg/${filename}" "/etc/${filename}" || true
read -p "File /etc/$filename modified. Persist changes in /cfg (y/n)? " key
case "$key" in
[yY])
rsync --info=COPY,DEL,FLIST,MISC,NAME,SYMSAFE -lptgo /etc/$filename /cfg/$filename
;;
esac
elif [ -f /etc/$filename ]
then
cmp -hs /etc/$filename /cfg/$filename && continue
diff -du "/cfg/${filename}" "/etc/${filename}" || true
read -p "File /etc/$filename modified. Persist changes in /cfg (y/n)? " key
case "$key" in
[yY])
cp -pfv /etc/$filename /cfg/$filename
;;
esac
else
#
# Give user an option to remove file from /cfg if this file is removed from /etc
#
read -p "File /cfg/$filename not found in /etc. Remove from /cfg (y/n)? " key
case "$key" in
[yY])
rm /cfg/$filename && echo "File /cfg/$filename removed"
;;
esac
fi
done
)
umount /cfg
trap 1 2 15 EXIT