]> git.p6c8.net - nextcloud-backup-restore.git/blob - setup.sh
43fcee45320c8d0671d31db0157d16878b57f984
[nextcloud-backup-restore.git] / setup.sh
1 #!/bin/bash
2
3 #
4 # Bash script an easy setup of NextcloudBackup.sh and NextcloudRestore.sh
5 #
6 # Version 2.3.2
7 #
8 # Usage:
9 # - call the setup.sh script
10 # - Enter the required information
11 # - You NextcloudBackup.sh and NextcloudRestore.sh scripts will be tailored to match you installation.
12 #
13 # 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/
14 #
15
16 #
17 # IMPORTANT
18 # The setup.sh script automated the configuration for the backup/restore scripts.
19 # However, you should always check the backup/restore scripts BEFORE executing these!
20 #
21
22 # Make sure the script exits when any command fails
23 set -Eeuo pipefail
24
25 #
26 # Pre defined variables
27 #
28 backupMainDir='/media/hdd/nextcloud_backup'
29 nextcloudFileDir='/var/www/nextcloud'
30 webserverUser='www-data'
31 webserverServiceName='nginx'
32
33 #
34 # Gather information
35 #
36 clear
37
38 echo "Enter the directory to which the backups should be saved."
39 echo "Default: ${backupMainDir}"
40 echo ""
41 read -p "Enter a directory or press ENTER if the backup directory should be ${backupMainDir}: " BACKUPMAINDIR
42
43 [ -z "$BACKUPMAINDIR" ] || backupMainDir=$BACKUPMAINDIR
44 clear
45
46 echo "Enter the path to the Nextcloud file directory."
47 echo "Usually: ${nextcloudFileDir}"
48 echo ""
49 read -p "Enter a directory or press ENTER if the file directory is ${nextcloudFileDir}: " NEXTCLOUDFILEDIRECTORY
50
51 [ -z "$NEXTCLOUDFILEDIRECTORY" ] || nextcloudFileDir=$NEXTCLOUDFILEDIRECTORY
52 clear
53
54 echo "Enter the webserver user."
55 echo "Usually: ${webserverUser}"
56 echo ""
57 read -p "Enter an new user or press ENTER if the webserver user is ${webserverUser}: " WEBSERVERUSER
58
59 [ -z "$WEBSERVERUSER" ] || webserverUser=$WEBSERVERUSER
60 clear
61
62 echo "Enter the webserver service name."
63 echo "Usually: nginx or apache2"
64 echo ""
65 read -p "Enter an new webserver service name or press ENTER if the webserver service name is ${webserverServiceName}: " WEBSERVERSERVICENAME
66
67 [ -z "$WEBSERVERSERVICENAME" ] || webserverServiceName=$WEBSERVERSERVICENAME
68 clear
69
70 echo "Backup directory: ${backupMainDir}"
71 echo "Nextcloud file directory: ${nextcloudFileDir}"
72 echo "Webserver user: ${webserverUser}"
73 echo "Webserver service name: ${webserverServiceName}"
74 echo ""
75 read -p "Is the information correct? [y/N] " CORRECTINFO
76
77 if [ "$CORRECTINFO" != 'y' ] ; then
78 echo "ABORTING!"
79 echo "No file has been altered."
80 exit 1
81 fi
82
83 function occ_get() {
84 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ config:system:get "$1"
85 }
86
87 # Make test call to OCC
88 occ_get datadirectory
89
90 if [ $? -ne 0 ]; then
91 echo "Error calling OCC: Please check if the information provided was correct."
92 echo "ABORTING!"
93 echo "No file has been altered."
94 exit 1
95 fi
96
97 #
98 # Read data from OCC and write to backup/restore scripts.
99 #
100
101 echo ""
102 echo ""
103 echo "Modifying NextcloudBackup.sh and NextcloudRestore.sh to match your installation..."
104 echo ""
105
106 # Backup main dir
107 sed -i "s@^\tbackupMainDir='/media/hdd/nextcloud_backup'@\tbackupMainDir='$backupMainDir'@" ./NextcloudBackup.sh
108 sed -i "s@^\tbackupMainDir='/media/hdd/nextcloud_backup'@\tbackupMainDir='$backupMainDir'@" ./NextcloudRestore.sh
109
110 # Nextcloud file dir
111 sed -i "s@^nextcloudFileDir.*@nextcloudFileDir='$nextcloudFileDir'@" ./NextcloudBackup.sh
112 sed -i "s@^nextcloudFileDir.*@nextcloudFileDir='$nextcloudFileDir'@" ./NextcloudRestore.sh
113
114 # Nextcloud data dir
115 nextcloudDataDir=$(occ_get datadirectory)
116
117 sed -i "s@^nextcloudDataDir=.*@nextcloudDataDir='$nextcloudDataDir'@" ./NextcloudBackup.sh
118 sed -i "s@^nextcloudDataDir=.*@nextcloudDataDir='$nextcloudDataDir'@" ./NextcloudRestore.sh
119
120 # Webserver service name
121 sed -i "s/^webserverServiceName.*/webserverServiceName='$webserverServiceName'/" ./NextcloudBackup.sh
122 sed -i "s/^webserverServiceName.*/webserverServiceName='$webserverServiceName'/" ./NextcloudRestore.sh
123
124 # Webserver user
125 sed -i "s/^webserverUser.*/webserverUser='$webserverUser'/" ./NextcloudBackup.sh
126 sed -i "s/^webserverUser.*/webserverUser='$webserverUser'/" ./NextcloudRestore.sh
127
128 # Database system
129 databaseSystem=$(occ_get dbtype)
130
131 # PostgreSQL is identified as pgsql
132 if [ "${databaseSystem,,}" = "pgsql" ]; then
133 databaseSystem='postgresql';
134 fi
135
136 sed -i "s/^databaseSystem.*/databaseSystem='$databaseSystem'/" ./NextcloudBackup.sh
137 sed -i "s/^databaseSystem.*/databaseSystem='$databaseSystem'/" ./NextcloudRestore.sh
138
139 # Database
140 nextcloudDatabase=$(occ_get dbname)
141
142 sed -i "s/^nextcloudDatabase.*/nextcloudDatabase='$nextcloudDatabase'/" ./NextcloudBackup.sh
143 sed -i "s/^nextcloudDatabase.*/nextcloudDatabase='$nextcloudDatabase'/" ./NextcloudRestore.sh
144
145 # Database user
146 dbUser=$(occ_get dbuser)
147
148 sed -i "s/^dbUser.*/dbUser='$dbUser'/" ./NextcloudBackup.sh
149 sed -i "s/^dbUser.*/dbUser='$dbUser'/" ./NextcloudRestore.sh
150
151 # Database password
152 dbPassword=$(occ_get dbpassword)
153
154 sed -i "s/^dbPassword.*/dbPassword='$dbPassword'/" ./NextcloudBackup.sh
155 sed -i "s/^dbPassword.*/dbPassword='$dbPassword'/" ./NextcloudRestore.sh
156
157 echo ""
158 echo "Done!"
159 echo ""
160 echo ""
161 echo "IMPORTANT: Please check NextcloudBackup.sh and NextcloudRestore.sh if all variables were set correctly BEFORE running these scripts!"
162 echo ""
163 echo "When using pigz compression, you also have to install pigz (e.g. for Debian/Ubuntu: apt install pigz)"
164 echo ""
165 echo ""

patrick-canterino.de