]> git.p6c8.net - nextcloud-backup-restore.git/blob - setup.sh
9af4338adfa3e58c30450c52fe2516bf111835ff
[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.0.0
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
29 #
30 # Gather information
31 #
32 read -p "In which directory the backups should be saved (default: ${backupMainDir}). Enter a directory or press ENTER if the backup directory should be ${backupMainDir}: " BACKUPMAINDIR
33
34 [ -z "$BACKUPMAINDIR" ] || backupMainDir=$BACKUPMAINDIR
35
36 read -p "Enter the path to the Nextcloud file directory (usually ${nextcloudFileDir}). Enter a directory or press ENTER if the file directory is ${nextcloudFileDir}: " NEXTCLOUDFILEDIRECTORY
37
38 [ -z "$NEXTCLOUDFILEDIRECTORY" ] || nextcloudFileDir=$NEXTCLOUDFILEDIRECTORY
39
40 read -p "Enter the webserver user (usually ${webserverUser}). Enter an new user or press ENTER if the webserver user is ${webserverUser}: " WEBSERVERUSER
41
42 [ -z "$WEBSERVERUSER" ] || webserverUser=$WEBSERVERUSER
43
44 echo ""
45 echo ""
46 echo "Backup directory: ${backupMainDir}"
47 echo "Nextcloud file directory: ${nextcloudFileDir}"
48 echo "Webserver user: ${webserverUser}"
49 echo ""
50 read -p "Is the information correct? [y/N] " CORRECTINFO
51
52 if [ "$CORRECTINFO" != 'y' ] ; then
53 echo "ABORTING!"
54 echo "No file has been altered."
55 exit 1
56 fi
57
58 function occ_get() {
59 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ config:system:get "$1"
60 }
61
62 # Make test call to OCC
63 occ_get datadirectory
64
65 if [ $? -ne 0 ]; then
66 echo "Error calling OCC: Please check if the information provided was correct."
67 echo "ABORTING!"
68 echo "No file has been altered."
69 exit 1
70 fi
71
72 #
73 # Read data from OCC and write to backup/restore scripts.
74 #
75
76 echo ""
77 echo ""
78 echo "Modifying NextcloudBackup.sh and NextcloudRestore.sh to match your installation..."
79 echo ""
80
81 # Backup main dir
82 sed -i "s@^ backupMainDir.*@ backupMainDir='$backupMainDir'@" ./NextcloudBackup.sh
83 sed -i "s@^ backupMainDir.*@ backupMainDir='$backupMainDir'@" ./NextcloudRestore.sh
84
85 # Nextcloud file dir
86 sed -i "s@^nextcloudFileDir.*@nextcloudFileDir='$nextcloudFileDir'@" ./NextcloudBackup.sh
87 sed -i "s@^nextcloudFileDir.*@nextcloudFileDir='$nextcloudFileDir'@" ./NextcloudRestore.sh
88
89 # Nextcloud data dir
90 nextcloudDataDir=$(occ_get datadirectory)
91
92 sed -i "s@^nextcloudDataDir=.*@nextcloudDataDir='$nextcloudDataDir'@" ./NextcloudBackup.sh
93 sed -i "s@^nextcloudDataDir=.*@nextcloudDataDir='$nextcloudDataDir'@" ./NextcloudRestore.sh
94
95 # Webserver service name
96
97 # Webserver user
98 sed -i "s/^webserverUser.*/webserverUser='$webserverUser'/" ./NextcloudBackup.sh
99 sed -i "s/^webserverUser.*/webserverUser='$webserverUser'/" ./NextcloudRestore.sh
100
101 # Database system
102 databaseSystem=$(occ_get dbtype)
103
104 sed -i "s/^databaseSystem.*/databaseSystem='$databaseSystem'/" ./NextcloudBackup.sh
105 sed -i "s/^databaseSystem.*/databaseSystem='$databaseSystem'/" ./NextcloudRestore.sh
106
107 # Database
108 nextcloudDatabase=$(occ_get dbname)
109
110 sed -i "s/^nextcloudDatabase.*/nextcloudDatabase='$nextcloudDatabase'/" ./NextcloudBackup.sh
111 sed -i "s/^nextcloudDatabase.*/nextcloudDatabase='$nextcloudDatabase'/" ./NextcloudRestore.sh
112
113 # Database user
114 dbUser=$(occ_get dbuser)
115
116 sed -i "s/^dbUser.*/dbUser='$dbUser'/" ./NextcloudBackup.sh
117 sed -i "s/^dbUser.*/dbUser='$dbUser'/" ./NextcloudRestore.sh
118
119 # Database password
120 dbPassword=$(occ_get dbpassword)
121
122 sed -i "s/^dbPassword.*/dbPassword='$dbPassword'/" ./NextcloudBackup.sh
123 sed -i "s/^dbPassword.*/dbPassword='$dbPassword'/" ./NextcloudRestore.sh
124
125 echo ""
126 echo "Done!"
127 echo ""
128 echo ""
129 echo "IMPORTANT: Please check NextcloudBackup.sh and NextcloudRestore.sh if all variables were set correctly BEFORE running these scripts!"
130 echo ""
131 echo ""

patrick-canterino.de