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

patrick-canterino.de