-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextcloudmount.sh
More file actions
140 lines (123 loc) · 4.18 KB
/
nextcloudmount.sh
File metadata and controls
140 lines (123 loc) · 4.18 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
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
#!/bin/bash
# Move Nextcloud data to another partition (for example an HDD) and mount it
# Author: Robin Labadie
# Website: https://www.lrob.fr
############
# Settings #
############
## Directories
# Where you want to move your Nextcloud Instances
clouddir="/srv/hdd-data/nextcloud_instances"
# Where your files are hosted (if not using Plesk, you might be able to adapt the script changing this)
hostdir="/var/www/vhosts"
# The location of the partent dir for the "data" dir of Nextcloud relative to the root directory for the domain
ncdir=".nextcloud"
# This one should probably suit any configuration for years
ncdatadir="${ncdir}/data"
#############
## Program ##
#############
## Misc Variables ##
selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
# Download bash API early (needed for fn_echo/fn_logecho)
if [ ! -f "ultimate-bash-api.sh" ]; then
wget https://raw.githubusercontent.com/UltimateByte/ultimate-bash-api/master/ultimate-bash-api.sh
chmod +x ultimate-bash-api.sh
fi
# shellcheck disable=SC1091
source ultimate-bash-api.sh
# Check that the script is launched with elevated privileges
if [ "$(id -u)" != "0" ]; then
fn_echo "[ERROR] This script must be run with elevated privileges"
exit 1
fi
# Check user input
# If nothing has been inputted
if [ -z "$1" ]; then
# Info about script usage
fn_echo "[ERROR] Please, specify FQN to migrate Nextcloud Data for."
exit 1
# If there is too much args
elif [ -n "$2" ]; then
fn_echo "[ERROR] Too many arguments!"
exit 1
else
# Defining domain name
fqn="${1}"
# Defining hosting path
sitedir="${hostdir}/${fqn}"
# Defining config file path (not used for now)
configfile="${sitedir}/httpdocs/config/config.php"
# Defining Nextcloud data path
sitencdatadir="${sitedir}/${ncdatadir}"
sitencdir="${sitedir}/${ncdir}"
# Testing if everything is OK
if [ ! -d "${sitedir}" ]; then
fn_logecho "[ERROR] Path ${sitedir} not found. Did you misspell the domain name?"
exit 1
elif [ ! -d "${sitencdatadir}" ]; then
fn_logecho "[ERROR] Path ${sitencdatadir} not found. Is this instance based on the usual architecture?"
exit 1
else
fn_logecho "NC Data found at ${sitencdatadir}. Proceeding..."
sleep 1
fi
fi
# Check if already in fstab
if grep -q "${sitencdatadir}" /etc/fstab; then
fn_logecho "[ERROR] Entry for ${sitencdatadir} already exists in /etc/fstab. Aborting."
exit 1
fi
# Finding username
username=$(stat "${sitencdatadir}" -c "%U")
if [ -n "${username}" ]; then
fn_logecho "Found username ${username}"
sleep 1
else
fn_logecho "[ERROR] Username could not be found, most likely bogus in this script."
exit 1
fi
# Check that target base directory exists
if [ ! -d "${clouddir}" ]; then
fn_logecho "[ERROR] Base directory ${clouddir} does not exist. Create it first."
exit 1
fi
# Creating path
fn_logecho "Creating ${clouddir}/${username}"
if ! mkdir "${clouddir}/${username}"; then
fn_logecho "[ERROR] Failed to create ${clouddir}/${username}"
exit 1
fi
fn_logecho "Creating ${clouddir}/${username}/data"
if ! mkdir "${clouddir}/${username}/data"; then
fn_logecho "[ERROR] Failed to create ${clouddir}/${username}/data"
exit 1
fi
sleep 1
fn_logecho "Setting permissions: chown ${username}:psaserv ${clouddir}/${username}"
chown "${username}:psaserv" "${clouddir}/${username}"
chown "${username}:psaserv" "${clouddir}/${username}/data"
sleep 1
# Moving files (dotglob includes hidden files)
shopt -s dotglob
fn_logecho "Moving ${sitencdatadir}/* to ${clouddir}/${username}/data/"
if ! mv "${sitencdatadir}"/* "${clouddir}/${username}/data/"; then
fn_logecho "[ERROR] Failed to move data. Check permissions and disk space."
exit 1
fi
sleep 1
# Ensure mount point has correct ownership
chown "${username}:psaserv" "${sitencdatadir}"
# Add fstab entry and mount
fn_logecho "Adding to /etc/fstab: ${clouddir}/${username}/data ${sitencdatadir} none defaults,bind 0 0"
sleep 1
echo "${clouddir}/${username}/data ${sitencdatadir} none defaults,bind 0 0" >> /etc/fstab
fn_logecho "Reloading systemd and mounting"
systemctl daemon-reload
if ! mount -a; then
fn_logecho "[ERROR] mount -a failed. Check /etc/fstab for errors."
exit 1
fi
sleep 1
fn_logecho "Job done, I don't know who made that script but it's very handy for this very specific situation!"
exit 0