-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-nas-setup.sh
executable file
·107 lines (73 loc) · 2.25 KB
/
2-nas-setup.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
#!/bin/bash
set -e
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root." >&2
exit 1
fi
SCRIPT_DIR=$(dirname -- "$( readlink -f -- "$0"; )")
if [ ! -f "$SCRIPT_DIR/parameters.sh" ]; then
printf "Canont load parameters from ./parameters.sh\n"
exit 1
fi
printf "\n== Loading parameters ==\n"
source "$SCRIPT_DIR/parameters.sh"
if [ -z "$DISK_FULL_UUID_PATH" ] || [ -z "$DISK_FULL_MOUNT_PATH" ]; then
printf "Not all required variables defined in the parameters file.\n"
exit 1
fi
function setup_disk_automount() {
printf "\n== Automount disk ==\n"
# Remove trailing slash
DISK_FULL_MOUNT_PATH="${DISK_FULL_MOUNT_PATH%/}"
# Remove leading slash and then replace "/" with "-"
systemd_service_prefix="${DISK_FULL_MOUNT_PATH#/}"
systemd_service_prefix="${systemd_service_prefix//\//-}"
mkdir -p "$DISK_FULL_MOUNT_PATH"
cat >"/etc/systemd/system/$systemd_service_prefix.mount" <<EOF
[Unit]
Description=Mount $DISK_FULL_UUID_PATH
[Mount]
Where=$DISK_FULL_MOUNT_PATH
What=$DISK_FULL_UUID_PATH
Type=btrfs
Options=defaults,nofail
[Install]
WantedBy=multi-user.target
EOF
cat >"/etc/systemd/system/$systemd_service_prefix.automount" <<EOF
[Unit]
Description=Automount $DISK_FULL_UUID_PATH
[Automount]
Where=$DISK_FULL_MOUNT_PATH
TimeoutIdleSec=300
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start "$systemd_service_prefix.automount"
systemctl enable "$systemd_service_prefix.automount"
}
function configure_ssh_for_sftp() {
printf "\n== Configure SSH for SFTP ==\n"
sed -i '/^\s*Subsystem\s*sftp/ s/^/#/' /etc/ssh/sshd_config
cat >"/etc/ssh/sshd_config.d/sftp.conf" <<EOF
Subsystem sftp internal-sftp
Match Group sftpusers
X11Forwarding no
AllowTcpForwarding no
ChrootDirectory $DISK_FULL_MOUNT_PATH/sftp/%u
ForceCommand internal-sftp
EOF
systemctl restart sshd
}
function install_snapshot_cron_script() {
printf "\n== Installing the script 'create-btrfs-snapshot.sh' as cron ==\n"
mkdir -p /opt/
cp "$SCRIPT_DIR/create-btrfs-snapshot.sh" /opt/
cat >"/etc/cron.d/sftp-disk-snapthots" <<EOF
30 3 * * 0 root /opt/create-btrfs-snapshot.sh "$DISK_FULL_MOUNT_PATH"
EOF
}
setup_disk_automount
configure_ssh_for_sftp
install_snapshot_cron_script