]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
Remove trailing slash when dir is given by parameter
[nextcloud-backup-restore.git] / NextcloudRestore.sh
1 #!/bin/bash
2
3 #
4 # Bash script for restoring backups of Nextcloud.
5 #
6 # Version 2.1.0
7 #
8 # Usage:
9 # - With backup directory specified in the script: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
10 # - With backup directory specified by parameter: ./NextcloudRestore.sh <BackupName> <BackupDirectory> (e.g. ./NextcloudRestore.sh 20170910_132703 /media/hdd/nextcloud_backup)
11 #
12 # 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/
13 #
14
15 #
16 # IMPORTANT
17 # You have to customize this script (directories, users, etc.) for your actual environment.
18 # All entries which need to be customized are tagged with "TODO".
19 #
20
21 # Variables
22 restore=$1
23 backupMainDir=$2
24
25 if [ -z "$backupMainDir" ]; then
26 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
27 backupMainDir='/media/hdd/nextcloud_backup'
28 fi
29
30 echo "Backup directory: $backupMainDir"
31
32 # TODO: Set this to true, if the backup was created with compression enabled, otherwiese false.
33 useCompression=true
34
35 currentRestoreDir="${backupMainDir}/${restore}"
36
37 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
38 nextcloudFileDir='/var/www/nextcloud'
39
40 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
41 # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be restored separately
42 nextcloudDataDir='/var/nextcloud_data'
43
44 # TODO: The directory of your Nextcloud's local external storage.
45 # Uncomment if you use local external storage.
46 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
47
48 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
49 webserverServiceName='nginx'
50
51 # TODO: Your web server user
52 webserverUser='www-data'
53
54 # TODO: The name of the database system (one of: mysql, mariadb, postgresql)
55 databaseSystem='mariadb'
56
57 # TODO: Your Nextcloud database name
58 nextcloudDatabase='nextcloud_db'
59
60 # TODO: Your Nextcloud database user
61 dbUser='nextcloud_db_user'
62
63 # TODO: The password of the Nextcloud database user
64 dbPassword='mYpAsSw0rd'
65
66 # File names for backup files
67 # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
68 fileNameBackupFileDir='nextcloud-filedir.tar'
69 fileNameBackupDataDir='nextcloud-datadir.tar'
70
71 if [ "$useCompression" = true ] ; then
72 fileNameBackupFileDir='nextcloud-filedir.tar.gz'
73 fileNameBackupDataDir='nextcloud-datadir.tar.gz'
74 fi
75
76 # TODO: Uncomment if you use local external storage
77 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
78 #
79 #if [ "$useCompression" = true ] ; then
80 # fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
81 #fi
82
83 fileNameBackupDb='nextcloud-db.sql'
84
85 # Function for error messages
86 errorecho() { cat <<< "$@" 1>&2; }
87
88 #
89 # Check if parameter(s) given
90 #
91 if [ $# != "1" ] && [ $# != "2" ]
92 then
93 errorecho "ERROR: No backup name to restore given, or wrong number of parameters!"
94 errorecho "Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
95 exit 1
96 fi
97
98 #
99 # Check for root
100 #
101 if [ "$(id -u)" != "0" ]
102 then
103 errorecho "ERROR: This script has to be run as root!"
104 exit 1
105 fi
106
107 #
108 # Check if backup dir exists
109 #
110 if [ ! -d "${currentRestoreDir}" ]
111 then
112 errorecho "ERROR: Backup ${restore} not found!"
113 exit 1
114 fi
115
116 #
117 # Check if the commands for restoring the database are available
118 #
119 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
120 if ! [ -x "$(command -v mysql)" ]; then
121 errorecho "ERROR: MySQL/MariaDB not installed (command mysql not found)."
122 errorecho "ERROR: No restore of database possible!"
123 errorecho "Cancel restore"
124 exit 1
125 fi
126 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
127 if ! [ -x "$(command -v psql)" ]; then
128 errorecho "ERROR: PostgreSQL not installed (command psql not found)."
129 errorecho "ERROR: No restore of database possible!"
130 errorecho "Cancel restore"
131 exit 1
132 fi
133 fi
134
135 #
136 # Set maintenance mode
137 #
138 echo "Set maintenance mode for Nextcloud..."
139 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
140 echo "Done"
141 echo
142
143 #
144 # Stop web server
145 #
146 echo "Stopping web server..."
147 systemctl stop "${webserverServiceName}"
148 echo "Done"
149 echo
150
151 #
152 # Delete old Nextcloud directories
153 #
154
155 # File directory
156 echo "Deleting old Nextcloud file directory..."
157 rm -r "${nextcloudFileDir}"
158 mkdir -p "${nextcloudFileDir}"
159 echo "Done"
160 echo
161
162 # Data directory
163 echo "Deleting old Nextcloud data directory..."
164 rm -r "${nextcloudDataDir}"
165 mkdir -p "${nextcloudDataDir}"
166 echo "Done"
167 echo
168
169 # Local external storage
170 # TODO: Uncomment if you use local external storage
171 #echo "Deleting old Nextcloud local external storage directory..."
172 #rm -r "${nextcloudLocalExternalDataDir}"
173 #mkdir -p "${nextcloudLocalExternalDataDir}"
174 #echo "Done"
175 #echo
176
177 #
178 # Restore file and data directory
179 #
180
181 # File directory
182 echo "Restoring Nextcloud file directory..."
183
184 if [ "$useCompression" = true ] ; then
185 tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
186 else
187 tar -xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
188 fi
189
190 echo "Done"
191 echo
192
193 # Data directory
194 echo "Restoring Nextcloud data directory..."
195
196 if [ "$useCompression" = true ] ; then
197 tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
198 else
199 tar -xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
200 fi
201
202 echo "Done"
203 echo
204
205 # Local external storage
206 # TODO: Uncomment if you use local external storage
207 #echo "Restoring Nextcloud data directory..."
208 #
209 #if [ "$useCompression" = true ] ; then
210 # tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
211 #else
212 # tar -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
213 #fi
214 #
215 #echo "Done"
216 #echo
217
218 #
219 # Restore database
220 #
221 echo "Dropping old Nextcloud DB..."
222
223 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
224 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
225 elif [ "${databaseSystem,,}" = "postgresql" ]; then
226 sudo -u postgres psql -c "DROP DATABASE ${nextcloudDatabase};"
227 fi
228
229 echo "Done"
230 echo
231
232 echo "Creating new DB for Nextcloud..."
233
234 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
235 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
236 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
237 # TODO: Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
238 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
239 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
240 sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UTF8\";"
241 fi
242
243 echo "Done"
244 echo
245
246 echo "Restoring backup DB..."
247
248 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
249 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
250 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
251 sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
252 fi
253
254 echo "Done"
255 echo
256
257 #
258 # Start web server
259 #
260 echo "Starting web server..."
261 systemctl start "${webserverServiceName}"
262 echo "Done"
263 echo
264
265 #
266 # Set directory permissions
267 #
268 echo "Setting directory permissions..."
269 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
270 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
271 # TODO: Uncomment if you use local external storage
272 #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
273 echo "Done"
274 echo
275
276 #
277 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
278 #
279 echo "Updating the system data-fingerprint..."
280 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:data-fingerprint
281 echo "Done"
282 echo
283
284 #
285 # Disbale maintenance mode
286 #
287 echo "Switching off maintenance mode..."
288 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
289 echo "Done"
290 echo
291
292 echo
293 echo "DONE!"
294 echo "Backup ${restore} successfully restored."

patrick-canterino.de