This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
setup.sh
285 lines (233 loc) · 8.93 KB
/
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
#
# Bash script for an easy setup of Nextcloud backup/restore scripts.
#
# Version 3.0.3
#
# Usage:
# - call the setup.sh script
# - Enter the required information
# - A central configuration file `NextcloudBackupRestore.conf` will be created to match you Nextcloud instance.
# - This configuration file then is used by the backup/restore scripts.
#
# The script is based on an installation of Nextcloud using nginx and MariaDB, see https://decatec.de/home-server/nextcloud-auf-ubuntu-server-18-04-lts-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/
#
#
# IMPORTANT
# The setup.sh script automated the configuration for the backup/restore scripts (file `NextcloudBackupRestore.conf`).
# However, you should always check this configuration BEFORE executing these!
#
# Make sure the script exits when any command fails
set -Eeuo pipefail
#
# Pre defined variables
#
backupMainDir='/media/hdd/nextcloud_backup'
nextcloudFileDir='/var/www/nextcloud'
webserverUser='www-data'
webserverServiceName='nginx'
useCompression=true
includeUpdaterBackups=false
NextcloudBackupRestoreConf='NextcloudBackupRestore.conf' # Holds the configuration for NextcloudBackup.sh and NextcloudRestore.sh
#
# Gather information
#
clear
echo "Enter the directory to which the backups should be saved."
echo "Default: ${backupMainDir}"
echo ""
read -p "Enter a directory or press ENTER if the backup directory should be ${backupMainDir}: " BACKUPMAINDIR
[ -z "$BACKUPMAINDIR" ] || backupMainDir=$BACKUPMAINDIR
clear
echo "Enter the path to the Nextcloud file directory."
echo "Usually: ${nextcloudFileDir}"
echo ""
read -p "Enter a directory or press ENTER if the file directory is ${nextcloudFileDir}: " NEXTCLOUDFILEDIRECTORY
[ -z "$NEXTCLOUDFILEDIRECTORY" ] || nextcloudFileDir=$NEXTCLOUDFILEDIRECTORY
clear
echo "Enter the webserver user."
echo "Usually: ${webserverUser}"
echo ""
read -p "Enter an new user or press ENTER if the webserver user is ${webserverUser}: " WEBSERVERUSER
[ -z "$WEBSERVERUSER" ] || webserverUser=$WEBSERVERUSER
clear
echo "Enter the webserver service name."
echo "Usually: nginx or apache2"
echo ""
read -p "Enter an new webserver service name or press ENTER if the webserver service name is ${webserverServiceName}: " WEBSERVERSERVICENAME
[ -z "$WEBSERVERSERVICENAME" ] || webserverServiceName=$WEBSERVERSERVICENAME
clear
echo ""
read -p "Should the backed up data be compressed (pigz should be installed in the machine)? [Y/n]: " USECOMPRESSION
if [ "$USECOMPRESSION" == 'n' ] ; then
useCompression=false
fi
clear
echo ""
read -p "Should the backups created by the Nextcloud updater be included in the backups (usually not necessary)? [y/N]: " INCLUDEPDATERBACKUPS
if [ "$INCLUDEPDATERBACKUPS" == 'y' ] ; then
includeUpdaterBackups=true
fi
clear
echo "How many backups should be kept?"
echo "If this is set to '0', no backups will be deleted."
echo ""
read -p "How many backups should be kept (default: '0'): " MAXNUMBEROFBACKUPS
maxNrOfBackups=0
if ! [ -z "$MAXNUMBEROFBACKUPS" ] && ! [[ "$MAXNUMBEROFBACKUPS" =~ ^[0-9]+$ ]] ; then
echo "ERROR: Number of backups must be a positive integer!"
echo ""
echo "ABORTING!"
echo "No file has been altered."
exit 1
fi
[ -z "$MAXNUMBEROFBACKUPS" ] || maxNrOfBackups=$MAXNUMBEROFBACKUPS
clear
echo "Backup directory: ${backupMainDir}"
echo "Nextcloud file directory: ${nextcloudFileDir}"
echo "Webserver user: ${webserverUser}"
echo "Webserver service name: ${webserverServiceName}"
if [ "$useCompression" = true ] ; then
echo "Compression: yes"
else
echo "Compression: no"
fi
if [ "$includeUpdaterBackups" = true ] ; then
echo "Include backups from the Nextcloud updater: yes"
else
echo "Include backups from the Nextcloud updater: no"
fi
echo "Number of backups to keep (0: keep all backups): ${maxNrOfBackups}"
echo ""
read -p "Is the information correct? [y/N] " CORRECTINFO
if [ "$CORRECTINFO" != 'y' ] ; then
echo ""
echo "ABORTING!"
echo "No file has been altered."
exit 1
fi
function occ_get() {
sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ config:system:get "$1"
}
# Make test call to OCC
occ_get datadirectory >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo ""
echo "Error calling OCC: Please check if the information provided was correct."
echo "ABORTING!"
echo "No file has been altered."
exit 1
fi
#
# Read data from OCC and write to config file.
#
if [ -e "$NextcloudBackupRestoreConf" ] ; then
echo -e "\n\nSaving existing $NextcloudBackupRestoreConf to ${NextcloudBackupRestoreConf}_bak"
cp --force "$NextcloudBackupRestoreConf" "${NextcloudBackupRestoreConf}_bak"
fi
echo ""
echo ""
echo "Creating $NextcloudBackupRestoreConf to match your Nextcloud instance..."
echo ""
# Nextcloud data dir
nextcloudDataDir=$(occ_get datadirectory)
# Database system
databaseSystem=$(occ_get dbtype)
# PostgreSQL is identified as pgsql
if [ "${databaseSystem,,}" = "pgsql" ]; then
databaseSystem='postgresql';
fi
# Database
nextcloudDatabase=$(occ_get dbname)
# Database user
dbUser=$(occ_get dbuser)
# Database password
dbPassword=$(occ_get dbpassword)
# File names for backup files
fileNameBackupFileDir='nextcloud-filedir.tar'
fileNameBackupDataDir='nextcloud-datadir.tar'
if [ "$useCompression" = true ] ; then
fileNameBackupFileDir='nextcloud-filedir.tar.gz'
fileNameBackupDataDir='nextcloud-datadir.tar.gz'
fi
fileNameBackupExternalDataDir=''
if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
if [ "$useCompression" = true ] ; then
fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
fi
fi
fileNameBackupDb='nextcloud-db.sql'
{ echo "# Configuration for Nextcloud-Backup-Restore scripts"
echo ""
echo "# TODO: The main backup directory"
echo "backupMainDir='$backupMainDir'"
echo ""
echo "# TODO: Use compression for file/data dir"
echo "# When this is the only script for backups, it is recommend to enable compression."
echo "# If the output of this script is used in another (compressing) backup (e.g. borg backup),"
echo "# you should probably disable compression here and only enable compression of your main backup script."
echo "useCompression=true"
echo ""
echo "# TOOD: The bare tar command for using compression while backup."
echo "# Use 'tar -cpzf' if you want to use gzip compression."
echo "compressionCommand='tar -I pigz -cpf'"
echo ""
echo "# TOOD: The bare tar command for using compression while restoring."
echo "# Use 'tar -xmpzf' if you want to use gzip compression."
echo "extractCommand='tar -I pigz -xmpf'"
echo ""
echo "# TODO: File names for backup files"
echo "fileNameBackupFileDir='$fileNameBackupFileDir'"
echo "fileNameBackupDataDir='$fileNameBackupDataDir'"
echo "fileNameBackupExternalDataDir='$fileNameBackupExternalDataDir'"
echo "fileNameBackupDb='$fileNameBackupDb'"
echo ""
echo "# TODO: The directory of your Nextcloud installation (this is a directory under your web root)"
echo "nextcloudFileDir='$nextcloudFileDir'"
echo ""
echo "# TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)"
echo "# If your data directory is located under Nextcloud's file directory (somewhere in the web root),"
echo "# the data directory should not be a separate part of the backup"
echo "nextcloudDataDir='$nextcloudDataDir'"
echo ""
echo "# TODO: The directory of your Nextcloud's local external storage."
echo "# Uncomment if you use local external storage."
echo "#nextcloudLocalExternalDataDir='/var/nextcloud_external_data'"
echo ""
echo "# TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')"
echo "webserverServiceName='$webserverServiceName'"
echo ""
echo "# TODO: Your web server user"
echo "webserverUser='$webserverUser'"
echo ""
echo "# TODO: The name of the database system (one of: mysql, mariadb, postgresql)"
echo "databaseSystem='$databaseSystem'"
echo ""
echo "# TODO: Your Nextcloud database name"
echo "nextcloudDatabase='$nextcloudDatabase'"
echo ""
echo "# TODO: Your Nextcloud database user"
echo "dbUser='$dbUser'"
echo ""
echo "# TODO: The password of the Nextcloud database user"
echo "dbPassword='$dbPassword'"
echo ""
echo "# TODO: The maximum number of backups to keep (when set to 0, all backups are kept)"
echo "maxNrOfBackups=$maxNrOfBackups"
echo ""
echo "# TODO: Setting to include/exclude the backup directory of the Nextcloud updater"
echo "# Set to true in order to include the backups of the Nextcloud updater"
echo "includeUpdaterBackups=$includeUpdaterBackups"
} > ./"${NextcloudBackupRestoreConf}"
echo ""
echo "Done!"
echo ""
echo ""
echo "IMPORTANT: Please check $NextcloudBackupRestoreConf if all variables were set correctly BEFORE running the backup/restore scripts!"
if [ "$useCompression" = true ] ; then
echo ""
echo "As compression should be used for backups, please make sure that 'pigz' is installed (e.g. for Debian/Ubuntu: apt install pigz)"
fi
echo ""
echo ""