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

patrick-canterino.de