-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable-syncoid-on-truenas.sh
197 lines (161 loc) · 4.57 KB
/
enable-syncoid-on-truenas.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
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
#!/bin/bash
# Set script to exit on error
set -e
set -o pipefail
set -u
# FIXME
# should use -v or --verbose to control terminal output
###################
# Variable set up #
###################
# YOU MUST HAVE THESE SANOID SCRIPTS AND CONF FILES IN THIS DIRECTORY
SANOID_FILES_PATH="/home/admin/Enable-Sanoid"
SANOID_FILES=(
"findoid"
"sanoid"
"sleepymutex"
"syncoid"
"sanoid.conf"
"sanoid.defaults.conf"
"sanoid-prune.service"
"sanoid.service"
"sanoid.timer"
)
# IF YOU CHANGE THE FILE LIST CHANGE THESE ARRAYS TO MATCH
SCRIPTS="0 1 2 3"
CONF="4 5"
SYSD="6 7 8"
# THIS SHOULDN'T NEED CHANGING
SCRIPTS_DIR="/usr/local/bin"
LINKS_DIR="/usr/local/sbin"
CONF_DIR="/etc/sanoid"
SYSD_DIR="/etc/systemd/system"
LOGFILE="/var/log/setup-script.log"
DATESTAMP=$(date "+%Y-%m-%d_%s")
APT_SOURCES_PATH="/etc/apt"
APT_SOURCES_FILE="sources.list"
################
# Script start #
################
# Check if run with enough privileges
ISROOT=$(id -u)
if [ "$ISROOT" -ne 0 ]; then
echo "--------------------------------------------------------";
echo "Error - insufficient privileges. Run with sudo or root.";
echo "--------------------------------------------------------";
exit 1;
fi
# Enable logging
exec > >(tee -i "$LOGFILE")
exec 2>&1
# Start
echo
echo "========================================================";
echo " Starting script execution"
echo "========================================================";
# Check if all sanoid files available
echo
echo -n "-- Checking required sanoid files "
for FILE in "${SANOID_FILES[@]}" ; do
([ -r "${SANOID_FILES_PATH}/${FILE}" ] && echo -n '.') || (echo "Missing $FILE, exiting" && exit 1)
done
echo
# Attempt to remount /usr read+write
echo
echo "-- Remounting usr as read write"
DATASET=$(findmnt -fn --output=source --mountpoint /usr)
if [ -z "$DATASET" ] ; then
echo "Error - couldn't determine /usr mount";
exit 1;
else
echo "Completed" ;
mount -o remount,rw "$DATASET";
fi
# Re-enabling apt utilities
echo
echo "-- Adding executable flag to re-enable apt utilities"
chmod -c +x /bin/apt*
chmod -c +x /usr/bin/dpkg
# Update sources list
# FIXME should check if additions are already in file and not dupicate them
echo
echo "-- Working on apt sources"
echo
CODENAME="$(lsb_release -cs)"
if [ -z "$CODENAME" ] ; then
echo "Error - couldn't determine debian codename";
exit 1;
else
echo "-- Backing up sources.list";
cp -v "${APT_SOURCES_PATH}/${APT_SOURCES_FILE}" "${APT_SOURCES_PATH}/${APT_SOURCES_FILE}-${DATESTAMP}"
echo
echo "-- Updating sources.list";
cat << EOF >> "${APT_SOURCES_PATH}/${APT_SOURCES_FILE}"
deb http://deb.debian.org/debian $CODENAME main
deb-src http://deb.debian.org/debian $CODENAME main
EOF
fi
# Install prerequisites
echo
echo "-- Updating repositories"
apt-get update
echo
echo "-- Installing prerequisites via apt"
apt-get install -y libcapture-tiny-perl libconfig-inifiles-perl pv lzop mbuffer
# Restore sources list
echo
echo "-- Restoring sources.list";
echo cp -vf "${APT_SOURCES_PATH}/${APT_SOURCES_FILE}-${DATESTAMP}" "${APT_SOURCES_PATH}/${APT_SOURCES_FILE}"
#FIXME include Br46
# Make sure scripts are executable
echo
echo "-- Setting scripts as executable"
for ITEM in $SCRIPTS ; do
chmod -c +x "${SANOID_FILES_PATH}/${SANOID_FILES[$ITEM]}" ;
done
# Copy scripts to usr local bin
echo
echo "-- Copying scripts into place"
for ITEM in $SCRIPTS ; do
cp -v "${SANOID_FILES_PATH}/${SANOID_FILES[$ITEM]}" "$SCRIPTS_DIR" ;
done
# Symlink scripts into usr sbin
echo
echo "-- Symlinking scripts"
for ITEM in $SCRIPTS ; do
ln -vs "${SCRIPTS_DIR}/${SANOID_FILES[$ITEM]}" "${LINKS_DIR}/" ;
done
# Create conf dir if required
echo
echo "-- Creating conf dir if required"
mkdir -pv "${CONF_DIR}"
# Copy conf files into conf dir
echo
echo "-- Copying conf files into place"
for ITEM in $CONF ; do
cp -v "${SANOID_FILES[$ITEM]}" "$CONF_DIR" ;
done
# Copy services and timer into place and activate
echo
echo "-- Moving conf files into place"
for ITEM in $SYSD ; do
cp -v "${SANOID_FILES[$ITEM]}" "$SYSD_DIR" ;
done
# Get systemd to see new files
echo
echo "-- Getting systemd to find new service files"
systemctl daemon-reload
# Enable sanoid timer
echo
echo "-- Enabling the sanoid timer"
systemctl enable --now sanoid.timer
# Put usr back as was
echo
echo "-- Remounting usr as read only"
mount -o remount,ro "$DATASET";
# Profit?
echo
echo "========================================================";
echo " Script execution completed successfully!"
echo "========================================================";
# vim: set filetype=sh syntax=sh ts=4 sw=4 tw=0 :