forked from takigama/docker-borg-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateuser.sh
More file actions
executable file
·66 lines (49 loc) · 1.77 KB
/
Copy pathcreateuser.sh
File metadata and controls
executable file
·66 lines (49 loc) · 1.77 KB
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
#!/usr/bin/env bash
# this script will get prettier, I promise
set -euo pipefail
usage() {
echo "Usage: $0 username ssh-key"
}
# clean our username
clean() {
local username=$1
username="${username//_/}"
username="${username// /_}"
username="${username//[^a-zA-Z0-9]/}"
username="${username,,}"
echo "$username"
}
if [[ -z "$1" ]] || [[ -z "$2" ]]; then
usage
exit 1
fi
# "scrub" the username
username=$(clean "$1")
if [[ ${#username} -gt 12 ]]; then
echo "Username can't be longer than 12 characters"
exit 1
fi
job="unknown"
if adduser -D -H -g "Borg Backup $username" "$username" -h "/backups/$username" >/dev/null 2>&1; then
job="created"
else
# the assumption is a non-0 exit status is "user exists".. should really be a bit more checking on that
job="checked"
fi
mkdir -p "/backups/$username/repo"
# on alpine, for some reason the account is locked by default
passwd -u "$username" &> /dev/null
sshkey=$(sed -E 's/.*(ssh-.*)/\1/' <<<"$2")
echo "command=\"cd \\\"/backups/$username/repo\\\" && borg serve --restrict-to-path \\\"/backups/$username/repo\\\"\",restrict $sshkey" > /config/users/"$username"
# make sure the user key is basically unmodifable
chown "root:$username" "/config/users/$username"
chmod 640 "/config/users/$username"
# make root own the user home directory, but group is for the user
chown "root:$username" "/backups/$username"
# make the user home directory un-readable by anyone except root
chmod 710 "/backups/$username"
# ensure permissions for the repo directory are writable and owned by the user doing the backups
chown -R "$username:$username" "/backups/$username/repo"
# set permissions for the user
chmod -R 770 "/backups/$username/repo"
echo "User $username $job, backup path is /backups/$username/repo"