]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
No need to modify code to restore local external storage anymore
[nextcloud-backup-restore.git] / NextcloudRestore.sh
1 #!/bin/bash
2
3 #
4 # Bash script for restoring backups of Nextcloud.
5 #
6 # Version 2.1.3
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 fileNameBackupExternalDataDir=''
77
78 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
79 fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
80
81 if [ "$useCompression" = true ] ; then
82 fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
83 fi
84 fi
85
86 fileNameBackupDb='nextcloud-db.sql'
87
88 # Function for error messages
89 errorecho() { cat <<< "$@" 1>&2; }
90
91 #
92 # Check if parameter(s) given
93 #
94 if [ $# != "1" ] && [ $# != "2" ]
95 then
96 errorecho "ERROR: No backup name to restore given, or wrong number of parameters!"
97 errorecho "Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
98 exit 1
99 fi
100
101 #
102 # Check for root
103 #
104 if [ "$(id -u)" != "0" ]
105 then
106 errorecho "ERROR: This script has to be run as root!"
107 exit 1
108 fi
109
110 #
111 # Check if backup dir exists
112 #
113 if [ ! -d "${currentRestoreDir}" ]
114 then
115 errorecho "ERROR: Backup ${restore} not found!"
116 exit 1
117 fi
118
119 #
120 # Check if the commands for restoring the database are available
121 #
122 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
123 if ! [ -x "$(command -v mysql)" ]; then
124 errorecho "ERROR: MySQL/MariaDB not installed (command mysql not found)."
125 errorecho "ERROR: No restore of database possible!"
126 errorecho "Cancel restore"
127 exit 1
128 fi
129 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
130 if ! [ -x "$(command -v psql)" ]; then
131 errorecho "ERROR: PostgreSQL not installed (command psql not found)."
132 errorecho "ERROR: No restore of database possible!"
133 errorecho "Cancel restore"
134 exit 1
135 fi
136 fi
137
138 #
139 # Set maintenance mode
140 #
141 echo "$(date +"%H:%M:%S"): Set maintenance mode for Nextcloud..."
142 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
143 echo "Done"
144 echo
145
146 #
147 # Stop web server
148 #
149 echo "$(date +"%H:%M:%S"): Stopping web server..."
150 systemctl stop "${webserverServiceName}"
151 echo "Done"
152 echo
153
154 #
155 # Delete old Nextcloud directories
156 #
157
158 # File directory
159 echo "$(date +"%H:%M:%S"): Deleting old Nextcloud file directory..."
160 rm -r "${nextcloudFileDir}"
161 mkdir -p "${nextcloudFileDir}"
162 echo "Done"
163 echo
164
165 # Data directory
166 echo "$(date +"%H:%M:%S"): Deleting old Nextcloud data directory..."
167 rm -r "${nextcloudDataDir}"
168 mkdir -p "${nextcloudDataDir}"
169 echo "Done"
170 echo
171
172 # Local external storage
173 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
174 echo "Deleting old Nextcloud local external storage directory..."
175 rm -r "${nextcloudLocalExternalDataDir}"
176 mkdir -p "${nextcloudLocalExternalDataDir}"
177 echo "Done"
178 echo
179 fi
180
181 #
182 # Restore file and data directory
183 #
184
185 # File directory
186 echo "$(date +"%H:%M:%S"): Restoring Nextcloud file directory..."
187
188 if [ "$useCompression" = true ] ; then
189 tar -I pigz -xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
190 else
191 tar -xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
192 fi
193
194 echo "Done"
195 echo
196
197 # Data directory
198 echo "$(date +"%H:%M:%S"): Restoring Nextcloud data directory..."
199
200 if [ "$useCompression" = true ] ; then
201 tar -I pigz -xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
202 else
203 tar -xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
204 fi
205
206 echo "Done"
207 echo
208
209 # Local external storage
210 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
211 echo "$(date +"%H:%M:%S"): Restoring Nextcloud data directory..."
212
213 if [ "$useCompression" = true ] ; then
214 tar -I pigz -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
215 else
216 tar -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
217 fi
218
219 echo "Done"
220 echo
221 fi
222
223 #
224 # Restore database
225 #
226 echo "$(date +"%H:%M:%S"): Dropping old Nextcloud DB..."
227
228 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
229 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
230 elif [ "${databaseSystem,,}" = "postgresql" ]; then
231 sudo -u postgres psql -c "DROP DATABASE ${nextcloudDatabase};"
232 fi
233
234 echo "Done"
235 echo
236
237 echo "$(date +"%H:%M:%S"): Creating new DB for Nextcloud..."
238
239 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
240 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
241 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
242 # TODO: Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
243 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
244 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
245 sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UNICODE\";"
246 fi
247
248 echo "Done"
249 echo
250
251 echo "$(date +"%H:%M:%S"): Restoring backup DB..."
252
253 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
254 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
255 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
256 sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
257 fi
258
259 echo "Done"
260 echo
261
262 #
263 # Start web server
264 #
265 echo "$(date +"%H:%M:%S"): Starting web server..."
266 systemctl start "${webserverServiceName}"
267 echo "Done"
268 echo
269
270 #
271 # Set directory permissions
272 #
273 echo "$(date +"%H:%M:%S"): Setting directory permissions..."
274 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
275 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
276
277 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
278 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
279 fi
280
281 echo "Done"
282 echo
283
284 #
285 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
286 #
287 echo "$(date +"%H:%M:%S"): Updating the system data-fingerprint..."
288 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:data-fingerprint
289 echo "Done"
290 echo
291
292 #
293 # Disbale maintenance mode
294 #
295 echo "$(date +"%H:%M:%S"): Switching off maintenance mode..."
296 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
297 echo "Done"
298 echo
299
300 echo
301 echo "DONE!"
302 echo "$(date +"%H:%M:%S"): Backup ${restore} successfully restored."

patrick-canterino.de