-
Notifications
You must be signed in to change notification settings - Fork 0
/
provision.sh.in
186 lines (160 loc) · 5.44 KB
/
provision.sh.in
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
#!/bin/sh
#
# Raspberry Pi Backup System for Classrooms - Node Provisioning Script
# Copyright (C) 2022 Kian Kasad
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Change the following two lines to match your backup server
SERVER_IP='m4_SERVER_IP'
SERVER_PORT='m4_HTTPD_PORT'
### Do not change anything below this line ###
hostname_pattern='[A-Z0-9][A-Z0-9\-]*'
info() {
printf '\033[1;34mInfo:\033[m ' >&2
printf "$@" >&2
printf '\n' >&2
}
warn() {
printf '\033[1;33mWarning:\033[m ' >&2
printf "$@" >&2
printf '\n' >&2
}
err() {
printf '\033[1;31mError:\033[m ' >&2
printf "$@" >&2
printf '\n' >&2
}
# Set new hostname given by user
change_hostname() {
local response
# Prompt user for new hostname
# Read from/write to /dev/tty as stdin might be a pipe
printf 'Enter a new hostname: ' > /dev/tty
IFS='' read -r response < /dev/tty
until
printf '%s' "$response" | grep -ixq "$hostname_pattern"
do
err 'The hostname may only contain letters, numbers, and hyphens.'
printf 'Enter a new hostname: ' > /dev/tty
IFS='' read -r response < /dev/tty
done
info "Setting new hostname to '%s'." "$response"
# Update /etc/hosts
sed -i "/^127\.0\.[01]\.1\s\+$(hostname)\s*$/d" /etc/hosts
printf '127.0.1.1\t%s\n' "$response" >> /etc/hosts
# Set new hostname. Attempts to use hostnamectl(1) first. If that fails,
# writes the new name to /etc/hostname then updates the live hostname using
# hostname(1) or /proc/sys/kernel/hostname.
if ! \
hostnamectl --no-ask-password set-hostname "$response" 2>/dev/null
then
warn 'Calling hostnamectl(1) failed. Falling back to hostname(1).'
printf '%s\n' "$response" > /etc/hostname
if ! \
hostname "$response" 2>/dev/null
then
warn 'Calling hostname(1) failed. Falling back to /proc/sys/kernel/hostname.'
printf '%s' "$response" > /proc/sys/kernel/hostname
fi
fi
}
set -e
# Ensure we're running as root
if [ $(id -u) -ne 0 ]
then
err 'This script must be run as root.'
if [ -x "$0" ]
then
printf "\033[1;32mHint:\033[m Try running 'sudo %s'.\n" "$0" >&2
fi
exit 1
fi
# Remove artifacts from previous installations. We don't need to remove all
# installed files, just ones that won't be overwritten later and may cause
# unexpected behavior.
for dir in \
/etc/systemd/system/backup.service.d \
/etc/systemd/system/backup.timer.d \
/var/lib/backup_client \
/usr/local/share/backup_client
do
warn 'Deleting directory %s left over previous installation...' "$dir"
rm -rf "$dir"
done
# If the hostname has not been changed, prompt for a new one.
# If it appears to have been changed, print the detected hostname.
hostname="$(hostname)"
if [ "$hostname" = "raspberrypi" ]
then
warn "You're using the default hostname '%s'." "$hostname"
change_hostname
elif ! printf '%s' "$hostname" | grep -ixq "$hostname_pattern"
then
warn 'Invalid or blank hostname detected.'
change_hostname
fi
# Install required packages
info 'Downloading package information...'
info 'This may take a while.'
DEBIAN_FRONTEND=noninteractive apt-get -qq update
info 'Installing required packages...'
info 'This may take a while.'
DEBIAN_FRONTEND=noninteractive \
apt-get -qq install --no-install-recommends \
borgbackup openssh-client python3 python3-requests curl bash
info 'Creating systemd units...'
# Create systemd parent directories
install -d -m 755 \
/usr/local/lib/systemd/system \
/etc/systemd/system/backup.timer.d
# Create backup service unit
umask 022
cat <<- 'EOF' > /usr/local/lib/systemd/system/backup.service
m4_include(`node/usr/local/lib/systemd/system/backup.service')
EOF
# Create backup on shutdown service unit
cat <<- 'EOF' > /usr/local/lib/systemd/system/backup-on-shutdown.service
m4_include(`node/usr/local/lib/systemd/system/backup-on-shutdown.service')
EOF
# Create backup timer unit
cat <<- 'EOF' > /usr/local/lib/systemd/system/backup.timer
m4_include(`node/usr/local/lib/systemd/system/backup.timer')
EOF
cat <<- 'EOF' > /etc/systemd/system/backup.timer.d/00-times.conf
m4_include(`node/etc/systemd/system/backup.timer.d/00-times.conf')
EOF
# Download SSH key
info 'Downloading & installing SSH key...'
install -d -m 755 /usr/local/share/backup_client
curl -fsSL "http://$SERVER_IP:$SERVER_PORT/setup/node_key" \
> /usr/local/share/backup_client/ssh_key
chmod 0400 /usr/local/share/backup_client/ssh_key
# Download client script
info 'Downloading & installing client script...'
install -d -m 755 /usr/local/lib
curl -fsSL "http://$SERVER_IP:$SERVER_PORT/setup/client.py" \
> /usr/local/lib/backup_client.py
chmod 750 /usr/local/lib/backup_client.py
# Reload systemd and enable units
info 'Reloading systemd configuration...'
systemctl daemon-reload
info 'Enabling systemd units...'
systemctl enable backup.timer backup-on-shutdown.service
info 'Running backup service...'
systemctl start backup.service
info 'Starting backup trigger units...'
systemctl start backup.timer backup-on-shutdown.service
printf '\n'
info 'Successfully installed backup system!'
exit 0