-
Notifications
You must be signed in to change notification settings - Fork 1
/
borg_setup
executable file
·211 lines (175 loc) · 5.79 KB
/
borg_setup
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
#!/bin/bash
# because I just can't sudo -u :-/
# This will setup borg as the currently logged in user, hopefully. fingers crossed!
# Workflow:
# 1. borg setup policy runs from capser, loading this script
# 2. this scrpt runs, creating launchagents that call borg_passgen and borg_init
# 3. those scripts are executed as the user
# 4. the passgen files are made and casper catches them during an inventory update
# 5. within an hour, an initial backup is made
# Set up Variables
repo_server_address="example.org"
APP_PATH="/var/exmaple"
BORG_PASSGEN="${APP_PATH}/borg_passgen"
BORG_INIT="${APP_PATH}/borg_init"
PASSGEN_ID="org.example.borg.passgen"
INIT_ID="org.example.borg.init"
BORG_BACKUP="${APP_PATH}/borg_backup"
HOURLY_ID="org.example.borg.hourly"
# Get logged in user
loggedInUserName=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
# the UID is needed to run the LaunchAgent
loggedInUserID=$(stat -f%u /dev/console)
ssh-keyscan ${repo_server_address} >>/Users/"${loggedInUserName}"/.ssh/known_hosts
# Check to see if we can reach the backup server
# Stolen from https://github.com/rtrouton/CasperCheck/blob/master/script/caspercheck.sh
# Thanks, Rich.
CheckForNetwork(){
# Determine if the network is up by looking for any non-loopback network interfaces.
local test
if [[ -z "${NETWORKUP:=}" ]]; then
test=$(/sbin/ifconfig -a inet 2>/dev/null | sed -n -e '/127.0.0.1/d' -e '/0.0.0.0/d' -e '/inet/p' | wc -l)
if [[ "${test}" -gt 0 ]]; then
NETWORKUP="-YES-"
else
NETWORKUP="-NO-"
fi
fi
}
CheckSiteNetwork (){
site_network="False"
ssh_check=$(echo quit | telnet ${repo_server_address} 22 2>/dev/null | grep Connected)
if [[ "${ssh_check}" == "Connected to ${repo_server_address}." ]]; then
site_network="True"
else
site_network="False"
fi
}
borgpassgen () {
echo "Creating passgen launchd"
# Create a LaunchAgent for the logged in user to call borg_passgen
(
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${PASSGEN_ID}</string>
<key>Program</key>
<string>${BORG_PASSGEN}</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
) > /Library/LaunchAgents/${PASSGEN_ID}.plist
echo "Loading passgen launchd"
# Load the above LaunchAgent as the logged in user
launchctl bootstrap gui/"${loggedInUserID}" /Library/LaunchAgents/${PASSGEN_ID}.plist
echo "Sleeping"
# not sure how long I need to sleep here
sleep 30
echo "UNloading passgen launchd"
# Unload and destroy the above LaunchAgent
launchctl bootout gui/"${loggedInUserID}" /Library/LaunchAgents/${PASSGEN_ID}.plist
rm -f /Library/LaunchAgents/${PASSGEN_ID}.plist
}
borginit () {
echo "Creating init launchd"
# Create a LaunchAgent for the logged in user to call borg_init
(
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${INIT_ID}</string>
<key>Program</key>
<string>${BORG_INIT}</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
) > /Library/LaunchAgents/${INIT_ID}.plist
echo "Loading init launchd"
# Load the above LaunchAgent as the logged in user
launchctl bootstrap gui/"${loggedInUserID}" /Library/LaunchAgents/${INIT_ID}.plist
echo "Sleeping"
# not sure how long I need to sleep here.
sleep 30
echo "UNloading init launchd"
# Unload and destroy the above LaunchAgent
launchctl bootout gui/"${loggedInUserID}" /Library/LaunchAgents/${INIT_ID}.plist
rm -f /Library/LaunchAgents/${INIT_ID}.plist
}
borgbackup () {
echo "Creating borg_backup launchd"
# Create a LaunchAgent for the logged in user to call borg_backup hourly
(
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${HOURLY_ID}</string>
<key>ProgramArguments</key>
<array>
<string>${BORG_BACKUP}</string>
</array>
<key>StartInterval</key>
<integer>3600</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
) > /Library/LaunchAgents/${HOURLY_ID}.plist
echo "Loading borg_backup launchd"
# Load the above LaunchAgent as the logged in user
launchctl bootstrap gui/"${loggedInUserID}" /Library/LaunchAgents/${HOURLY_ID}.plist
}
# Wait up to 30 minutes for a network connection to become
# available which doesn't use a loopback address. This
# is a condition which may occur if this script is run by
# a LaunchDaemon at boot time.
#
# The network connection check will occur every 5 seconds
# until the 30 minute limit is reached.
echo "Checking for active network connection."
CheckForNetwork
i=1
while [[ "${NETWORKUP}" != "-YES-" ]] && [[ $i -ne 360 ]]
do
sleep 5
NETWORKUP=
CheckForNetwork
echo $i
i=$(( $i + 1 ))
done
# If no network connection is found within 30 minutes,
# the script will exit.
if [[ "${NETWORKUP}" != "-YES-" ]]; then
echo "Error: Network connection check failed."
exit 1
fi
if [[ "${NETWORKUP}" == "-YES-" ]]; then
echo "Network connection appears to be live."
# Sleeping for 60 seconds to give WiFi time to come online.
echo "Pausing for 1 minute to give WiFi and DNS time to come online."
sleep 60
CheckSiteNetwork
if [[ "$site_network" == "False" ]]; then
echo "Error: Site network verification failed."
exit 1
fi
if [[ "$site_network" == "True" ]]; then
echo "Access to site network verified"
borgpassgen
borginit
borgbackup
fi
fi