-
Notifications
You must be signed in to change notification settings - Fork 64
/
save_config.sh
161 lines (129 loc) · 4.83 KB
/
save_config.sh
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
#!/bin/sh
#####
# Backup the TrueNAS/FreeNAS configuration database and password secret encryption files
#####
# REQUIRED: Specify the dataset on your system where you want the configuration files copied.
# Don't include the trailing slash.
# Example: configdir="/mnt/tank/sysadmin/config"
configdir=""
# Remove this code once you've defined configdir above... :-)
if [ -z "${configdir}" ]; then
echo "Edit script and specify the target directory ('configdir') before using $0"
exit 2
fi
# Optional: Set non-zero 'do_tar' flag to have both files stored in a tarball as typically
# needed when restoring a configuration.
do_tar=1
# Optional: specify your email address here if you want to receive a notification message.
notifyemail=""
# Optional: specify the short name of your ESXi host if you are running FreeNAS
# as a VM and you want to back up the ESXi host's configuration
esxihost=""
# Get the date and version of TrueNAS/FreeNAS:
rundate=$(date)
osvers=$(grep -i truenas /etc/version)
if [ -z "${osvers}" ]; then
osvers=$(grep -i freenas /etc/version)
if [ -z "${osvers}" ]; then
osvers="UNKNOWN"
else
osvers="FreeNAS"
fi
else
osvers="TrueNAS"
fi
# Form a unique, timestamped filename for the backup configuration database and tarball
P1=$(hostname -s)
P2=$(< /etc/version sed -e 's/)//;s/(//;s/ /-/' | tr -d '\n')
P3=$(date +%Y%m%d%H%M%S)
fnconfigdest_base="$P1"-"$P2"-"$P3"
fnconfigdestdb="${configdir}"/"${fnconfigdest_base}".db
fnconfigtarball="${configdir}"/"${fnconfigdest_base}".tar
# Copy the source database and password encryption secret key to the destination:
echo "Backup ${osvers} configuration database file: ${fnconfigdestdb}"
cp -f /data/pwenc_secret "$configdir"
/usr/local/bin/sqlite3 /data/freenas-v1.db ".backup main '${fnconfigdestdb}'"
l_status=$?
# Validate the configuration file and create tarball:
if [ $l_status -eq 0 ]; then
dbstatus=$(sqlite3 "$fnconfigdestdb" "pragma integrity_check;")
printf 'sqlite3 status: [%s]\n' "${dbstatus}"
if [ "${dbstatus}" = "ok" ]; then
l_status=0
if [ $do_tar -ne 0 ]; then
# Save the config DB w/ its original name in the tarball -- makes restoring them easier:
cp -f "${fnconfigdestdb}" "${configdir}"/freenas-v1.db
tar -cvf "${fnconfigtarball}" -C "${configdir}" freenas-v1.db pwenc_secret
l_status=$?
printf 'tar status: [%s]\n' "${l_status}"
fi
else
l_status=1
fi
fi
if [ $l_status -eq 0 ]; then
echo "Success backing up configuration files to directory ${configdir}"
else
echo "Error backing up configuration files to directory ${configdir}"
fi
l_status=$?
# Backup the VMware ESXi host configuration:
if [ -n "${esxihost}" ]; then
esxihostname=$(ssh root@"${esxihost}" hostname)
esxiversion=$(ssh root@"${esxihost}" uname -a | sed -e "s|VMkernel ||;s|$esxihostname ||")
esxiconfig_url=$(ssh root@"${esxihost}" vim-cmd hostsvc/firmware/backup_config | awk '{print $7}' | sed -e "s|*|$esxihostname|")
esxiconfig_date=$(date +%Y%m%d%H%M%S)
esxiconfig_file="${configdir}"/"${esxihost}"-configBundle-"${esxiconfig_date}".tgz
echo "Downloading $esxiconfig_url to $esxiconfig_file"
wget --no-check-certificate --output-document="${esxiconfig_file}" "${esxiconfig_url}"
fi
# Send email notification if indicated:
if [ -n "${notifyemail}" ]; then
freenashostuc=$(hostname -s | tr '[:lower:]' '[:upper:]')
freenashostname=$(hostname)
freenasversion=$(< /etc/version sed -e 's/)//;s/(//;s/ /-/' | tr -d '\n')
boundary="===== MIME boundary; ${osvers} server ${freenashostname} ====="
logfile="/tmp/save_config.tmp"
if [ $l_status -eq 0 ]; then
subject="${osvers} configuration saved on server ${freenashostuc}"
else
subject="${osvers} configuration backup failed on server ${freenashostuc}"
fi
printf "%s\n" "To: ${notifyemail}
Subject: ${subject}
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"
--${boundary}
Content-Type: text/html; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<html><head></head><body><pre style=\"font-size:14px; white-space:pre\">" > ${logfile}
(
if [ $l_status -eq 0 ]; then
echo "Configuration file saved successfully on ${rundate}"
else
echo "Configuration backup failed with status=${l_status} on ${rundate}"
fi
echo ""
echo "--- ${osvers} ---"
echo "Server: ${freenashostname}"
echo "Version: ${freenasversion}"
echo "Files:"
echo " ${fnconfigdestdb}"
echo " ${configdir}/pwenc_secret"
if [ "$do_tar" -ne 0 ]; then
echo " ${fnconfigtarball}"
fi
if [ -n "${esxihost}" ]; then
echo ""
echo "--- ESXi ---"
echo "Server: ${esxihostname}"
echo "Version: ${esxiversion}"
echo "File: ${esxiconfig_file}"
fi
) >> ${logfile}
printf "%s\n" "</pre></body></html>
--${boundary}--" >> ${logfile}
sendmail -t -oi < ${logfile}
rm ${logfile}
fi