-
Notifications
You must be signed in to change notification settings - Fork 2
/
backupessential
executable file
·328 lines (294 loc) · 9.87 KB
/
backupessential
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/sh
#########################################################
# Script Requirements
#
# Files:
# vars_to_skip
# vars_preferred
# vars_to_include
#
# Programs:
# curl
#########################################################
#########################################################
# setup variables
#
# DATE - Date
# MAC - Mac address
# FILE - File Name Prefix
# CUR_DIR - Current Directory
# TRANSFER - FTP Transfer ON/OFF (Default is OFF)
# FOLDER - Location where backup scripts are stored
# VARFILE - Location & Name of Temp File
# TO_ALL - Location & Name of script File with all nvram variables
# TO_INCLUDE - Location & Name of script File with essential nvram variables
# TO_EXCLUDE - Location & Name of script File with dangerous nvram variables
# TO_PREFERRED - Location & Name of script File with preferred nvram variables
#
# wget -O backupessential https://raw2.github.com/WolverineFan/dd-wrt.backupessential/master/vars_preferred
# wget -O vars_to_skip https://raw2.github.com/WolverineFan/dd-wrt.backupessential/master/vars_to_skip
# wget -O vars_to_include https://raw2.github.com/WolverineFan/dd-wrt.backupessential/master/vars_to_include
# wget -O vars_preferred https://raw2.github.com/WolverineFan/dd-wrt.backupessential/master/vars_preferred
#
#########################################################
export PATH=/opt/bin:/opt/sbin:/opt/usr/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin:/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin:/mmc/sbin:/mmc/bin:/mmc/usr/sbin:/mmc/usr/bin
DATE=`date +%m%d%Y`
MAC=`nvram get lan_hwaddr | tr -d ":"`
FILE=${MAC}.${DATE}
NAME="$0"
[ -z "`which readlink`" ] || NAME="`readlink -f "${NAME}"`"
CUR_DIR=`dirname ${NAME}`
TRANSFER=0
if [ -d /opt/tmp ]; then
FOLDER=/opt/var/backups
VARFILE=/opt/tmp/all_vars
elif [ -d /mmc/jffs ]; then
FOLDER=/mmc/jffs
VARFILE=/mmc/jffs/all_vars
else
FOLDER=/tmp/backups
VARFILE=/tmp/all_vars
fi
flavours='all
essential
dangerous
preferred
minimal'
#########################################################
#FTP Login information change to your info
#########################################################
FTPS=ftp://192.168.10.210/backups
USERPASS=user:pass
#########################################################
# read command line switches
#
# example command lines
#
# ./backupessential -t
#
# The above command with use the user and password and
# server information embedded in this script.
# (See FTP Login information above)
#
#
# ./backupessential -d <backupdir> -t -u user:pass -f ftp://192.168.1.100/backups
#
# The above command with use the user and password and
# server information from the command line
#
#########################################################
while getopts tu:f:d: name
do
case $name in
t) TRANSFER=1;;
u) USERPASS="$OPTARG";;
f) FTPS="$OPTARG";;
d) BACKUPDIR="$OPTARG";;
?) printf "Usage: %s: [-t] [-u username:password] [-f ftpserver]\n" $0
exit 2;;
esac
done
shift $(($OPTIND - 1))
if [ "${BACKUPDIR}" != "" ]; then
if [ ! -d "${BACKUPDIR}" ]; then
echo "ERROR: ${BACKUPDIR} does not exist"
exit 1
fi
FOLDER="${BACKUPDIR}"
VARFILE="${BACKUPDIR}"/all_vars
else
mkdir -p $FOLDER
if [ ! -d $FOLDER ]; then
echo "ERROR: Unable to write to $FOLDER"
exit 1
fi
fi
TO_ALL="${FOLDER}/${MAC}.${DATE}.all.sh"
TO_INCLUDE="${FOLDER}/${MAC}.${DATE}.essential.sh"
TO_EXCLUDE="${FOLDER}/${MAC}.${DATE}.dangerous.sh"
TO_PREFERRED="${FOLDER}/${MAC}.${DATE}.preferred.sh"
TO_MINIMAL="${FOLDER}/${MAC}.${DATE}.minimal.sh"
CR=`printf "\r"`
if [ ! -f "${CUR_DIR}/vars_to_skip" ] ; then
echo "${CUR_DIR}/vars_to_skip is missing"
exit 1
fi
if [ ! -f "${CUR_DIR}/vars_to_include" ] ; then
echo "${CUR_DIR}/vars_to_include is missing"
exit 1
fi
if [ ! -f "${CUR_DIR}/vars_preferred" ] ; then
echo "${CUR_DIR}/vars_preferred is missing"
exit 1
fi
if [ ! -f "${CUR_DIR}/vars_minimal" ] ; then
echo '^cron
^wan_wins
^wan_up
^wl_ssid
^wl_net_mode
^wl_channel
^wl_nm
^wl_net_mode
^wl_nband
^wl_mrate
^wl0_wds1_
^wl0_wds
^wl0_ssid
^wl0_wchannel
^wl0_ssid
^wl0_phy
^wl0_net_mode
^wl0_nctrlsb
^wl0_nbw
^wl0_nband
^wl0_nm
^wl0_nreqd
^wk_mode
^wan_proto
^wan_hostname
^wan_dns
^wan_domain
^usb_
^qos
^wshaper_uplink
^wshaper_downlink
^wshaper_enable
^wshaper_downlink
^wshaper_dev
^svqos_svcs
^svqos_macs
^static_lease
^sshd_enable
^sshd_wanport
^sshd_authorized_keys
^snmpd_
^syslogd_enable
^remote_mgt_ssh
^remote_management
^dns_dnsmasq
^dnsmasq_
^dmz_
^enable_jffs2
^log_
^local_dns
^lan_ipaddr
^lan_domain
^rc_
^http_wanport
^http_username
^http_passwd
^http_lanport
^https_enable
^http_enable
^fullswitch
^forwardspec_entries
^forward_port$
^forward_spec
^forward_entries
^forward_cur
^ddns_enable
^ddns_username
^ddns_hostname
^ddns_passwd
^dhcp_
^block_wan
^cron' > "${CUR_DIR}/vars_minimal"
fi
grep -q "${CR}" "${CUR_DIR}/vars_to_skip" && sed -i -e 's/\r//g' "${CUR_DIR}/vars_to_skip"
grep -q "${CR}" "${CUR_DIR}/vars_to_include" && sed -i -e 's/\r//g' "${CUR_DIR}/vars_to_include"
grep -q "${CR}" "${CUR_DIR}/vars_preferred" && sed -i -e 's/\r//g' "${CUR_DIR}/vars_preferred"
grep -q "${CR}" "${CUR_DIR}/vars_minimal" && sed -i -e 's/\r//g' "${CUR_DIR}/vars_minimal"
grep -q "^ *$" "${CUR_DIR}/vars_minimal" && sed -i -e '/^ *$/d' "${CUR_DIR}/vars_minimal"
grep -q "^ *$" "${CUR_DIR}/vars_preferred" && sed -i -e '/^ *$/d' "${CUR_DIR}/vars_preferred"
grep -q "^ *$" "${CUR_DIR}/vars_to_include" && sed -i -e '/^ *$/d' "${CUR_DIR}/vars_to_include"
grep -q "^ *$" "${CUR_DIR}/vars_to_skip" && sed -i -e '/^ *$/d' "${CUR_DIR}/vars_to_skip"
#########################################################
# Create NVRAM variale list and write to /opt/tmp/all_vars
#########################################################
nvram show 2>/dev/null | egrep '^[A-Za-z][A-Za-z0-9_\.\-]*=' | awk -F= '{print $1}' | sort -ruo "${VARFILE}"
#########################################################
# Write header to restore scripts
#########################################################
echo -e "#!/bin/sh\n#\necho \"Write variables\"\n" | tee -i "${TO_EXCLUDE}" | tee -i "${TO_PREFERRED}" | tee -i "${TO_ALL}" | tee -i "${TO_MINIMAL}" > "${TO_INCLUDE}"
#########################################################
# Scan NVRAM variable list and send variable to proper
# restore script
#########################################################
while read var ; do
pref=0
minim=0
if echo "${var}" | grep -q -f "${CUR_DIR}/vars_to_skip" ; then
bfile="$TO_EXCLUDE"
else
bfile="$TO_INCLUDE"
pref=`echo "${var}" | grep -cf "${CUR_DIR}/vars_preferred"`
minim=`echo "${var}" | grep -cf "${CUR_DIR}/vars_minimal"`
fi
# get the data out of the variable
data="`nvram get ${var}`"
# write the var to the file and use \ for special chars: (\$`")
echo -en "nvram set ${var}=\"" | tee -ia ${TO_ALL} >> ${bfile}
echo -n "${data}" | sed -e 's/[$`"\]/\\&/g' | tee -ia ${TO_ALL} >> ${bfile}
echo -e "\"" | tee -ia ${TO_ALL} >> ${bfile}
if [ ${pref} -ne 0 ]; then
echo -en "nvram set ${var}=\"" >> ${TO_PREFERRED}
echo -n "${data}" | sed -e 's/[$`"\]/\\&/g' >> ${TO_PREFERRED}
echo -e "\"" >> ${TO_PREFERRED}
fi
if [ ${minim} -ne 0 ]; then
echo -en "nvram set ${var}=\"" >> ${TO_MINIMAL}
echo -n "${data}" | sed -e 's/[$`"\]/\\&/g' >> ${TO_MINIMAL}
echo -e "\"" >> ${TO_MINIMAL}
fi
done <"${VARFILE}"
#########################################################
# Cleanup remove /opt/tmp/all_vars
# uncomment to remove file
#########################################################
# rm ${VARFILE}
#########################################################
# Write footer to restore script
#########################################################
echo -e "\n# Commit variables\necho \"Save variables to nvram\"\nnvram commit" | tee -ia "${TO_ALL}" | tee -ia "${TO_PREFERRED}" | tee -ia "${TO_EXCLUDE}" | tee -ia "${TO_MINIMAL}" >> "${TO_INCLUDE}"
#########################################################
# Change permissions on restore scripts to make them
# executable
#########################################################
chmod +x "${TO_INCLUDE}"
chmod +x "${TO_PREFERRED}"
chmod +x "${TO_EXCLUDE}"
chmod +x "${TO_ALL}"
chmod +x "${TO_MINIMAL}"
# Remove the previous backup if it's (almost) the same
if cd "${FOLDER}" ; then
for flavour in ${flavours} ; do
echo "Checking ${MAC}.*.${flavour}.sh"
LAST3=`ls --color=never -1rt ${MAC}.*.${flavour}.sh | tail -n3`
PREVIOUS=`echo "${LAST3}" | tail -n+2 | head -n1`
CURRENT=`echo "${LAST3}" | tail -n1`
if [ ! -z "${PREVIOUS}" ] && [ ! "${PREVIOUS}" = "${CURRENT}" ]; then
grep -vE 'nvram set (forward_|traff|dnsmasq_lease_|http_client_)' ${PREVIOUS} >/tmp/chk1
grep -vE 'nvram set (forward_|traff|dnsmasq_lease_|http_client_)' ${CURRENT} >/tmp/chk2
echo "Comparing ${PREVIOUS} with ${CURRENT}"
if [ -z "`diff /tmp/chk1 /tmp/chk2`" ] ; then
echo "Removing ${PREVIOUS} because it's the same as ${CURRENT}"
rm -f ${PREVIOUS}
fi
fi
done
rm -f /tmp/chk1
rm -f /tmp/chk2
fi
[ -h /opt/restore ] && rm /opt/restore
ln -s ${TO_MINIMAL} /opt/restore
#########################################################
# Compress restore scripts and send them to ftp server
#########################################################
if [ "${TRANSFER}" -ne 0 ] ; then
tar cpf - -C / "${TO_INCLUDE}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.essential.sh.tgz" -T -
tar cpf - -C / "${TO_MINIMAL}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.minimal.sh.tgz" -T -
tar cpf - -C / "${TO_PREFERRED}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.preferred.sh.tgz" -T -
tar cpf - -C / "${TO_EXCLUDE}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.dangerous.sh.tgz" -T -
tar cpf - -C / "${TO_ALL}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.all.sh.tgz" -T -
fi