-
Notifications
You must be signed in to change notification settings - Fork 4
/
restore.sh
executable file
·314 lines (282 loc) · 7.97 KB
/
restore.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
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
#!/bin/bash
# @author Chris Iverach-Brereton <civerachb@clearpathrobotics.com>
# @author David Niewinski <dniewinski@clearpathrobotics.com>
# @description Restores a backup of a single robot's integration setup.
# This script should be run locally on the robot
# the username used during the original backup
# by default on robots we ship this should always be "administrator"
USERNAME=administrator
# the version of _this_ script
VERSION=2.0.2
RED='\033[0;31m'
NC='\033[0m' # No Color
function cleanup {
echo "Cleaning Up $(pwd)/$1"
rm -rf $1
echo "Done Cleaning"
}
function promptDefaultNo {
# $1: the prompt
# return: 0 for no, 1 for yes
read -r -p "$1 [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]];
then
echo 1
else
echo 0
fi
}
function promptDefaultYes {
# $1: the prompt
# return: 0 for no, 1 for yes
read -r -p "$1 [Y/n] " response
if [[ "$response" =~ ^([nN][oO]|[nN])$ ]];
then
echo 0
else
echo 1
fi
}
if [ $# -ge 1 ]
then
if [ $# == 2 ];
then
echo "Overriding default username"
USERNAME=$2
echo "Username: $USERNAME"
fi
CUSTOMER=$1
######################## UNPACK ARCHIVE ##########################
echo "Unpacking backup"
tar -xf $CUSTOMER.tar.gz $CUSTOMER
cd $CUSTOMER
############################ METADATA CHECK ###############################
echo "Checking backup version"
if [ ! -f "BKUP_VERSION" ];
then
echo "ERROR: no backup script version specified; cannot restore this backup!"
exit 1
else
BKUP_VERSION=$(cat BKUP_VERSION)
if [ "$VERSION" != "$BKUP_VERSION" ];
then
cd ..
cleanup "$1"
echo "ERROR: backup was made with a different version; please download v$BKUP_VERSION of this script"
exit 1
else
echo "Backup can be restored with this script"
fi
fi
echo "Checking backup ROS distro"
if [ ! -f "ROS_DISTRO" ];
then
echo "ERROR: no ROS distro specified in backup. Aborting."
cd ..
cleanup "$1"
exit 1
fi
ROSDISTRO=$(cat ROS_DISTRO)
echo "ROS distro in backup is $ROSDISTRO"
if [ "$USERNAME" != "$(whoami)" ];
then
echo "WARNING: current user ($(whoami)) does not match expected account ($USERNAME)"
CONTINUE=$(promptDefaultNo "Continue?")
if [ $CONTINUE == 1 ]
then
echo "Ignoring username mismatch"
else
cd ..
cleanup "$1"
echo "User aborted"
exit 0
fi
fi
############################ ROS & BRINGUP #############################
if [ -f setup.bash ];
then
echo "Restoring etc/ros/setup.bash"
sudo cp setup.bash /etc/ros/setup.bash
else
echo "Skipping setup.bash; no backup"
fi
if [ -d ros.d ];
then
echo "Restoring Bringup"
sudo cp -r ros.d/. /etc/ros/$ROSDISTRO/ros.d
else
echo "Skipping bringup; no backup"
fi
if [ -d sbin ];
then
echo "Restoring sbin"
sudo cp -r sbin/. /usr/sbin
else
echo "Skipping sbin; no backup"
fi
if [ -d rosdep ];
then
echo "Restoring RosDep sources"
sudo cp -r rosdep/. /etc/ros/rosdep/
else
echo "Skipping rosdep sources; no backup"
fi
if [ -d system ];
then
echo "Restoring Systemd"
sudo cp -r system/. /etc/systemd/system
else
echo "Skipping systemd; no backup"
fi
############################ HOME FOLDER #############################
echo "Restoring Home Folder"
cp -r $USERNAME/. ~
############################ UDEV #############################
if [ -d rules.d ];
then
echo "Restoring udev rules"
sudo cp -r rules.d/. /etc/udev/rules.d
else
echo "Skipping udev rules; no backup"
fi
############################ NETWORK #############################
echo "Restoring Network Setup"
if [ -f interfaces ];
then
echo "Restoring interfaces"
sudo cp interfaces /etc/network/interfaces
else
echo "Skipping /etc/network/interfaces; no backup"
fi
if [ -d netplan ];
then
echo "Restoring netplan files"
sudo cp -r netplan/. /etc/netplan
else
echo "Skipping netplan; no backup"
fi
if [ -f hostname ];
then
echo "Restoring hostname"
sudo cp hostname /etc/hostname
else
echo "Skipping /etc/hostname; no backup"
fi
if [ -f hosts ];
then
echo "Restoring hosts"
sudo cp hosts /etc/hosts
else
echo "Skipping /etc/hosts; no backup"
fi
if [ -d iptables ];
then
echo "Restoring iptables"
sudo cp -r iptables/. /etc/iptables
else
echo "Skipping /etc/iptables; no backup"
fi
############################ RC.LOCAL #############################
if [ -f rc.local ];
then
echo "Restoring rclocal"
sudo cp rc.local /etc/rc.local
else
echo "Skipping rc.local; no backup"
fi
############################ USER GROUPS #############################
echo "Restoring user groups"
while read LINE; do
for GROUP in ${LINE}; do
echo "Adding user to group $GROUP"
echo "sudo usermod -a -G $GROUP $(whoami)"
sudo usermod -a -G $GROUP $(whoami)
done
done < groups
############################ APT #############################
if [ -d sources.list.d ];
then
echo "Restoring APT sources"
sudo cp -r sources.list.d/. /etc/apt/sources.list.d
else
echo "Skipping additional APT sources; no backup present"
fi
INSTALL_APT=$(promptDefaultYes "Reinstall APT packages?")
if [ $INSTALL_APT == 1 ];
then
echo "Reinstalling APT packages"
else
echo "Creating a script to let you reinstall APT packages later"
echo "#!/bin/bash" > $HOME/restore-apt.sh
echo "# Automatically generated by $0 v$VERSION" >> $HOME/restore-apt.sh
echo "# $(date)" >> $HOME/restore-apt.sh
echo "sudo apt-get install --reinstall --yes \\" >> $HOME/restore-apt.sh
chmod +x $HOME/restore-apt.sh
fi
while read PKG; do
if [ $INSTALL_APT == 1 ];
then
sudo apt-get install --reinstall --yes $PKG
else
echo " $PKG \\" >> $HOME/restore-apt.sh
fi
done < installed_pkgs.list
############################ PIP #############################
# we have to do pip last, as some pip packages may be provided via apt
# this should _only_ install pip packages that were manually installed
INSTALL_PIP=$(promptDefaultYes "Restore PIP packages?")
if [ $INSTALL_PIP == 1 ];
then
echo "Restoring pip packages"
echo "If you have not already restored APT packages you may encounter errors"
else
echo "Creating script so you can restore PIP packages later..."
echo "#!/bin/bash" > $HOME/restore-pip.sh
echo "# Automatically generated by $0 v$VERSION" >> $HOME/restore-pip.sh
echo "# $(date)" >> $HOME/restore-pip.sh
chmod +x $HOME/restore-pip.sh
fi
while read line; do
TOKENS=($line)
PKG=${TOKENS[0]}
VERSION=${TOKENS[1]}
PIP_OUT=$(pip list|grep "^$PKG\s")
TOKENS=($PIP_OUT)
INSTALLED_PKG=${TOKENS[0]}
INSTALLED_VERSION=${TOKENS[1]}
if [ $INSTALL_PIP == 1 ];
then
# check if the package is installed, if it's not then install it
if [[ "$INSTALLED_PKG" == "$PKG" && "$INSTALLED_VERSION" == "$VERSION" ]];
then
echo "pip package $PKG $VERSION is already installed"
else
echo "Installing pip package $PKG"
pip install -Iv $PKG==$VERSION
fi
else
# add this package to the install script
if [[ "$INSTALLED_PKG" == "$PKG" && "$INSTALLED_VERSION" == "$VERSION" ]];
then
echo "pip package $PKG $VERSION is already installed"
else
echo "pip install -Iv $PKG==$VERSION" >> $HOME/restore-pip.sh
fi
fi
done < pip.list
############################ CLEANUP #############################
echo "Done Restoring Files"
cd ..
cleanup "$CUSTOMER"
############################ FINAL MESSAGES #############################
if [ $INSTALL_APT == 0 ];
then
echo -e "Run ${RED}$HOME/restore-apt.sh${NC} to resintall APT packages"
fi
if [ $INSTALL_PIP == 0 ];
then
echo -e "Run ${RED}$HOME/restore-pip.sh${NC} to resintall PIP packages"
fi
else
echo "USAGE: bash restore.sh customer_name [username]"
fi